Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Side by Side Diff: third_party/WebKit/Source/platform/fonts/shaping/HarfBuzzFace.cpp

Issue 1980913002: Unify glyph metrics to use the same Skia API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Don't remove glyphToBoundsMap yet Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 18 matching lines...) Expand all
29 */ 29 */
30 30
31 #include "platform/fonts/shaping/HarfBuzzFace.h" 31 #include "platform/fonts/shaping/HarfBuzzFace.h"
32 32
33 #include "platform/Histogram.h" 33 #include "platform/Histogram.h"
34 #include "platform/fonts/FontCache.h" 34 #include "platform/fonts/FontCache.h"
35 #include "platform/fonts/FontPlatformData.h" 35 #include "platform/fonts/FontPlatformData.h"
36 #include "platform/fonts/SimpleFontData.h" 36 #include "platform/fonts/SimpleFontData.h"
37 #include "platform/fonts/UnicodeRangeSet.h" 37 #include "platform/fonts/UnicodeRangeSet.h"
38 #include "platform/fonts/shaping/HarfBuzzShaper.h" 38 #include "platform/fonts/shaping/HarfBuzzShaper.h"
39 #include "platform/fonts/skia/SkiaTextMetrics.h"
39 #include "wtf/HashMap.h" 40 #include "wtf/HashMap.h"
40 #include "wtf/MathExtras.h" 41 #include "wtf/MathExtras.h"
41 #include "wtf/PtrUtil.h" 42 #include "wtf/PtrUtil.h"
42 #include <memory> 43 #include <memory>
43 44
44 #include <hb-ot.h> 45 #include <hb-ot.h>
45 #include <hb.h> 46 #include <hb.h>
46 #if OS(MACOSX) 47 #if OS(MACOSX)
47 #include <hb-coretext.h> 48 #include <hb-coretext.h>
48 #endif 49 #endif
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 harfBuzzFontCache()->remove(m_uniqueID); 158 harfBuzzFontCache()->remove(m_uniqueID);
158 } 159 }
159 160
160 static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) 161 static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value)
161 { 162 {
162 // We treat HarfBuzz hb_position_t as 16.16 fixed-point. 163 // We treat HarfBuzz hb_position_t as 16.16 fixed-point.
163 static const int kHbPosition1 = 1 << 16; 164 static const int kHbPosition1 = 1 << 16;
164 return clampTo<int>(value * kHbPosition1); 165 return clampTo<int>(value * kHbPosition1);
165 } 166 }
166 167
167 static void SkiaGetGlyphWidthAndExtents(SkPaint* paint, hb_codepoint_t codepoint , hb_position_t* width, hb_glyph_extents_t* extents)
168 {
169 ASSERT(codepoint <= 0xFFFF);
170 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
171
172 SkScalar skWidth;
173 SkRect skBounds;
174 uint16_t glyph = codepoint;
175
176 paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, &skBounds);
177 if (width) {
178 if (!paint->isSubpixelText())
179 skWidth = SkScalarRoundToInt(skWidth);
180 *width = SkiaScalarToHarfBuzzPosition(skWidth);
181 }
182 if (extents) {
183 if (!paint->isSubpixelText()) {
184 // Use roundOut() rather than round() to avoid rendering glyphs
185 // outside the visual overflow rect. crbug.com/452914.
186 SkIRect ir;
187 skBounds.roundOut(&ir);
188 skBounds.set(ir);
189 }
190 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be y-grows-up.
191 extents->x_bearing = SkiaScalarToHarfBuzzPosition(skBounds.fLeft);
192 extents->y_bearing = SkiaScalarToHarfBuzzPosition(-skBounds.fTop);
193 extents->width = SkiaScalarToHarfBuzzPosition(skBounds.width());
194 extents->height = SkiaScalarToHarfBuzzPosition(-skBounds.height());
195 }
196 }
197
198 static hb_bool_t harfBuzzGetGlyph(hb_font_t* hbFont, void* fontData, hb_codepoin t_t unicode, hb_codepoint_t variationSelector, hb_codepoint_t* glyph, void* user Data) 168 static hb_bool_t harfBuzzGetGlyph(hb_font_t* hbFont, void* fontData, hb_codepoin t_t unicode, hb_codepoint_t variationSelector, hb_codepoint_t* glyph, void* user Data)
199 { 169 {
200 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData) ; 170 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData) ;
201 171
202 RELEASE_ASSERT(hbFontData); 172 RELEASE_ASSERT(hbFontData);
203 if (hbFontData->m_rangeSet && !hbFontData->m_rangeSet->contains(unicode)) 173 if (hbFontData->m_rangeSet && !hbFontData->m_rangeSet->contains(unicode))
204 return false; 174 return false;
205 175
206 return hb_font_get_glyph(hb_font_get_parent(hbFont), unicode, variationSelec tor, glyph); 176 return hb_font_get_glyph(hb_font_get_parent(hbFont), unicode, variationSelec tor, glyph);
207 } 177 }
208 178
209 static hb_position_t harfBuzzGetGlyphHorizontalAdvance(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, void* userData) 179 static hb_position_t harfBuzzGetGlyphHorizontalAdvance(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, void* userData)
210 { 180 {
211 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData) ; 181 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData) ;
212 hb_position_t advance = 0; 182 hb_position_t advance = 0;
213 183
214 SkiaGetGlyphWidthAndExtents(&hbFontData->m_paint, glyph, &advance, 0); 184 SkiaTextMetrics(&hbFontData->m_paint).getGlyphWidthAndExtentsForHarfBuzz(gly ph, &advance, 0);
215 return advance; 185 return advance;
216 } 186 }
217 187
218 static hb_bool_t harfBuzzGetGlyphVerticalOrigin(hb_font_t* hbFont, void* fontDat a, hb_codepoint_t glyph, hb_position_t* x, hb_position_t* y, void* userData) 188 static hb_bool_t harfBuzzGetGlyphVerticalOrigin(hb_font_t* hbFont, void* fontDat a, hb_codepoint_t glyph, hb_position_t* x, hb_position_t* y, void* userData)
219 { 189 {
220 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData) ; 190 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData) ;
221 const OpenTypeVerticalData* verticalData = hbFontData->m_simpleFontData->ver ticalData(); 191 const OpenTypeVerticalData* verticalData = hbFontData->m_simpleFontData->ver ticalData();
222 if (!verticalData) 192 if (!verticalData)
223 return false; 193 return false;
224 194
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 return SkiaScalarToHarfBuzzPosition(SkScalarMulDiv(SkIntToScalar(kerning Adjustments[0]), size, upm)); 231 return SkiaScalarToHarfBuzzPosition(SkScalarMulDiv(SkIntToScalar(kerning Adjustments[0]), size, upm));
262 } 232 }
263 233
264 return 0; 234 return 0;
265 } 235 }
266 236
267 static hb_bool_t harfBuzzGetGlyphExtents(hb_font_t* hbFont, void* fontData, hb_c odepoint_t glyph, hb_glyph_extents_t* extents, void* userData) 237 static hb_bool_t harfBuzzGetGlyphExtents(hb_font_t* hbFont, void* fontData, hb_c odepoint_t glyph, hb_glyph_extents_t* extents, void* userData)
268 { 238 {
269 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData) ; 239 HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData) ;
270 240
271 SkiaGetGlyphWidthAndExtents(&hbFontData->m_paint, glyph, 0, extents); 241 SkiaTextMetrics(&hbFontData->m_paint).getGlyphWidthAndExtentsForHarfBuzz(gly ph, 0, extents);
272 return true; 242 return true;
273 } 243 }
274 244
275 static hb_font_funcs_t* harfBuzzSkiaGetFontFuncs() 245 static hb_font_funcs_t* harfBuzzSkiaGetFontFuncs()
276 { 246 {
277 static hb_font_funcs_t* harfBuzzSkiaFontFuncs = 0; 247 static hb_font_funcs_t* harfBuzzSkiaFontFuncs = 0;
278 248
279 // We don't set callback functions which we can't support. 249 // We don't set callback functions which we can't support.
280 // HarfBuzz will use the fallback implementation if they aren't set. 250 // HarfBuzz will use the fallback implementation if they aren't set.
281 if (!harfBuzzSkiaFontFuncs) { 251 if (!harfBuzzSkiaFontFuncs) {
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 // are found from the parent. 336 // are found from the parent.
367 hb_font_t* unscaledFont = hb_font_create_sub_font(otFont.get()); 337 hb_font_t* unscaledFont = hb_font_create_sub_font(otFont.get());
368 RefPtr<HbFontCacheEntry> cacheEntry = HbFontCacheEntry::create(unscaledFont) ; 338 RefPtr<HbFontCacheEntry> cacheEntry = HbFontCacheEntry::create(unscaledFont) ;
369 hb_font_set_funcs(unscaledFont, harfBuzzSkiaGetFontFuncs(), cacheEntry->hbFo ntData(), nullptr); 339 hb_font_set_funcs(unscaledFont, harfBuzzSkiaGetFontFuncs(), cacheEntry->hbFo ntData(), nullptr);
370 return cacheEntry; 340 return cacheEntry;
371 } 341 }
372 342
373 hb_font_t* HarfBuzzFace::getScaledFont(PassRefPtr<UnicodeRangeSet> rangeSet) con st 343 hb_font_t* HarfBuzzFace::getScaledFont(PassRefPtr<UnicodeRangeSet> rangeSet) con st
374 { 344 {
375 m_platformData->setupPaint(&m_harfBuzzFontData->m_paint); 345 m_platformData->setupPaint(&m_harfBuzzFontData->m_paint);
346 m_harfBuzzFontData->m_paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
376 m_harfBuzzFontData->m_rangeSet = rangeSet; 347 m_harfBuzzFontData->m_rangeSet = rangeSet;
377 m_harfBuzzFontData->m_simpleFontData = FontCache::fontCache()->fontDataFromF ontPlatformData(m_platformData).get(); 348 m_harfBuzzFontData->m_simpleFontData = FontCache::fontCache()->fontDataFromF ontPlatformData(m_platformData).get();
378 ASSERT(m_harfBuzzFontData->m_simpleFontData); 349 ASSERT(m_harfBuzzFontData->m_simpleFontData);
379 int scale = SkiaScalarToHarfBuzzPosition(m_platformData->size()); 350 int scale = SkiaScalarToHarfBuzzPosition(m_platformData->size());
380 hb_font_set_scale(m_unscaledFont, scale, scale); 351 hb_font_set_scale(m_unscaledFont, scale, scale);
381 return m_unscaledFont; 352 return m_unscaledFont;
382 } 353 }
383 354
384 } // namespace blink 355 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698