FontProvider.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  30. #include "FontProvider.h"
  31. #include "FontFaceHandle.h"
  32. #include "../FontDatabaseDefault.h"
  33. #include <RmlUi/Core/StreamMemory.h>
  34. #include "FontFamily.h"
  35. #include <RmlUi/Core.h>
  36. #include "BitmapFontDefinitions.h"
  37. #include "FontParser.h"
  38. namespace Rml {
  39. namespace Core {
  40. BitmapFont::FontProvider* BitmapFont::FontProvider::instance = nullptr;
  41. BitmapFont::FontProvider::FontProvider()
  42. {
  43. RMLUI_ASSERT(instance == nullptr);
  44. instance = this;
  45. }
  46. BitmapFont::FontProvider::~FontProvider()
  47. {
  48. RMLUI_ASSERT(instance == this);
  49. instance = nullptr;
  50. }
  51. bool BitmapFont::FontProvider::Initialise()
  52. {
  53. if (instance == nullptr)
  54. {
  55. new FontProvider();
  56. FontDatabaseDefault::AddFontProvider(instance);
  57. }
  58. return true;
  59. }
  60. void BitmapFont::FontProvider::Shutdown()
  61. {
  62. if (instance != nullptr)
  63. {
  64. FontDatabaseDefault::RemoveFontProvider(instance);
  65. delete instance;
  66. instance = nullptr;
  67. }
  68. }
  69. // Adds a new font face to the database, ignoring any family, style and weight information stored in the face itself.
  70. bool BitmapFont::FontProvider::LoadFontFace(const String& file_name)
  71. {
  72. BitmapFont::BitmapFontDefinitions *bm_font = (BitmapFont::BitmapFontDefinitions*) instance->LoadFace(file_name);
  73. if (bm_font == nullptr)
  74. {
  75. Log::Message(Log::LT_ERROR, "Failed to load font face from %s.", file_name.c_str());
  76. return false;
  77. }
  78. Style::FontStyle style = bm_font->Face.Style;
  79. Style::FontWeight weight = bm_font->Face.Weight;
  80. if (instance->AddFace(bm_font, bm_font->Face.FamilyName, style, weight, true))
  81. {
  82. Log::Message(Log::LT_INFO, "Loaded font face %s (from %s).", bm_font->Face.FamilyName.c_str(), file_name.c_str());
  83. return true;
  84. }
  85. else
  86. {
  87. Log::Message(Log::LT_ERROR, "Failed to load font face %s (from %s).", bm_font->Face.FamilyName.c_str(), file_name.c_str());
  88. return false;
  89. }
  90. return true;
  91. }
  92. // Adds a loaded face to the appropriate font family.
  93. bool BitmapFont::FontProvider::AddFace(void* face, const String& family, Style::FontStyle style, Style::FontWeight weight, bool release_stream)
  94. {
  95. String family_lower = StringUtilities::ToLower(family);
  96. Rml::Core::FontFamily* font_family = nullptr;
  97. FontFamilyMap::iterator iterator = instance->font_families.find(family_lower);
  98. if (iterator != instance->font_families.end())
  99. font_family = (*iterator).second;
  100. else
  101. {
  102. font_family = new FontFamily(family_lower);
  103. instance->font_families[family_lower] = font_family;
  104. }
  105. return font_family->AddFace((BitmapFontDefinitions *) face, style, weight, release_stream);
  106. }
  107. // Loads a FreeType face.
  108. void* BitmapFont::FontProvider::LoadFace(const String& file_name)
  109. {
  110. BitmapFont::BitmapFontDefinitions *bm_face = new BitmapFont::BitmapFontDefinitions();
  111. BitmapFont::FontParser parser( bm_face );
  112. FileInterface* file_interface = GetFileInterface();
  113. FileHandle handle = file_interface->Open(file_name);
  114. if (!handle)
  115. {
  116. return nullptr;
  117. }
  118. size_t length = file_interface->Length(handle);
  119. byte* buffer = new byte[length];
  120. file_interface->Read(buffer, length, handle);
  121. file_interface->Close(handle);
  122. StreamMemory* stream = new StreamMemory( buffer, length );
  123. stream->SetSourceURL( file_name );
  124. parser.Parse( stream );
  125. bm_face->Face.Source = file_name;
  126. return bm_face;
  127. }
  128. // Loads a FreeType face from memory.
  129. void* BitmapFont::FontProvider::LoadFace(const byte* data, int data_length, const String& source, bool local_data)
  130. {
  131. URL file_url = source + ".fnt";
  132. BitmapFont::BitmapFontDefinitions *bm_face = new BitmapFont::BitmapFontDefinitions();
  133. BitmapFont::FontParser parser( bm_face );
  134. StreamMemory* stream = new StreamMemory( data, data_length );
  135. stream->SetSourceURL( file_url );
  136. parser.Parse( stream );
  137. bm_face->Face.Source = file_url.GetPathedFileName();
  138. return bm_face;
  139. }
  140. }
  141. }
  142. #endif