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