llfontfreetype.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @file llfontfreetype.h
  3. * @brief Font library wrapper
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2010, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_FONTFREETYPE_H
  33. #define LL_FONTFREETYPE_H
  34. #include <unordered_map>
  35. #include <utility>
  36. #include "hbfastmap.h"
  37. #include "llfontbitmapcache.h"
  38. #include "llimagegl.h"
  39. #include "llpointer.h"
  40. #include "llrefcount.h"
  41. #include "llstl.h"
  42. // Hack. FT_Face is just a typedef for a pointer to a struct, but there is no
  43. // simple forward declarations file for FreeType and the main include file is
  44. // 200K. We forward declare the struct here. JC
  45. struct FT_FaceRec_;
  46. typedef struct FT_FaceRec_* LLFT_Face;
  47. class LLFontManager
  48. {
  49. public:
  50. static void initClass();
  51. static void cleanupClass();
  52. public:
  53. LLFontManager();
  54. ~LLFontManager();
  55. };
  56. struct LLFontGlyphInfo
  57. {
  58. LL_INLINE LLFontGlyphInfo(U32 index, U32 glyph_type, U32 bitmap_type,
  59. S32 bitmap_num, S32 pos_x, S32 pos_y, S32 width,
  60. S32 height, S32 x_bearing, S32 y_bearing,
  61. F32 x_advance, F32 y_advance)
  62. : mGlyphIndex(index),
  63. mGlyphType(glyph_type),
  64. mBitmapEntry(std::make_pair(bitmap_type, bitmap_num)),
  65. mXBitmapOffset(pos_x),
  66. mYBitmapOffset(pos_y),
  67. mWidth(width),
  68. mHeight(height),
  69. mXBearing(x_bearing),
  70. mYBearing(y_bearing),
  71. mXAdvance(x_advance),
  72. mYAdvance(y_advance)
  73. {
  74. }
  75. LL_INLINE LLFontGlyphInfo(const LLFontGlyphInfo& fgi)
  76. : mGlyphIndex(fgi.mGlyphIndex),
  77. mGlyphType(fgi.mGlyphType),
  78. mBitmapEntry(fgi.mBitmapEntry),
  79. mXBitmapOffset(fgi.mXBitmapOffset),
  80. mYBitmapOffset(fgi.mYBitmapOffset),
  81. mWidth(fgi.mWidth),
  82. mHeight(fgi.mHeight),
  83. mXBearing(fgi.mXBearing),
  84. mYBearing(fgi.mYBearing),
  85. mXAdvance(fgi.mXAdvance),
  86. mYAdvance(fgi.mYAdvance)
  87. {
  88. }
  89. // Which bitmap in the bitmap cache contains this glyph
  90. std::pair<U32, S32> mBitmapEntry;
  91. U32 mGlyphIndex;
  92. U32 mGlyphType;
  93. // Metrics in pixels
  94. S32 mWidth;
  95. S32 mHeight;
  96. F32 mXAdvance;
  97. F32 mYAdvance;
  98. // Information for actually rendering
  99. S32 mXBitmapOffset; // Offset to the origin in the bitmap
  100. S32 mYBitmapOffset; // Offset to the origin in the bitmap
  101. S32 mXBearing; // Distance from baseline to left in pixels
  102. S32 mYBearing; // Distance from baseline to top in pixels
  103. };
  104. extern LLFontManager* gFontManagerp;
  105. class LLFontFreetype final : public LLRefCount
  106. {
  107. protected:
  108. LOG_CLASS(LLFontFreetype);
  109. public:
  110. LLFontFreetype();
  111. ~LLFontFreetype() override;
  112. // is_fallback should be true for fallback fonts that aren't used
  113. // to render directly (Unicode backup, primarily)
  114. bool loadFace(const std::string& filename, F32 point_size, F32 vert_dpi,
  115. F32 horz_dpi, bool is_fallback);
  116. typedef std::function<bool(llwchar)> char_functor_t;
  117. void addFallbackFont(const LLPointer<LLFontFreetype>& fallback_font,
  118. const char_functor_t& functor = NULL);
  119. // Global font metrics - in units of pixels
  120. LL_INLINE F32 getLineHeight() const { return mLineHeight; }
  121. LL_INLINE F32 getAscenderHeight() const { return mAscender; }
  122. LL_INLINE F32 getDescenderHeight() const { return mDescender; }
  123. // For a lowercase "g":
  124. //
  125. // ------------------------------
  126. // ^ ^
  127. // | |
  128. // xxx x |Ascender
  129. // x x v |
  130. // --------- xxxx-------------- Baseline
  131. // ^ x |
  132. // | Descender x |
  133. // v xxxx |LineHeight
  134. // ----------------------- |
  135. // v
  136. // ------------------------------
  137. enum
  138. {
  139. FIRST_CHAR = 32,
  140. NUM_CHARS = 127 - 32,
  141. LAST_CHAR_BASIC = 127,
  142. // Need full 8-bit ascii range for spanish
  143. NUM_CHARS_FULL = 255 - 32,
  144. LAST_CHAR_FULL = 255
  145. };
  146. LLFontGlyphInfo* getGlyphInfo(llwchar wch,
  147. U32 glyph_type =
  148. EFontGlyphType::Unspecified) const;
  149. LL_INLINE F32 getXAdvance(const LLFontGlyphInfo* glyph) const
  150. {
  151. return mFTFace && glyph ? glyph->mXAdvance : 0.f;
  152. }
  153. F32 getXAdvance(llwchar wc) const;
  154. F32 getXKerning(const LLFontGlyphInfo* left_glyph_info,
  155. const LLFontGlyphInfo* right_glyph_info) const;
  156. // Gets the kerning between the two characters
  157. LL_INLINE F32 getXKerning(llwchar char_left, llwchar char_right) const
  158. {
  159. return getXKerning(getGlyphInfo(char_left), getGlyphInfo(char_right));
  160. }
  161. void reset(F32 vert_dpi, F32 horz_dpi);
  162. void destroyGL();
  163. LL_INLINE const std::string& getName() const { return mName; }
  164. LL_INLINE const LLPointer<LLFontBitmapCache> getFontBitmapCache() const
  165. {
  166. return mFontBitmapCachep;
  167. }
  168. LL_INLINE void setStyle(U8 style) { mStyle = style; }
  169. LL_INLINE U8 getStyle() const { return mStyle; }
  170. private:
  171. void resetBitmapCache();
  172. void setSubImageLuminanceAlpha(U32 x, U32 y, U32 bitmap_num,
  173. U32 width, U32 height,
  174. U8* datap, S32 stride = 0) const;
  175. void setSubImageBGRA(U32 x, U32 y, U32 bitmap_num, U32 width, U32 height,
  176. U8* datap, S32 stride) const;
  177. // Has a glyph for this character
  178. bool hasGlyph(const llwchar wch) const;
  179. // Add a new character to the font if necessary
  180. LLFontGlyphInfo* addGlyph(llwchar wch, U32 glyph_type) const;
  181. // Add a glyph from this font to the other (returns the glyph_index, NULL
  182. // if not found)
  183. LLFontGlyphInfo* addGlyphFromFont(const LLFontFreetype* fontp,
  184. llwchar wch, U32 glyph_index,
  185. U32 glyph_type =
  186. EFontGlyphType::Grayscale) const;
  187. void insertGlyphInfo(llwchar wch, LLFontGlyphInfo* gip) const;
  188. void renderGlyph(U32 bitmap_type, U32 glyph_index) const;
  189. private:
  190. std::string mName;
  191. mutable LLPointer<LLFontBitmapCache> mFontBitmapCachep;
  192. typedef std::pair<LLPointer<LLFontFreetype>, char_functor_t> font_t;
  193. // A list of fallback fonts to look for glyphs in (for Unicode chars)
  194. typedef std::vector<font_t> font_vector_t;
  195. font_vector_t mFallbackFonts;
  196. // Note: the same glyph can be present with multiple representations (but
  197. // the pointer is always unique).
  198. typedef std::unordered_multimap<llwchar, LLFontGlyphInfo*> glyph_info_map_t;
  199. // Information about glyph location in bitmap
  200. mutable glyph_info_map_t mCharGlyphInfoMap;
  201. typedef flat_hmap<U64, F32> kerning_cache_map_t;
  202. mutable kerning_cache_map_t mKerningCache;
  203. LLFT_Face mFTFace;
  204. mutable S32 mRenderGlyphCount;
  205. mutable S32 mAddGlyphCount;
  206. F32 mPointSize;
  207. F32 mAscender;
  208. F32 mDescender;
  209. F32 mLineHeight;
  210. U8 mStyle;
  211. bool mIsFallback;
  212. };
  213. #endif // LL_FONTFREETYPE_H