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 128 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 279 BFont& operator=(const BFont& font); 280 bool operator==(const BFont& font) const; 281 bool operator!=(const BFont& font) const; 282 283 void PrintToStream() const; 284 285 status_t LoadFont(const char* path); 286 status_t LoadFont(const area_id fontAreaID, 287 uint32 size = 0, uint32 offset = 0); 288 status_t UnloadFont(); 289 290 private: 291 friend void _init_global_fonts_(); 292 293 void _GetExtraFlags() const; 294 void _GetBoundingBoxes(const char charArray[], 295 int32 numChars, font_metric_mode mode, 296 bool string_escapement, 297 escapement_delta* delta, 298 BRect boundingBoxArray[], 299 bool asString) const; 300 301 private: 302 uint16 fFamilyID; 303 uint16 fStyleID; 304 float fSize; 305 float fShear; 306 float fRotation; 307 float fFalseBoldWidth; 308 uint8 fSpacing; 309 uint8 fEncoding; 310 uint16 fFace; 311 uint32 fFlags; 312 mutable font_height fHeight; 313 mutable uint32 fExtraFlags; 314 315 uint32 _reserved[1]; 316 }; 317 318 319 // #pragma mark - BFont related declarations 320 321 extern const BFont* be_plain_font; 322 extern const BFont* be_bold_font; 323 extern const BFont* be_fixed_font; 324 325 int32 count_font_families(void); 326 status_t get_font_family(int32 index, font_family* name, 327 uint32* flags = NULL); 328 329 int32 count_font_styles(font_family name); 330 status_t get_font_style(font_family family, int32 index, font_style* name, 331 uint32* flags = NULL); 332 status_t get_font_style(font_family family, int32 index, font_style* name, 333 uint16* face, uint32* flags = NULL); 334 bool update_font_families(bool checkOnly); 335 336 337 338 // #pragma mark - unicode_block inlines 339 340 341 unicode_block::unicode_block() 342 { 343 fData[0] = fData[1] = 0LL; 344 } 345 346 347 unicode_block::unicode_block(uint64 block2, uint64 block1) 348 { 349 fData[0] = block1; 350 fData[1] = block2; 351 } 352 353 354 bool 355 unicode_block::Includes(const unicode_block& block) const 356 { 357 return (fData[0] & block.fData[0]) == block.fData[0] 358 && (fData[1] & block.fData[1]) == block.fData[1]; 359 } 360 361 362 unicode_block 363 unicode_block::operator&(const unicode_block& block) const 364 { 365 unicode_block result; 366 result.fData[0] = fData[0] & block.fData[0]; 367 result.fData[1] = fData[1] & block.fData[1]; 368 369 return result; 370 } 371 372 373 unicode_block 374 unicode_block::operator|(const unicode_block& block) const 375 { 376 unicode_block result; 377 result.fData[0] = fData[0] | block.fData[0]; 378 result.fData[1] = fData[1] | block.fData[1]; 379 380 return result; 381 } 382 383 384 unicode_block& 385 unicode_block::operator=(const unicode_block& block) 386 { 387 fData[0] = block.fData[0]; 388 fData[1] = block.fData[1]; 389 390 return *this; 391 } 392 393 394 bool 395 unicode_block::operator==(const unicode_block& block) const 396 { 397 return fData[0] == block.fData[0] && fData[1] == block.fData[1]; 398 } 399 400 401 bool 402 unicode_block::operator!=(const unicode_block& block) const 403 { 404 return fData[0] != block.fData[0] || fData[1] != block.fData[1]; 405 } 406 407 408 #endif // _FONT_H_ 409