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 } 147 148 149 bool 150 FontCacheEntry::Init(const ServerFont& font, bool forceVector) 151 { 152 if (!fGlyphCache.IsSet()) 153 return false; 154 155 glyph_rendering renderingType = _RenderTypeFor(font, forceVector); 156 157 // TODO: encoding from font 158 FT_Encoding charMap = FT_ENCODING_NONE; 159 bool hinting = font.Hinting(); 160 161 if (!fEngine.Init(font.Path(), 0, font.Size(), charMap, 162 renderingType, hinting)) { 163 fprintf(stderr, "FontCacheEntry::Init() - some error loading font " 164 "file %s\n", font.Path()); 165 return false; 166 } 167 if (fGlyphCache->Init() != B_OK) { 168 fprintf(stderr, "FontCacheEntry::Init() - failed to allocate " 169 "GlyphCache table for font file %s\n", font.Path()); 170 return false; 171 } 172 173 return true; 174 } 175 176 177 bool 178 FontCacheEntry::HasGlyphs(const char* utf8String, ssize_t length) const 179 { 180 uint32 glyphCode; 181 const char* start = utf8String; 182 while ((glyphCode = UTF8ToCharCode(&utf8String))) { 183 if (fGlyphCache->FindGlyph(glyphCode) == NULL) 184 return false; 185 if (utf8String - start + 1 > length) 186 break; 187 } 188 return true; 189 } 190 191 192 inline bool 193 render_as_space(uint32 glyphCode) 194 { 195 // whitespace: render as space 196 // as per Unicode PropList.txt: White_Space 197 return (glyphCode >= 0x0009 && glyphCode <= 0x000d) 198 // control characters 199 || (glyphCode == 0x0085) 200 // another control 201 || (glyphCode == 0x00a0) 202 // no-break space 203 || (glyphCode == 0x1680) 204 // ogham space mark 205 || (glyphCode == 0x180e) 206 // mongolian vowel separator 207 || (glyphCode >= 0x2000 && glyphCode <= 0x200a) 208 // en quand, hair space 209 || (glyphCode >= 0x2028 && glyphCode <= 0x2029) 210 // line and paragraph separators 211 || (glyphCode == 0x202f) 212 // narrow no-break space 213 || (glyphCode == 0x205f) 214 // medium math space 215 || (glyphCode == 0x3000) 216 // ideographic space 217 ; 218 } 219 220 221 inline bool 222 render_as_zero_width(uint32 glyphCode) 223 { 224 // ignorable chars: render as invisible 225 // as per Unicode DerivedCoreProperties.txt: Default_Ignorable_Code_Point 226 return (glyphCode == 0x00ad) 227 // soft hyphen 228 || (glyphCode == 0x034f) 229 // combining grapheme joiner 230 || (glyphCode >= 0x115f && glyphCode <= 0x1160) 231 // hangul fillers 232 || (glyphCode >= 0x17b4 && glyphCode <= 0x17b5) 233 // ignorable khmer vowels 234 || (glyphCode >= 0x180b && glyphCode <= 0x180d) 235 // variation selectors 236 || (glyphCode >= 0x200b && glyphCode <= 0x200f) 237 // zero width space, cursive joiners, ltr marks 238 || (glyphCode >= 0x202a && glyphCode <= 0x202e) 239 // left to right embed, override 240 || (glyphCode >= 0x2060 && glyphCode <= 0x206f) 241 // word joiner, invisible math operators, reserved 242 || (glyphCode == 0x3164) 243 // hangul filler 244 || (glyphCode >= 0xfe00 && glyphCode <= 0xfe0f) 245 // variation selectors 246 || (glyphCode == 0xfeff) 247 // zero width no-break space 248 || (glyphCode == 0xffa0) 249 // halfwidth hangul filler 250 || (glyphCode >= 0xfff0 && glyphCode <= 0xfff8) 251 // reserved 252 || (glyphCode >= 0x1d173 && glyphCode <= 0x1d17a) 253 // musical symbols 254 || (glyphCode >= 0xe0000 && glyphCode <= 0xe01ef) 255 // variation selectors, tag space, reserved 256 ; 257 } 258 259 260 const GlyphCache* 261 FontCacheEntry::CachedGlyph(uint32 glyphCode) 262 { 263 // Only requires a read lock. 264 return fGlyphCache->FindGlyph(glyphCode); 265 } 266 267 268 bool 269 FontCacheEntry::CanCreateGlyph(uint32 glyphCode) 270 { 271 // Note that this bypass any fallback or caching because it is used in 272 // the fallback code itself. 273 uint32 glyphIndex = fEngine.GlyphIndexForGlyphCode(glyphCode); 274 return glyphIndex != 0; 275 } 276 277 278 const GlyphCache* 279 FontCacheEntry::CreateGlyph(uint32 glyphCode, FontCacheEntry* fallbackEntry) 280 { 281 // We cache the glyph by the requested glyphCode. The FontEngine of this 282 // FontCacheEntry may not contain a glyph for the given code, in which case 283 // we ask the fallbackEntry for the code to index translation and let it 284 // generate the glyph data. We will still use our own cache for storing the 285 // glyph. The next time it will be found (by glyphCode). 286 287 // NOTE: Both this and the fallback FontCacheEntry are expected to be 288 // write-locked! 289 290 const GlyphCache* glyph = fGlyphCache->FindGlyph(glyphCode); 291 if (glyph != NULL) 292 return glyph; 293 294 FontEngine* engine = &fEngine; 295 uint32 glyphIndex = engine->GlyphIndexForGlyphCode(glyphCode); 296 if (glyphIndex == 0 && fallbackEntry != NULL) { 297 // Our FontEngine does not contain this glyph, but we can retry with 298 // the fallbackEntry. 299 engine = &fallbackEntry->fEngine; 300 glyphIndex = engine->GlyphIndexForGlyphCode(glyphCode); 301 } 302 303 if (glyphIndex == 0) { 304 if (render_as_zero_width(glyphCode)) { 305 // cache and return a zero width glyph 306 return fGlyphCache->CacheGlyph(glyphCode, 0, glyph_data_invalid, 307 agg::rect_i(0, 0, -1, -1), 0, 0, 0, 0, 0, 0); 308 } 309 310 // reset to our engine 311 engine = &fEngine; 312 if (render_as_space(glyphCode)) { 313 // get the normal space glyph 314 glyphIndex = engine->GlyphIndexForGlyphCode(0x20 /* space */); 315 } 316 } 317 318 if (engine->PrepareGlyph(glyphIndex)) { 319 glyph = fGlyphCache->CacheGlyph(glyphCode, 320 engine->DataSize(), engine->DataType(), engine->Bounds(), 321 engine->AdvanceX(), engine->AdvanceY(), 322 engine->PreciseAdvanceX(), engine->PreciseAdvanceY(), 323 engine->InsetLeft(), engine->InsetRight()); 324 325 if (glyph != NULL) 326 engine->WriteGlyphTo(glyph->data); 327 } 328 329 return glyph; 330 } 331 332 333 void 334 FontCacheEntry::InitAdaptors(const GlyphCache* glyph, 335 double x, double y, GlyphMonoAdapter& monoAdapter, 336 GlyphGray8Adapter& gray8Adapter, GlyphPathAdapter& pathAdapter, 337 double scale) 338 { 339 if (glyph == NULL) 340 return; 341 342 switch(glyph->data_type) { 343 case glyph_data_mono: 344 monoAdapter.init(glyph->data, glyph->data_size, x, y); 345 break; 346 347 case glyph_data_gray8: 348 gray8Adapter.init(glyph->data, glyph->data_size, x, y); 349 break; 350 351 case glyph_data_subpix: 352 gray8Adapter.init(glyph->data, glyph->data_size, x, y); 353 break; 354 355 case glyph_data_outline: 356 pathAdapter.init(glyph->data, glyph->data_size, x, y, scale); 357 break; 358 359 default: 360 break; 361 } 362 } 363 364 365 bool 366 FontCacheEntry::GetKerning(uint32 glyphCode1, uint32 glyphCode2, 367 double* x, double* y) 368 { 369 return fEngine.GetKerning(glyphCode1, glyphCode2, x, y); 370 } 371 372 373 /*static*/ void 374 FontCacheEntry::GenerateSignature(char* signature, size_t signatureSize, 375 const ServerFont& font, bool forceVector) 376 { 377 glyph_rendering renderingType = _RenderTypeFor(font, forceVector); 378 379 // TODO: read more of these from the font 380 FT_Encoding charMap = FT_ENCODING_NONE; 381 bool hinting = font.Hinting(); 382 uint8 averageWeight = gSubpixelAverageWeight; 383 384 snprintf(signature, signatureSize, "%" B_PRId32 ",%u,%d,%d,%.1f,%d,%d", 385 font.GetFamilyAndStyle(), charMap, 386 font.Face(), int(renderingType), font.Size(), hinting, averageWeight); 387 } 388 389 390 void 391 FontCacheEntry::UpdateUsage() 392 { 393 // this is a static lock to prevent usage of too many semaphores, 394 // but on the other hand, it is not so nice to be using a lock 395 // here at all 396 // the hope is that the time is so short to hold this lock, that 397 // there is not much contention 398 BAutolock _(sUsageUpdateLock); 399 400 fLastUsedTime = system_time(); 401 fUseCounter++; 402 } 403 404 405 /*static*/ glyph_rendering 406 FontCacheEntry::_RenderTypeFor(const ServerFont& font, bool forceVector) 407 { 408 glyph_rendering renderingType = gSubpixelAntialiasing ? 409 glyph_ren_subpix : glyph_ren_native_gray8; 410 411 if (forceVector || font.Rotation() != 0.0 || font.Shear() != 90.0 412 || font.FalseBoldWidth() != 0.0 413 || (font.Flags() & B_DISABLE_ANTIALIASING) != 0 414 || font.Size() > 30 415 || !font.Hinting()) { 416 renderingType = glyph_ren_outline; 417 } 418 419 return renderingType; 420 } 421