summaryrefslogtreecommitdiff
path: root/indra/llrender/llfontgl.h
blob: 6cb1727ff4b5cc56ee568d4330a455271121359a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/** 
 * @file llfontgl.h
 * @author Doug Soo
 * @brief Wrapper around FreeType
 *
 * $LicenseInfo:firstyear=2001&license=viewergpl$
 * 
 * Copyright (c) 2001-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#ifndef LL_LLFONTGL_H
#define LL_LLFONTGL_H

#include "llfont.h"
#include "llimagegl.h"
#include "v2math.h"
#include "llcoord.h"
#include "llrect.h"

#include "llfontregistry.h"

class LLColor4;

// Key used to request a font.
class LLFontDescriptor;

// Structure used to store previously requested fonts.
class LLFontRegistry;

class LLFontGL : public LLFont
{
public:
	enum HAlign
	{
		// Horizontal location of x,y coord to render.
		LEFT = 0,		// Left align
		RIGHT = 1,		// Right align
		HCENTER = 2,	// Center
	};

	enum VAlign
	{
		// Vertical location of x,y coord to render.
		TOP = 3,		// Top align
		VCENTER = 4,	// Center
		BASELINE = 5,	// Baseline
		BOTTOM = 6		// Bottom
	};

	enum StyleFlags
	{
		// text style to render.  May be combined (these are bit flags)
		NORMAL = 0,	
		BOLD = 1,
		ITALIC = 2,
		UNDERLINE = 4,
		DROP_SHADOW = 8,
		DROP_SHADOW_SOFT = 16
	};
	
	// Takes a string with potentially several flags, i.e. "NORMAL|BOLD|ITALIC"
	static U8 getStyleFromString(const std::string &style);

	LLFontGL();
	LLFontGL(const LLFontGL &source);
	~LLFontGL();

	void init(); // Internal init, or reinitialization
	void reset(); // Reset a font after GL cleanup.  ONLY works on an already loaded font.

	LLFontGL &operator=(const LLFontGL &source);

	static BOOL initDefaultFonts(F32 screen_dpi, F32 x_scale, F32 y_scale,
								 const std::string& app_dir,
								 const std::vector<std::string>& xui_paths);

	static void	destroyDefaultFonts();
	static void destroyAllGL();
	void destroyGL();

	/* virtual*/ BOOL loadFace(const std::string& filename,
							    const F32 point_size, const F32 vert_dpi, const F32 horz_dpi,
							    const S32 components, BOOL is_fallback);


	S32 renderUTF8(const std::string &text, const S32 begin_offset,
				   S32 x, S32 y,
				   const LLColor4 &color) const
	{
		return renderUTF8(text, begin_offset, (F32)x, (F32)y, color,
						  LEFT, BASELINE, NORMAL,
						  S32_MAX, S32_MAX, NULL, FALSE);
	}
	
	S32 renderUTF8(const std::string &text, const S32 begin_offset,
				   S32 x, S32 y,
				   const LLColor4 &color,
				   HAlign halign, VAlign valign, U8 style = NORMAL) const
	{
		return renderUTF8(text, begin_offset, (F32)x, (F32)y, color,
						  halign, valign, style,
						  S32_MAX, S32_MAX, NULL, FALSE);
	}
	
	// renderUTF8 does a conversion, so is slower!
	S32 renderUTF8(const std::string &text,
		S32 begin_offset,
		F32 x, F32 y,
		const LLColor4 &color,
		HAlign halign, 
		VAlign valign,
		U8 style,
		S32 max_chars,
		S32 max_pixels, 
		F32* right_x,
		BOOL use_ellipses) const;

	S32 render(const LLWString &text, const S32 begin_offset,
			   F32 x, F32 y,
			   const LLColor4 &color) const
	{
		return render(text, begin_offset, x, y, color,
					  LEFT, BASELINE, NORMAL,
					  S32_MAX, S32_MAX, NULL, FALSE, FALSE);
	}
	

	S32 render(const LLWString &text,
		S32 begin_offset,
		F32 x, F32 y,
		const LLColor4 &color,
		HAlign halign = LEFT, 
		VAlign valign = BASELINE,
		U8 style = NORMAL,
		S32 max_chars = S32_MAX,
		S32 max_pixels = S32_MAX, 
		F32* right_x=NULL,
		BOOL use_embedded = FALSE,
		BOOL use_ellipses = FALSE) const;

	// font metrics - override for LLFont that returns units of virtual pixels
	/*virtual*/ F32 getLineHeight() const		{ return (F32)llround(mLineHeight / sScaleY); }
	/*virtual*/ F32 getAscenderHeight() const	{ return (F32)llround(mAscender / sScaleY); }
	/*virtual*/ F32 getDescenderHeight() const	{ return (F32)llround(mDescender / sScaleY); }
	
	virtual S32 getWidth(const std::string& utf8text) const;
	virtual S32 getWidth(const llwchar* wchars) const;
	virtual S32 getWidth(const std::string& utf8text, const S32 offset, const S32 max_chars ) const;
	virtual S32 getWidth(const llwchar* wchars, const S32 offset, const S32 max_chars, BOOL use_embedded = FALSE) const;

	virtual F32 getWidthF32(const std::string& utf8text) const;
	virtual F32 getWidthF32(const llwchar* wchars) const;
	virtual F32 getWidthF32(const std::string& text, const S32 offset, const S32 max_chars ) const;
	virtual F32 getWidthF32(const llwchar* wchars, const S32 offset, const S32 max_chars, BOOL use_embedded = FALSE ) const;

	// The following are called often, frequently with large buffers, so do not use a string interface
	
	// Returns the max number of complete characters from text (up to max_chars) that can be drawn in max_pixels
	virtual S32	maxDrawableChars(const llwchar* wchars, F32 max_pixels, S32 max_chars = S32_MAX,
								 BOOL end_on_word_boundary = FALSE, const BOOL use_embedded = FALSE,
								 F32* drawn_pixels = NULL) const;

	// Returns the index of the first complete characters from text that can be drawn in max_pixels
	// given that the character at start_pos should be the last character (or as close to last as possible).
	virtual S32	firstDrawableChar(const llwchar* wchars, F32 max_pixels, S32 text_len, S32 start_pos=S32_MAX, S32 max_chars = S32_MAX) const;

	// Returns the index of the character closest to pixel position x (ignoring text to the right of max_pixels and max_chars)
	virtual S32 charFromPixelOffset(const llwchar* wchars, const S32 char_offset,
									F32 x, F32 max_pixels=F32_MAX, S32 max_chars = S32_MAX,
									BOOL round = TRUE, BOOL use_embedded = FALSE) const;


	LLImageGL *getImageGL() const;

	void	   addEmbeddedChar( llwchar wc, LLImageGL* image, const std::string& label) const;
	void	   addEmbeddedChar( llwchar wc, LLImageGL* image, const LLWString& label) const;
	void	   removeEmbeddedChar( llwchar wc ) const;

	static std::string nameFromFont(const LLFontGL* fontp);

	static std::string nameFromHAlign(LLFontGL::HAlign align);
	static LLFontGL::HAlign hAlignFromName(const std::string& name);

	static std::string nameFromVAlign(LLFontGL::VAlign align);
	static LLFontGL::VAlign vAlignFromName(const std::string& name);

	static void setFontDisplay(BOOL flag) { sDisplayFont = flag ; }

