/indra/llrender/llfontfreetype.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 176 lines · 89 code · 38 blank · 49 comment · 0 complexity · 3c352e4dbb1e1c385fed1b86b7a1aaf7 MD5 · raw file

  1. /**
  2. * @file llfontfreetype.h
  3. * @brief Font library wrapper
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLFONTFREETYPE_H
  27. #define LL_LLFONTFREETYPE_H
  28. #include <boost/unordered_map.hpp>
  29. #include "llpointer.h"
  30. #include "llstl.h"
  31. #include "llimagegl.h"
  32. #include "llfontbitmapcache.h"
  33. // Hack. FT_Face is just a typedef for a pointer to a struct,
  34. // but there's no simple forward declarations file for FreeType,
  35. // and the main include file is 200K.
  36. // We'll forward declare the struct here. JC
  37. struct FT_FaceRec_;
  38. typedef struct FT_FaceRec_* LLFT_Face;
  39. class LLFontManager
  40. {
  41. public:
  42. static void initClass();
  43. static void cleanupClass();
  44. private:
  45. LLFontManager();
  46. ~LLFontManager();
  47. };
  48. struct LLFontGlyphInfo
  49. {
  50. LLFontGlyphInfo(U32 index);
  51. U32 mGlyphIndex;
  52. // Metrics
  53. S32 mWidth; // In pixels
  54. S32 mHeight; // In pixels
  55. F32 mXAdvance; // In pixels
  56. F32 mYAdvance; // In pixels
  57. // Information for actually rendering
  58. S32 mXBitmapOffset; // Offset to the origin in the bitmap
  59. S32 mYBitmapOffset; // Offset to the origin in the bitmap
  60. S32 mXBearing; // Distance from baseline to left in pixels
  61. S32 mYBearing; // Distance from baseline to top in pixels
  62. S32 mBitmapNum; // Which bitmap in the bitmap cache contains this glyph
  63. };
  64. extern LLFontManager *gFontManagerp;
  65. class LLFontFreetype : public LLRefCount
  66. {
  67. public:
  68. LLFontFreetype();
  69. ~LLFontFreetype();
  70. // is_fallback should be true for fallback fonts that aren't used
  71. // to render directly (Unicode backup, primarily)
  72. BOOL loadFace(const std::string& filename, F32 point_size, F32 vert_dpi, F32 horz_dpi, S32 components, BOOL is_fallback);
  73. typedef std::vector<LLPointer<LLFontFreetype> > font_vector_t;
  74. void setFallbackFonts(const font_vector_t &font);
  75. const font_vector_t &getFallbackFonts() const;
  76. // Global font metrics - in units of pixels
  77. F32 getLineHeight() const;
  78. F32 getAscenderHeight() const;
  79. F32 getDescenderHeight() const;
  80. // For a lowercase "g":
  81. //
  82. // ------------------------------
  83. // ^ ^
  84. // | |
  85. // xxx x |Ascender
  86. // x x v |
  87. // --------- xxxx-------------- Baseline
  88. // ^ x |
  89. // | Descender x |
  90. // v xxxx |LineHeight
  91. // ----------------------- |
  92. // v
  93. // ------------------------------
  94. enum
  95. {
  96. FIRST_CHAR = 32,
  97. NUM_CHARS = 127 - 32,
  98. LAST_CHAR_BASIC = 127,
  99. // Need full 8-bit ascii range for spanish
  100. NUM_CHARS_FULL = 255 - 32,
  101. LAST_CHAR_FULL = 255
  102. };
  103. F32 getXAdvance(llwchar wc) const;
  104. F32 getXAdvance(const LLFontGlyphInfo* glyph) const;
  105. F32 getXKerning(llwchar char_left, llwchar char_right) const; // Get the kerning between the two characters
  106. F32 getXKerning(const LLFontGlyphInfo* left_glyph_info, const LLFontGlyphInfo* right_glyph_info) const; // Get the kerning between the two characters
  107. LLFontGlyphInfo* getGlyphInfo(llwchar wch) const;
  108. void reset(F32 vert_dpi, F32 horz_dpi);
  109. void destroyGL();
  110. const std::string& getName() const;
  111. const LLPointer<LLFontBitmapCache> getFontBitmapCache() const;
  112. void setStyle(U8 style);
  113. U8 getStyle() const;
  114. private:
  115. void resetBitmapCache();
  116. void setSubImageLuminanceAlpha(U32 x, U32 y, U32 bitmap_num, U32 width, U32 height, U8 *data, S32 stride = 0) const;
  117. BOOL hasGlyph(llwchar wch) const; // Has a glyph for this character
  118. LLFontGlyphInfo* addGlyph(llwchar wch) const; // Add a new character to the font if necessary
  119. LLFontGlyphInfo* addGlyphFromFont(const LLFontFreetype *fontp, llwchar wch, U32 glyph_index) const; // Add a glyph from this font to the other (returns the glyph_index, 0 if not found)
  120. void renderGlyph(U32 glyph_index) const;
  121. void insertGlyphInfo(llwchar wch, LLFontGlyphInfo* gi) const;
  122. std::string mName;
  123. U8 mStyle;
  124. F32 mPointSize;
  125. F32 mAscender;
  126. F32 mDescender;
  127. F32 mLineHeight;
  128. LLFT_Face mFTFace;
  129. BOOL mIsFallback;
  130. font_vector_t mFallbackFonts; // A list of fallback fonts to look for glyphs in (for Unicode chars)
  131. BOOL mValid;
  132. typedef boost::unordered_map<llwchar, LLFontGlyphInfo*> char_glyph_info_map_t;
  133. mutable char_glyph_info_map_t mCharGlyphInfoMap; // Information about glyph location in bitmap
  134. mutable LLPointer<LLFontBitmapCache> mFontBitmapCachep;
  135. mutable S32 mRenderGlyphCount;
  136. mutable S32 mAddGlyphCount;
  137. };
  138. #endif // LL_FONTFREETYPE_H