Text3DBitmap.cpp 13 KB

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