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

Side by Side Diff: third_party/WebKit/Source/platform/fonts/skia/SkiaTextMetrics.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, 5 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "SkiaTextMetrics.h"
6
7 #include "wtf/MathExtras.h"
8
9 #include <SkPath.h>
10
11 namespace blink {
12
13 static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value)
14 {
15 // We treat HarfBuzz hb_position_t as 16.16 fixed-point.
16 static const int kHbPosition1 = 1 << 16;
17 return clampTo<int>(value * kHbPosition1);
18 }
19
20 SkiaTextMetrics::SkiaTextMetrics(const SkPaint* paint)
21 : m_paint(paint)
22 {
23 CHECK(m_paint->getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
24 }
25
26 void SkiaTextMetrics::getGlyphWidthAndExtentsForHarfBuzz(hb_codepoint_t codepoin t, hb_position_t* width, hb_glyph_extents_t* extents)
27 {
28 DCHECK_LE(codepoint, 0xFFFFu);
29
30 SkScalar skWidth;
31 SkRect skBounds;
32 uint16_t glyph = codepoint;
33
34 m_paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, &skBounds);
35 if (width) {
36 if (!m_paint->isSubpixelText())
37 skWidth = SkScalarRoundToInt(skWidth);
38 *width = SkiaScalarToHarfBuzzPosition(skWidth);
39 }
40 if (extents) {
41 if (!m_paint->isSubpixelText()) {
42 // Use roundOut() rather than round() to avoid rendering glyphs
43 // outside the visual overflow rect. crbug.com/452914.
44 SkIRect ir;
45 skBounds.roundOut(&ir);
46 skBounds.set(ir);
47 }
48 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be y-grows-up.
49 extents->x_bearing = SkiaScalarToHarfBuzzPosition(skBounds.fLeft);
50 extents->y_bearing = SkiaScalarToHarfBuzzPosition(-skBounds.fTop);
51 extents->width = SkiaScalarToHarfBuzzPosition(skBounds.width());
52 extents->height = SkiaScalarToHarfBuzzPosition(-skBounds.height());
53 }
54 }
55
56 void SkiaTextMetrics::getSkiaBoundsForGlyph(Glyph glyph, SkRect *bounds)
57 {
58 #if OS(MACOSX)
59 // TODO(drott): Remove this once we have better metrics bounds
60 // on Mac, https://bugs.chromium.org/p/skia/issues/detail?id=5328
61 SkPath path;
62 m_paint->getTextPath(&glyph, sizeof(glyph), 0, 0, &path);
63 *bounds = path.getBounds();
64 #else
65 m_paint->getTextWidths(&glyph, sizeof(glyph), nullptr, bounds);
66 #endif
67
68 if (!m_paint->isSubpixelText()) {
69 SkIRect ir;
70 bounds->roundOut(&ir);
71 bounds->set(ir);
72 }
73
74 }
75
76 float SkiaTextMetrics::getSkiaWidthForGlyph(Glyph glyph)
77 {
78 SkScalar skWidth;
79 m_paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, nullptr);
80
81 if (!m_paint->isSubpixelText())
82 skWidth = SkScalarRoundToInt(skWidth);
83
84 return SkScalarToFloat(skWidth);
85 }
86
87
88 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698