| 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 "skia/ext/cdl_paint.h" |
| 16 #include "third_party/skia/include/core/SkPaint.h" | 17 #include "third_party/skia/include/core/SkPaint.h" |
| 17 #include "third_party/skia/include/core/SkTypeface.h" | 18 #include "third_party/skia/include/core/SkTypeface.h" |
| 18 #include "ui/gfx/render_text.h" | 19 #include "ui/gfx/render_text.h" |
| 19 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
| 20 | 21 |
| 21 namespace gfx { | 22 namespace gfx { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 class HarfBuzzFace; | 26 class HarfBuzzFace; |
| 26 | 27 |
| 27 // Maps from code points to glyph indices in a font. | 28 // Maps from code points to glyph indices in a font. |
| 28 typedef std::map<uint32_t, uint16_t> GlyphCache; | 29 typedef std::map<uint32_t, uint16_t> GlyphCache; |
| 29 | 30 |
| 30 typedef std::pair<HarfBuzzFace, GlyphCache> FaceCache; | 31 typedef std::pair<HarfBuzzFace, GlyphCache> FaceCache; |
| 31 | 32 |
| 32 // Font data provider for HarfBuzz using Skia. Copied from Blink. | 33 // Font data provider for HarfBuzz using Skia. Copied from Blink. |
| 33 // TODO(ckocagil): Eliminate the duplication. http://crbug.com/368375 | 34 // TODO(ckocagil): Eliminate the duplication. http://crbug.com/368375 |
| 34 struct FontData { | 35 struct FontData { |
| 35 FontData(GlyphCache* glyph_cache) : glyph_cache_(glyph_cache) {} | 36 FontData(GlyphCache* glyph_cache) : glyph_cache_(glyph_cache) {} |
| 36 | 37 |
| 37 SkPaint paint_; | 38 CdlPaint paint_; |
| 38 GlyphCache* glyph_cache_; | 39 GlyphCache* glyph_cache_; |
| 39 }; | 40 }; |
| 40 | 41 |
| 41 // Deletes the object at the given pointer after casting it to the given type. | 42 // Deletes the object at the given pointer after casting it to the given type. |
| 42 template<typename Type> | 43 template<typename Type> |
| 43 void DeleteByType(void* data) { | 44 void DeleteByType(void* data) { |
| 44 Type* typed_data = reinterpret_cast<Type*>(data); | 45 Type* typed_data = reinterpret_cast<Type*>(data); |
| 45 delete typed_data; | 46 delete typed_data; |
| 46 } | 47 } |
| 47 | 48 |
| 48 template<typename Type> | 49 template<typename Type> |
| 49 void DeleteArrayByType(void* data) { | 50 void DeleteArrayByType(void* data) { |
| 50 Type* typed_data = reinterpret_cast<Type*>(data); | 51 Type* typed_data = reinterpret_cast<Type*>(data); |
| 51 delete[] typed_data; | 52 delete[] typed_data; |
| 52 } | 53 } |
| 53 | 54 |
| 54 // Outputs the |width| and |extents| of the glyph with index |codepoint| in | 55 // Outputs the |width| and |extents| of the glyph with index |codepoint| in |
| 55 // |paint|'s font. | 56 // |paint|'s font. |
| 56 void GetGlyphWidthAndExtents(SkPaint* paint, | 57 void GetGlyphWidthAndExtents(CdlPaint* paint, |
| 57 hb_codepoint_t codepoint, | 58 hb_codepoint_t codepoint, |
| 58 hb_position_t* width, | 59 hb_position_t* width, |
| 59 hb_glyph_extents_t* extents) { | 60 hb_glyph_extents_t* extents) { |
| 60 DCHECK_LE(codepoint, std::numeric_limits<uint16_t>::max()); | 61 DCHECK_LE(codepoint, std::numeric_limits<uint16_t>::max()); |
| 61 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); | 62 paint->setTextEncoding(CdlPaint::kGlyphID_TextEncoding); |
| 62 | 63 |
| 63 SkScalar sk_width; | 64 SkScalar sk_width; |
| 64 SkRect sk_bounds; | 65 SkRect sk_bounds; |
| 65 uint16_t glyph = static_cast<uint16_t>(codepoint); | 66 uint16_t glyph = static_cast<uint16_t>(codepoint); |
| 66 | 67 |
| 67 paint->getTextWidths(&glyph, sizeof(glyph), &sk_width, &sk_bounds); | 68 ToSkPaint(*paint).getTextWidths(&glyph, sizeof(glyph), &sk_width, &sk_bounds); |
| 68 if (width) | 69 if (width) |
| 69 *width = SkiaScalarToHarfBuzzUnits(sk_width); | 70 *width = SkiaScalarToHarfBuzzUnits(sk_width); |
| 70 if (extents) { | 71 if (extents) { |
| 71 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be | 72 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be |
| 72 // y-grows-up. | 73 // y-grows-up. |
| 73 extents->x_bearing = SkiaScalarToHarfBuzzUnits(sk_bounds.fLeft); | 74 extents->x_bearing = SkiaScalarToHarfBuzzUnits(sk_bounds.fLeft); |
| 74 extents->y_bearing = SkiaScalarToHarfBuzzUnits(-sk_bounds.fTop); | 75 extents->y_bearing = SkiaScalarToHarfBuzzUnits(-sk_bounds.fTop); |
| 75 extents->width = SkiaScalarToHarfBuzzUnits(sk_bounds.width()); | 76 extents->width = SkiaScalarToHarfBuzzUnits(sk_bounds.width()); |
| 76 extents->height = SkiaScalarToHarfBuzzUnits(-sk_bounds.height()); | 77 extents->height = SkiaScalarToHarfBuzzUnits(-sk_bounds.height()); |
| 77 } | 78 } |
| 78 } | 79 } |
| 79 | 80 |
| 80 // Writes the |glyph| index for the given |unicode| code point. Returns whether | 81 // Writes the |glyph| index for the given |unicode| code point. Returns whether |
| 81 // the glyph exists, i.e. it is not a missing glyph. | 82 // the glyph exists, i.e. it is not a missing glyph. |
| 82 hb_bool_t GetGlyph(hb_font_t* font, | 83 hb_bool_t GetGlyph(hb_font_t* font, |
| 83 void* data, | 84 void* data, |
| 84 hb_codepoint_t unicode, | 85 hb_codepoint_t unicode, |
| 85 hb_codepoint_t variation_selector, | 86 hb_codepoint_t variation_selector, |
| 86 hb_codepoint_t* glyph, | 87 hb_codepoint_t* glyph, |
| 87 void* user_data) { | 88 void* user_data) { |
| 88 FontData* font_data = reinterpret_cast<FontData*>(data); | 89 FontData* font_data = reinterpret_cast<FontData*>(data); |
| 89 GlyphCache* cache = font_data->glyph_cache_; | 90 GlyphCache* cache = font_data->glyph_cache_; |
| 90 | 91 |
| 91 bool exists = cache->count(unicode) != 0; | 92 bool exists = cache->count(unicode) != 0; |
| 92 if (!exists) { | 93 if (!exists) { |
| 93 SkPaint* paint = &font_data->paint_; | 94 CdlPaint* paint = &font_data->paint_; |
| 94 paint->setTextEncoding(SkPaint::kUTF32_TextEncoding); | 95 paint->setTextEncoding(CdlPaint::kUTF32_TextEncoding); |
| 95 paint->textToGlyphs(&unicode, sizeof(hb_codepoint_t), &(*cache)[unicode]); | 96 ToSkPaint(*paint).textToGlyphs(&unicode, sizeof(hb_codepoint_t), |
| 97 &(*cache)[unicode]); |
| 96 } | 98 } |
| 97 *glyph = (*cache)[unicode]; | 99 *glyph = (*cache)[unicode]; |
| 98 return !!*glyph; | 100 return !!*glyph; |
| 99 } | 101 } |
| 100 | 102 |
| 101 // Returns the horizontal advance value of the |glyph|. | 103 // Returns the horizontal advance value of the |glyph|. |
| 102 hb_position_t GetGlyphHorizontalAdvance(hb_font_t* font, | 104 hb_position_t GetGlyphHorizontalAdvance(hb_font_t* font, |
| 103 void* data, | 105 void* data, |
| 104 hb_codepoint_t glyph, | 106 hb_codepoint_t glyph, |
| 105 void* user_data) { | 107 void* user_data) { |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 // TODO(ckocagil): Do we need to update these params later? | 282 // TODO(ckocagil): Do we need to update these params later? |
| 281 internal::ApplyRenderParams(params, subpixel_rendering_suppressed, | 283 internal::ApplyRenderParams(params, subpixel_rendering_suppressed, |
| 282 &hb_font_data->paint_); | 284 &hb_font_data->paint_); |
| 283 hb_font_set_funcs(harfbuzz_font, g_font_funcs.Get().get(), hb_font_data, | 285 hb_font_set_funcs(harfbuzz_font, g_font_funcs.Get().get(), hb_font_data, |
| 284 DeleteByType<FontData>); | 286 DeleteByType<FontData>); |
| 285 hb_font_make_immutable(harfbuzz_font); | 287 hb_font_make_immutable(harfbuzz_font); |
| 286 return harfbuzz_font; | 288 return harfbuzz_font; |
| 287 } | 289 } |
| 288 | 290 |
| 289 } // namespace gfx | 291 } // namespace gfx |
| OLD | NEW |