FontFaceBitmap.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Graphics/Graphics.h"
  6. #include "../GraphicsAPI/Texture2D.h"
  7. #include "../IO/File.h"
  8. #include "../IO/FileSystem.h"
  9. #include "../IO/Log.h"
  10. #include "../IO/MemoryBuffer.h"
  11. #include "../Resource/ResourceCache.h"
  12. #include "../UI/Font.h"
  13. #include "../UI/FontFaceBitmap.h"
  14. #include "../UI/UI.h"
  15. #include "../DebugNew.h"
  16. namespace Urho3D
  17. {
  18. FontFaceBitmap::FontFaceBitmap(Font* font) :
  19. FontFace(font)
  20. {
  21. }
  22. FontFaceBitmap::~FontFaceBitmap() = default;
  23. // FIXME: The Load() and Save() should be refactored accordingly after the recent FontGlyph struct changes
  24. bool FontFaceBitmap::Load(const unsigned char* fontData, unsigned fontDataSize, float pointSize)
  25. {
  26. Context* context = font_->GetContext();
  27. SharedPtr<XMLFile> xmlReader(new XMLFile(context));
  28. MemoryBuffer memoryBuffer(fontData, fontDataSize);
  29. if (!xmlReader->Load(memoryBuffer))
  30. {
  31. URHO3D_LOGERROR("Could not load XML file");
  32. return false;
  33. }
  34. XMLElement root = xmlReader->GetRoot("font");
  35. if (root.IsNull())
  36. {
  37. URHO3D_LOGERROR("Could not find Font element");
  38. return false;
  39. }
  40. XMLElement pagesElem = root.GetChild("pages");
  41. if (pagesElem.IsNull())
  42. {
  43. URHO3D_LOGERROR("Could not find Pages element");
  44. return false;
  45. }
  46. XMLElement infoElem = root.GetChild("info");
  47. if (!infoElem.IsNull())
  48. pointSize_ = infoElem.GetInt("size");
  49. XMLElement commonElem = root.GetChild("common");
  50. rowHeight_ = commonElem.GetInt("lineHeight");
  51. unsigned pages = commonElem.GetUInt("pages");
  52. textures_.Reserve(pages);
  53. auto* resourceCache = font_->GetSubsystem<ResourceCache>();
  54. String fontPath = GetPath(font_->GetName());
  55. unsigned totalTextureSize = 0;
  56. XMLElement pageElem = pagesElem.GetChild("page");
  57. for (unsigned i = 0; i < pages; ++i)
  58. {
  59. if (pageElem.IsNull())
  60. {
  61. URHO3D_LOGERROR("Could not find Page element for page: " + String(i));
  62. return false;
  63. }
  64. // Assume the font image is in the same directory as the font description file
  65. String textureFile = fontPath + pageElem.GetAttribute("file");
  66. // Load texture manually to allow controlling the alpha channel mode
  67. SharedPtr<File> fontFile = resourceCache->GetFile(textureFile);
  68. SharedPtr<Image> fontImage(new Image(context));
  69. if (!fontFile || !fontImage->Load(*fontFile))
  70. {
  71. URHO3D_LOGERROR("Failed to load font image file");
  72. return false;
  73. }
  74. SharedPtr<Texture2D> texture = LoadFaceTexture(fontImage);
  75. if (!texture)
  76. return false;
  77. textures_.Push(texture);
  78. // Add texture to resource cache
  79. texture->SetName(fontFile->GetName());
  80. resourceCache->AddManualResource(texture);
  81. totalTextureSize += fontImage->GetWidth() * fontImage->GetHeight() * fontImage->GetComponents();
  82. pageElem = pageElem.GetNext("page");
  83. }
  84. XMLElement charsElem = root.GetChild("chars");
  85. int count = charsElem.GetInt("count");
  86. XMLElement charElem = charsElem.GetChild("char");
  87. while (!charElem.IsNull())
  88. {
  89. int id = charElem.GetInt("id");
  90. FontGlyph glyph;
  91. glyph.x_ = (short)charElem.GetInt("x");
  92. glyph.y_ = (short)charElem.GetInt("y");
  93. glyph.width_ = glyph.texWidth_ = (short)charElem.GetInt("width");
  94. glyph.height_ = glyph.texHeight_ = (short)charElem.GetInt("height");
  95. glyph.offsetX_ = (short)charElem.GetInt("xoffset");
  96. glyph.offsetY_ = (short)charElem.GetInt("yoffset");
  97. glyph.advanceX_ = (short)charElem.GetInt("xadvance");
  98. glyph.page_ = charElem.GetUInt("page");
  99. glyphMapping_[id] = glyph;
  100. charElem = charElem.GetNext("char");
  101. }
  102. XMLElement kerningsElem = root.GetChild("kernings");
  103. if (kerningsElem.NotNull())
  104. {
  105. XMLElement kerningElem = kerningsElem.GetChild("kerning");
  106. while (!kerningElem.IsNull())
  107. {
  108. unsigned first = kerningElem.GetInt("first");
  109. unsigned second = kerningElem.GetInt("second");
  110. unsigned value = first << 16u | second;
  111. kerningMapping_[value] = (short)kerningElem.GetInt("amount");
  112. kerningElem = kerningElem.GetNext("kerning");
  113. }
  114. }
  115. URHO3D_LOGDEBUGF("Bitmap font face %s has %d glyphs", GetFileName(font_->GetName()).CString(), count);
  116. font_->SetMemoryUse(font_->GetMemoryUse() + totalTextureSize);
  117. return true;
  118. }
  119. bool FontFaceBitmap::Load(FontFace* fontFace, bool usedGlyphs)
  120. {
  121. if (this == fontFace)
  122. return true;
  123. if (!usedGlyphs)
  124. {
  125. glyphMapping_ = fontFace->glyphMapping_;
  126. kerningMapping_ = fontFace->kerningMapping_;
  127. textures_ = fontFace->textures_;
  128. pointSize_ = fontFace->pointSize_;
  129. rowHeight_ = fontFace->rowHeight_;
  130. return true;
  131. }
  132. pointSize_ = fontFace->pointSize_;
  133. rowHeight_ = fontFace->rowHeight_;
  134. unsigned numPages = 1;
  135. int maxTextureSize = font_->GetSubsystem<UI>()->GetMaxFontTextureSize();
  136. AreaAllocator allocator(FONT_TEXTURE_MIN_SIZE, FONT_TEXTURE_MIN_SIZE, maxTextureSize, maxTextureSize);
  137. for (HashMap<c32, FontGlyph>::ConstIterator i = fontFace->glyphMapping_.Begin(); i != fontFace->glyphMapping_.End(); ++i)
  138. {
  139. FontGlyph fontGlyph = i->second_;
  140. if (!fontGlyph.used_)
  141. continue;
  142. int x, y;
  143. if (!allocator.Allocate(fontGlyph.width_ + 1, fontGlyph.height_ + 1, x, y))
  144. {
  145. ++numPages;
  146. allocator = AreaAllocator(FONT_TEXTURE_MIN_SIZE, FONT_TEXTURE_MIN_SIZE, maxTextureSize, maxTextureSize);
  147. if (!allocator.Allocate(fontGlyph.width_ + 1, fontGlyph.height_ + 1, x, y))
  148. return false;
  149. }
  150. fontGlyph.x_ = (short)x;
  151. fontGlyph.y_ = (short)y;
  152. fontGlyph.page_ = numPages - 1;
  153. glyphMapping_[i->first_] = fontGlyph;
  154. }
  155. // Assume that format is the same for all textures and that bitmap font type may have more than one component
  156. unsigned components = ConvertFormatToNumComponents(fontFace->textures_[0]->GetFormat());
  157. // Save the existing textures as image resources
  158. Vector<SharedPtr<Image> > oldImages;
  159. for (unsigned i = 0; i < fontFace->textures_.Size(); ++i)
  160. oldImages.Push(SaveFaceTexture(fontFace->textures_[i]));
  161. Vector<SharedPtr<Image> > newImages(numPages);
  162. for (unsigned i = 0; i < numPages; ++i)
  163. {
  164. SharedPtr<Image> image(new Image(font_->GetContext()));
  165. int width = maxTextureSize;
  166. int height = maxTextureSize;
  167. if (i == numPages - 1)
  168. {
  169. width = allocator.GetWidth();
  170. height = allocator.GetHeight();
  171. }
  172. image->SetSize(width, height, components);
  173. memset(image->GetData(), 0, (size_t)width * height * components);
  174. newImages[i] = image;
  175. }
  176. for (HashMap<c32, FontGlyph>::Iterator i = glyphMapping_.Begin(); i != glyphMapping_.End(); ++i)
  177. {
  178. FontGlyph& newGlyph = i->second_;
  179. const FontGlyph& oldGlyph = fontFace->glyphMapping_[i->first_];
  180. Blit(newImages[newGlyph.page_], newGlyph.x_, newGlyph.y_, newGlyph.width_, newGlyph.height_, oldImages[oldGlyph.page_],
  181. oldGlyph.x_, oldGlyph.y_, components);
  182. }
  183. textures_.Resize(newImages.Size());
  184. for (unsigned i = 0; i < newImages.Size(); ++i)
  185. textures_[i] = LoadFaceTexture(newImages[i]);
  186. for (HashMap<u32, float>::ConstIterator i = fontFace->kerningMapping_.Begin(); i != fontFace->kerningMapping_.End(); ++i)
  187. {
  188. unsigned first = (i->first_) >> 16u;
  189. unsigned second = (i->first_) & 0xffffu;
  190. if (glyphMapping_.Find(first) != glyphMapping_.End() && glyphMapping_.Find(second) != glyphMapping_.End())
  191. kerningMapping_[i->first_] = i->second_;
  192. }
  193. return true;
  194. }
  195. bool FontFaceBitmap::Save(Serializer& dest, int pointSize, const String& indentation)
  196. {
  197. Context* context = font_->GetContext();
  198. SharedPtr<XMLFile> xml(new XMLFile(context));
  199. XMLElement rootElem = xml->CreateRoot("font");
  200. // Information
  201. XMLElement childElem = rootElem.CreateChild("info");
  202. String fileName = GetFileName(font_->GetName());
  203. childElem.SetAttribute("face", fileName);
  204. childElem.SetAttribute("size", String(pointSize));
  205. // Common
  206. childElem = rootElem.CreateChild("common");
  207. childElem.SetInt("lineHeight", rowHeight_);
  208. unsigned pages = textures_.Size();
  209. childElem.SetUInt("pages", pages);
  210. // Construct the path to store the texture
  211. String pathName;
  212. auto* file = dynamic_cast<File*>(&dest);
  213. if (file)
  214. // If serialize to file, use the file's path
  215. pathName = GetPath(file->GetName());
  216. else
  217. // Otherwise, use the font resource's path
  218. pathName = "Data/" + GetPath(font_->GetName());
  219. // Pages
  220. childElem = rootElem.CreateChild("pages");
  221. for (unsigned i = 0; i < pages; ++i)
  222. {
  223. XMLElement pageElem = childElem.CreateChild("page");
  224. pageElem.SetInt("id", i);
  225. String texFileName = fileName + "_" + String(i) + ".png";
  226. pageElem.SetAttribute("file", texFileName);
  227. // Save the font face texture to image file
  228. SaveFaceTexture(textures_[i], pathName + texFileName);
  229. }
  230. // Chars and kernings
  231. XMLElement charsElem = rootElem.CreateChild("chars");
  232. unsigned numGlyphs = glyphMapping_.Size();
  233. charsElem.SetInt("count", numGlyphs);
  234. for (HashMap<c32, FontGlyph>::ConstIterator i = glyphMapping_.Begin(); i != glyphMapping_.End(); ++i)
  235. {
  236. // Char
  237. XMLElement charElem = charsElem.CreateChild("char");
  238. charElem.SetInt("id", i->first_);
  239. const FontGlyph& glyph = i->second_;
  240. charElem.SetInt("x", glyph.x_);
  241. charElem.SetInt("y", glyph.y_);
  242. charElem.SetInt("width", glyph.width_);
  243. charElem.SetInt("height", glyph.height_);
  244. charElem.SetInt("xoffset", glyph.offsetX_);
  245. charElem.SetInt("yoffset", glyph.offsetY_);
  246. charElem.SetInt("xadvance", glyph.advanceX_);
  247. charElem.SetUInt("page", glyph.page_);
  248. }
  249. if (!kerningMapping_.Empty())
  250. {
  251. XMLElement kerningsElem = rootElem.CreateChild("kernings");
  252. for (HashMap<u32, float>::ConstIterator i = kerningMapping_.Begin(); i != kerningMapping_.End(); ++i)
  253. {
  254. XMLElement kerningElem = kerningsElem.CreateChild("kerning");
  255. kerningElem.SetInt("first", i->first_ >> 16u);
  256. kerningElem.SetInt("second", i->first_ & 0xffffu);
  257. kerningElem.SetInt("amount", i->second_);
  258. }
  259. }
  260. return xml->Save(dest, indentation);
  261. }
  262. unsigned FontFaceBitmap::ConvertFormatToNumComponents(unsigned format)
  263. {
  264. if (format == Graphics::GetRGBAFormat())
  265. return 4;
  266. else if (format == Graphics::GetRGBFormat())
  267. return 3;
  268. else if (format == Graphics::GetLuminanceAlphaFormat())
  269. return 2;
  270. else
  271. return 1;
  272. }
  273. SharedPtr<Image> FontFaceBitmap::SaveFaceTexture(Texture2D* texture)
  274. {
  275. SharedPtr<Image> image(new Image(font_->GetContext()));
  276. image->SetSize(texture->GetWidth(), texture->GetHeight(), ConvertFormatToNumComponents(texture->GetFormat()));
  277. if (!texture->GetData(0, image->GetData()))
  278. {
  279. URHO3D_LOGERROR("Could not save texture to image resource");
  280. return SharedPtr<Image>();
  281. }
  282. return image;
  283. }
  284. bool FontFaceBitmap::SaveFaceTexture(Texture2D* texture, const String& fileName)
  285. {
  286. SharedPtr<Image> image = SaveFaceTexture(texture);
  287. return image ? image->SavePNG(fileName) : false;
  288. }
  289. void FontFaceBitmap::Blit(Image* dest, int x, int y, int width, int height, Image* source, int sourceX, int sourceY, int components)
  290. {
  291. unsigned char* destData = dest->GetData() + (y * dest->GetWidth() + x) * components;
  292. unsigned char* sourceData = source->GetData() + (sourceY * source->GetWidth() + sourceX) * components;
  293. for (int i = 0; i < height; ++i)
  294. {
  295. memcpy(destData, sourceData, (size_t)width * components);
  296. destData += dest->GetWidth() * components;
  297. sourceData += source->GetWidth() * components;
  298. }
  299. }
  300. }