FontFaceBitmap.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 "Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../IO/File.h"
  25. #include "../IO/FileSystem.h"
  26. #include "../UI/Font.h"
  27. #include "../UI/FontFaceBitmap.h"
  28. #include "../Graphics/Graphics.h"
  29. #include "../Resource/Image.h"
  30. #include "../IO/Log.h"
  31. #include "../IO/MemoryBuffer.h"
  32. #include "../Resource/ResourceCache.h"
  33. #include "../Graphics/Texture2D.h"
  34. #include "../UI/UI.h"
  35. #include "../Resource/XMLFile.h"
  36. #include "../DebugNew.h"
  37. namespace Atomic
  38. {
  39. FontFaceBitmap::FontFaceBitmap(Font* font) :
  40. FontFace(font)
  41. {
  42. }
  43. FontFaceBitmap::~FontFaceBitmap()
  44. {
  45. }
  46. bool FontFaceBitmap::Load(const unsigned char* fontData, unsigned fontDataSize, int pointSize)
  47. {
  48. Context* context = font_->GetContext();
  49. SharedPtr<XMLFile> xmlReader(new XMLFile(context));
  50. MemoryBuffer memoryBuffer(fontData, fontDataSize);
  51. if (!xmlReader->Load(memoryBuffer))
  52. {
  53. LOGERROR("Could not load XML file");
  54. return false;
  55. }
  56. XMLElement root = xmlReader->GetRoot("font");
  57. if (root.IsNull())
  58. {
  59. LOGERROR("Could not find Font element");
  60. return false;
  61. }
  62. XMLElement pagesElem = root.GetChild("pages");
  63. if (pagesElem.IsNull())
  64. {
  65. LOGERROR("Could not find Pages element");
  66. return false;
  67. }
  68. XMLElement infoElem = root.GetChild("info");
  69. if (!infoElem.IsNull())
  70. pointSize_ = infoElem.GetInt("size");
  71. XMLElement commonElem = root.GetChild("common");
  72. rowHeight_ = commonElem.GetInt("lineHeight");
  73. unsigned pages = commonElem.GetInt("pages");
  74. textures_.Reserve(pages);
  75. ResourceCache* resourceCache = font_->GetSubsystem<ResourceCache>();
  76. String fontPath = GetPath(font_->GetName());
  77. unsigned totalTextureSize = 0;
  78. XMLElement pageElem = pagesElem.GetChild("page");
  79. for (unsigned i = 0; i < pages; ++i)
  80. {
  81. if (pageElem.IsNull())
  82. {
  83. LOGERROR("Could not find Page element for page: " + String(i));
  84. return 0;
  85. }
  86. // Assume the font image is in the same directory as the font description file
  87. String textureFile = fontPath + pageElem.GetAttribute("file");
  88. // Load texture manually to allow controlling the alpha channel mode
  89. SharedPtr<File> fontFile = resourceCache->GetFile(textureFile);
  90. SharedPtr<Image> fontImage(new Image(context));
  91. if (!fontFile || !fontImage->Load(*fontFile))
  92. {
  93. LOGERROR("Failed to load font image file");
  94. return 0;
  95. }
  96. SharedPtr<Texture2D> texture = LoadFaceTexture(fontImage);
  97. if (!texture)
  98. return 0;
  99. textures_.Push(texture);
  100. // Add texture to resource cache
  101. texture->SetName(fontFile->GetName());
  102. resourceCache->AddManualResource(texture);
  103. totalTextureSize += fontImage->GetWidth() * fontImage->GetHeight() * fontImage->GetComponents();
  104. pageElem = pageElem.GetNext("page");
  105. }
  106. XMLElement charsElem = root.GetChild("chars");
  107. int count = charsElem.GetInt("count");
  108. XMLElement charElem = charsElem.GetChild("char");
  109. while (!charElem.IsNull())
  110. {
  111. int id = charElem.GetInt("id");
  112. FontGlyph glyph;
  113. glyph.x_ = charElem.GetInt("x");
  114. glyph.y_ = charElem.GetInt("y");
  115. glyph.width_ = charElem.GetInt("width");
  116. glyph.height_ = charElem.GetInt("height");
  117. glyph.offsetX_ = charElem.GetInt("xoffset");
  118. glyph.offsetY_ = charElem.GetInt("yoffset");
  119. glyph.advanceX_ = charElem.GetInt("xadvance");
  120. glyph.page_ = charElem.GetInt("page");
  121. glyphMapping_[id] = glyph;
  122. charElem = charElem.GetNext("char");
  123. }
  124. XMLElement kerningsElem = root.GetChild("kernings");
  125. if (kerningsElem.NotNull())
  126. {
  127. XMLElement kerningElem = kerningsElem.GetChild("kerning");
  128. while (!kerningElem.IsNull())
  129. {
  130. int first = kerningElem.GetInt("first");
  131. int second = kerningElem.GetInt("second");
  132. unsigned value = (first << 16) + second;
  133. kerningMapping_[value] = (short)kerningElem.GetInt("amount");
  134. kerningElem = kerningElem.GetNext("kerning");
  135. }
  136. }
  137. LOGDEBUGF("Bitmap font face %s has %d glyphs", GetFileName(font_->GetName()).CString(), count);
  138. font_->SetMemoryUse(font_->GetMemoryUse() + totalTextureSize);
  139. return true;
  140. }
  141. bool FontFaceBitmap::Load(FontFace* fontFace, bool usedGlyphs)
  142. {
  143. if (this == fontFace)
  144. return true;
  145. if (!usedGlyphs)
  146. {
  147. glyphMapping_ = fontFace->glyphMapping_;
  148. kerningMapping_ = fontFace->kerningMapping_;
  149. textures_ = fontFace->textures_;
  150. pointSize_ = fontFace->pointSize_;
  151. rowHeight_ = fontFace->rowHeight_;
  152. return true;
  153. }
  154. pointSize_ = fontFace->pointSize_;
  155. rowHeight_ = fontFace->rowHeight_;
  156. int numPages = 1;
  157. int maxTextureSize = font_->GetSubsystem<UI>()->GetMaxFontTextureSize();
  158. AreaAllocator allocator(FONT_TEXTURE_MIN_SIZE, FONT_TEXTURE_MIN_SIZE, maxTextureSize, maxTextureSize);
  159. for (HashMap<unsigned, FontGlyph>::ConstIterator i = fontFace->glyphMapping_.Begin(); i != fontFace->glyphMapping_.End(); ++i)
  160. {
  161. FontGlyph fontGlyph = i->second_;
  162. if (!fontGlyph.used_)
  163. continue;
  164. int x, y;
  165. if (!allocator.Allocate(fontGlyph.width_ + 1, fontGlyph.height_ + 1, x, y))
  166. {
  167. ++numPages;
  168. allocator = AreaAllocator(FONT_TEXTURE_MIN_SIZE, FONT_TEXTURE_MIN_SIZE, maxTextureSize, maxTextureSize);
  169. if (!allocator.Allocate(fontGlyph.width_ + 1, fontGlyph.height_ + 1, x, y))
  170. return false;
  171. }
  172. fontGlyph.x_ = x;
  173. fontGlyph.y_ = y;
  174. fontGlyph.page_ = numPages - 1;
  175. glyphMapping_[i->first_] = fontGlyph;
  176. }
  177. // Assume that format is the same for all textures and that bitmap font type may have more than one component
  178. unsigned components = ConvertFormatToNumComponents(fontFace->textures_[0]->GetFormat());
  179. // Save the existing textures as image resources
  180. Vector<SharedPtr<Image> > oldImages;
  181. for (unsigned i = 0; i < fontFace->textures_.Size(); ++i)
  182. oldImages.Push(SaveFaceTexture(fontFace->textures_[i]));
  183. Vector<SharedPtr<Image> > newImages(numPages);
  184. for (int i = 0; i < numPages; ++i)
  185. {
  186. SharedPtr<Image> image(new Image(font_->GetContext()));
  187. int width = maxTextureSize;
  188. int height = maxTextureSize;
  189. if (i == numPages - 1)
  190. {
  191. width = allocator.GetWidth();
  192. height = allocator.GetHeight();
  193. }
  194. image->SetSize(width, height, components);
  195. memset(image->GetData(), 0, width * height * components);
  196. newImages.Push(image);
  197. }
  198. for (HashMap<unsigned, FontGlyph>::Iterator i = glyphMapping_.Begin(); i != glyphMapping_.End(); ++i)
  199. {
  200. FontGlyph& newGlyph = i->second_;
  201. const FontGlyph& oldGlyph = fontFace->glyphMapping_[i->first_];
  202. Blit(newImages[newGlyph.page_], newGlyph.x_, newGlyph.y_, newGlyph.width_, newGlyph.height_, oldImages[oldGlyph.page_], 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.SetInt("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.SetInt("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 (!static_cast<Texture2D*>(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, width * components);
  318. destData += dest->GetWidth() * components;
  319. sourceData += source->GetWidth() * components;
  320. }
  321. }
  322. }