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