protected:
	struct embedded_data_t
	{
		embedded_data_t(LLImageGL* image, const LLWString& label) : mImage(image), mLabel(label) {}
		LLPointer<LLImageGL> mImage;
		LLWString			 mLabel;
	};
	const embedded_data_t* getEmbeddedCharData(const llwchar wch) const;
	F32 getEmbeddedCharAdvance(const embedded_data_t* ext_data) const;
	void clearEmbeddedChars();
	void renderQuad(const LLRectf& screen_rect, const LLRectf& uv_rect, F32 slant_amt) const;
	void drawGlyph(const LLRectf& screen_rect, const LLRectf& uv_rect, const LLColor4& color, U8 style, F32 drop_shadow_fade) const;

public:
	static F32 sVertDPI;
	static F32 sHorizDPI;
	static F32 sScaleX;
	static F32 sScaleY;
	static BOOL     sDisplayFont ;
	static std::string sAppDir;			// For loading fonts
		
	static LLFontGL* getFontMonospace();
	static LLFontGL* getFontSansSerifSmall();
	static LLFontGL* getFontSansSerif();
	static LLFontGL* getFontSansSerifBig();
	static LLFontGL* getFontSansSerifHuge();
	static LLFontGL* getFontSansSerifBold();
	static LLFontGL* getFontExtChar();
	static LLFontGL* getFont(const LLFontDescriptor& desc);

	static LLColor4 sShadowColor;

	friend class LLTextBillboard;
	friend class LLHUDText;

protected:
	/*virtual*/ BOOL addChar(const llwchar wch) const;

protected:
	typedef std::map<llwchar,embedded_data_t*> embedded_map_t;
	mutable embedded_map_t mEmbeddedChars;
	
	LLFontDescriptor mFontDesc;

	// Registry holds all instantiated fonts.
	static LLFontRegistry* sFontRegistry;

public:
	static std::string getFontPathLocal();
	static std::string getFontPathSystem();

	static LLCoordFont sCurOrigin;
	static std::vector<LLCoordFont> sOriginStack;

	const LLFontDescriptor &getFontDesc() const { return mFontDesc; }
	void setFontDesc(const LLFontDescriptor& font_desc) { mFontDesc = font_desc; }
};

#endif