| 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 "third_party/skia/include/core/SkPaint.h" | 16 #include "third_party/skia/include/core/SkPaint.h" |
| 17 #include "third_party/skia/include/core/SkTypeface.h" | 17 #include "third_party/skia/include/core/SkTypeface.h" |
| 18 #include "ui/gfx/render_text.h" | 18 #include "ui/gfx/render_text.h" |
| 19 #include "ui/gfx/skia_util.h" |
| 19 | 20 |
| 20 namespace gfx { | 21 namespace gfx { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 class HarfBuzzFace; | 25 class HarfBuzzFace; |
| 25 | 26 |
| 26 // Maps from code points to glyph indices in a font. | 27 // Maps from code points to glyph indices in a font. |
| 27 typedef std::map<uint32_t, uint16_t> GlyphCache; | 28 typedef std::map<uint32_t, uint16_t> GlyphCache; |
| 28 | 29 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 58 hb_glyph_extents_t* extents) { | 59 hb_glyph_extents_t* extents) { |
| 59 DCHECK_LE(codepoint, std::numeric_limits<uint16_t>::max()); | 60 DCHECK_LE(codepoint, std::numeric_limits<uint16_t>::max()); |
| 60 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); | 61 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 61 | 62 |
| 62 SkScalar sk_width; | 63 SkScalar sk_width; |
| 63 SkRect sk_bounds; | 64 SkRect sk_bounds; |
| 64 uint16_t glyph = static_cast<uint16_t>(codepoint); | 65 uint16_t glyph = static_cast<uint16_t>(codepoint); |
| 65 | 66 |
| 66 paint->getTextWidths(&glyph, sizeof(glyph), &sk_width, &sk_bounds); | 67 paint->getTextWidths(&glyph, sizeof(glyph), &sk_width, &sk_bounds); |
| 67 if (width) | 68 if (width) |
| 68 *width = SkScalarToFixed(sk_width); | 69 *width = SkiaScalarToHarfBuzzUnits(sk_width); |
| 69 if (extents) { | 70 if (extents) { |
| 70 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be | 71 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be |
| 71 // y-grows-up. | 72 // y-grows-up. |
| 72 extents->x_bearing = SkScalarToFixed(sk_bounds.fLeft); | 73 extents->x_bearing = SkiaScalarToHarfBuzzUnits(sk_bounds.fLeft); |
| 73 extents->y_bearing = SkScalarToFixed(-sk_bounds.fTop); | 74 extents->y_bearing = SkiaScalarToHarfBuzzUnits(-sk_bounds.fTop); |
| 74 extents->width = SkScalarToFixed(sk_bounds.width()); | 75 extents->width = SkiaScalarToHarfBuzzUnits(sk_bounds.width()); |
| 75 extents->height = SkScalarToFixed(-sk_bounds.height()); | 76 extents->height = SkiaScalarToHarfBuzzUnits(-sk_bounds.height()); |
| 76 } | 77 } |
| 77 } | 78 } |
| 78 | 79 |
| 79 // Writes the |glyph| index for the given |unicode| code point. Returns whether | 80 // Writes the |glyph| index for the given |unicode| code point. Returns whether |
| 80 // the glyph exists, i.e. it is not a missing glyph. | 81 // the glyph exists, i.e. it is not a missing glyph. |
| 81 hb_bool_t GetGlyph(hb_font_t* font, | 82 hb_bool_t GetGlyph(hb_font_t* font, |
| 82 void* data, | 83 void* data, |
| 83 hb_codepoint_t unicode, | 84 hb_codepoint_t unicode, |
| 84 hb_codepoint_t variation_selector, | 85 hb_codepoint_t variation_selector, |
| 85 hb_codepoint_t* glyph, | 86 hb_codepoint_t* glyph, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 SkTypeface* typeface = font_data->paint_.getTypeface(); | 126 SkTypeface* typeface = font_data->paint_.getTypeface(); |
| 126 const uint16_t glyphs[2] = { static_cast<uint16_t>(first_glyph), | 127 const uint16_t glyphs[2] = { static_cast<uint16_t>(first_glyph), |
| 127 static_cast<uint16_t>(second_glyph) }; | 128 static_cast<uint16_t>(second_glyph) }; |
| 128 int32_t kerning_adjustments[1] = { 0 }; | 129 int32_t kerning_adjustments[1] = { 0 }; |
| 129 | 130 |
| 130 if (!typeface->getKerningPairAdjustments(glyphs, 2, kerning_adjustments)) | 131 if (!typeface->getKerningPairAdjustments(glyphs, 2, kerning_adjustments)) |
| 131 return 0; | 132 return 0; |
| 132 | 133 |
| 133 SkScalar upm = SkIntToScalar(typeface->getUnitsPerEm()); | 134 SkScalar upm = SkIntToScalar(typeface->getUnitsPerEm()); |
| 134 SkScalar size = font_data->paint_.getTextSize(); | 135 SkScalar size = font_data->paint_.getTextSize(); |
| 135 return SkScalarToFixed( | 136 return SkiaScalarToHarfBuzzUnits( |
| 136 SkScalarMulDiv(SkIntToScalar(kerning_adjustments[0]), size, upm)); | 137 SkScalarMulDiv(SkIntToScalar(kerning_adjustments[0]), size, upm)); |
| 137 } | 138 } |
| 138 | 139 |
| 139 hb_position_t GetGlyphHorizontalKerning(hb_font_t* font, | 140 hb_position_t GetGlyphHorizontalKerning(hb_font_t* font, |
| 140 void* data, | 141 void* data, |
| 141 hb_codepoint_t left_glyph, | 142 hb_codepoint_t left_glyph, |
| 142 hb_codepoint_t right_glyph, | 143 hb_codepoint_t right_glyph, |
| 143 void* user_data) { | 144 void* user_data) { |
| 144 FontData* font_data = reinterpret_cast<FontData*>(data); | 145 FontData* font_data = reinterpret_cast<FontData*>(data); |
| 145 if (font_data->paint_.isVerticalText()) { | 146 if (font_data->paint_.isVerticalText()) { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 const FontRenderParams& params, | 265 const FontRenderParams& params, |
| 265 bool subpixel_rendering_suppressed) { | 266 bool subpixel_rendering_suppressed) { |
| 266 // TODO(ckocagil): This shouldn't grow indefinitely. Maybe use base::MRUCache? | 267 // TODO(ckocagil): This shouldn't grow indefinitely. Maybe use base::MRUCache? |
| 267 static std::map<SkFontID, FaceCache> face_caches; | 268 static std::map<SkFontID, FaceCache> face_caches; |
| 268 | 269 |
| 269 FaceCache* face_cache = &face_caches[skia_face->uniqueID()]; | 270 FaceCache* face_cache = &face_caches[skia_face->uniqueID()]; |
| 270 if (face_cache->first.get() == NULL) | 271 if (face_cache->first.get() == NULL) |
| 271 face_cache->first.Init(skia_face); | 272 face_cache->first.Init(skia_face); |
| 272 | 273 |
| 273 hb_font_t* harfbuzz_font = hb_font_create(face_cache->first.get()); | 274 hb_font_t* harfbuzz_font = hb_font_create(face_cache->first.get()); |
| 274 const int scale = SkScalarToFixed(text_size); | 275 const int scale = SkiaScalarToHarfBuzzUnits(text_size); |
| 275 hb_font_set_scale(harfbuzz_font, scale, scale); | 276 hb_font_set_scale(harfbuzz_font, scale, scale); |
| 276 FontData* hb_font_data = new FontData(&face_cache->second); | 277 FontData* hb_font_data = new FontData(&face_cache->second); |
| 277 hb_font_data->paint_.setTypeface(skia_face); | 278 hb_font_data->paint_.setTypeface(skia_face); |
| 278 hb_font_data->paint_.setTextSize(text_size); | 279 hb_font_data->paint_.setTextSize(text_size); |
| 279 // TODO(ckocagil): Do we need to update these params later? | 280 // TODO(ckocagil): Do we need to update these params later? |
| 280 internal::ApplyRenderParams(params, subpixel_rendering_suppressed, | 281 internal::ApplyRenderParams(params, subpixel_rendering_suppressed, |
| 281 &hb_font_data->paint_); | 282 &hb_font_data->paint_); |
| 282 hb_font_set_funcs(harfbuzz_font, g_font_funcs.Get().get(), hb_font_data, | 283 hb_font_set_funcs(harfbuzz_font, g_font_funcs.Get().get(), hb_font_data, |
| 283 DeleteByType<FontData>); | 284 DeleteByType<FontData>); |
| 284 hb_font_make_immutable(harfbuzz_font); | 285 hb_font_make_immutable(harfbuzz_font); |
| 285 return harfbuzz_font; | 286 return harfbuzz_font; |
| 286 } | 287 } |
| 287 | 288 |
| 288 } // namespace gfx | 289 } // namespace gfx |
| OLD | NEW |