| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/render_text_harfbuzz.h" | 5 #include "ui/gfx/render_text_harfbuzz.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
| 10 #include "base/i18n/char_iterator.h" | 10 #include "base/i18n/char_iterator.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 hb_bool_t GetGlyphHorizontalOrigin(hb_font_t* font, | 119 hb_bool_t GetGlyphHorizontalOrigin(hb_font_t* font, |
| 120 void* data, | 120 void* data, |
| 121 hb_codepoint_t glyph, | 121 hb_codepoint_t glyph, |
| 122 hb_position_t* x, | 122 hb_position_t* x, |
| 123 hb_position_t* y, | 123 hb_position_t* y, |
| 124 void* user_data) { | 124 void* user_data) { |
| 125 // Just return true, like the HarfBuzz-FreeType implementation. | 125 // Just return true, like the HarfBuzz-FreeType implementation. |
| 126 return true; | 126 return true; |
| 127 } | 127 } |
| 128 | 128 |
| 129 hb_position_t GetGlyphKerning(FontData* font_data, |
| 130 hb_codepoint_t first_glyph, |
| 131 hb_codepoint_t second_glyph) { |
| 132 SkTypeface* typeface = font_data->paint_.getTypeface(); |
| 133 const uint16_t glyphs[2] = { first_glyph, second_glyph }; |
| 134 int32_t kerning_adjustments[1] = { 0 }; |
| 135 |
| 136 if (!typeface->getKerningPairAdjustments(glyphs, 2, kerning_adjustments)) |
| 137 return 0; |
| 138 |
| 139 SkScalar upm = SkIntToScalar(typeface->getUnitsPerEm()); |
| 140 SkScalar size = font_data->paint_.getTextSize(); |
| 141 return SkiaScalarToHarfBuzzPosition( |
| 142 SkScalarMulDiv(SkIntToScalar(kerning_adjustments[0]), size, upm)); |
| 143 } |
| 144 |
| 145 hb_position_t GetGlyphHorizontalKerning(hb_font_t* font, |
| 146 void* data, |
| 147 hb_codepoint_t left_glyph, |
| 148 hb_codepoint_t right_glyph, |
| 149 void* user_data) { |
| 150 FontData* font_data = reinterpret_cast<FontData*>(data); |
| 151 if (font_data->paint_.isVerticalText()) { |
| 152 // We don't support cross-stream kerning. |
| 153 return 0; |
| 154 } |
| 155 |
| 156 return GetGlyphKerning(font_data, left_glyph, right_glyph); |
| 157 } |
| 158 |
| 159 hb_position_t GetGlyphVerticalKerning(hb_font_t* font, |
| 160 void* data, |
| 161 hb_codepoint_t top_glyph, |
| 162 hb_codepoint_t bottom_glyph, |
| 163 void* user_data) { |
| 164 FontData* font_data = reinterpret_cast<FontData*>(data); |
| 165 if (!font_data->paint_.isVerticalText()) { |
| 166 // We don't support cross-stream kerning. |
| 167 return 0; |
| 168 } |
| 169 |
| 170 return GetGlyphKerning(font_data, top_glyph, bottom_glyph); |
| 171 } |
| 172 |
| 129 // Writes the |extents| of |glyph|. | 173 // Writes the |extents| of |glyph|. |
| 130 hb_bool_t GetGlyphExtents(hb_font_t* font, | 174 hb_bool_t GetGlyphExtents(hb_font_t* font, |
| 131 void* data, | 175 void* data, |
| 132 hb_codepoint_t glyph, | 176 hb_codepoint_t glyph, |
| 133 hb_glyph_extents_t* extents, | 177 hb_glyph_extents_t* extents, |
| 134 void* user_data) { | 178 void* user_data) { |
| 135 FontData* font_data = reinterpret_cast<FontData*>(data); | 179 FontData* font_data = reinterpret_cast<FontData*>(data); |
| 136 | 180 |
| 137 GetGlyphWidthAndExtents(&font_data->paint_, glyph, 0, extents); | 181 GetGlyphWidthAndExtents(&font_data->paint_, glyph, 0, extents); |
| 138 return true; | 182 return true; |
| 139 } | 183 } |
| 140 | 184 |
| 141 // Returns a HarfBuzz font data provider that uses Skia. | 185 // Returns a HarfBuzz font data provider that uses Skia. |
| 142 hb_font_funcs_t* GetFontFuncs() { | 186 hb_font_funcs_t* GetFontFuncs() { |
| 143 static hb_font_funcs_t* font_funcs = 0; | 187 static hb_font_funcs_t* font_funcs = 0; |
| 144 | 188 |
| 145 // We don't set callback functions which we can't support. | 189 // We don't set callback functions which we can't support. |
| 146 // HarfBuzz will use the fallback implementation if they aren't set. | 190 // HarfBuzz will use the fallback implementation if they aren't set. |
| 147 // TODO(ckocagil): Merge Blink's kerning funcs. | |
| 148 if (!font_funcs) { | 191 if (!font_funcs) { |
| 149 // The object created by |hb_font_funcs_create()| below lives indefinitely | 192 // The object created by |hb_font_funcs_create()| below lives indefinitely |
| 150 // and is intentionally leaked. | 193 // and is intentionally leaked. |
| 151 font_funcs = hb_font_funcs_create(); | 194 font_funcs = hb_font_funcs_create(); |
| 152 hb_font_funcs_set_glyph_func(font_funcs, GetGlyph, 0, 0); | 195 hb_font_funcs_set_glyph_func(font_funcs, GetGlyph, 0, 0); |
| 153 hb_font_funcs_set_glyph_h_advance_func( | 196 hb_font_funcs_set_glyph_h_advance_func( |
| 154 font_funcs, GetGlyphHorizontalAdvance, 0, 0); | 197 font_funcs, GetGlyphHorizontalAdvance, 0, 0); |
| 198 hb_font_funcs_set_glyph_h_kerning_func( |
| 199 font_funcs, GetGlyphHorizontalKerning, 0, 0); |
| 155 hb_font_funcs_set_glyph_h_origin_func( | 200 hb_font_funcs_set_glyph_h_origin_func( |
| 156 font_funcs, GetGlyphHorizontalOrigin, 0, 0); | 201 font_funcs, GetGlyphHorizontalOrigin, 0, 0); |
| 202 hb_font_funcs_set_glyph_v_kerning_func( |
| 203 font_funcs, GetGlyphVerticalKerning, 0, 0); |
| 157 hb_font_funcs_set_glyph_extents_func( | 204 hb_font_funcs_set_glyph_extents_func( |
| 158 font_funcs, GetGlyphExtents, 0, 0); | 205 font_funcs, GetGlyphExtents, 0, 0); |
| 159 hb_font_funcs_make_immutable(font_funcs); | 206 hb_font_funcs_make_immutable(font_funcs); |
| 160 } | 207 } |
| 161 return font_funcs; | 208 return font_funcs; |
| 162 } | 209 } |
| 163 | 210 |
| 164 // Returns the raw data of the font table |tag|. | 211 // Returns the raw data of the font table |tag|. |
| 165 hb_blob_t* GetFontTable(hb_face_t* face, hb_tag_t tag, void* user_data) { | 212 hb_blob_t* GetFontTable(hb_face_t* face, hb_tag_t tag, void* user_data) { |
| 166 SkTypeface* typeface = reinterpret_cast<SkTypeface*>(user_data); | 213 SkTypeface* typeface = reinterpret_cast<SkTypeface*>(user_data); |
| (...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 943 run->positions[i].set(run->width + x_offset, y_offset); | 990 run->positions[i].set(run->width + x_offset, y_offset); |
| 944 run->width += | 991 run->width += |
| 945 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance)); | 992 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance)); |
| 946 } | 993 } |
| 947 | 994 |
| 948 hb_buffer_destroy(buffer); | 995 hb_buffer_destroy(buffer); |
| 949 hb_font_destroy(harfbuzz_font); | 996 hb_font_destroy(harfbuzz_font); |
| 950 } | 997 } |
| 951 | 998 |
| 952 } // namespace gfx | 999 } // namespace gfx |
| OLD | NEW |