1 /* 2 * Copyright 2007-2009, Haiku. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Maxim Shemanarev <mcseemagg@yahoo.com> 7 * Stephan Aßmus <superstippi@gmx.de> 8 * Andrej Spielmann, <andrej.spielmann@seh.ox.ac.uk> 9 */ 10 11 //---------------------------------------------------------------------------- 12 // Anti-Grain Geometry - Version 2.4 13 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) 14 // 15 // Permission to copy, use, modify, sell and distribute this software 16 // is granted provided this copyright notice appears in all copies. 17 // This software is provided "as is" without express or implied 18 // warranty, and with no claim as to its suitability for any purpose. 19 // 20 //---------------------------------------------------------------------------- 21 // Contact: mcseem@antigrain.com 22 // mcseemagg@yahoo.com 23 // http://www.antigrain.com 24 //---------------------------------------------------------------------------- 25 26 27 #include "FontCacheEntry.h" 28 29 #include <string.h> 30 31 #include <new> 32 33 #include <Autolock.h> 34 35 #include <agg_array.h> 36 #include <utf8_functions.h> 37 #include <util/OpenHashTable.h> 38 39 #include "GlobalSubpixelSettings.h" 40 41 42 BLocker FontCacheEntry::sUsageUpdateLock("FontCacheEntry usage lock"); 43 44 45 class FontCacheEntry::GlyphCachePool { 46 // This class needs to be defined before any inline functions, as otherwise 47 // gcc2 will barf in debug mode. 48 struct GlyphHashTableDefinition { 49 typedef uint32 KeyType; 50 typedef GlyphCache ValueType; 51 52 size_t HashKey(uint32 key) const 53 { 54 return key; 55 } 56 57 size_t Hash(GlyphCache* value) const 58 { 59 return value->glyph_index; 60 } 61 62 bool Compare(uint32 key, GlyphCache* value) const 63 { 64 return value->glyph_index == key; 65 } 66 67 GlyphCache*& GetLink(GlyphCache* value) const 68 { 69 return value->hash_link; 70 } 71 }; 72 public: 73 GlyphCachePool() 74 { 75 } 76 77 ~GlyphCachePool() 78 { 79 GlyphCache* glyph = fGlyphTable.Clear(true); 80 while (glyph != NULL) { 81 GlyphCache* next = glyph->hash_link; 82 delete glyph; 83 glyph = next; 84 } 85 } 86 87 status_t Init() 88 { 89 return fGlyphTable.Init(); 90 } 91 92 const GlyphCache* FindGlyph(uint32 glyphIndex) const 93 { 94 return fGlyphTable.Lookup(glyphIndex); 95 } 96 97 GlyphCache* CacheGlyph(uint32 glyphIndex, 98 uint32 dataSize, glyph_data_type dataType, const agg::rect_i& bounds, 99 float advanceX, float advanceY, float preciseAdvanceX, 100 float preciseAdvanceY, float insetLeft, float insetRight) 101 { 102 GlyphCache* glyph = fGlyphTable.Lookup(glyphIndex); 103 if (glyph != NULL) 104 return NULL; 105 106 glyph = new(std::nothrow) GlyphCache(glyphIndex, dataSize, dataType, 107 bounds, advanceX, advanceY, preciseAdvanceX, preciseAdvanceY, 108 insetLeft, insetRight); 109 if (glyph == NULL || glyph->data == NULL) { 110 delete glyph; 111 return NULL; 112 } 113 114 // TODO: The HashTable grows without bounds. We should cleanup 115 // older entries from time to time. 116 117 fGlyphTable.Insert(glyph); 118 119 return glyph; 120 } 121 122 private: 123 typedef BOpenHashTable<GlyphHashTableDefinition> GlyphTable; 124 125 GlyphTable fGlyphTable; 126 }; 127 128 129 // #pragma mark - 130 131 132 FontCacheEntry::FontCacheEntry() 133 : 134 MultiLocker("FontCacheEntry lock"), 135 fGlyphCache(new(std::nothrow) GlyphCachePool()), 136 fEngine(), 137 fLastUsedTime(LONGLONG_MIN), 138 fUseCounter(0) 139 { 140 } 141 142 143 FontCacheEntry::~FontCacheEntry() 144 { 145 //printf("~FontCacheEntry()\n"); 146 delete fGlyphCache; 147 } 148 149 150 bool 151 FontCacheEntry::Init(const ServerFont& font, bool forceVector) 152 { 153 if (fGlyphCache == NULL) 154 return false; 155 156 glyph_rendering renderingType = _RenderTypeFor(font, forceVector); 157 158 // TODO: encoding from font 159 FT_Encoding charMap = FT_ENCODING_NONE; 160 bool hinting = font.Hinting(); 161 162 if (!fEngine.Init(font.Path(), 0, font.Size(), charMap, 163 renderingType, hinting)) { 164 fprintf(stderr, "FontCacheEntry::Init() - some error loading font " 165 "file %s\n", font.Path()); 166 return false; 167 } 168 if (fGlyphCache->Init() != B_OK) { 169 fprintf(stderr, "FontCacheEntry::Init() - failed to allocate " 170 "GlyphCache table for font file %s\n", font.Path()); 171 return false; 172 } 173 174 return true; 175 } 176 177 178 bool 179 FontCacheEntry::HasGlyphs(const char* utf8String, ssize_t length) const 180 { 181 uint32 glyphCode; 182 const char* start = utf8String; 183 while ((glyphCode = UTF8ToCharCode(&utf8String))) { 184 if (fGlyphCache->FindGlyph(glyphCode) == NULL) 185 return false; 186 if (utf8String - start + 1 > length) 187 break; 188 } 189 return true; 190 } 191 192 193 inline bool 194 render_as_space(uint32 glyphCode) 195 { 196 // whitespace: render as space 197 // as per Unicode PropList.txt: White_Space 198 return (glyphCode >= 0x0009 && glyphCode <= 0x000d) 199 // control characters 200 || (glyphCode == 0x0085) 201 // another control 202 || (glyphCode == 0x00a0) 203 // no-break space 204 || (glyphCode == 0x1680) 205 // ogham space mark 206 || (glyphCode == 0x180e) 207 // mongolian vowel separator 208 || (glyphCode >= 0x2000 && glyphCode <= 0x200a) 209 // en quand, hair space 210 || (glyphCode >= 0x2028 && glyphCode <= 0x2029) 211 // line and paragraph separators 212 || (glyphCode == 0x202f) 213 // narrow no-break space 214 || (glyphCode == 0x205f) 215 // medium math space 216 || (glyphCode == 0x3000) 217 // ideographic space 218 ; 219 } 220 221 222 inline bool 223 render_as_zero_width(uint32 glyphCode) 224 { 225 // ignorable chars: render as invisible 226 // as per Unicode DerivedCoreProperties.txt: Default_Ignorable_Code_Point 227 return (glyphCode == 0x00ad) 228 // soft hyphen 229 || (glyphCode == 0x034f) 230 // combining grapheme joiner 231 || (glyphCode >= 0x115f && glyphCode <= 0x1160) 232 // hangul fillers 233 || (glyphCode >= 0x17b4 && glyphCode <= 0x17b5) 234 // ignorable khmer vowels 235 || (glyphCode >= 0x180b && glyphCode <= 0x180d) 236 // variation selectors 237 || (glyphCode >= 0x200b && glyphCode <= 0x200f) 238 // zero width space, cursive joiners, ltr marks 239 || (glyphCode >= 0x202a && glyphCode <= 0x202e) 240 // left to right embed, override 241 || (glyphCode >= 0x2060 && glyphCode <= 0x206f) 242 // word joiner, invisible math operators, reserved 243 || (glyphCode == 0x3164) 244 // hangul filler 245 || (glyphCode >= 0xfe00 && glyphCode <= 0xfe0f) 246 // variation selectors 247 || (glyphCode == 0xfeff) 248 // zero width no-break space 249 || (glyphCode == 0xffa0) 250 // halfwidth hangul filler 251 || (glyphCode >= 0xfff0 && glyphCode <= 0xfff8) 252 // reserved 253 || (glyphCode >= 0x1d173 && glyphCode <= 0x1d17a) 254 // musical symbols 255 || (glyphCode >= 0xe0000 && glyphCode <= 0xe01ef) 256 // variation selectors, tag space, reserved 257 ; 258 } 259 260 261 const GlyphCache* 262 FontCacheEntry::CachedGlyph(uint32 glyphCode) 263 { 264 // Only requires a read lock. 265 return fGlyphCache->FindGlyph(glyphCode); 266 } 267 268 269 bool 270 FontCacheEntry::CanCreateGlyph(uint32 glyphCode) 271 { 272 // Note that this bypass any fallback or caching because it is used in 273 // the fallback code itself. 274 uint32 glyphIndex = fEngine.GlyphIndexForGlyphCode(glyphCode); 275 return glyphIndex != 0; 276 } 277 278 279 const GlyphCache* 280 FontCacheEntry::CreateGlyph(uint32 glyphCode, FontCacheEntry* fallbackEntry) 281 { 282 // We cache the glyph by the requested glyphCode. The FontEngine of this 283 // FontCacheEntry may not contain a glyph for the given code, in which case 284 // we ask the fallbackEntry for the code to index translation and let it 285 // generate the glyph data. We will still use our own cache for storing the 286 // glyph. The next time it will be found (by glyphCode). 287 288 // NOTE: Both this and the fallback FontCacheEntry are expected to be 289 // write-locked! 290 291 const GlyphCache* glyph = fGlyphCache->FindGlyph(glyphCode); 292 if (glyph != NULL) 293 return glyph; 294 295 FontEngine* engine = &fEngine; 296 uint32 glyphIndex = engine->GlyphIndexForGlyphCode(glyphCode); 297 if (glyphIndex == 0 && fallbackEntry != NULL) { 298 // Our FontEngine does not contain this glyph, but we can retry with 299 // the fallbackEntry. 300 engine = &fallbackEntry->fEngine; 301 glyphIndex = engine->GlyphIndexForGlyphCode(glyphCode); 302 } 303 304 if (glyphIndex == 0) { 305 if (render_as_zero_width(glyphCode)) { 306 // cache and return a zero width glyph 307 return fGlyphCache->CacheGlyph(glyphCode, 0, glyph_data_invalid, 308 agg::rect_i(0, 0, -1, -1), 0, 0, 0, 0, 0, 0); 309 } 310 311 // reset to our engine 312 engine = &fEngine; 313 if (render_as_space(glyphCode)) { 314 // get the normal space glyph 315 glyphIndex = engine->GlyphIndexForGlyphCode(0x20 /* space */); 316 } else { 317 // The glyph was not found anywhere. 318 return NULL; 319 } 320 } 321 322 if (engine->PrepareGlyph(glyphIndex)) { 323 glyph = fGlyphCache->CacheGlyph(glyphCode, 324 engine->DataSize(), engine->DataType(), engine->Bounds(), 325 engine->AdvanceX(), engine->AdvanceY(), 326 engine->PreciseAdvanceX(), engine->PreciseAdvanceY(), 327 engine->InsetLeft(), engine->InsetRight()); 328 329 if (glyph != NULL) 330 engine->WriteGlyphTo(glyph->data); 331 } 332 333 return glyph; 334 } 335 336 337 void 338 FontCacheEntry::InitAdaptors(const GlyphCache* glyph, 339 double x, double y, GlyphMonoAdapter& monoAdapter, 340 GlyphGray8Adapter& gray8Adapter, GlyphPathAdapter& pathAdapter, 341 double scale) 342 { 343 if (glyph == NULL) 344 return; 345 346 switch(glyph->data_type) { 347 case glyph_data_mono: 348 monoAdapter.init(glyph->data, glyph->data_size, x, y); 349 break; 350 351 case glyph_data_gray8: 352 gray8Adapter.init(glyph->data, glyph->data_size, x, y); 353 break; 354 355 case glyph_data_subpix: 356 gray8Adapter.init(glyph->data, glyph->data_size, x, y); 357 break; 358 359 case glyph_data_outline: 360 pathAdapter.init(glyph->data, glyph->data_size, x, y, scale); 361 break; 362 363 default: 364 break; 365 } 366 } 367 368 369 bool 370 FontCacheEntry::GetKerning(uint32 glyphCode1, uint32 glyphCode2, 371 double* x, double* y) 372 { 373 return fEngine.GetKerning(glyphCode1, glyphCode2, x, y); 374 } 375 376 377 /*static*/ void 378 FontCacheEntry::GenerateSignature(char* signature, size_t signatureSize, 379 const ServerFont& font, bool forceVector) 380 { 381 glyph_rendering renderingType = _RenderTypeFor(font, forceVector); 382 383 // TODO: read more of these from the font 384 FT_Encoding charMap = FT_ENCODING_NONE; 385 bool hinting = font.Hinting(); 386 uint8 averageWeight = gSubpixelAverageWeight; 387 388 snprintf(signature, signatureSize, "%" B_PRId32 ",%u,%d,%d,%.1f,%d,%d", 389 font.GetFamilyAndStyle(), charMap, 390 font.Face(), int(renderingType), font.Size(), hinting, averageWeight); 391 } 392 393 394 void 395 FontCacheEntry::UpdateUsage() 396 { 397 // this is a static lock to prevent usage of too many semaphores, 398 // but on the other hand, it is not so nice to be using a lock 399 // here at all 400 // the hope is that the time is so short to hold this lock, that 401 // there is not much contention 402 BAutolock _(sUsageUpdateLock); 403 404 fLastUsedTime = system_time(); 405 fUseCounter++; 406 } 407 408 409 /*static*/ glyph_rendering 410 FontCacheEntry::_RenderTypeFor(const ServerFont& font, bool forceVector) 411 { 412 glyph_rendering renderingType = gSubpixelAntialiasing ? 413 glyph_ren_subpix : glyph_ren_native_gray8; 414 415 if (forceVector || font.Rotation() != 0.0 || font.Shear() != 90.0 416 || font.FalseBoldWidth() != 0.0 417 || (font.Flags() & B_DISABLE_ANTIALIASING) != 0 418 || font.Size() > 30 419 || !font.Hinting()) { 420 renderingType = glyph_ren_outline; 421 } 422 423 return renderingType; 424 } 425