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