Font.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * Copyright (c) 2006-2011 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include <common/config.h>
  21. #include "Font.h"
  22. #include <font/GlyphData.h>
  23. #include "Quad.h"
  24. #include <libraries/utf8/utf8.h>
  25. #include <common/math.h>
  26. #include <common/Matrix.h>
  27. #include <math.h>
  28. #include <sstream>
  29. #include <algorithm> // for max
  30. namespace love
  31. {
  32. namespace graphics
  33. {
  34. namespace opengl
  35. {
  36. Font::Font(love::font::Rasterizer * r, const Image::Filter& filter)
  37. : rasterizer(r), height(r->getHeight()), lineHeight(1), mSpacing(1), filter(filter)
  38. {
  39. r->retain();
  40. love::font::GlyphData * gd = r->getGlyphData(32);
  41. type = (gd->getFormat() == love::font::GlyphData::FORMAT_LUMINANCE_ALPHA ? FONT_TRUETYPE : FONT_IMAGE);
  42. delete gd;
  43. createTexture();
  44. }
  45. Font::~Font()
  46. {
  47. rasterizer->release();
  48. unloadVolatile();
  49. }
  50. void Font::createTexture()
  51. {
  52. texture_x = texture_y = rowHeight = 0;
  53. GLuint t;
  54. glGenTextures(1, &t);
  55. textures.push_back(t);
  56. glBindTexture(GL_TEXTURE_2D, t);
  57. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  58. (filter.mag == Image::FILTER_LINEAR) ? GL_LINEAR : GL_NEAREST);
  59. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  60. (filter.min == Image::FILTER_LINEAR) ? GL_LINEAR : GL_NEAREST);
  61. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  62. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  63. GLint format = (type == FONT_TRUETYPE ? GL_LUMINANCE_ALPHA : GL_RGBA);
  64. glTexImage2D(GL_TEXTURE_2D,
  65. 0,
  66. GL_RGBA,
  67. (GLsizei)TEXTURE_WIDTH,
  68. (GLsizei)TEXTURE_HEIGHT,
  69. 0,
  70. format,
  71. GL_UNSIGNED_BYTE,
  72. NULL);
  73. }
  74. Font::Glyph * Font::addGlyph(int glyph)
  75. {
  76. Glyph * g = new Glyph;
  77. g->list = glGenLists(1);
  78. if (g->list == 0)
  79. { // opengl failed to generate the list
  80. delete g;
  81. return NULL;
  82. }
  83. love::font::GlyphData *gd = rasterizer->getGlyphData(glyph);
  84. g->spacing = gd->getAdvance();
  85. int w = gd->getWidth();
  86. int h = gd->getHeight();
  87. if (texture_x + w > TEXTURE_WIDTH)
  88. { // out of space - new row!
  89. texture_x = 0;
  90. texture_y += rowHeight;
  91. rowHeight = 0;
  92. }
  93. if (texture_y + h > TEXTURE_HEIGHT)
  94. { // totally out of space - new texture!
  95. createTexture();
  96. }
  97. GLuint t = textures.back();
  98. glBindTexture(GL_TEXTURE_2D, t);
  99. glTexSubImage2D(GL_TEXTURE_2D, 0, texture_x, texture_y, w, h, (type == FONT_TRUETYPE ? GL_LUMINANCE_ALPHA : GL_RGBA), GL_UNSIGNED_BYTE, gd->getData());
  100. Quad::Viewport v;
  101. v.x = (float) texture_x;
  102. v.y = (float) texture_y;
  103. v.w = (float) w;
  104. v.h = (float) h;
  105. Quad * q = new Quad(v, (const float) TEXTURE_WIDTH, (const float) TEXTURE_HEIGHT);
  106. const vertex * verts = q->getVertices();
  107. glEnableClientState(GL_VERTEX_ARRAY);
  108. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  109. glVertexPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&verts[0].x);
  110. glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&verts[0].s);
  111. glNewList(g->list, GL_COMPILE);
  112. glBindTexture(GL_TEXTURE_2D, t);
  113. glPushMatrix();
  114. glTranslatef(static_cast<float>(gd->getBearingX()), static_cast<float>(-gd->getBearingY()), 0.0f);
  115. glDrawArrays(GL_QUADS, 0, 4);
  116. glPopMatrix();
  117. glEndList();
  118. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  119. glDisableClientState(GL_VERTEX_ARRAY);
  120. delete q;
  121. delete gd;
  122. texture_x += w;
  123. rowHeight = std::max(rowHeight, h);
  124. glyphs[glyph] = g;
  125. return g;
  126. }
  127. float Font::getHeight() const
  128. {
  129. return static_cast<float>(height);
  130. }
  131. void Font::print(std::string text, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
  132. {
  133. float dx = 0.0f; // spacing counter for newline handling
  134. glPushMatrix();
  135. Matrix t;
  136. t.setTransformation(ceil(x), ceil(y), angle, sx, sy, ox, oy, kx, ky);
  137. glMultMatrixf((const GLfloat*)t.getElements());
  138. try
  139. {
  140. utf8::iterator<std::string::iterator> i (text.begin(), text.begin(), text.end());
  141. utf8::iterator<std::string::iterator> end (text.end(), text.begin(), text.end());
  142. while (i != end)
  143. {
  144. int g = *i++;
  145. if (g == '\n')
  146. { // wrap newline, but do not print it
  147. glTranslatef(-dx, floor(getHeight() * getLineHeight() + 0.5f), 0);
  148. dx = 0.0f;
  149. continue;
  150. }
  151. Glyph * glyph = glyphs[g];
  152. if (!glyph) glyph = addGlyph(g);
  153. glPushMatrix();
  154. // 1.25 is magic line height for true type fonts
  155. if (type == FONT_TRUETYPE) glTranslatef(0, floor(getHeight() / 1.25f + 0.5f), 0);
  156. glCallList(glyph->list);
  157. glPopMatrix();
  158. glTranslatef(static_cast<GLfloat>(glyph->spacing), 0, 0);
  159. dx += glyph->spacing;
  160. }
  161. }
  162. catch (utf8::exception & e)
  163. {
  164. glPopMatrix();
  165. throw love::Exception("%s", e.what());
  166. }
  167. glPopMatrix();
  168. }
  169. void Font::print(char character, float x, float y)
  170. {
  171. Glyph * glyph = glyphs[character];
  172. if (!glyph) glyph = addGlyph(character);
  173. glPushMatrix();
  174. glTranslatef(x, floor(y+getHeight() + 0.5f), 0.0f);
  175. glCallList(glyph->list);
  176. glPopMatrix();
  177. }
  178. int Font::getWidth(const std::string & line)
  179. {
  180. if (line.size() == 0) return 0;
  181. int temp = 0;
  182. Glyph * g;
  183. try
  184. {
  185. utf8::iterator<std::string::const_iterator> i (line.begin(), line.begin(), line.end());
  186. utf8::iterator<std::string::const_iterator> end (line.end(), line.begin(), line.end());
  187. while (i != end)
  188. {
  189. int c = *i++;
  190. g = glyphs[c];
  191. if (!g) g = addGlyph(c);
  192. temp += static_cast<int>(g->spacing * mSpacing);
  193. }
  194. }
  195. catch (utf8::exception & e)
  196. {
  197. throw love::Exception("%s", e.what());
  198. }
  199. return temp;
  200. }
  201. int Font::getWidth(const char * line)
  202. {
  203. return this->getWidth(std::string(line));
  204. }
  205. int Font::getWidth(const char character)
  206. {
  207. Glyph * g = glyphs[character];
  208. if (!g) g = addGlyph(character);
  209. return g->spacing;
  210. }
  211. std::vector<std::string> Font::getWrap(const std::string text, float wrap, int * max_width)
  212. {
  213. using namespace std;
  214. const float width_space = static_cast<float>(getWidth(' '));
  215. vector<string> lines_to_draw;
  216. int maxw = 0;
  217. //split text at newlines
  218. istringstream iss( text );
  219. string line;
  220. while (getline(iss, line, '\n'))
  221. {
  222. // split line into words
  223. vector<string> words;
  224. istringstream word_iss(line);
  225. copy(istream_iterator<string>(word_iss), istream_iterator<string>(),
  226. back_inserter< vector<string> >(words));
  227. // put words back together until a wrap occurs
  228. float width = 0.0f;
  229. float oldwidth = 0.0f;
  230. ostringstream string_builder;
  231. vector<string>::const_iterator word_iter;
  232. for (word_iter = words.begin(); word_iter != words.end(); ++word_iter)
  233. {
  234. string word( *word_iter );
  235. width += getWidth( word );
  236. // on wordwrap, push line to line buffer and clear string builder
  237. if (width >= wrap && oldwidth > 0)
  238. {
  239. int realw = width;
  240. lines_to_draw.push_back( string_builder.str() );
  241. string_builder.str( "" );
  242. width = static_cast<float>(getWidth( word ));
  243. realw -= width;
  244. if (realw > maxw)
  245. maxw = realw;
  246. }
  247. string_builder << word << " ";
  248. width += width_space;
  249. oldwidth = width;
  250. }
  251. // push last line
  252. if (width > maxw)
  253. maxw = width;
  254. lines_to_draw.push_back( string_builder.str() );
  255. }
  256. if (max_width)
  257. *max_width = maxw;
  258. return lines_to_draw;
  259. }
  260. void Font::setLineHeight(float height)
  261. {
  262. this->lineHeight = height;
  263. }
  264. float Font::getLineHeight() const
  265. {
  266. return lineHeight;
  267. }
  268. void Font::setSpacing(float amount)
  269. {
  270. mSpacing = amount;
  271. }
  272. float Font::getSpacing() const
  273. {
  274. return mSpacing;
  275. }
  276. bool Font::loadVolatile()
  277. {
  278. createTexture();
  279. return true;
  280. }
  281. void Font::unloadVolatile()
  282. {
  283. // nuke everything from orbit
  284. std::map<int, Glyph *>::iterator it = glyphs.begin();
  285. Glyph * g;
  286. while (it != glyphs.end())
  287. {
  288. g = it->second;
  289. glDeleteLists(g->list, 1);
  290. delete g;
  291. glyphs.erase(it++);
  292. }
  293. std::vector<GLuint>::iterator iter = textures.begin();
  294. while (iter != textures.end())
  295. {
  296. glDeleteTextures(1, (GLuint*)&*iter);
  297. iter++;
  298. }
  299. textures.clear();
  300. }
  301. } // opengl
  302. } // graphics
  303. } // love