2
0

PolyFontGlyphSheet.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyFont.h"
  20. #include "PolyFontGlyphSheet.h"
  21. #include "OSBasics.h"
  22. #include "PolyLogger.h"
  23. #include "PolyImage.h"
  24. #include "PolyTexture.h"
  25. #include "PolyCoreServices.h"
  26. #include <algorithm>
  27. #include <set>
  28. #include <vector>
  29. using namespace Polycode;
  30. FontGlyphSheet::FontGlyphSheet(Font* font, int size, FontTextureGlyphMode mode)
  31. : font(font)
  32. , size(size)
  33. , mode(mode)
  34. , tabWidth(100)
  35. {
  36. }
  37. FontGlyphSheet::~FontGlyphSheet() {
  38. if (texture)
  39. CoreServices::getInstance()->getMaterialManager()->deleteTexture(texture);
  40. }
  41. struct GlyphData {
  42. wchar_t character;
  43. short off_x,off_y;
  44. short size_x,size_y;
  45. short pitch;
  46. Vector2 advance;
  47. unsigned char* data;
  48. int texture_u,texture_v;
  49. GlyphData()
  50. : data(NULL)
  51. {
  52. }
  53. ~GlyphData() {
  54. delete data;
  55. }
  56. };
  57. bool fontGlyphSorter(GlyphData* a, GlyphData* b) {
  58. return a->size_y > b->size_y;
  59. }
  60. inline int pot_ceil(int x) {
  61. --x;
  62. x |= x >> 1;
  63. x |= x >> 2;
  64. x |= x >> 4;
  65. x |= x >> 8;
  66. x |= x >> 16;
  67. return x+1;
  68. }
  69. std::set<wchar_t> FontGlyphSheet::getCharacters() const {
  70. std::set<wchar_t> set;
  71. for (std::map<wchar_t,FontTextureGlyph>::const_iterator it = locations.begin(); it != locations.end(); it++) {
  72. set.insert(it->first);
  73. }
  74. return set;
  75. }
  76. void FontGlyphSheet::buildVisibleAscii() {
  77. std::set<wchar_t> chars;
  78. for (wchar_t i = 32; i < 127; i++) {
  79. chars.insert(i);
  80. }
  81. buildGlyphs(chars);
  82. }
  83. void FontGlyphSheet::addGlyphs(String extraCharacters) {
  84. extraCharacters.getWDataWithEncoding(String::ENCODING_UTF8);
  85. std::wstring& str = extraCharacters.w_contents;
  86. for (std::wstring::iterator it = str.begin(); it != str.end(); it++ ) {
  87. if (locations.find(*it) == locations.end()) {
  88. std::set<wchar_t> characterSet = getCharacters();
  89. characterSet.insert(str.begin(), str.end());
  90. buildGlyphs(characterSet);
  91. break;
  92. }
  93. }
  94. }
  95. void FontGlyphSheet::setSize(int size) {
  96. if (this->size == size) return;
  97. this->size = size;
  98. buildGlyphs(getCharacters());
  99. }
  100. void FontGlyphSheet::buildGlyphs(String charactersIn) {
  101. charactersIn.getWDataWithEncoding(String::ENCODING_UTF8);
  102. buildGlyphs(std::set<wchar_t>(charactersIn.w_contents.begin(), charactersIn.w_contents.end()));
  103. }
  104. void FontGlyphSheet::buildGlyphs(std::set<wchar_t> characters) {
  105. typedef std::set<wchar_t> character_container_t;
  106. typedef std::vector<GlyphData*> glyph_list_t;
  107. glyph_list_t glyphData;
  108. characters.insert('?');//Good backup character - make sure it's always present
  109. int shift = 0;
  110. Number scaleDown = 1.0f;
  111. FT_UInt height = size;
  112. if (mode == ALPHA_TEST) {
  113. shift = 2;
  114. scaleDown /= (1<<shift);
  115. height <<= shift;
  116. }
  117. FT_Face ftFace = font->getFace();
  118. FT_Set_Pixel_Sizes(ftFace, 0, height);
  119. //Get all the glyph data from freetype
  120. for (character_container_t::iterator it = characters.begin(); it != characters.end(); it++) {
  121. FT_Int32 load_flags = FT_LOAD_RENDER;
  122. // if (glyphMode == ALPHA_TEST) {
  123. // load_flags |= FT_LOAD_MONOCHROME;
  124. // }
  125. int error = FT_Load_Char(ftFace, *it, load_flags);
  126. if (error) {
  127. Logger::log("Failed to load glyph for codepoint %d '%lc' error %#x\n",*it,*it,error);
  128. }
  129. else {
  130. glyphData.push_back(new GlyphData());
  131. GlyphData& gd = *glyphData.back();
  132. FT_GlyphSlot slot = ftFace->glyph;
  133. gd.character = *it;
  134. gd.off_x = slot->bitmap_left;
  135. gd.off_y = slot->bitmap_top;
  136. gd.size_x = slot->bitmap.width;
  137. gd.size_y = slot->bitmap.rows;
  138. gd.advance.set(Number(slot->advance.x)/(64<<shift), Number(slot->advance.y)/(64<<shift));
  139. gd.pitch = slot->bitmap.pitch;
  140. int dataLength = slot->bitmap.pitch * gd.size_y;
  141. if (dataLength) {
  142. gd.data = new unsigned char[dataLength];
  143. memcpy(gd.data, slot->bitmap.buffer, dataLength);
  144. }
  145. }
  146. }
  147. std::sort(glyphData.begin(), glyphData.end(), fontGlyphSorter);
  148. //Compute the layout for the glyphs on the texture
  149. const int padding = 1;
  150. int sheet_width = 512, sheet_height;
  151. {
  152. int sheet_y = padding, sheet_x = padding;
  153. int row_size_y = 0;
  154. for (glyph_list_t::iterator it = glyphData.begin(); it != glyphData.end(); it++) {
  155. GlyphData& gd = **it;
  156. int size_x = gd.size_x + ((1<<shift)-1) >> shift;
  157. int size_y = gd.size_y + ((1<<shift)-1) >> shift;
  158. if (sheet_x + size_x + padding >= sheet_width) {
  159. sheet_x = padding;
  160. sheet_y += row_size_y + padding;
  161. row_size_y = 0;
  162. }
  163. if (size_y > row_size_y) {
  164. row_size_y = size_y;
  165. }
  166. gd.texture_u = sheet_x;
  167. gd.texture_v = sheet_y;
  168. sheet_x += size_x + padding;
  169. }
  170. sheet_y += row_size_y;
  171. sheet_height = pot_ceil(sheet_y);
  172. }
  173. //Paste all the glyphs onto the texture and calculate the render data
  174. locations.clear();
  175. Image* glyphsImage = new Image(sheet_width, sheet_height);
  176. for (glyph_list_t::iterator it = glyphData.begin(); it != glyphData.end(); it++) {
  177. GlyphData& gd = **it;
  178. int size_x = gd.size_x + ((1<<shift)-1) >> shift;
  179. int size_y = gd.size_y + ((1<<shift)-1) >> shift;
  180. for (int glyph_y = 0, i = 0; glyph_y < size_y; glyph_y++) {
  181. for (int glyph_x = 0; glyph_x < size_x; glyph_x++, i++) {
  182. unsigned char value = gd.data[i];
  183. int x = gd.texture_u + glyph_x;
  184. int y = gd.texture_v + glyph_y;
  185. if (mode == ALPHA_TEST) {
  186. const int SEARCH_RANGE = 2;
  187. //Don't quite use the full range of 128 either side
  188. const Number ALPHA_SCALE = 112.0f / (SEARCH_RANGE << shift);
  189. int scan_x0 = glyph_x - SEARCH_RANGE << shift;
  190. int scan_x1 = glyph_x + SEARCH_RANGE << shift;
  191. int scan_y0 = glyph_y - SEARCH_RANGE << shift;
  192. int scan_y1 = glyph_y + SEARCH_RANGE << shift;
  193. if (scan_x0 < 0) scan_x0 = 0;
  194. if (scan_y0 < 0) scan_y0 = 0;
  195. if (scan_x1 >= gd.size_x) scan_x1 = gd.size_x - 1;
  196. if (scan_y1 >= gd.size_y) scan_y1 = gd.size_y - 1;
  197. // value = (gd.data[glyph_y * gd.pitch + (glyph_x>>3)] >> (7-(glyph_x&7))) & 1;
  198. value = (gd.data[(glyph_y<<shift) * gd.pitch + (glyph_x<<shift)]>>7) & 1;
  199. Number dist = SEARCH_RANGE << shift;
  200. for (int scan_y = scan_y0; scan_y <= scan_y1; scan_y++) {
  201. int dy = (glyph_y<<shift) - scan_y;
  202. int dy2 = dy*dy;
  203. for (int scan_x = scan_x0; scan_x <= scan_x1; scan_x++) {
  204. int dx = (glyph_x<<shift) - scan_x;
  205. // int v = (gd.data[scan_y * gd.pitch + (scan_x>>3)] >> (7-(scan_x&7))) & 1;
  206. int v = (gd.data[scan_y * gd.pitch + scan_x] >> 7) & 1;
  207. if (v != value) {
  208. int dx2 = dx*dx;
  209. Number d(sqrt((Number)(dx2+dy2)));
  210. if (d < dist) {
  211. dist = d;
  212. }
  213. }
  214. }
  215. }
  216. value = (int)round(128 + ((value+value)-1) * (dist * ALPHA_SCALE));
  217. // value *= 255;
  218. }
  219. glyphsImage->setPixel(x, y, Color(255, 255, 255, value));
  220. }
  221. }
  222. Number x0 = gd.off_x * scaleDown;
  223. Number y0 = gd.off_y * scaleDown;
  224. Number x1 = x0 + gd.size_x * scaleDown;
  225. Number y1 = y0 - gd.size_y * scaleDown;
  226. Number u0 = Number(gd.texture_u) / sheet_width;
  227. Number v0 = 1.0f - Number(gd.texture_v) / sheet_height;
  228. Number u1 = Number(gd.texture_u + size_x) / sheet_width;
  229. Number v1 = 1.0f - Number(gd.texture_v + size_y) / sheet_height;
  230. FontTextureGlyph& glyph = locations[gd.character];
  231. glyph.offset[0].set(x0, y0);
  232. glyph.offset[1].set(x0, y1);
  233. glyph.offset[2].set(x1, y1);
  234. glyph.offset[3].set(x1, y0);
  235. glyph.texCoord[0].set(u0, v0);
  236. glyph.texCoord[1].set(u0, v1);
  237. glyph.texCoord[2].set(u1, v1);
  238. glyph.texCoord[3].set(u1, v0);
  239. glyph.advance = gd.advance;
  240. }
  241. MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
  242. if(texture)
  243. materialManager->deleteTexture(texture);
  244. texture = materialManager->createTextureFromImage(glyphsImage, true, materialManager->mipmapsDefault);
  245. delete glyphsImage;
  246. for (glyph_list_t::iterator it = glyphData.begin(); it != glyphData.end(); it++) delete *it;
  247. }
  248. int FontGlyphSheet::renderStringVertices(String textIn, std::vector<Vertex*>& vertices, int index) {
  249. textIn.getWDataWithEncoding(String::ENCODING_UTF8);
  250. std::wstring& text = textIn.w_contents;
  251. Vector2 cursor;
  252. wchar_t prevChar = -1;
  253. for (std::wstring::const_iterator it = text.begin(); it != text.end(); it++) {
  254. switch(*it) {
  255. case '\t':
  256. cursor.x = (int(cursor.x / tabWidth) + 1) * tabWidth;
  257. break;
  258. case '\n':
  259. cursor.x = 0;
  260. cursor.y += size;
  261. break;
  262. default:
  263. std::map<wchar_t,FontTextureGlyph>::iterator glyphLoc = locations.find(*it);
  264. if (glyphLoc == locations.end()) {
  265. Logger::log("Missing glyph for codepoint %d '%lc'\n",*it,*it);
  266. glyphLoc = locations.find('?');
  267. }
  268. // if (prevChar != -1) {
  269. // FT_Vector delta;
  270. // FT_Get_Kerning( ftFace, FT_Get_Char_Index(ftFace, prevChar), FT_Get_Char_Index(ftFace, *it), FT_KERNING_DEFAULT, &delta);
  271. // cursor.x += delta.x / Number(64);
  272. // }
  273. for (int i = 0; i < 4; i++, index++) {
  274. Vertex* vertex;
  275. if (index == vertices.size()) {
  276. vertices.push_back(vertex = new Vertex());
  277. }
  278. else {
  279. vertex = vertices[index];
  280. }
  281. vertex->set(cursor.x + glyphLoc->second.offset[i].x, cursor.y + glyphLoc->second.offset[i].y, 0);
  282. vertex->texCoord = glyphLoc->second.texCoord[i];
  283. }
  284. cursor += glyphLoc->second.advance;
  285. break;
  286. }
  287. prevChar = *it;
  288. }
  289. return index;
  290. }