xref: /haiku/headers/os/interface/Font.h (revision a5a3b2d9a3d95cbae71eaf371708c73a1780ac0d)
1 /*
2  * Copyright 2005-2015, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _FONT_H_
6 #define _FONT_H_
7 
8 
9 #include <SupportDefs.h>
10 #include <InterfaceDefs.h>
11 
12 
13 class BPoint;
14 
15 
16 #define B_FONT_FAMILY_LENGTH 63
17 #define B_FONT_STYLE_LENGTH 63
18 typedef char font_family[B_FONT_FAMILY_LENGTH + 1];
19 typedef char font_style[B_FONT_STYLE_LENGTH + 1];
20 
21 
22 // font spacing
23 enum {
24 	B_CHAR_SPACING			= 0,
25 	B_STRING_SPACING		= 1,
26 	B_BITMAP_SPACING		= 2,
27 	B_FIXED_SPACING			= 3
28 };
29 
30 
31 enum font_direction {
32 	B_FONT_LEFT_TO_RIGHT	= 0,
33 	B_FONT_RIGHT_TO_LEFT	= 1
34 };
35 
36 
37 // font flags
38 enum {
39 	B_DISABLE_ANTIALIASING	= 0x00000001,
40 	B_FORCE_ANTIALIASING	= 0x00000002
41 };
42 
43 
44 // truncation modes
45 enum {
46 	B_NO_TRUNCATION			= ~0U,
47 	B_TRUNCATE_END			= 0,
48 	B_TRUNCATE_BEGINNING	= 1,
49 	B_TRUNCATE_MIDDLE		= 2,
50 	B_TRUNCATE_SMART		= 3
51 };
52 
53 
54 // font encodings
55 enum {
56 	B_UNICODE_UTF8			= 0,
57 	B_ISO_8859_1			= 1,
58 	B_ISO_8859_2			= 2,
59 	B_ISO_8859_3			= 3,
60 	B_ISO_8859_4			= 4,
61 	B_ISO_8859_5			= 5,
62 	B_ISO_8859_6			= 6,
63 	B_ISO_8859_7			= 7,
64 	B_ISO_8859_8			= 8,
65 	B_ISO_8859_9			= 9,
66 	B_ISO_8859_10			= 10,
67 	B_MACINTOSH_ROMAN		= 11
68 };
69 
70 
71 // flags for get_font_family() and get_font_style()
72 enum {
73 	B_HAS_TUNED_FONT		= 0x0001,
74 	B_IS_FIXED				= 0x0002
75 };
76 
77 
78 // font face flags
79 enum {
80 	B_ITALIC_FACE			= 0x0001,
81 	B_UNDERSCORE_FACE		= 0x0002,
82 	B_NEGATIVE_FACE			= 0x0004,
83 	B_OUTLINED_FACE			= 0x0008,
84 	B_STRIKEOUT_FACE		= 0x0010,
85 	B_BOLD_FACE				= 0x0020,
86 	B_REGULAR_FACE			= 0x0040,
87 	// new in Haiku:
88 	B_CONDENSED_FACE		= 0x0080,
89 	B_LIGHT_FACE			= 0x0100,
90 	B_HEAVY_FACE			= 0x0200,
91 };
92 
93 
94 enum font_metric_mode {
95 	B_SCREEN_METRIC			= 0,
96 	B_PRINTING_METRIC		= 1
97 };
98 
99 
100 enum font_file_format {
101 	B_TRUETYPE_WINDOWS		= 0,
102 	B_POSTSCRIPT_TYPE1_WINDOWS = 1
103 };
104 
105 
106 class unicode_block {
107 public:
108 	inline						unicode_block();
109 	inline						unicode_block(uint64 block2, uint64 block1);
110 
111 	inline	bool				Includes(const unicode_block& block) const;
112 	inline	unicode_block		operator&(const unicode_block& block) const;
113 	inline	unicode_block		operator|(const unicode_block& block) const;
114 	inline	unicode_block&		operator=(const unicode_block& block);
115 	inline	bool				operator==(const unicode_block& block) const;
116 	inline	bool				operator!=(const unicode_block& block) const;
117 
118 private:
119 	uint64					fData[2];
120 };
121 
122 
123 struct edge_info {
124 	float	left;
125 	float	right;
126 };
127 
128 
129 struct font_height {
130 	float	ascent;
131 	float	descent;
132 	float	leading;
133 };
134 
135 
136 struct escapement_delta {
137 	float	nonspace;
138 	float	space;
139 };
140 
141 
142 struct font_cache_info {
143 	int32    sheared_font_penalty;
144 	int32    rotated_font_penalty;
145 	float    oversize_threshold;
146 	int32    oversize_penalty;
147 	int32    cache_size;
148 	float    spacing_size_threshold;
149 };
150 
151 
152 struct tuned_font_info {
153 	float    size;
154 	float    shear;
155 	float    rotation;
156 	uint32   flags;
157 	uint16   face;
158 };
159 
160 
161 class BShape;
162 class BString;
163 class BFontPrivate;
164 
165 
166 class BFont {
167 public:
168 								BFont();
169 								BFont(const BFont& font);
170 								BFont(const BFont* font);
171 
172 			status_t			SetFamilyAndStyle(const font_family family,
173 									const font_style style);
174 			void				SetFamilyAndStyle(uint32 code);
175 			status_t			SetFamilyAndFace(const font_family family,
176 									uint16 face);
177 
178 			void				SetSize(float size);
179 			void				SetShear(float shear);
180 			void				SetRotation(float rotation);
181 			void				SetFalseBoldWidth(float width);
182 			void				SetSpacing(uint8 spacing);
183 			void				SetEncoding(uint8 encoding);
184 			void				SetFace(uint16 face);
185 			void				SetFlags(uint32 flags);
186 
187 			void				GetFamilyAndStyle(font_family* family,
188 									font_style* style) const;
189 			uint32				FamilyAndStyle() const;
190 			float				Size() const;
191 			float				Shear() const;
192 			float				Rotation() const;
193 			float				FalseBoldWidth() const;
194 			uint8				Spacing() const;
195 			uint8				Encoding() const;
196 			uint16				Face() const;
197 			uint32				Flags() const;
198 
199 			font_direction		Direction() const;
200 			bool				IsFixed() const;
201 			bool				IsFullAndHalfFixed() const;
202 			BRect				BoundingBox() const;
203 			unicode_block		Blocks() const;
204 			font_file_format	FileFormat() const;
205 
206 			int32				CountTuned() const;
207 			void				GetTunedInfo(int32 index,
208 									tuned_font_info* info) const;
209 
210 			void				TruncateString(BString* inOut, uint32 mode,
211 									float width) const;
212 			void            	GetTruncatedStrings(const char* stringArray[],
213 									int32 numStrings, uint32 mode, float width,
214 									BString resultArray[]) const;
215 			void            	GetTruncatedStrings(const char* stringArray[],
216 									int32 numStrings, uint32 mode,
217 									float width, char* resultArray[]) const;
218 
219 			float				StringWidth(const char* string) const;
220 			float				StringWidth(const char* string,
221 									int32 length) const;
222 			void				GetStringWidths(const char* stringArray[],
223 									const int32 lengthArray[],
224 									int32 numStrings,
225 									float widthArray[]) const;
226 
227 			void				GetEscapements(const char charArray[],
228 									int32 numChars,
229 									float escapementArray[]) const;
230 			void				GetEscapements(const char charArray[],
231 									int32 numChars, escapement_delta* delta,
232 									float escapementArray[]) const;
233 			void				GetEscapements(const char charArray[],
234 									int32 numChars, escapement_delta* delta,
235 									BPoint escapementArray[]) const;
236 			void				GetEscapements(const char charArray[],
237 									int32 numChars, escapement_delta* delta,
238 									BPoint escapementArray[],
239 									BPoint offsetArray[]) const;
240 
241 			void				GetEdges(const char charArray[],
242 									int32 numBytes,
243 									edge_info edgeArray[]) const;
244 			void				GetHeight(font_height* height) const;
245 
246 			void				GetBoundingBoxesAsGlyphs(
247 									const char charArray[], int32 numChars,
248 									font_metric_mode mode,
249 									BRect boundingBoxArray[]) const;
250 			void				GetBoundingBoxesAsString(
251 									const char charArray[], int32 numChars,
252 									font_metric_mode mode,
253 									escapement_delta* delta,
254 									BRect boundingBoxArray[]) const;
255 			void				GetBoundingBoxesForStrings(
256 									const char* stringArray[],
257 									int32 numStrings, font_metric_mode mode,
258 									escapement_delta deltas[],
259 									BRect boundingBoxArray[]) const;
260 
261 			void				GetGlyphShapes(const char charArray[],
262 									int32 numChars,
263 									BShape* glyphShapeArray[]) const;
264 
265 			void				GetHasGlyphs(const char charArray[],
266 									int32 numChars,
267 									bool hasArray[]) const;
268 
269 			BFont&				operator=(const BFont& font);
270 			bool				operator==(const BFont& font) const;
271 			bool				operator!=(const BFont& font) const;
272 
273 			void				PrintToStream() const;
274 
275 private:
276 		friend void _init_global_fonts_();
277 
278 			void				_GetExtraFlags() const;
279 			void				_GetBoundingBoxes(const char charArray[],
280 									int32 numChars, font_metric_mode mode,
281 									bool string_escapement,
282 									escapement_delta* delta,
283 									BRect boundingBoxArray[],
284 									bool asString) const;
285 
286 private:
287 			uint16				fFamilyID;
288 			uint16				fStyleID;
289 			float				fSize;
290 			float				fShear;
291 			float				fRotation;
292 			float				fFalseBoldWidth;
293 			uint8				fSpacing;
294 			uint8				fEncoding;
295 			uint16				fFace;
296 			uint32				fFlags;
297 	mutable	font_height			fHeight;
298 	mutable	uint32				fExtraFlags;
299 
300 			uint32				_reserved[1];
301 };
302 
303 
304 // #pragma mark - BFont related declarations
305 
306 extern const BFont* be_plain_font;
307 extern const BFont* be_bold_font;
308 extern const BFont* be_fixed_font;
309 
310 int32 count_font_families(void);
311 status_t get_font_family(int32 index, font_family* name,
312 	uint32* flags = NULL);
313 
314 int32 count_font_styles(font_family name);
315 status_t get_font_style(font_family family, int32 index, font_style* name,
316 	uint32* flags = NULL);
317 status_t get_font_style(font_family family, int32 index, font_style* name,
318 	uint16* face, uint32* flags = NULL);
319 bool update_font_families(bool checkOnly);
320 
321 
322 
323 // #pragma mark - unicode_block inlines
324 
325 
326 unicode_block::unicode_block()
327 {
328 	fData[0] = fData[1] = 0LL;
329 }
330 
331 
332 unicode_block::unicode_block(uint64 block2, uint64 block1)
333 {
334 	fData[0] = block1;
335 	fData[1] = block2;
336 }
337 
338 
339 bool
340 unicode_block::Includes(const unicode_block& block) const
341 {
342 	return (fData[0] & block.fData[0]) == block.fData[0]
343 		&& (fData[1] & block.fData[1]) == block.fData[1];
344 }
345 
346 
347 unicode_block
348 unicode_block::operator&(const unicode_block& block) const
349 {
350 	unicode_block result;
351 	result.fData[0] = fData[0] & block.fData[0];
352 	result.fData[1] = fData[1] & block.fData[1];
353 
354 	return result;
355 }
356 
357 
358 unicode_block
359 unicode_block::operator|(const unicode_block& block) const
360 {
361 	unicode_block result;
362 	result.fData[0] = fData[0] | block.fData[0];
363 	result.fData[1] = fData[1] | block.fData[1];
364 
365 	return result;
366 }
367 
368 
369 unicode_block&
370 unicode_block::operator=(const unicode_block& block)
371 {
372 	fData[0] = block.fData[0];
373 	fData[1] = block.fData[1];
374 	return *this;
375 }
376 
377 
378 bool
379 unicode_block::operator==(const unicode_block& block) const
380 {
381 	return fData[0] == block.fData[0] && fData[1] == block.fData[1];
382 }
383 
384 
385 bool
386 unicode_block::operator!=(const unicode_block& block) const
387 {
388 	return fData[0] != block.fData[0] || fData[1] != block.fData[1];
389 }
390 
391 
392 #endif // _FONT_H_
393