Font.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "Font.h"
  21. #include <font/GlyphData.h>
  22. #include <common/math.h>
  23. #include <math.h>
  24. namespace love
  25. {
  26. namespace graphics
  27. {
  28. namespace opengl
  29. {
  30. Font::Font(love::font::Rasterizer * r, const Image::Filter& filter)
  31. : rasterizer(r), height(r->getHeight()), lineHeight(1), mSpacing(1)
  32. {
  33. type = FONT_UNKNOWN;
  34. love::font::GlyphData * gd;
  35. for(unsigned int i = 0; i < MAX_CHARS; i++)
  36. {
  37. gd = r->getGlyphData(i);
  38. widths[i] = gd->getWidth();
  39. spacing[i] = gd->getAdvance();
  40. bearingX[i] = gd->getBearingX();
  41. bearingY[i] = gd->getBearingY();
  42. if (type == FONT_UNKNOWN) type = (gd->getFormat() == love::font::GlyphData::FORMAT_LUMINANCE_ALPHA ? FONT_TRUETYPE : FONT_IMAGE);
  43. }
  44. }
  45. Font::~Font()
  46. {
  47. }
  48. float Font::getHeight() const
  49. {
  50. return static_cast<float>(height);
  51. }
  52. void Font::print(std::string text, float x, float y, float angle, float sx, float sy) const
  53. {
  54. /*float dx = 0.0f; // spacing counter for newline handling
  55. glPushMatrix();
  56. glTranslatef(ceil(x), ceil(y), 0.0f);
  57. glRotatef(LOVE_TODEG(angle), 0, 0, 1.0f);
  58. glScalef(sx, sy, 1.0f);
  59. for (unsigned int i = 0; i < text.size(); i++) {
  60. unsigned char g = (unsigned char)text[i];
  61. if (g == '\n') { // wrap newline, but do not print it
  62. glTranslatef(-dx, floor(getHeight() * getLineHeight() + 0.5f), 0);
  63. dx = 0.0f;
  64. continue;
  65. }
  66. if (!glyphs[g]) g = 32; // space
  67. glPushMatrix();
  68. // 1.25 is magic line height for true type fonts
  69. if (type == FONT_TRUETYPE) glTranslatef(0, floor(getHeight() / 1.25f + 0.5f), 0);
  70. glyphs[g]->draw(0, 0, 0, 1, 1, 0, 0);
  71. glPopMatrix();
  72. glTranslatef(static_cast<GLfloat>(spacing[g]), 0, 0);
  73. dx += spacing[g];
  74. }
  75. glPopMatrix();*/
  76. }
  77. void Font::print(char character, float x, float y) const
  78. {
  79. /*if (!glyphs[(int)character]) character = ' ';
  80. glPushMatrix();
  81. glTranslatef(x, floor(y+getHeight() + 0.5f), 0.0f);
  82. glCallList(list+character);
  83. glPopMatrix();*/
  84. }
  85. int Font::getWidth(const std::string & line) const
  86. {
  87. if(line.size() == 0) return 0;
  88. int temp = 0;
  89. for(unsigned int i = 0; i < line.size(); i++)
  90. {
  91. temp += static_cast<int>((spacing[(int)line[i]] * mSpacing));
  92. }
  93. return temp;
  94. }
  95. int Font::getWidth(const char * line) const
  96. {
  97. return this->getWidth(std::string(line));
  98. }
  99. int Font::getWidth(const char character) const
  100. {
  101. return spacing[(int)character];
  102. }
  103. int Font::getWrap(const std::string & line, float wrap, int * lines) const
  104. {
  105. if(line.size() == 0) return 0;
  106. int maxw = 0;
  107. int linen = 1;
  108. int temp = 0;
  109. std::string text;
  110. for(unsigned int i = 0; i < line.size(); i++)
  111. {
  112. if(temp > wrap && text.find(" ") != std::string::npos)
  113. {
  114. unsigned int space = text.find_last_of(' ');
  115. std::string tmp = text.substr(0, space);
  116. int w = getWidth(tmp);
  117. if(w > maxw) maxw = w;
  118. text = text.substr(space+1);
  119. temp = getWidth(text);
  120. linen++;
  121. }
  122. temp += static_cast<int>((spacing[(int)line[i]] * mSpacing));
  123. text += line[i];
  124. }
  125. if(temp > maxw) maxw = temp;
  126. if(lines) *lines = linen;
  127. return maxw;
  128. }
  129. int Font::getWrap(const char * line, float wrap, int * lines) const
  130. {
  131. return getWrap(std::string(line), wrap, lines);
  132. }
  133. void Font::setLineHeight(float height)
  134. {
  135. this->lineHeight = height;
  136. }
  137. float Font::getLineHeight() const
  138. {
  139. return lineHeight;
  140. }
  141. void Font::setSpacing(float amount)
  142. {
  143. mSpacing = amount;
  144. }
  145. float Font::getSpacing() const
  146. {
  147. return mSpacing;
  148. }
  149. bool Font::loadVolatile()
  150. {
  151. /*// reload all glyphs
  152. for(unsigned int i = 0; i < MAX_CHARS; i++)
  153. {
  154. glyphs[i]->load();
  155. glNewList(list + i, GL_COMPILE);
  156. glyphs[i]->draw(0, 0, 0, 1, 1, 0, 0);
  157. glEndList();
  158. }
  159. return true;*/
  160. }
  161. void Font::unloadVolatile()
  162. {
  163. /*// delete the glyphs
  164. glDeleteLists(list, MAX_CHARS);*/
  165. }
  166. } // opengl
  167. } // graphics
  168. } // love