FontFaceBitmap.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Core/Context.h"
  23. #include "../../Graphics/Graphics.h"
  24. #include "../../Graphics/Texture2D.h"
  25. #include "../../IO/File.h"
  26. #include "../../IO/FileSystem.h"
  27. #include "../../IO/Log.h"
  28. #include "../../IO/MemoryBuffer.h"
  29. #include "../../Resource/ResourceCache.h"
  30. #include "Font.h"
  31. #include "FontFaceBitmap.h"
  32. #include "UI.h"
  33. #include "../../DebugNew.h"
  34. namespace Atomic
  35. {
  36. namespace SystemUI
  37. {
  38. FontFaceBitmap::FontFaceBitmap(Font* font) :
  39. FontFace(font)
  40. {
  41. }
  42. FontFaceBitmap::~FontFaceBitmap()
  43. {
  44. }
  45. bool FontFaceBitmap::Load(const unsigned char* fontData, unsigned fontDataSize, int pointSize)
  46. {
  47. Context* context = font_->GetContext();
  48. SharedPtr<XMLFile> xmlReader(new XMLFile(context));
  49. MemoryBuffer memoryBuffer(fontData, fontDataSize);
  50. if (!xmlReader->Load(memoryBuffer))
  51. {
  52. LOGERROR("Could not load XML file");
  53. return false;
  54. }
  55. XMLElement root = xmlReader->GetRoot("font");
  56. if (root.IsNull())
  57. {
  58. LOGERROR("Could not find Font element");
  59. return false;
  60. }
  61. XMLElement pagesElem = root.GetChild("pages");
  62. if (pagesElem.IsNull())
  63. {
  64. LOGERROR("Could not find Pages element");
  65. return false;
  66. }
  67. XMLElement infoElem = root.GetChild("info");
  68. if (!infoElem.IsNull())
  69. pointSize_ = infoElem.GetInt("size");
  70. XMLElement commonElem = root.GetChild("common");
  71. rowHeight_ = commonElem.GetInt("lineHeight");
  72. unsigned pages = commonElem.GetUInt("pages");
  73. textures_.Reserve(pages);
  74. ResourceCache* resourceCache = font_->GetSubsystem<ResourceCache>();
  75. String fontPath = GetPath(font_->GetName());
  76. unsigned totalTextureSize = 0;
  77. XMLElement pageElem = pagesElem.GetChild("page");
  78. for (unsigned i = 0; i < pages; ++i)
  79. {
  80. if (pageElem.IsNull())
  81. {
  82. LOGERROR("Could not find Page element for page: " + String(i));
  83. return 0;
  84. }
  85. // Assume the font image is in the same directory as the font description file
  86. String textureFile = fontPath + pageElem.GetAttribute("file");
  87. // Load texture manually to allow controlling the alpha channel mode
  88. SharedPtr<File> fontFile = resourceCache->GetFile(textureFile);
  89. SharedPtr<Image> fontImage(new Image(context));
  90. if (!fontFile || !fontImage->Load(*fontFile))
  91. {
  92. LOGERROR("Failed to load font image file");
  93. return 0;
  94. }
  95. SharedPtr<Texture2D> texture = LoadFaceTexture(fontImage);
  96. if (!texture)
  97. return 0;
  98. textures_.Push(texture);
  99. // Add texture to resource cache
  100. texture->SetName(fontFile->GetName());
  101. resourceCache->AddManualResource(texture);
  102. totalTextureSize += fontImage->GetWidth() * fontImage->GetHeight() * fontImage->GetComponents();
  103. pageElem = pageElem.GetNext("page");
  104. }
  105. XMLElement charsElem = root.GetChild("chars");
  106. int count = charsElem.GetInt("count");
  107. XMLElement charElem = charsElem.GetChild("char");
  108. while (!charElem.IsNull())
  109. {
  110. int id = charElem.GetInt("id");
  111. FontGlyph glyph;
  112. glyph.x_ = (short)charElem.GetInt("x");
  113. glyph.y_ = (short)charElem.GetInt("y");
  114. glyph.width_ = (short)charElem.GetInt("width");
  115. glyph.height_ = (short)charElem.GetInt("height");
  116. glyph.offsetX_ = (short)charElem.GetInt("xoffset");
  117. glyph.offsetY_ = (short)charElem.GetInt("yoffset");
  118. glyph.advanceX_ = (short)charElem.GetInt("xadvance");
  119. glyph.page_ = charElem.GetUInt("page");
  120. glyphMapping_[id] = glyph;
  121. charElem = charElem.GetNext("char");
  122. }
  123. XMLElement kerningsElem = root.GetChild("kernings");
  124. if (kerningsElem.NotNull())
  125. {
  126. XMLElement kerningElem = kerningsElem.GetChild("kerning");
  127. while (!kerningElem.IsNull())
  128. {
  129. int first = kerningElem.GetInt("first");
  130. int second = kerningElem.GetInt("second");
  131. unsigned value = (unsigned)((first << 16) + second);
  132. kerningMapping_[value] = (short)kerningElem.GetInt("amount");
  133. kerningElem = kerningElem.GetNext("kerning");
  134. }
  135. }
  136. LOGDEBUGF("Bitmap font face %s has %d glyphs", GetFileName(font_->GetName()).CString(), count);
  137. font_->SetMemoryUse(font_->GetMemoryUse() + totalTextureSize);
  138. return true;
  139. }
  140. bool FontFaceBitmap::Load(FontFace* fontFace, bool usedGlyphs)
  141. {
  142. if (this == fontFace)
  143. return true;
  144. if (!usedGlyphs)
  145. {
  146. glyphMapping_ = fontFace->glyphMapping_;
  147. kerningMapping_ = fontFace->kerningMapping_;
  148. textures_ = fontFace->textures_;
  149. pointSize_ = fontFace->pointSize_;
  150. rowHeight_ = fontFace->rowHeight_;
  151. return true;
  152. }
  153. pointSize_ = fontFace->pointSize_;
  154. rowHeight_ = fontFace->rowHeight_;
  155. unsigned numPages = 1;
  156. int maxTextureSize = font_->GetSubsystem<UI>()->GetMaxFontTextureSize();
  157. AreaAllocator allocator(FONT_TEXTURE_MIN_SIZE, FONT_TEXTURE_MIN_SIZE, maxTextureSize, maxTextureSize);
  158. for (HashMap<unsigned, FontGlyph>::ConstIterator i = fontFace->glyphMapping_.Begin(); i != fontFace->glyphMapping_.End(); ++i)
  159. {
  160. FontGlyph fontGlyph = i->second_;
  161. if (!fontGlyph.used_)
  162. continue;
  163. int x, y;
  164. if (!allocator.Allocate(fontGlyph.width_ + 1, fontGlyph.height_ + 1, x, y))
  165. {
  166. ++numPages;
  167. allocator = AreaAllocator(FONT_TEXTURE_MIN_SIZE, FONT_TEXTURE_MIN_SIZE, maxTextureSize, maxTextureSize);
  168. if (!allocator.Allocate(fontGlyph.width_ + 1, fontGlyph.height_ + 1, x, y))
  169. return false;
  170. }
  171. fontGlyph.x_ = (short)x;
  172. fontGlyph.y_ = (short)y;
  173. fontGlyph.page_ = numPages - 1;
  174. glyphMapping_[i->first_] = fontGlyph;
  175. }
  176. // Assume that format is the same for all textures and that bitmap font type may have more than one component
  177. unsigned components = ConvertFormatToNumComponents(fontFace->textures_[0]->GetFormat());
  178. // Save the existing textures as image resources
  179. Vector<SharedPtr<Image> > oldImages;
  180. for (unsigned i = 0; i < fontFace->textures_.Size(); ++i)
  181. oldImages.Push(SaveFaceTexture(fontFace->textures_[i]));
  182. Vector<SharedPtr<Image> > newImages(numPages);
  183. for (unsigned i = 0; i < numPages; ++i)
  184. {
  185. SharedPtr<Image> image(new Image(font_->GetContext()));
  186. int width = maxTextureSize;
  187. int height = maxTextureSize;
  188. if (i == numPages - 1)
  189. {
  190. width = allocator.GetWidth();
  191. height = allocator.GetHeight();
  192. }
  193. image->SetSize(width, height, components);
  194. memset(image->GetData(), 0, width * height * components);
  195. newImages.Push(image);
  196. }
  197. for (HashMap<unsigned, FontGlyph>::Iterator i = glyphMapping_.Begin(); i != glyphMapping_.End(); ++i)
  198. {
  199. FontGlyph& newGlyph = i->second_;
  200. const FontGlyph& oldGlyph = fontFace->glyphMapping_[i->first_];
  201. Blit(newImages[newGlyph.page_], newGlyph.x_, newGlyph.y_, newGlyph.width_, newGlyph.height_, oldImages[oldGlyph.page_],
  202. oldGlyph.x_, oldGlyph.y_, components);
  203. }
  204. textures_.Resize(newImages.Size());
  205. for (unsigned i = 0; i < newImages.Size(); ++i)
  206. textures_[i] = LoadFaceTexture(newImages[i]);
  207. for (HashMap<unsigned, short>::ConstIterator i = fontFace->kerningMapping_.Begin(); i != fontFace->kerningMapping_.End(); ++i)
  208. {
  209. unsigned first = (i->first_) >> 16;
  210. unsigned second = (i->first_) & 0xffff;
  211. if (glyphMapping_.Find(first) != glyphMapping_.End() && glyphMapping_.Find(second) != glyphMapping_.End())
  212. kerningMapping_[i->first_] = i->second_;
  213. }
  214. return true;
  215. }
  216. bool FontFaceBitmap::Save(Serializer& dest, int pointSize, const String& indentation)
  217. {
  218. Context* context = font_->GetContext();
  219. SharedPtr<XMLFile> xml(new XMLFile(context));
  220. XMLElement rootElem = xml->CreateRoot("font");
  221. // Information
  222. XMLElement childElem = rootElem.CreateChild("info");
  223. String fileName = GetFileName(font_->GetName());
  224. childElem.SetAttribute("face", fileName);
  225. childElem.SetAttribute("size", String(pointSize));
  226. // Common
  227. childElem = rootElem.CreateChild("common");
  228. childElem.SetInt("lineHeight", rowHeight_);
  229. unsigned pages = textures_.Size();
  230. childElem.SetUInt("pages", pages);
  231. // Construct the path to store the texture
  232. String pathName;
  233. File* file = dynamic_cast<File*>(&dest);
  234. if (file)
  235. // If serialize to file, use the file's path
  236. pathName = GetPath(file->GetName());
  237. else
  238. // Otherwise, use the font resource's path
  239. pathName = "Data/" + GetPath(font_->GetName());
  240. // Pages
  241. childElem = rootElem.CreateChild("pages");
  242. for (unsigned i = 0; i < pages; ++i)
  243. {
  244. XMLElement pageElem = childElem.CreateChild("page");
  245. pageElem.SetInt("id", i);
  246. String texFileName = fileName + "_" + String(i) + ".png";
  247. pageElem.SetAttribute("file", texFileName);
  248. // Save the font face texture to image file
  249. SaveFaceTexture(textures_[i], pathName + texFileName);
  250. }
  251. // Chars and kernings
  252. XMLElement charsElem = rootElem.CreateChild("chars");
  253. unsigned numGlyphs = glyphMapping_.Size();
  254. charsElem.SetInt("count", numGlyphs);
  255. for (HashMap<unsigned, FontGlyph>::ConstIterator i = glyphMapping_.Begin(); i != glyphMapping_.End(); ++i)
  256. {
  257. // Char
  258. XMLElement charElem = charsElem.CreateChild("char");
  259. charElem.SetInt("id", i->first_);
  260. const FontGlyph& glyph = i->second_;
  261. charElem.SetInt("x", glyph.x_);
  262. charElem.SetInt("y", glyph.y_);
  263. charElem.SetInt("width", glyph.width_);
  264. charElem.SetInt("height", glyph.height_);
  265. charElem.SetInt("xoffset", glyph.offsetX_);
  266. charElem.SetInt("yoffset", glyph.offsetY_);
  267. charElem.SetInt("xadvance", glyph.advanceX_);
  268. charElem.SetUInt("page", glyph.page_);
  269. }
  270. if (!kerningMapping_.Empty())
  271. {
  272. XMLElement kerningsElem = rootElem.CreateChild("kernings");
  273. for (HashMap<unsigned, short>::ConstIterator i = kerningMapping_.Begin(); i != kerningMapping_.End(); ++i)
  274. {
  275. XMLElement kerningElem = kerningsElem.CreateChild("kerning");
  276. kerningElem.SetInt("first", i->first_ >> 16);
  277. kerningElem.SetInt("second", i->first_ & 0xffff);
  278. kerningElem.SetInt("amount", i->second_);
  279. }
  280. }
  281. return xml->Save(dest, indentation);
  282. }
  283. unsigned FontFaceBitmap::ConvertFormatToNumComponents(unsigned format)
  284. {
  285. if (format == Graphics::GetRGBAFormat())
  286. return 4;
  287. else if (format == Graphics::GetRGBFormat())
  288. return 3;
  289. else if (format == Graphics::GetLuminanceAlphaFormat())
  290. return 2;
  291. else
  292. return 1;
  293. }
  294. SharedPtr<Image> FontFaceBitmap::SaveFaceTexture(Texture2D* texture)
  295. {
  296. Image* image = new Image(font_->GetContext());
  297. image->SetSize(texture->GetWidth(), texture->GetHeight(), ConvertFormatToNumComponents(texture->GetFormat()));
  298. if (!texture->GetData(0, image->GetData()))
  299. {
  300. delete image;
  301. LOGERROR("Could not save texture to image resource");
  302. return SharedPtr<Image>();
  303. }
  304. return SharedPtr<Image>(image);
  305. }
  306. bool FontFaceBitmap::SaveFaceTexture(Texture2D* texture, const String& fileName)
  307. {
  308. SharedPtr<Image> image = SaveFaceTexture(texture);
  309. return image ? image->SavePNG(fileName) : false;
  310. }
  311. void FontFaceBitmap::Blit(Image* dest, int x, int y, int width, int height, Image* source, int sourceX, int sourceY, int components)
  312. {
  313. unsigned char* destData = dest->GetData() + (y * dest->GetWidth() + x) * components;
  314. unsigned char* sourceData = source->GetData() + (sourceY * source->GetWidth() + sourceX) * components;
  315. for (int i = 0; i < height; ++i)
  316. {
  317. memcpy(destData, sourceData, (size_t)(width * components));
  318. destData += dest->GetWidth() * components;
  319. sourceData += source->GetWidth() * components;
  320. }
  321. }
  322. }
  323. }