| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 Google Inc. All rights reserved. | 2 * Copyright (c) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 // Though we have FontCache class, which provides the cache mechanism for | 107 // Though we have FontCache class, which provides the cache mechanism for |
| 108 // WebKit's font objects, we also need additional caching layer for HarfBuzz to | 108 // WebKit's font objects, we also need additional caching layer for HarfBuzz to |
| 109 // reduce the number of hb_font_t objects created. Without it, we would create | 109 // reduce the number of hb_font_t objects created. Without it, we would create |
| 110 // an hb_font_t object for every FontPlatformData object. But insted, we only | 110 // an hb_font_t object for every FontPlatformData object. But insted, we only |
| 111 // need one for each unique SkTypeface. | 111 // need one for each unique SkTypeface. |
| 112 // FIXME, crbug.com/609099: We should fix the FontCache to only keep one | 112 // FIXME, crbug.com/609099: We should fix the FontCache to only keep one |
| 113 // FontPlatformData object independent of size, then consider using this here. | 113 // FontPlatformData object independent of size, then consider using this here. |
| 114 class HbFontCacheEntry : public RefCounted<HbFontCacheEntry> { | 114 class HbFontCacheEntry : public RefCounted<HbFontCacheEntry> { |
| 115 public: | 115 public: |
| 116 static PassRefPtr<HbFontCacheEntry> Create(hb_font_t* hb_font) { | 116 static PassRefPtr<HbFontCacheEntry> Create(hb_font_t* hb_font) { |
| 117 ASSERT(hb_font); | 117 DCHECK(hb_font); |
| 118 return AdoptRef(new HbFontCacheEntry(hb_font)); | 118 return AdoptRef(new HbFontCacheEntry(hb_font)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 hb_font_t* HbFont() { return hb_font_.get(); } | 121 hb_font_t* HbFont() { return hb_font_.get(); } |
| 122 HarfBuzzFontData* HbFontData() { return hb_font_data_.get(); } | 122 HarfBuzzFontData* HbFontData() { return hb_font_data_.get(); } |
| 123 | 123 |
| 124 private: | 124 private: |
| 125 explicit HbFontCacheEntry(hb_font_t* font) | 125 explicit HbFontCacheEntry(hb_font_t* font) |
| 126 : hb_font_(HbFontUniquePtr(font)), | 126 : hb_font_(HbFontUniquePtr(font)), |
| 127 hb_font_data_(WTF::MakeUnique<HarfBuzzFontData>()){}; | 127 hb_font_data_(WTF::MakeUnique<HarfBuzzFontData>()){}; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 152 result.stored_value->value = CreateHbFontCacheEntry(face.get()); | 152 result.stored_value->value = CreateHbFontCacheEntry(face.get()); |
| 153 } | 153 } |
| 154 result.stored_value->value->Ref(); | 154 result.stored_value->value->Ref(); |
| 155 unscaled_font_ = result.stored_value->value->HbFont(); | 155 unscaled_font_ = result.stored_value->value->HbFont(); |
| 156 harf_buzz_font_data_ = result.stored_value->value->HbFontData(); | 156 harf_buzz_font_data_ = result.stored_value->value->HbFontData(); |
| 157 } | 157 } |
| 158 | 158 |
| 159 HarfBuzzFace::~HarfBuzzFace() { | 159 HarfBuzzFace::~HarfBuzzFace() { |
| 160 HarfBuzzFontCache::iterator result = GetHarfBuzzFontCache()->Find(unique_id_); | 160 HarfBuzzFontCache::iterator result = GetHarfBuzzFontCache()->Find(unique_id_); |
| 161 SECURITY_DCHECK(result != GetHarfBuzzFontCache()->end()); | 161 SECURITY_DCHECK(result != GetHarfBuzzFontCache()->end()); |
| 162 ASSERT(result.Get()->value->RefCount() > 1); | 162 DCHECK_GT(result.Get()->value->RefCount(), 1); |
| 163 result.Get()->value->Deref(); | 163 result.Get()->value->Deref(); |
| 164 if (result.Get()->value->RefCount() == 1) | 164 if (result.Get()->value->RefCount() == 1) |
| 165 GetHarfBuzzFontCache()->erase(unique_id_); | 165 GetHarfBuzzFontCache()->erase(unique_id_); |
| 166 } | 166 } |
| 167 | 167 |
| 168 static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) { | 168 static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) { |
| 169 // We treat HarfBuzz hb_position_t as 16.16 fixed-point. | 169 // We treat HarfBuzz hb_position_t as 16.16 fixed-point. |
| 170 static const int kHbPosition1 = 1 << 16; | 170 static const int kHbPosition1 = 1 << 16; |
| 171 return clampTo<int>(value * kHbPosition1); | 171 return clampTo<int>(value * kHbPosition1); |
| 172 } | 172 } |
| 173 | 173 |
| 174 static hb_bool_t HarfBuzzGetGlyph(hb_font_t* hb_font, | 174 static hb_bool_t HarfBuzzGetGlyph(hb_font_t* hb_font, |
| 175 void* font_data, | 175 void* font_data, |
| 176 hb_codepoint_t unicode, | 176 hb_codepoint_t unicode, |
| 177 hb_codepoint_t variation_selector, | 177 hb_codepoint_t variation_selector, |
| 178 hb_codepoint_t* glyph, | 178 hb_codepoint_t* glyph, |
| 179 void* user_data) { | 179 void* user_data) { |
| 180 HarfBuzzFontData* hb_font_data = | 180 HarfBuzzFontData* hb_font_data = |
| 181 reinterpret_cast<HarfBuzzFontData*>(font_data); | 181 reinterpret_cast<HarfBuzzFontData*>(font_data); |
| 182 | 182 |
| 183 RELEASE_ASSERT(hb_font_data); | 183 CHECK(hb_font_data); |
| 184 if (hb_font_data->range_set_ && !hb_font_data->range_set_->Contains(unicode)) | 184 if (hb_font_data->range_set_ && !hb_font_data->range_set_->Contains(unicode)) |
| 185 return false; | 185 return false; |
| 186 | 186 |
| 187 return hb_font_get_glyph(hb_font_get_parent(hb_font), unicode, | 187 return hb_font_get_glyph(hb_font_get_parent(hb_font), unicode, |
| 188 variation_selector, glyph); | 188 variation_selector, glyph); |
| 189 } | 189 } |
| 190 | 190 |
| 191 static hb_position_t HarfBuzzGetGlyphHorizontalAdvance(hb_font_t* hb_font, | 191 static hb_position_t HarfBuzzGetGlyphHorizontalAdvance(hb_font_t* hb_font, |
| 192 void* font_data, | 192 void* font_data, |
| 193 hb_codepoint_t glyph, | 193 hb_codepoint_t glyph, |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 | 365 |
| 366 // Fallback to table copies if there is no in-memory access. | 366 // Fallback to table copies if there is no in-memory access. |
| 367 if (!face) { | 367 if (!face) { |
| 368 face = hb_face_create_for_tables(HarfBuzzSkiaGetTable, | 368 face = hb_face_create_for_tables(HarfBuzzSkiaGetTable, |
| 369 platform_data_->Typeface(), 0); | 369 platform_data_->Typeface(), 0); |
| 370 zero_copy_success_histogram.Count(false); | 370 zero_copy_success_histogram.Count(false); |
| 371 } else { | 371 } else { |
| 372 zero_copy_success_histogram.Count(true); | 372 zero_copy_success_histogram.Count(true); |
| 373 } | 373 } |
| 374 #endif | 374 #endif |
| 375 ASSERT(face); | 375 DCHECK(face); |
| 376 return face; | 376 return face; |
| 377 } | 377 } |
| 378 | 378 |
| 379 PassRefPtr<HbFontCacheEntry> CreateHbFontCacheEntry(hb_face_t* face) { | 379 PassRefPtr<HbFontCacheEntry> CreateHbFontCacheEntry(hb_face_t* face) { |
| 380 HbFontUniquePtr ot_font(hb_font_create(face)); | 380 HbFontUniquePtr ot_font(hb_font_create(face)); |
| 381 hb_ot_font_set_funcs(ot_font.get()); | 381 hb_ot_font_set_funcs(ot_font.get()); |
| 382 // Creating a sub font means that non-available functions | 382 // Creating a sub font means that non-available functions |
| 383 // are found from the parent. | 383 // are found from the parent. |
| 384 hb_font_t* unscaled_font = hb_font_create_sub_font(ot_font.get()); | 384 hb_font_t* unscaled_font = hb_font_create_sub_font(ot_font.get()); |
| 385 RefPtr<HbFontCacheEntry> cache_entry = | 385 RefPtr<HbFontCacheEntry> cache_entry = |
| (...skipping 14 matching lines...) Expand all Loading... |
| 400 "Skia and HarfBuzz Variation parameter types must match in structure and " | 400 "Skia and HarfBuzz Variation parameter types must match in structure and " |
| 401 "size."); | 401 "size."); |
| 402 | 402 |
| 403 hb_font_t* HarfBuzzFace::GetScaledFont( | 403 hb_font_t* HarfBuzzFace::GetScaledFont( |
| 404 PassRefPtr<UnicodeRangeSet> range_set) const { | 404 PassRefPtr<UnicodeRangeSet> range_set) const { |
| 405 platform_data_->SetupPaint(&harf_buzz_font_data_->paint_); | 405 platform_data_->SetupPaint(&harf_buzz_font_data_->paint_); |
| 406 harf_buzz_font_data_->paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | 406 harf_buzz_font_data_->paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 407 harf_buzz_font_data_->range_set_ = std::move(range_set); | 407 harf_buzz_font_data_->range_set_ = std::move(range_set); |
| 408 harf_buzz_font_data_->UpdateSimpleFontData(platform_data_); | 408 harf_buzz_font_data_->UpdateSimpleFontData(platform_data_); |
| 409 | 409 |
| 410 ASSERT(harf_buzz_font_data_->simple_font_data_); | 410 DCHECK(harf_buzz_font_data_->simple_font_data_); |
| 411 int scale = SkiaScalarToHarfBuzzPosition(platform_data_->size()); | 411 int scale = SkiaScalarToHarfBuzzPosition(platform_data_->size()); |
| 412 hb_font_set_scale(unscaled_font_, scale, scale); | 412 hb_font_set_scale(unscaled_font_, scale, scale); |
| 413 | 413 |
| 414 SkTypeface* typeface = harf_buzz_font_data_->paint_.getTypeface(); | 414 SkTypeface* typeface = harf_buzz_font_data_->paint_.getTypeface(); |
| 415 int axis_count = typeface->getVariationDesignPosition(nullptr, 0); | 415 int axis_count = typeface->getVariationDesignPosition(nullptr, 0); |
| 416 if (axis_count > 0) { | 416 if (axis_count > 0) { |
| 417 Vector<SkFontArguments::VariationPosition::Coordinate> axis_values; | 417 Vector<SkFontArguments::VariationPosition::Coordinate> axis_values; |
| 418 axis_values.Resize(axis_count); | 418 axis_values.Resize(axis_count); |
| 419 if (typeface->getVariationDesignPosition(axis_values.Data(), | 419 if (typeface->getVariationDesignPosition(axis_values.Data(), |
| 420 axis_values.size()) > 0) { | 420 axis_values.size()) > 0) { |
| 421 hb_font_set_variations( | 421 hb_font_set_variations( |
| 422 unscaled_font_, reinterpret_cast<hb_variation_t*>(axis_values.Data()), | 422 unscaled_font_, reinterpret_cast<hb_variation_t*>(axis_values.Data()), |
| 423 axis_values.size()); | 423 axis_values.size()); |
| 424 } | 424 } |
| 425 } | 425 } |
| 426 | 426 |
| 427 return unscaled_font_; | 427 return unscaled_font_; |
| 428 } | 428 } |
| 429 | 429 |
| 430 } // namespace blink | 430 } // namespace blink |
| OLD | NEW |