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