xref: /haiku/headers/os/interface/Font.h (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
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			SetSpacing(uint8 spacing);
157 		void			SetEncoding(uint8 encoding);
158 		void			SetFace(uint16 face);
159 		void			SetFlags(uint32 flags);
160 
161 		void			GetFamilyAndStyle(font_family *family,
162 							font_style *style) const;
163 		uint32			FamilyAndStyle() const;
164 		float			Size() const;
165 		float			Shear() const;
166 		float			Rotation() const;
167 		uint8			Spacing() const;
168 		uint8			Encoding() const;
169 		uint16			Face() const;
170 		uint32			Flags() const;
171 
172 		font_direction	Direction() const;
173 		bool			IsFixed() const;
174 		bool			IsFullAndHalfFixed() const;
175 		BRect			BoundingBox() const;
176 		unicode_block	Blocks() const;
177 		font_file_format FileFormat() const;
178 
179 		int32			CountTuned() const;
180 		void			GetTunedInfo(int32 index, tuned_font_info *info) const;
181 
182 		void			TruncateString(BString* inOut, uint32 mode,
183 							float width) const;
184 		void            GetTruncatedStrings(const char *stringArray[],
185 							int32 numStrings, uint32 mode, float width,
186 							BString resultArray[]) const;
187 		void            GetTruncatedStrings(const char *stringArray[],
188 							int32 numStrings, uint32 mode, float width,
189 							char *resultArray[]) const;
190 
191 		float			StringWidth(const char *string) const;
192 		float			StringWidth(const char *string, int32 length) const;
193 		void			GetStringWidths(const char *stringArray[],
194 							const int32 lengthArray[], int32 numStrings,
195 							float widthArray[]) const;
196 
197 		void			GetEscapements(const char charArray[], int32 numChars,
198 							float escapementArray[]) const;
199 		void			GetEscapements(const char charArray[], int32 numChars,
200 							escapement_delta *delta, float escapementArray[]) const;
201 		void			GetEscapements(const char charArray[], int32 numChars,
202 							escapement_delta *delta, BPoint escapementArray[]) const;
203 		void			GetEscapements(const char charArray[], int32 numChars,
204 							escapement_delta *delta, BPoint escapementArray[],
205 							BPoint offsetArray[]) const;
206 
207 		void			GetEdges(const char charArray[], int32 numBytes,
208 							edge_info edgeArray[]) const;
209 		void			GetHeight(font_height *height) const;
210 
211 		void			GetBoundingBoxesAsGlyphs(const char charArray[],
212 							int32 numChars, font_metric_mode mode,
213 							BRect boundingBoxArray[]) const;
214 		void			GetBoundingBoxesAsString(const char charArray[],
215 							int32 numChars, font_metric_mode mode,
216 							escapement_delta *delta, BRect boundingBoxArray[]) const;
217 		void			GetBoundingBoxesForStrings(const char *stringArray[],
218 							int32 numStrings, font_metric_mode mode,
219 							escapement_delta deltas[],
220 							BRect boundingBoxArray[]) const;
221 
222 		void			GetGlyphShapes(const char charArray[], int32 numChars,
223 							BShape *glyphShapeArray[]) const;
224 
225 		void			GetHasGlyphs(const char charArray[], int32 numChars,
226 							bool hasArray[]) const;
227 
228 		BFont&			operator=(const BFont &font);
229 		bool			operator==(const BFont &font) const;
230 		bool			operator!=(const BFont &font) const;
231 
232 		void			PrintToStream() const;
233 
234 	private:
235 		friend void _init_global_fonts_();
236 
237 		uint16			fFamilyID;
238 		uint16			fStyleID;
239 		float			fSize;
240 		float			fShear;
241 		float			fRotation;
242 		uint8			fSpacing;
243 		uint8			fEncoding;
244 		uint16			fFace;
245 		uint32			fFlags;
246 		mutable font_height	fHeight;
247 		mutable uint32	fExtraFlags;
248 		uint32			_reserved[2];
249 
250 		void			_GetExtraFlags() const;
251 		void			_GetBoundingBoxes(const char charArray[],
252 							int32 numChars, font_metric_mode mode,
253 							bool string_escapement, escapement_delta *delta,
254 							BRect boundingBoxArray[]) const;
255 };
256 
257 
258 // BFont related declarations
259 
260 extern const BFont *be_plain_font;
261 extern const BFont *be_bold_font;
262 extern const BFont *be_fixed_font;
263 
264 int32 count_font_families(void);
265 status_t get_font_family(int32 index, font_family *name, uint32 *flags = NULL);
266 
267 int32 count_font_styles(font_family name);
268 status_t get_font_style(font_family family, int32 index, font_style *name,
269 		uint32 *flags = NULL);
270 status_t get_font_style(font_family family, int32 index, font_style *name,
271 		uint16 *face, uint32 *flags = NULL);
272 bool update_font_families(bool checkOnly);
273 
274 
275 // unicode_block inlines
276 
277 unicode_block::unicode_block()
278 {
279 	fData[0] = fData[1] = 0LL;
280 }
281 
282 unicode_block::unicode_block(uint64 block2, uint64 block1)
283 {
284 	fData[0] = block1;
285 	fData[1] = block2;
286 }
287 
288 bool
289 unicode_block::Includes(const unicode_block &block) const
290 {
291 	return (fData[0] & block.fData[0]) == block.fData[0]
292 		&& (fData[1] & block.fData[1]) == block.fData[1];
293 }
294 
295 unicode_block
296 unicode_block::operator&(const unicode_block &block) const
297 {
298 	unicode_block result;
299 	result.fData[0] = fData[0] & block.fData[0];
300 	result.fData[1] = fData[1] & block.fData[1];
301 
302 	return result;
303 }
304 
305 unicode_block
306 unicode_block::operator|(const unicode_block &block) const
307 {
308 	unicode_block result;
309 	result.fData[0] = fData[0] | block.fData[0];
310 	result.fData[1] = fData[1] | block.fData[1];
311 
312 	return result;
313 }
314 
315 unicode_block &
316 unicode_block::operator=(const unicode_block &block)
317 {
318 	fData[0] = block.fData[0];
319 	fData[1] = block.fData[1];
320 	return *this;
321 }
322 
323 bool
324 unicode_block::operator==(const unicode_block &block) const
325 {
326 	return fData[0] == block.fData[0] && fData[1] == block.fData[1];
327 }
328 
329 bool
330 unicode_block::operator!=(const unicode_block &block) const
331 {
332 	return fData[0] != block.fData[0] || fData[1] != block.fData[1];
333 }
334 
335 #endif	/* _FONT_H_ */
336