PageRenderTime 117ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llrender/llfontgl.h

https://bitbucket.org/lindenlab/viewer-beta/
C Header | 221 lines | 134 code | 44 blank | 43 comment | 0 complexity | 332a4bc4a497708907bc3368ddb05297 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llfontgl.h
  3. * @author Doug Soo
  4. * @brief Wrapper around FreeType
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #ifndef LL_LLFONTGL_H
  28. #define LL_LLFONTGL_H
  29. #include "llcoord.h"
  30. #include "llfontregistry.h"
  31. #include "llimagegl.h"
  32. #include "llpointer.h"
  33. #include "llrect.h"
  34. #include "v2math.h"
  35. class LLColor4;
  36. // Key used to request a font.
  37. class LLFontDescriptor;
  38. class LLFontFreetype;
  39. // Structure used to store previously requested fonts.
  40. class LLFontRegistry;
  41. class LLFontGL
  42. {
  43. public:
  44. enum HAlign
  45. {
  46. // Horizontal location of x,y coord to render.
  47. LEFT = 0, // Left align
  48. RIGHT = 1, // Right align
  49. HCENTER = 2, // Center
  50. };
  51. enum VAlign
  52. {
  53. // Vertical location of x,y coord to render.
  54. TOP = 3, // Top align
  55. VCENTER = 4, // Center
  56. BASELINE = 5, // Baseline
  57. BOTTOM = 6 // Bottom
  58. };
  59. enum StyleFlags
  60. {
  61. // text style to render. May be combined (these are bit flags)
  62. NORMAL = 0x00,
  63. BOLD = 0x01,
  64. ITALIC = 0x02,
  65. UNDERLINE = 0x04
  66. };
  67. enum ShadowType
  68. {
  69. NO_SHADOW,
  70. DROP_SHADOW,
  71. DROP_SHADOW_SOFT
  72. };
  73. LLFontGL();
  74. ~LLFontGL();
  75. void reset(); // Reset a font after GL cleanup. ONLY works on an already loaded font.
  76. void destroyGL();
  77. BOOL loadFace(const std::string& filename, F32 point_size, const F32 vert_dpi, const F32 horz_dpi, const S32 components, BOOL is_fallback);
  78. S32 render(const LLWString &text, S32 begin_offset,
  79. const LLRect& rect,
  80. const LLColor4 &color,
  81. HAlign halign = LEFT, VAlign valign = BASELINE,
  82. U8 style = NORMAL, ShadowType shadow = NO_SHADOW,
  83. S32 max_chars = S32_MAX,
  84. F32* right_x=NULL,
  85. BOOL use_ellipses = FALSE) const;
  86. S32 render(const LLWString &text, S32 begin_offset,
  87. F32 x, F32 y,
  88. const LLColor4 &color,
  89. HAlign halign = LEFT, VAlign valign = BASELINE,
  90. U8 style = NORMAL, ShadowType shadow = NO_SHADOW,
  91. S32 max_chars = S32_MAX, S32 max_pixels = S32_MAX,
  92. F32* right_x=NULL,
  93. BOOL use_ellipses = FALSE) const;
  94. S32 render(const LLWString &text, S32 begin_offset, F32 x, F32 y, const LLColor4 &color) const;
  95. // renderUTF8 does a conversion, so is slower!
  96. S32 renderUTF8(const std::string &text, S32 begin_offset, F32 x, F32 y, const LLColor4 &color, HAlign halign, VAlign valign, U8 style, ShadowType shadow, S32 max_chars, S32 max_pixels, F32* right_x, BOOL use_ellipses) const;
  97. S32 renderUTF8(const std::string &text, S32 begin_offset, S32 x, S32 y, const LLColor4 &color) const;
  98. S32 renderUTF8(const std::string &text, S32 begin_offset, S32 x, S32 y, const LLColor4 &color, HAlign halign, VAlign valign, U8 style = NORMAL, ShadowType shadow = NO_SHADOW) const;
  99. // font metrics - override for LLFontFreetype that returns units of virtual pixels
  100. F32 getLineHeight() const;
  101. F32 getAscenderHeight() const;
  102. F32 getDescenderHeight() const;
  103. S32 getWidth(const std::string& utf8text) const;
  104. S32 getWidth(const llwchar* wchars) const;
  105. S32 getWidth(const std::string& utf8text, S32 offset, S32 max_chars ) const;
  106. S32 getWidth(const llwchar* wchars, S32 offset, S32 max_chars) const;
  107. F32 getWidthF32(const std::string& utf8text) const;
  108. F32 getWidthF32(const llwchar* wchars) const;
  109. F32 getWidthF32(const std::string& text, S32 offset, S32 max_chars ) const;
  110. F32 getWidthF32(const llwchar* wchars, S32 offset, S32 max_chars) const;
  111. // The following are called often, frequently with large buffers, so do not use a string interface
  112. // Returns the max number of complete characters from text (up to max_chars) that can be drawn in max_pixels
  113. typedef enum e_word_wrap_style
  114. {
  115. ONLY_WORD_BOUNDARIES,
  116. WORD_BOUNDARY_IF_POSSIBLE,
  117. ANYWHERE
  118. } EWordWrapStyle ;
  119. S32 maxDrawableChars(const llwchar* wchars, F32 max_pixels, S32 max_chars = S32_MAX, EWordWrapStyle end_on_word_boundary = ANYWHERE) const;
  120. // Returns the index of the first complete characters from text that can be drawn in max_pixels
  121. // given that the character at start_pos should be the last character (or as close to last as possible).
  122. S32 firstDrawableChar(const llwchar* wchars, F32 max_pixels, S32 text_len, S32 start_pos=S32_MAX, S32 max_chars = S32_MAX) const;
  123. // Returns the index of the character closest to pixel position x (ignoring text to the right of max_pixels and max_chars)
  124. S32 charFromPixelOffset(const llwchar* wchars, S32 char_offset, F32 x, F32 max_pixels=F32_MAX, S32 max_chars = S32_MAX, BOOL round = TRUE) const;
  125. const LLFontDescriptor& getFontDesc() const;
  126. static void initClass(F32 screen_dpi, F32 x_scale, F32 y_scale, const std::string& app_dir, const std::vector<std::string>& xui_paths, bool create_gl_textures = true);
  127. // Load sans-serif, sans-serif-small, etc.
  128. // Slow, requires multiple seconds to load fonts.
  129. static bool loadDefaultFonts();
  130. static void destroyDefaultFonts();
  131. static void destroyAllGL();
  132. // Takes a string with potentially several flags, i.e. "NORMAL|BOLD|ITALIC"
  133. static U8 getStyleFromString(const std::string &style);
  134. static std::string getStringFromStyle(U8 style);
  135. static std::string nameFromFont(const LLFontGL* fontp);
  136. static std::string sizeFromFont(const LLFontGL* fontp);
  137. static std::string nameFromHAlign(LLFontGL::HAlign align);
  138. static LLFontGL::HAlign hAlignFromName(const std::string& name);
  139. static std::string nameFromVAlign(LLFontGL::VAlign align);
  140. static LLFontGL::VAlign vAlignFromName(const std::string& name);
  141. static void setFontDisplay(BOOL flag) { sDisplayFont = flag; }
  142. static LLFontGL* getFontMonospace();
  143. static LLFontGL* getFontSansSerifSmall();
  144. static LLFontGL* getFontSansSerif();
  145. static LLFontGL* getFontSansSerifBig();
  146. static LLFontGL* getFontSansSerifHuge();
  147. static LLFontGL* getFontSansSerifBold();
  148. static LLFontGL* getFontExtChar();
  149. static LLFontGL* getFont(const LLFontDescriptor& desc);
  150. // Use with legacy names like "SANSSERIF_SMALL" or "OCRA"
  151. static LLFontGL* getFontByName(const std::string& name);
  152. static LLFontGL* getFontDefault(); // default fallback font
  153. static std::string getFontPathLocal();
  154. static std::string getFontPathSystem();
  155. static LLCoordFont sCurOrigin;
  156. static std::vector<LLCoordFont> sOriginStack;
  157. static LLColor4 sShadowColor;
  158. static F32 sVertDPI;
  159. static F32 sHorizDPI;
  160. static F32 sScaleX;
  161. static F32 sScaleY;
  162. static BOOL sDisplayFont ;
  163. static std::string sAppDir; // For loading fonts
  164. private:
  165. friend class LLFontRegistry;
  166. friend class LLTextBillboard;
  167. friend class LLHUDText;
  168. LLFontGL(const LLFontGL &source);
  169. LLFontGL &operator=(const LLFontGL &source);
  170. LLFontDescriptor mFontDescriptor;
  171. LLPointer<LLFontFreetype> mFontFreetype;
  172. void renderQuad(LLVector3* vertex_out, LLVector2* uv_out, LLColor4U* colors_out, const LLRectf& screen_rect, const LLRectf& uv_rect, const LLColor4U& color, F32 slant_amt) const;
  173. void drawGlyph(S32& glyph_count, LLVector3* vertex_out, LLVector2* uv_out, LLColor4U* colors_out, const LLRectf& screen_rect, const LLRectf& uv_rect, const LLColor4U& color, U8 style, ShadowType shadow, F32 drop_shadow_fade) const;
  174. // Registry holds all instantiated fonts.
  175. static LLFontRegistry* sFontRegistry;
  176. };
  177. #endif