OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "SkiaTextMetrics.h" | 5 #include "SkiaTextMetrics.h" |
6 | 6 |
7 #include "wtf/MathExtras.h" | 7 #include "wtf/MathExtras.h" |
8 | 8 |
9 #include <SkPath.h> | 9 #include <SkPath.h> |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 | 12 |
13 static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) | 13 static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) |
14 { | 14 { |
15 // We treat HarfBuzz hb_position_t as 16.16 fixed-point. | 15 // We treat HarfBuzz hb_position_t as 16.16 fixed-point. |
16 static const int kHbPosition1 = 1 << 16; | 16 static const int kHbPosition1 = 1 << 16; |
17 return clampTo<int>(value * kHbPosition1); | 17 return clampTo<int>(value * kHbPosition1); |
18 } | 18 } |
19 | 19 |
20 SkiaTextMetrics::SkiaTextMetrics(const SkPaint* paint) | 20 SkiaTextMetrics::SkiaTextMetrics(const SkPaint* paint) |
21 : m_paint(paint) | 21 : m_paint(paint) |
22 { | 22 { |
23 CHECK(m_paint->getTextEncoding() == SkPaint::kGlyphID_TextEncoding); | 23 CHECK(m_paint->getTextEncoding() == SkPaint::kGlyphID_TextEncoding); |
24 } | 24 } |
25 | 25 |
26 void SkiaTextMetrics::getGlyphWidthAndExtentsForHarfBuzz(hb_codepoint_t codepoin
t, hb_position_t* width, hb_glyph_extents_t* extents) | 26 |
| 27 void SkiaTextMetrics::getGlyphWidthForHarfBuzz(hb_codepoint_t codepoint, hb_posi
tion_t* width) |
27 { | 28 { |
28 DCHECK_LE(codepoint, 0xFFFFu); | 29 DCHECK_LE(codepoint, 0xFFFFu); |
| 30 CHECK(width); |
29 | 31 |
30 SkScalar skWidth; | 32 SkScalar skWidth; |
| 33 uint16_t glyph = codepoint; |
| 34 |
| 35 m_paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, nullptr); |
| 36 if (!m_paint->isSubpixelText()) |
| 37 skWidth = SkScalarRoundToInt(skWidth); |
| 38 *width = SkiaScalarToHarfBuzzPosition(skWidth); |
| 39 } |
| 40 |
| 41 void SkiaTextMetrics::getGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint, hb_gl
yph_extents_t* extents) |
| 42 { |
| 43 DCHECK_LE(codepoint, 0xFFFFu); |
| 44 CHECK(extents); |
| 45 |
31 SkRect skBounds; | 46 SkRect skBounds; |
32 uint16_t glyph = codepoint; | 47 uint16_t glyph = codepoint; |
33 | 48 |
34 m_paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, &skBounds); | 49 m_paint->getTextWidths(&glyph, sizeof(glyph), nullptr, &skBounds); |
35 if (width) { | 50 if (!m_paint->isSubpixelText()) { |
36 if (!m_paint->isSubpixelText()) | 51 // Use roundOut() rather than round() to avoid rendering glyphs |
37 skWidth = SkScalarRoundToInt(skWidth); | 52 // outside the visual overflow rect. crbug.com/452914. |
38 *width = SkiaScalarToHarfBuzzPosition(skWidth); | 53 SkIRect ir; |
| 54 skBounds.roundOut(&ir); |
| 55 skBounds.set(ir); |
39 } | 56 } |
40 if (extents) { | 57 |
41 if (!m_paint->isSubpixelText()) { | 58 // Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be y
-grows-up. |
42 // Use roundOut() rather than round() to avoid rendering glyphs | 59 extents->x_bearing = SkiaScalarToHarfBuzzPosition(skBounds.fLeft); |
43 // outside the visual overflow rect. crbug.com/452914. | 60 extents->y_bearing = SkiaScalarToHarfBuzzPosition(-skBounds.fTop); |
44 SkIRect ir; | 61 extents->width = SkiaScalarToHarfBuzzPosition(skBounds.width()); |
45 skBounds.roundOut(&ir); | 62 extents->height = SkiaScalarToHarfBuzzPosition(-skBounds.height()); |
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 } | 63 } |
55 | 64 |
56 void SkiaTextMetrics::getSkiaBoundsForGlyph(Glyph glyph, SkRect *bounds) | 65 void SkiaTextMetrics::getSkiaBoundsForGlyph(Glyph glyph, SkRect *bounds) |
57 { | 66 { |
58 #if OS(MACOSX) | 67 #if OS(MACOSX) |
59 // TODO(drott): Remove this once we have better metrics bounds | 68 // TODO(drott): Remove this once we have better metrics bounds |
60 // on Mac, https://bugs.chromium.org/p/skia/issues/detail?id=5328 | 69 // on Mac, https://bugs.chromium.org/p/skia/issues/detail?id=5328 |
61 SkPath path; | 70 SkPath path; |
62 m_paint->getTextPath(&glyph, sizeof(glyph), 0, 0, &path); | 71 m_paint->getTextPath(&glyph, sizeof(glyph), 0, 0, &path); |
63 *bounds = path.getBounds(); | 72 *bounds = path.getBounds(); |
(...skipping 15 matching lines...) Expand all Loading... |
79 m_paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, nullptr); | 88 m_paint->getTextWidths(&glyph, sizeof(glyph), &skWidth, nullptr); |
80 | 89 |
81 if (!m_paint->isSubpixelText()) | 90 if (!m_paint->isSubpixelText()) |
82 skWidth = SkScalarRoundToInt(skWidth); | 91 skWidth = SkScalarRoundToInt(skWidth); |
83 | 92 |
84 return SkScalarToFloat(skWidth); | 93 return SkScalarToFloat(skWidth); |
85 } | 94 } |
86 | 95 |
87 | 96 |
88 } // namespace blink | 97 } // namespace blink |
OLD | NEW |