| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/harfbuzz_font_skia.h" | 5 #include "ui/gfx/harfbuzz_font_skia.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| 11 #include <map> | 11 #include <map> |
| 12 | 12 |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "cc/paint/paint_flags.h" | |
| 17 #include "third_party/skia/include/core/SkTypeface.h" | 16 #include "third_party/skia/include/core/SkTypeface.h" |
| 18 #include "ui/gfx/render_text.h" | 17 #include "ui/gfx/render_text.h" |
| 19 #include "ui/gfx/skia_util.h" | 18 #include "ui/gfx/skia_util.h" |
| 20 | 19 |
| 21 namespace gfx { | 20 namespace gfx { |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 class HarfBuzzFace; | 24 class HarfBuzzFace; |
| 26 | 25 |
| 27 // Maps from code points to glyph indices in a font. | 26 // Maps from code points to glyph indices in a font. |
| 28 typedef std::map<uint32_t, uint16_t> GlyphCache; | 27 typedef std::map<uint32_t, uint16_t> GlyphCache; |
| 29 | 28 |
| 30 typedef std::pair<HarfBuzzFace, GlyphCache> FaceCache; | 29 typedef std::pair<HarfBuzzFace, GlyphCache> FaceCache; |
| 31 | 30 |
| 32 // Font data provider for HarfBuzz using Skia. Copied from Blink. | 31 // Font data provider for HarfBuzz using Skia. Copied from Blink. |
| 33 // TODO(ckocagil): Eliminate the duplication. http://crbug.com/368375 | 32 // TODO(ckocagil): Eliminate the duplication. http://crbug.com/368375 |
| 34 struct FontData { | 33 struct FontData { |
| 35 FontData(GlyphCache* glyph_cache) : glyph_cache_(glyph_cache) {} | 34 FontData(GlyphCache* glyph_cache) : glyph_cache_(glyph_cache) {} |
| 36 | 35 |
| 37 cc::PaintFlags paint_; | 36 SkPaint paint_; |
| 38 GlyphCache* glyph_cache_; | 37 GlyphCache* glyph_cache_; |
| 39 }; | 38 }; |
| 40 | 39 |
| 41 // Deletes the object at the given pointer after casting it to the given type. | 40 // Deletes the object at the given pointer after casting it to the given type. |
| 42 template<typename Type> | 41 template<typename Type> |
| 43 void DeleteByType(void* data) { | 42 void DeleteByType(void* data) { |
| 44 Type* typed_data = reinterpret_cast<Type*>(data); | 43 Type* typed_data = reinterpret_cast<Type*>(data); |
| 45 delete typed_data; | 44 delete typed_data; |
| 46 } | 45 } |
| 47 | 46 |
| 48 template<typename Type> | 47 template<typename Type> |
| 49 void DeleteArrayByType(void* data) { | 48 void DeleteArrayByType(void* data) { |
| 50 Type* typed_data = reinterpret_cast<Type*>(data); | 49 Type* typed_data = reinterpret_cast<Type*>(data); |
| 51 delete[] typed_data; | 50 delete[] typed_data; |
| 52 } | 51 } |
| 53 | 52 |
| 54 // Outputs the |width| and |extents| of the glyph with index |codepoint| in | 53 // Outputs the |width| and |extents| of the glyph with index |codepoint| in |
| 55 // |paint|'s font. | 54 // |paint|'s font. |
| 56 void GetGlyphWidthAndExtents(cc::PaintFlags* paint, | 55 void GetGlyphWidthAndExtents(SkPaint* paint, |
| 57 hb_codepoint_t codepoint, | 56 hb_codepoint_t codepoint, |
| 58 hb_position_t* width, | 57 hb_position_t* width, |
| 59 hb_glyph_extents_t* extents) { | 58 hb_glyph_extents_t* extents) { |
| 60 DCHECK_LE(codepoint, std::numeric_limits<uint16_t>::max()); | 59 DCHECK_LE(codepoint, std::numeric_limits<uint16_t>::max()); |
| 61 paint->setTextEncoding(cc::PaintFlags::kGlyphID_TextEncoding); | 60 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 62 | 61 |
| 63 SkScalar sk_width; | 62 SkScalar sk_width; |
| 64 SkRect sk_bounds; | 63 SkRect sk_bounds; |
| 65 uint16_t glyph = static_cast<uint16_t>(codepoint); | 64 uint16_t glyph = static_cast<uint16_t>(codepoint); |
| 66 | 65 |
| 67 paint->getTextWidths(&glyph, sizeof(glyph), &sk_width, &sk_bounds); | 66 paint->getTextWidths(&glyph, sizeof(glyph), &sk_width, &sk_bounds); |
| 68 if (width) | 67 if (width) |
| 69 *width = SkiaScalarToHarfBuzzUnits(sk_width); | 68 *width = SkiaScalarToHarfBuzzUnits(sk_width); |
| 70 if (extents) { | 69 if (extents) { |
| 71 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be | 70 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be |
| (...skipping 11 matching lines...) Expand all Loading... |
| 83 void* data, | 82 void* data, |
| 84 hb_codepoint_t unicode, | 83 hb_codepoint_t unicode, |
| 85 hb_codepoint_t variation_selector, | 84 hb_codepoint_t variation_selector, |
| 86 hb_codepoint_t* glyph, | 85 hb_codepoint_t* glyph, |
| 87 void* user_data) { | 86 void* user_data) { |
| 88 FontData* font_data = reinterpret_cast<FontData*>(data); | 87 FontData* font_data = reinterpret_cast<FontData*>(data); |
| 89 GlyphCache* cache = font_data->glyph_cache_; | 88 GlyphCache* cache = font_data->glyph_cache_; |
| 90 | 89 |
| 91 bool exists = cache->count(unicode) != 0; | 90 bool exists = cache->count(unicode) != 0; |
| 92 if (!exists) { | 91 if (!exists) { |
| 93 cc::PaintFlags* paint = &font_data->paint_; | 92 SkPaint* paint = &font_data->paint_; |
| 94 paint->setTextEncoding(cc::PaintFlags::kUTF32_TextEncoding); | 93 paint->setTextEncoding(SkPaint::kUTF32_TextEncoding); |
| 95 paint->textToGlyphs(&unicode, sizeof(hb_codepoint_t), &(*cache)[unicode]); | 94 paint->textToGlyphs(&unicode, sizeof(hb_codepoint_t), &(*cache)[unicode]); |
| 96 } | 95 } |
| 97 *glyph = (*cache)[unicode]; | 96 *glyph = (*cache)[unicode]; |
| 98 return !!*glyph; | 97 return !!*glyph; |
| 99 } | 98 } |
| 100 | 99 |
| 101 // Returns the horizontal advance value of the |glyph|. | 100 // Returns the horizontal advance value of the |glyph|. |
| 102 hb_position_t GetGlyphHorizontalAdvance(hb_font_t* font, | 101 hb_position_t GetGlyphHorizontalAdvance(hb_font_t* font, |
| 103 void* data, | 102 void* data, |
| 104 hb_codepoint_t glyph, | 103 hb_codepoint_t glyph, |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 // TODO(ckocagil): Do we need to update these params later? | 279 // TODO(ckocagil): Do we need to update these params later? |
| 281 internal::ApplyRenderParams(params, subpixel_rendering_suppressed, | 280 internal::ApplyRenderParams(params, subpixel_rendering_suppressed, |
| 282 &hb_font_data->paint_); | 281 &hb_font_data->paint_); |
| 283 hb_font_set_funcs(harfbuzz_font, g_font_funcs.Get().get(), hb_font_data, | 282 hb_font_set_funcs(harfbuzz_font, g_font_funcs.Get().get(), hb_font_data, |
| 284 DeleteByType<FontData>); | 283 DeleteByType<FontData>); |
| 285 hb_font_make_immutable(harfbuzz_font); | 284 hb_font_make_immutable(harfbuzz_font); |
| 286 return harfbuzz_font; | 285 return harfbuzz_font; |
| 287 } | 286 } |
| 288 | 287 |
| 289 } // namespace gfx | 288 } // namespace gfx |
| OLD | NEW |