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