Font.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 = 1;
  53. rowHeight = 0;
  54. GLuint t;
  55. glGenTextures(1, &t);
  56. textures.push_back(t);
  57. glBindTexture(GL_TEXTURE_2D, t);
  58. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  59. (filter.mag == Image::FILTER_LINEAR) ? GL_LINEAR : GL_NEAREST);
  60. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  61. (filter.min == Image::FILTER_LINEAR) ? GL_LINEAR : GL_NEAREST);
  62. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  63. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  64. GLint format = (type == FONT_TRUETYPE ? GL_LUMINANCE_ALPHA : GL_RGBA);
  65. glTexImage2D(GL_TEXTURE_2D,
  66. 0,
  67. GL_RGBA,
  68. (GLsizei)TEXTURE_WIDTH,
  69. (GLsizei)TEXTURE_HEIGHT,
  70. 0,
  71. format,
  72. GL_UNSIGNED_BYTE,
  73. NULL);
  74. }
  75. Font::Glyph * Font::addGlyph(int glyph)
  76. {
  77. Glyph * g = new Glyph;
  78. g->list = glGenLists(1);
  79. if (g->list == 0)
  80. { // opengl failed to generate the list
  81. delete g;
  82. return NULL;
  83. }
  84. love::font::GlyphData *gd = rasterizer->getGlyphData(glyph);
  85. g->spacing = gd->getAdvance();
  86. int w = gd->getWidth()+1;
  87. int h = gd->getHeight()+1;
  88. if (texture_x + w > TEXTURE_WIDTH)
  89. { // out of space - new row!
  90. texture_x = 1;
  91. texture_y += rowHeight+1;
  92. rowHeight = 0;
  93. }
  94. if (texture_y + h > TEXTURE_HEIGHT)
  95. { // totally out of space - new texture!
  96. createTexture();
  97. }
  98. GLuint t = textures.back();
  99. glBindTexture(GL_TEXTURE_2D, t);
  100. glTexSubImage2D(GL_TEXTURE_2D, 0, texture_x, texture_y, w-1, h-1, (type == FONT_TRUETYPE ? GL_LUMINANCE_ALPHA : GL_RGBA), GL_UNSIGNED_BYTE, gd->getData());
  101. Quad::Viewport v;
  102. v.x = (float) texture_x;
  103. v.y = (float) texture_y;
  104. v.w = (float) w;
  105. v.h = (float) h;
  106. Quad * q = new Quad(v, (const float) TEXTURE_WIDTH, (const float) TEXTURE_HEIGHT);
  107. const vertex * verts = q->getVertices();
  108. glEnableClientState(GL_VERTEX_ARRAY);
  109. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  110. glVertexPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&verts[0].x);
  111. glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&verts[0].s);
  112. glNewList(g->list, GL_COMPILE);
  113. glBindTexture(GL_TEXTURE_2D, t);
  114. glPushMatrix();
  115. glTranslatef(static_cast<float>(gd->getBearingX()), static_cast<float>(-gd->getBearingY()), 0.0f);
  116. glDrawArrays(GL_QUADS, 0, 4);
  117. glPopMatrix();
  118. glEndList();
  119. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  120. glDisableClientState(GL_VERTEX_ARRAY);
  121. delete q;
  122. delete gd;
  123. texture_x += w;
  124. rowHeight = std::max(rowHeight, h);
  125. glyphs[glyph] = g;
  126. return g;
  127. }
  128. float Font::getHeight() const
  129. {
  130. return static_cast<float>(height);
  131. }
  132. void Font::print(std::string text, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
  133. {
  134. float dx = 0.0f; // spacing counter for newline handling
  135. glPushMatrix();
  136. Matrix t;
  137. t.setTransformation(ceil(x), ceil(y), angle, sx, sy, ox, oy, kx, ky);
  138. glMultMatrixf((const GLfloat*)t.getElements());
  139. try
  140. {
  141. utf8::iterator<std::string::iterator> i (text.begin(), text.begin(), text.end());
  142. utf8::iterator<std::string::iterator> end (text.end(), text.begin(), text.end());
  143. while (i != end)
  144. {
  145. int g = *i++;
  146. if (g == '\n')
  147. { // wrap newline, but do not print it
  148. glTranslatef(-dx, floor(getHeight() * getLineHeight() + 0.5f), 0);
  149. dx = 0.0f;
  150. continue;
  151. }
  152. Glyph * glyph = glyphs[g];
  153. if (!glyph) glyph = addGlyph(g);
  154. glPushMatrix();
  155. // 1.25 is magic line height for true type fonts
  156. if (type == FONT_TRUETYPE) glTranslatef(0, floor(getHeight() / 1.25f + 0.5f), 0);
  157. glCallList(glyph->list);
  158. glPopMatrix();
  159. glTranslatef(static_cast<GLfloat>(glyph->spacing), 0, 0);
  160. dx += glyph->spacing;
  161. }
  162. }
  163. catch (utf8::exception & e)
  164. {
  165. glPopMatrix();
  166. throw love::Exception("%s", e.what());
  167. }
  168. glPopMatrix();
  169. }
  170. void Font::print(char character, float x, float y)
  171. {
  172. Glyph * glyph = glyphs[character];
  173. if (!glyph) glyph = addGlyph(character);
  174. glPushMatrix();
  175. glTranslatef(x, floor(y+getHeight() + 0.5f), 0.0f);
  176. glCallList(glyph->list);
  177. glPopMatrix();
  178. }
  179. int Font::getWidth(const std::string & line)
  180. {
  181. if (line.size() == 0) return 0;
  182. int temp = 0;
  183. Glyph * g;
  184. try
  185. {
  186. utf8::iterator<std::string::const_iterator> i (line.begin(), line.begin(), line.end());
  187. utf8::iterator<std::string::const_iterator> end (line.end(), line.begin(), line.end());
  188. while (i != end)
  189. {
  190. int c = *i++;
  191. g = glyphs[c];
  192. if (!g) g = addGlyph(c);
  193. temp += static_cast<int>(g->spacing * mSpacing);
  194. }
  195. }
  196. catch (utf8::exception & e)
  197. {
  198. throw love::Exception("%s", e.what());
  199. }
  200. return temp;
  201. }
  202. int Font::getWidth(const char * line)
  203. {
  204. return this->getWidth(std::string(line));
  205. }
  206. int Font::getWidth(const char character)
  207. {
  208. Glyph * g = glyphs[character];
  209. if (!g) g = addGlyph(character);
  210. return g->spacing;
  211. }
  212. std::vector<std::string> Font::getWrap(const std::string text, float wrap, int * max_width)
  213. {
  214. using namespace std;
  215. const float width_space = static_cast<float>(getWidth(' '));
  216. vector<string> lines_to_draw;
  217. int maxw = 0;
  218. //split text at newlines
  219. istringstream iss( text );
  220. string line;
  221. while (getline(iss, line, '\n'))
  222. {
  223. // split line into words
  224. vector<string> words;
  225. istringstream word_iss(line);
  226. copy(istream_iterator<string>(word_iss), istream_iterator<string>(),
  227. back_inserter< vector<string> >(words));
  228. // put words back together until a wrap occurs
  229. float width = 0.0f;
  230. float oldwidth = 0.0f;
  231. ostringstream string_builder;
  232. vector<string>::const_iterator word_iter;
  233. for (word_iter = words.begin(); word_iter != words.end(); ++word_iter)
  234. {
  235. string word( *word_iter );
  236. width += getWidth( word );
  237. // on wordwrap, push line to line buffer and clear string builder
  238. if (width >= wrap && oldwidth > 0)
  239. {
  240. int realw = (int) width;
  241. lines_to_draw.push_back( string_builder.str() );
  242. string_builder.str( "" );
  243. width = static_cast<float>(getWidth( word ));
  244. realw -= (int) width;
  245. if (realw > maxw)
  246. maxw = realw;
  247. }
  248. string_builder << word << " ";
  249. width += width_space;
  250. oldwidth = width;
  251. }
  252. // push last line
  253. if (width > maxw)
  254. maxw = (int) width;
  255. lines_to_draw.push_back( string_builder.str() );
  256. }
  257. if (max_width)
  258. *max_width = maxw;
  259. return lines_to_draw;
  260. }
  261. void Font::setLineHeight(float height)
  262. {
  263. this->lineHeight = height;
  264. }
  265. float Font::getLineHeight() const
  266. {
  267. return lineHeight;
  268. }
  269. void Font::setSpacing(float amount)
  270. {
  271. mSpacing = amount;
  272. }
  273. float Font::getSpacing() const
  274. {
  275. return mSpacing;
  276. }
  277. bool Font::loadVolatile()
  278. {
  279. createTexture();
  280. return true;
  281. }
  282. void Font::unloadVolatile()
  283. {
  284. // nuke everything from orbit
  285. std::map<int, Glyph *>::iterator it = glyphs.begin();
  286. Glyph * g;
  287. while (it != glyphs.end())
  288. {
  289. g = it->second;
  290. glDeleteLists(g->list, 1);
  291. delete g;
  292. glyphs.erase(it++);
  293. }
  294. std::vector<GLuint>::iterator iter = textures.begin();
  295. while (iter != textures.end())
  296. {
  297. glDeleteTextures(1, (GLuint*)&*iter);
  298. iter++;
  299. }
  300. textures.clear();
  301. }
  302. } // opengl
  303. } // graphics
  304. } // love