llfontgl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. * @file llfontgl.h
  3. * @author Doug Soo
  4. * @brief Wrapper around FreeType
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewergpl$
  7. *
  8. * Copyright (c) 2001-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLFONTGL_H
  34. #define LL_LLFONTGL_H
  35. #include "llcoord.h"
  36. #include "hbfastmap.h"
  37. #include "llfontregistry.h"
  38. #include "llimagegl.h"
  39. class LLColor4;
  40. class LLFontDescriptor;
  41. class LLFontFreetype;
  42. class LLGLTexture;
  43. // Structure used to store previously requested fonts.
  44. class LLFontRegistry;
  45. // IMPORTANT: if you change this, also change LLFontGL::getFont() accordingly !
  46. enum LLFONT_ID
  47. {
  48. LLFONT_SANSSERIF,
  49. LLFONT_SANSSERIF_SMALL,
  50. LLFONT_SANSSERIF_LARGE,
  51. LLFONT_SMALL,
  52. LLFONT_EMOJI,
  53. };
  54. class LLFontGL
  55. {
  56. friend class LLFontRegistry;
  57. friend class LLTextBillboard;
  58. friend class LLHUDText;
  59. protected:
  60. LOG_CLASS(LLFontGL);
  61. public:
  62. enum HAlign
  63. {
  64. // Horizontal location of x, y coord to render.
  65. LEFT = 0, // Left align
  66. RIGHT = 1, // Right align
  67. HCENTER = 2, // Center
  68. };
  69. enum VAlign
  70. {
  71. // Vertical location of x, y coord to render.
  72. TOP = 3, // Top align
  73. VCENTER = 4, // Center
  74. BASELINE = 5, // Baseline
  75. BOTTOM = 6 // Bottom
  76. };
  77. enum StyleFlags : U32
  78. {
  79. // Text style to render. May be combined (these are bit flags)
  80. NORMAL = 0,
  81. BOLD = 1,
  82. ITALIC = 2,
  83. UNDERLINE = 4,
  84. DROP_SHADOW = 8,
  85. DROP_SHADOW_SOFT = 16
  86. };
  87. // Takes a string with potentially several flags, i.e. "NORMAL|BOLD|ITALIC"
  88. static U8 getStyleFromString(const std::string& style);
  89. LLFontGL() = default;
  90. ~LLFontGL();
  91. LLFontGL(const LLFontGL&) = delete;
  92. LLFontGL& operator=(const LLFontGL&) = delete;
  93. void init(); // Internal init, or reinitialization
  94. // Reset a font after GL cleanup. ONLY works on an already loaded font.
  95. void reset();
  96. void destroyGL();
  97. bool loadFace(const std::string& filename, F32 point_size,
  98. F32 vert_dpi, F32 horz_dpi, bool is_fallback);
  99. S32 render(const LLWString& text, S32 begin_offset, F32 x, F32 y,
  100. const LLColor4& color,
  101. HAlign halign = LEFT, VAlign valign = BASELINE,
  102. U8 style = NORMAL,
  103. S32 max_chars = S32_MAX, S32 max_pixels = S32_MAX,
  104. F32* right_x = NULL,
  105. bool use_embedded = false, bool use_ellipses = false,
  106. bool use_color = true) const;
  107. S32 render(const LLWString& text, S32 begin_offset, F32 x, F32 y,
  108. const LLColor4& color) const;
  109. // The renderUTF8() methods perform a conversion, so they are slower...
  110. S32 renderUTF8(const std::string& text, S32 begin_offset, F32 x, F32 y,
  111. const LLColor4& color, HAlign halign, VAlign valign,
  112. U8 style, S32 max_chars, S32 max_pixels, F32* right_x,
  113. bool use_ellipses) const;
  114. S32 renderUTF8(const std::string& text, S32 begin_offset, S32 x, S32 y,
  115. const LLColor4& color) const;
  116. S32 renderUTF8(const std::string& text, S32 begin_offset, S32 x, S32 y,
  117. const LLColor4& color,
  118. HAlign halign, VAlign valign, U8 style = NORMAL) const;
  119. // Font metrics - override for LLFontFreetype that returns units of virtual
  120. // pixels
  121. F32 getAscenderHeight() const;
  122. F32 getDescenderHeight() const;
  123. F32 getLineHeight() const;
  124. S32 getWidth(const std::string& utf8text) const;
  125. S32 getWidth(const llwchar* wchars) const;
  126. S32 getWidth(const std::string& utf8text, S32 offset, S32 max_chars) const;
  127. S32 getWidth(const llwchar* wchars, S32 offset, S32 max_chars,
  128. bool use_embedded = false) const;
  129. F32 getWidthF32(const std::string& utf8text) const;
  130. F32 getWidthF32(const llwchar* wchars) const;
  131. F32 getWidthF32(const std::string& text, S32 offset, S32 max_chars) const;
  132. F32 getWidthF32(const llwchar* wchars, S32 offset, S32 max_chars,
  133. bool use_embedded = false) const;
  134. // The following are called often, frequently with large buffers, so do not
  135. // use a string interface
  136. // Returns the max number of complete characters from text (up to
  137. // max_chars) that can be drawn in max_pixels
  138. S32 maxDrawableChars(const llwchar* wchars, F32 max_pixels,
  139. S32 max_chars = S32_MAX,
  140. bool end_on_word_boundary = false,
  141. bool use_embedded = false,
  142. F32* drawn_pixels = NULL) const;
  143. // Returns the index of the first complete characters from text that can be
  144. // drawn in max_pixels given that the character at start_pos should be the
  145. // last character (or as close to last as possible).
  146. S32 firstDrawableChar(const llwchar* wchars, F32 max_pixels,
  147. S32 text_len, S32 start_pos = S32_MAX,
  148. S32 max_chars = S32_MAX) const;
  149. // Returns the index of the character closest to pixel position x (ignoring
  150. // text to the right of max_pixels and max_chars)
  151. S32 charFromPixelOffset(const llwchar* wchars, S32 char_offset, F32 x,
  152. F32 max_pixels = F32_MAX, S32 max_chars = S32_MAX,
  153. bool round = true, bool embedded = false) const;
  154. LL_INLINE const LLFontDescriptor& getFontDesc() const
  155. {
  156. return mFontDescriptor;
  157. }
  158. void generateASCIIglyphs();
  159. static void initClass(F32 screen_dpi, F32 x_scale, F32 y_scale,
  160. const std::vector<std::string>& xui_paths,
  161. bool create_gl_textures = true);
  162. // Load sans-serif, sans-serif-small, etc.
  163. // Slow, requires multiple seconds to load fonts.
  164. static bool loadDefaultFonts();
  165. static void destroyDefaultFonts();
  166. static void destroyAllGL();
  167. LLImageGL* getImageGL() const;
  168. void addEmbeddedChar(llwchar wc, LLGLTexture* image,
  169. const std::string& label) const;
  170. void addEmbeddedChar(llwchar wc, LLGLTexture* image,
  171. const LLWString& label) const;
  172. void removeEmbeddedChar(llwchar wc) const;
  173. static std::string nameFromFont(const LLFontGL* fontp);
  174. static const std::string& nameFromHAlign(LLFontGL::HAlign align);
  175. static LLFontGL::HAlign hAlignFromName(const std::string& name);
  176. static const std::string& nameFromVAlign(LLFontGL::VAlign align);
  177. static LLFontGL::VAlign vAlignFromName(const std::string& name);
  178. static void setFontDisplay(bool flag) { sDisplayFont = flag; }
  179. static LLFontGL* getFontMonospace();
  180. static LLFontGL* getFontSansSerifSmall();
  181. static LLFontGL* getFontSansSerif();
  182. static LLFontGL* getFontSansSerifLarge();
  183. static LLFontGL* getFontSansSerifHuge();
  184. static LLFontGL* getFontSansSerifBold();
  185. static LLFontGL* getFontEmoji();
  186. static LLFontGL* getFont(const LLFontDescriptor& desc,
  187. bool normalize = true);
  188. // Only to try and use other fonts than the default ones. HB
  189. static LLFontGL* getFont(const char* name, const char* size = NULL,
  190. U8 style = 0);
  191. // Use with names like "SANSSERIF_SMALL"
  192. static LLFontGL* getFont(const std::string& name);
  193. // Use with font ids like LLFONT_SANSSERIF_SMALL
  194. static LLFontGL* getFont(S32 font_id);
  195. // Fallback to sans serif as default font
  196. LL_INLINE static LLFontGL* getFontDefault() { return getFontSansSerif(); }
  197. LL_INLINE static void setColorUse(bool allow) { sAllowColorUse = allow; }
  198. private:
  199. struct embedded_data_t
  200. {
  201. embedded_data_t(LLImageGL* image, const LLWString& label)
  202. : mImage(image),
  203. mLabel(label)
  204. {
  205. }
  206. LLPointer<LLImageGL> mImage;
  207. LLWString mLabel;
  208. };
  209. // New, optimized routines for texts without embedded data:
  210. S32 newrender(const LLWString& wstr, S32 begin_offset, F32 x, F32 y,
  211. const LLColor4& color, HAlign halign, VAlign valign,
  212. U8 style, S32 max_chars, S32 max_pixels, F32* right_x,
  213. bool use_ellipses, bool use_color) const;
  214. void renderQuad(LLVector3* vertex_out, LLVector2* uv_out,
  215. LLColor4U* colors_out, const LLRectf& screen_rect,
  216. const LLRectf& uv_rect, const LLColor4U& color,
  217. F32 slant_amt) const;
  218. void drawGlyph(S32& glyph_count, LLVector3* vertex_out, LLVector2* uv_out,
  219. LLColor4U* colors_out, const LLRectf& screen_rect,
  220. const LLRectf& uv_rect, const LLColor4U& color, U8 style,
  221. F32 drop_shadow_fade) const;
  222. // Old, slower routines for texts with embedded data:
  223. // *TODO: change the UI code to allow getting fully rid of these
  224. S32 oldrender(const LLWString& wstr, S32 begin_offset, F32 x, F32 y,
  225. const LLColor4& color, HAlign halign, VAlign valign,
  226. U8 style, S32 max_chars, S32 max_pixels, F32* right_x,
  227. bool use_ellipses) const;
  228. void renderQuad(const LLRectf& screen_rect, const LLRectf& uv_rect,
  229. F32 slant_amt) const;
  230. void drawGlyph(const LLRectf& screen_rect, const LLRectf& uv_rect,
  231. const LLColor4& color, U8 style,
  232. F32 drop_shadow_fade) const;
  233. const embedded_data_t* getEmbeddedCharData(llwchar wch) const;
  234. F32 getEmbeddedCharAdvance(const embedded_data_t* ext_data) const;
  235. public:
  236. static LLColor4 sShadowColor;
  237. // Converted value of sShadowColor, for speed
  238. static LLColor4U sShadowColorU;
  239. static LLCoordGL sCurOrigin;
  240. static F32 sCurDepth;
  241. static F32 sVertDPI;
  242. static F32 sHorizDPI;
  243. static F32 sScaleX;
  244. static F32 sScaleY;
  245. static bool sDisplayFont;
  246. static std::vector<std::pair<LLCoordGL, F32> > sOriginStack;
  247. private:
  248. LLFontDescriptor mFontDescriptor;
  249. LLPointer<LLFontFreetype> mFontFreetype;
  250. typedef fast_hmap<llwchar, embedded_data_t> embedded_map_t;
  251. mutable embedded_map_t mEmbeddedChars;
  252. // Registry holds all instantiated fonts:
  253. static LLFontRegistry* sFontRegistry;
  254. static bool sAllowColorUse;
  255. };
  256. #endif