PageRenderTime 73ms CodeModel.GetById 49ms app.highlight 20ms RepoModel.GetById 1ms 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
  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
 28#ifndef LL_LLFONTGL_H
 29#define LL_LLFONTGL_H
 30
 31#include "llcoord.h"
 32#include "llfontregistry.h"
 33#include "llimagegl.h"
 34#include "llpointer.h"
 35#include "llrect.h"
 36#include "v2math.h"
 37
 38class LLColor4;
 39// Key used to request a font.
 40class LLFontDescriptor;
 41class LLFontFreetype;
 42
 43// Structure used to store previously requested fonts.
 44class LLFontRegistry;
 45
 46class LLFontGL
 47{
 48public:
 49	enum HAlign
 50	{
 51		// Horizontal location of x,y coord to render.
 52		LEFT = 0,		// Left align
 53		RIGHT = 1,		// Right align
 54		HCENTER = 2,	// Center
 55	};
 56
 57	enum VAlign
 58	{
 59		// Vertical location of x,y coord to render.
 60		TOP = 3,		// Top align
 61		VCENTER = 4,	// Center
 62		BASELINE = 5,	// Baseline
 63		BOTTOM = 6		// Bottom
 64	};
 65
 66	enum StyleFlags
 67	{
 68		// text style to render.  May be combined (these are bit flags)
 69		NORMAL    = 0x00,	
 70		BOLD      = 0x01,
 71		ITALIC    = 0x02,
 72		UNDERLINE = 0x04
 73	};
 74
 75	enum ShadowType
 76	{
 77		NO_SHADOW,
 78		DROP_SHADOW,
 79		DROP_SHADOW_SOFT
 80	};
 81
 82	LLFontGL();
 83	~LLFontGL();
 84
 85
 86	void reset(); // Reset a font after GL cleanup.  ONLY works on an already loaded font.
 87
 88	void destroyGL();
 89
 90	BOOL loadFace(const std::string& filename, F32 point_size, const F32 vert_dpi, const F32 horz_dpi, const S32 components, BOOL is_fallback);
 91
 92	S32 render(const LLWString &text, S32 begin_offset, 
 93				const LLRect& rect, 
 94				const LLColor4 &color, 
 95				HAlign halign = LEFT,  VAlign valign = BASELINE, 
 96				U8 style = NORMAL, ShadowType shadow = NO_SHADOW, 
 97				S32 max_chars = S32_MAX,
 98				F32* right_x=NULL, 
 99				BOOL use_ellipses = FALSE) const;
100
101	S32 render(const LLWString &text, S32 begin_offset, 
102				F32 x, F32 y, 
103				const LLColor4 &color, 
104				HAlign halign = LEFT,  VAlign valign = BASELINE, 
105				U8 style = NORMAL, ShadowType shadow = NO_SHADOW, 
106				S32 max_chars = S32_MAX, S32 max_pixels = S32_MAX, 
107				F32* right_x=NULL, 
108				BOOL use_ellipses = FALSE) const;
109
110	S32 render(const LLWString &text, S32 begin_offset, F32 x, F32 y, const LLColor4 &color) const;
111
112	// renderUTF8 does a conversion, so is slower!
113	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;
114	S32 renderUTF8(const std::string &text, S32 begin_offset, S32 x, S32 y, const LLColor4 &color) const;
115	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;
116
117	// font metrics - override for LLFontFreetype that returns units of virtual pixels
118	F32 getLineHeight() const;
119	F32 getAscenderHeight() const;
120	F32 getDescenderHeight() const;
121
122	S32 getWidth(const std::string& utf8text) const;
123	S32 getWidth(const llwchar* wchars) const;
124	S32 getWidth(const std::string& utf8text, S32 offset, S32 max_chars ) const;
125	S32 getWidth(const llwchar* wchars, S32 offset, S32 max_chars) const;
126
127	F32 getWidthF32(const std::string& utf8text) const;
128	F32 getWidthF32(const llwchar* wchars) const;
129	F32 getWidthF32(const std::string& text, S32 offset, S32 max_chars ) const;
130	F32 getWidthF32(const llwchar* wchars, S32 offset, S32 max_chars) const;
131
132	// The following are called often, frequently with large buffers, so do not use a string interface
133	
134	// Returns the max number of complete characters from text (up to max_chars) that can be drawn in max_pixels
135	typedef enum e_word_wrap_style
136	{
137		ONLY_WORD_BOUNDARIES,
138		WORD_BOUNDARY_IF_POSSIBLE,
139		ANYWHERE
140	} EWordWrapStyle ;
141	S32	maxDrawableChars(const llwchar* wchars, F32 max_pixels, S32 max_chars = S32_MAX, EWordWrapStyle end_on_word_boundary = ANYWHERE) const;
142
143	// Returns the index of the first complete characters from text that can be drawn in max_pixels
144	// given that the character at start_pos should be the last character (or as close to last as possible).
145	S32	firstDrawableChar(const llwchar* wchars, F32 max_pixels, S32 text_len, S32 start_pos=S32_MAX, S32 max_chars = S32_MAX) const;
146
147	// Returns the index of the character closest to pixel position x (ignoring text to the right of max_pixels and max_chars)
148	S32 charFromPixelOffset(const llwchar* wchars, S32 char_offset, F32 x, F32 max_pixels=F32_MAX, S32 max_chars = S32_MAX, BOOL round = TRUE) const;
149
150	const LLFontDescriptor& getFontDesc() const;
151
152
153	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);
154
155	// Load sans-serif, sans-serif-small, etc.
156	// Slow, requires multiple seconds to load fonts.
157	static bool loadDefaultFonts();
158	static void	destroyDefaultFonts();
159	static void destroyAllGL();
160
161	// Takes a string with potentially several flags, i.e. "NORMAL|BOLD|ITALIC"
162	static U8 getStyleFromString(const std::string &style);
163	static std::string getStringFromStyle(U8 style);
164
165	static std::string nameFromFont(const LLFontGL* fontp);
166	static std::string sizeFromFont(const LLFontGL* fontp);
167
168	static std::string nameFromHAlign(LLFontGL::HAlign align);
169	static LLFontGL::HAlign hAlignFromName(const std::string& name);
170
171	static std::string nameFromVAlign(LLFontGL::VAlign align);
172	static LLFontGL::VAlign vAlignFromName(const std::string& name);
173
174	static void setFontDisplay(BOOL flag) { sDisplayFont = flag; }
175		
176	static LLFontGL* getFontMonospace();
177	static LLFontGL* getFontSansSerifSmall();
178	static LLFontGL* getFontSansSerif();
179	static LLFontGL* getFontSansSerifBig();
180	static LLFontGL* getFontSansSerifHuge();
181	static LLFontGL* getFontSansSerifBold();
182	static LLFontGL* getFontExtChar();
183	static LLFontGL* getFont(const LLFontDescriptor& desc);
184	// Use with legacy names like "SANSSERIF_SMALL" or "OCRA"
185	static LLFontGL* getFontByName(const std::string& name);
186	static LLFontGL* getFontDefault(); // default fallback font
187
188	static std::string getFontPathLocal();
189	static std::string getFontPathSystem();
190
191	static LLCoordFont sCurOrigin;
192	static std::vector<LLCoordFont> sOriginStack;
193
194	static LLColor4 sShadowColor;
195
196	static F32 sVertDPI;
197	static F32 sHorizDPI;
198	static F32 sScaleX;
199	static F32 sScaleY;
200	static BOOL sDisplayFont ;
201	static std::string sAppDir;			// For loading fonts
202
203private:
204	friend class LLFontRegistry;
205	friend class LLTextBillboard;
206	friend class LLHUDText;
207
208	LLFontGL(const LLFontGL &source);
209	LLFontGL &operator=(const LLFontGL &source);
210
211	LLFontDescriptor mFontDescriptor;
212	LLPointer<LLFontFreetype> mFontFreetype;
213
214	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;
215	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;
216
217	// Registry holds all instantiated fonts.
218	static LLFontRegistry* sFontRegistry;
219};
220
221#endif