1/*
2 * Copyright 2005-2016, 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
13class BPoint;
14
15
16#define B_FONT_FAMILY_LENGTH 63
17#define B_FONT_STYLE_LENGTH 63
18typedef char font_family[B_FONT_FAMILY_LENGTH + 1];
19typedef char font_style[B_FONT_STYLE_LENGTH + 1];
20
21
22// font spacing
23enum {
24	B_CHAR_SPACING			= 0,
25	B_STRING_SPACING		= 1,
26	B_BITMAP_SPACING		= 2,
27	B_FIXED_SPACING			= 3
28};
29
30
31enum font_direction {
32	B_FONT_LEFT_TO_RIGHT	= 0,
33	B_FONT_RIGHT_TO_LEFT	= 1
34};
35
36
37// font flags
38enum {
39	B_DISABLE_ANTIALIASING	= 0x00000001,
40	B_FORCE_ANTIALIASING	= 0x00000002
41};
42
43
44// truncation modes
45enum {
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
55enum {
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()
72enum {
73	B_HAS_TUNED_FONT		= 0x0001,
74	B_IS_FIXED				= 0x0002
75};
76
77
78// font face flags
79enum {
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
94enum font_metric_mode {
95	B_SCREEN_METRIC			= 0,
96	B_PRINTING_METRIC		= 1
97};
98
99
100enum font_file_format {
101	B_TRUETYPE_WINDOWS		= 0,
102	B_POSTSCRIPT_TYPE1_WINDOWS = 1
103};
104
105
106class unicode_block {
107public:
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
118private:
119	uint64					fData[2];
120};
121
122
123struct unicode_block_range {
124	uint32					start;
125	uint32					end;
126	const unicode_block&	block;
127
128	uint32 Count() const { return end + 1 - start; }
129};
130
131
132struct edge_info {
133	float	left;
134	float	right;
135};
136
137
138struct font_height {
139	float	ascent;
140	float	descent;
141	float	leading;
142};
143
144
145struct escapement_delta {
146	float	nonspace;
147	float	space;
148};
149
150
151struct font_cache_info {
152	int32    sheared_font_penalty;
153	int32    rotated_font_penalty;
154	float    oversize_threshold;
155	int32    oversize_penalty;
156	int32    cache_size;
157	float    spacing_size_threshold;
158};
159
160
161struct tuned_font_info {
162	float    size;
163	float    shear;
164	float    rotation;
165	uint32   flags;
166	uint16   face;
167};
168
169
170class BShape;
171class BString;
172class BFontPrivate;
173
174
175class BFont {
176public:
177								BFont();
178								BFont(const BFont& font);
179								BFont(const BFont* font);
180
181			status_t			SetFamilyAndStyle(const font_family family,
182									const font_style style);
183			void				SetFamilyAndStyle(uint32 code);
184			status_t			SetFamilyAndFace(const font_family family,
185									uint16 face);
186
187			void				SetSize(float size);
188			void				SetShear(float shear);
189			void				SetRotation(float rotation);
190			void				SetFalseBoldWidth(float width);
191			void				SetSpacing(uint8 spacing);
192			void				SetEncoding(uint8 encoding);
193			void				SetFace(uint16 face);
194			void				SetFlags(uint32 flags);
195
196			void				GetFamilyAndStyle(font_family* family,
197									font_style* style) const;
198			uint32				FamilyAndStyle() const;
199			float				Size() const;
200			float				Shear() const;
201			float				Rotation() const;
202			float				FalseBoldWidth() const;
203			uint8				Spacing() const;
204			uint8				Encoding() const;
205			uint16				Face() const;
206			uint32				Flags() const;
207
208			font_direction		Direction() const;
209			bool				IsFixed() const;
210			bool				IsFullAndHalfFixed() const;
211			BRect				BoundingBox() const;
212			unicode_block		Blocks() const;
213			bool				IncludesBlock(uint32 start, uint32 end) const;
214			font_file_format	FileFormat() const;
215
216			int32				CountTuned() const;
217			void				GetTunedInfo(int32 index,
218									tuned_font_info* info) const;
219
220			void				TruncateString(BString* inOut, uint32 mode,
221									float width) const;
222			void            	GetTruncatedStrings(const char* stringArray[],
223									int32 numStrings, uint32 mode, float width,
224									BString resultArray[]) const;
225			void            	GetTruncatedStrings(const char* stringArray[],
226									int32 numStrings, uint32 mode,
227									float width, char* resultArray[]) const;
228
229			float				StringWidth(const char* string) const;
230			float				StringWidth(const char* string,
231									int32 length) const;
232			void				GetStringWidths(const char* stringArray[],
233									const int32 lengthArray[],
234									int32 numStrings,
235									float widthArray[]) const;
236
237			void				GetEscapements(const char charArray[],
238									int32 numChars,
239									float escapementArray[]) const;
240			void				GetEscapements(const char charArray[],
241									int32 numChars, escapement_delta* delta,
242									float escapementArray[]) const;
243			void				GetEscapements(const char charArray[],
244									int32 numChars, escapement_delta* delta,
245									BPoint escapementArray[]) const;
246			void				GetEscapements(const char charArray[],
247									int32 numChars, escapement_delta* delta,
248									BPoint escapementArray[],
249									BPoint offsetArray[]) const;
250
251			void				GetEdges(const char charArray[],
252									int32 numBytes,
253									edge_info edgeArray[]) const;
254			void				GetHeight(font_height* height) const;
255
256			void				GetBoundingBoxesAsGlyphs(
257									const char charArray[], int32 numChars,
258									font_metric_mode mode,
259									BRect boundingBoxArray[]) const;
260			void				GetBoundingBoxesAsString(
261									const char charArray[], int32 numChars,
262									font_metric_mode mode,
263									escapement_delta* delta,
264									BRect boundingBoxArray[]) const;
265			void				GetBoundingBoxesForStrings(
266									const char* stringArray[],
267									int32 numStrings, font_metric_mode mode,
268									escapement_delta deltas[],
269									BRect boundingBoxArray[]) const;
270
271			void				GetGlyphShapes(const char charArray[],
272									int32 numChars,
273									BShape* glyphShapeArray[]) const;
274
275			void				GetHasGlyphs(const char charArray[],
276									int32 numChars,
277									bool hasArray[]) const;
278
279			BFont&				operator=(const BFont& font);
280			bool				operator==(const BFont& font) const;
281			bool				operator!=(const BFont& font) const;
282
283			void				PrintToStream() const;
284
285private:
286		friend void _init_global_fonts_();
287
288			void				_GetExtraFlags() const;
289			void				_GetBoundingBoxes(const char charArray[],
290									int32 numChars, font_metric_mode mode,
291									bool string_escapement,
292									escapement_delta* delta,
293									BRect boundingBoxArray[],
294									bool asString) const;
295
296private:
297			uint16				fFamilyID;
298			uint16				fStyleID;
299			float				fSize;
300			float				fShear;
301			float				fRotation;
302			float				fFalseBoldWidth;
303			uint8				fSpacing;
304			uint8				fEncoding;
305			uint16				fFace;
306			uint32				fFlags;
307	mutable	font_height			fHeight;
308	mutable	uint32				fExtraFlags;
309
310			uint32				_reserved[1];
311};
312
313
314// #pragma mark - BFont related declarations
315
316extern const BFont* be_plain_font;
317extern const BFont* be_bold_font;
318extern const BFont* be_fixed_font;
319
320int32 count_font_families(void);
321status_t get_font_family(int32 index, font_family* name,
322	uint32* flags = NULL);
323
324int32 count_font_styles(font_family name);
325status_t get_font_style(font_family family, int32 index, font_style* name,
326	uint32* flags = NULL);
327status_t get_font_style(font_family family, int32 index, font_style* name,
328	uint16* face, uint32* flags = NULL);
329bool update_font_families(bool checkOnly);
330
331
332
333// #pragma mark - unicode_block inlines
334
335
336unicode_block::unicode_block()
337{
338	fData[0] = fData[1] = 0LL;
339}
340
341
342unicode_block::unicode_block(uint64 block2, uint64 block1)
343{
344	fData[0] = block1;
345	fData[1] = block2;
346}
347
348
349bool
350unicode_block::Includes(const unicode_block& block) const
351{
352	return (fData[0] & block.fData[0]) == block.fData[0]
353		&& (fData[1] & block.fData[1]) == block.fData[1];
354}
355
356
357unicode_block
358unicode_block::operator&(const unicode_block& block) const
359{
360	unicode_block result;
361	result.fData[0] = fData[0] & block.fData[0];
362	result.fData[1] = fData[1] & block.fData[1];
363
364	return result;
365}
366
367
368unicode_block
369unicode_block::operator|(const unicode_block& block) const
370{
371	unicode_block result;
372	result.fData[0] = fData[0] | block.fData[0];
373	result.fData[1] = fData[1] | block.fData[1];
374
375	return result;
376}
377
378
379unicode_block&
380unicode_block::operator=(const unicode_block& block)
381{
382	fData[0] = block.fData[0];
383	fData[1] = block.fData[1];
384
385	return *this;
386}
387
388
389bool
390unicode_block::operator==(const unicode_block& block) const
391{
392	return fData[0] == block.fData[0] && fData[1] == block.fData[1];
393}
394
395
396bool
397unicode_block::operator!=(const unicode_block& block) const
398{
399	return fData[0] != block.fData[0] || fData[1] != block.fData[1];
400}
401
402
403#endif // _FONT_H_
404