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