PageRenderTime 25ms CodeModel.GetById 2ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 27#ifndef LL_LLFONTFREETYPE_H
 28#define LL_LLFONTFREETYPE_H
 29
 30#include <boost/unordered_map.hpp>
 31#include "llpointer.h"
 32#include "llstl.h"
 33
 34#include "llimagegl.h"
 35#include "llfontbitmapcache.h"
 36
 37// Hack.  FT_Face is just a typedef for a pointer to a struct,
 38// but there's no simple forward declarations file for FreeType, 
 39// and the main include file is 200K.  
 40// We'll forward declare the struct here.  JC
 41struct FT_FaceRec_;
 42typedef struct FT_FaceRec_* LLFT_Face;
 43
 44class LLFontManager
 45{
 46public:
 47	static void initClass();
 48	static void cleanupClass();
 49
 50private:
 51	LLFontManager();
 52	~LLFontManager();
 53};
 54
 55struct LLFontGlyphInfo
 56{
 57	LLFontGlyphInfo(U32 index);
 58
 59	U32 mGlyphIndex;
 60
 61	// Metrics
 62	S32 mWidth;			// In pixels
 63	S32 mHeight;		// In pixels
 64	F32 mXAdvance;		// In pixels
 65	F32 mYAdvance;		// In pixels
 66
 67	// Information for actually rendering
 68	S32 mXBitmapOffset; // Offset to the origin in the bitmap
 69	S32 mYBitmapOffset; // Offset to the origin in the bitmap
 70	S32 mXBearing;	// Distance from baseline to left in pixels
 71	S32 mYBearing;	// Distance from baseline to top in pixels
 72	S32 mBitmapNum; // Which bitmap in the bitmap cache contains this glyph
 73};
 74
 75extern LLFontManager *gFontManagerp;
 76
 77class LLFontFreetype : public LLRefCount
 78{
 79public:
 80	LLFontFreetype();
 81	~LLFontFreetype();
 82
 83	// is_fallback should be true for fallback fonts that aren't used
 84	// to render directly (Unicode backup, primarily)
 85	BOOL loadFace(const std::string& filename, F32 point_size, F32 vert_dpi, F32 horz_dpi, S32 components, BOOL is_fallback);
 86
 87	typedef std::vector<LLPointer<LLFontFreetype> > font_vector_t;
 88
 89	void setFallbackFonts(const font_vector_t &font);
 90	const font_vector_t &getFallbackFonts() const;
 91
 92	// Global font metrics - in units of pixels
 93	F32 getLineHeight() const;
 94	F32 getAscenderHeight() const;
 95	F32 getDescenderHeight() const;
 96
 97
 98// For a lowercase "g":
 99//
100//	------------------------------
101//	                     ^     ^
102//						 |     |
103//				xxx x    |Ascender
104//	           x   x     v     |
105//	---------   xxxx-------------- Baseline
106//	^		       x	       |
107//  | Descender    x           |
108//	v			xxxx           |LineHeight
109//  -----------------------    |
110//                             v
111//	------------------------------
112
113	enum
114	{
115		FIRST_CHAR = 32, 
116		NUM_CHARS = 127 - 32, 
117		LAST_CHAR_BASIC = 127,
118
119		// Need full 8-bit ascii range for spanish
120		NUM_CHARS_FULL = 255 - 32,
121		LAST_CHAR_FULL = 255
122	};
123
124	F32 getXAdvance(llwchar wc) const;
125	F32 getXAdvance(const LLFontGlyphInfo* glyph) const;
126	F32 getXKerning(llwchar char_left, llwchar char_right) const; // Get the kerning between the two characters
127	F32 getXKerning(const LLFontGlyphInfo* left_glyph_info, const LLFontGlyphInfo* right_glyph_info) const; // Get the kerning between the two characters
128
129	LLFontGlyphInfo* getGlyphInfo(llwchar wch) const;
130
131	void reset(F32 vert_dpi, F32 horz_dpi);
132
133	void destroyGL();
134
135	const std::string& getName() const;
136
137	const LLPointer<LLFontBitmapCache> getFontBitmapCache() const;
138
139	void setStyle(U8 style);
140	U8 getStyle() const;
141
142private:
143	void resetBitmapCache();
144	void setSubImageLuminanceAlpha(U32 x, U32 y, U32 bitmap_num, U32 width, U32 height, U8 *data, S32 stride = 0) const;
145	BOOL hasGlyph(llwchar wch) const;		// Has a glyph for this character
146	LLFontGlyphInfo* addGlyph(llwchar wch) const;		// Add a new character to the font if necessary
147	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)
148	void renderGlyph(U32 glyph_index) const;
149	void insertGlyphInfo(llwchar wch, LLFontGlyphInfo* gi) const;
150
151	std::string mName;
152
153	U8 mStyle;
154
155	F32 mPointSize;
156	F32 mAscender;			
157	F32 mDescender;
158	F32 mLineHeight;
159
160	LLFT_Face mFTFace;
161
162	BOOL mIsFallback;
163	font_vector_t mFallbackFonts; // A list of fallback fonts to look for glyphs in (for Unicode chars)
164
165	BOOL mValid;
166
167	typedef boost::unordered_map<llwchar, LLFontGlyphInfo*> char_glyph_info_map_t;
168	mutable char_glyph_info_map_t mCharGlyphInfoMap; // Information about glyph location in bitmap
169
170	mutable LLPointer<LLFontBitmapCache> mFontBitmapCachep;
171
172	mutable S32 mRenderGlyphCount;
173	mutable S32 mAddGlyphCount;
174};
175
176#endif // LL_FONTFREETYPE_H