1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "third_party/blink/renderer/platform/fonts/skia/skia_text_metrics.h"
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/fonts/shaping/harfbuzz_face.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include "third_party/skia/include/core/SkFont.h"
#include "third_party/skia/include/core/SkPath.h"
namespace blink {
namespace {
template <class T>
T* advance_by_byte_size(T* p, unsigned byte_size) {
return reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(p) + byte_size);
}
template <class T>
const T* advance_by_byte_size(const T* p, unsigned byte_size) {
return reinterpret_cast<const T*>(reinterpret_cast<const uint8_t*>(p) +
byte_size);
}
} // namespace
void SkFontGetGlyphWidthForHarfBuzz(const SkFont& font,
hb_codepoint_t codepoint,
hb_position_t* width) {
// We don't want to compute glyph extents for kUnmatchedVSGlyphId
// cases yet. Since we will do that during the second shaping pass,
// when VariationSelectorMode is set to kIgnoreVariationSelector.
if (codepoint == kUnmatchedVSGlyphId) {
return;
}
DCHECK_LE(codepoint, 0xFFFFu);
CHECK(width);
SkScalar sk_width;
uint16_t glyph = codepoint;
font.getWidths(&glyph, 1, &sk_width);
if (!font.isSubpixel())
sk_width = SkScalarRoundToInt(sk_width);
*width = SkiaScalarToHarfBuzzPosition(sk_width);
}
void SkFontGetGlyphWidthForHarfBuzz(const SkFont& font,
unsigned count,
const hb_codepoint_t* glyphs,
const unsigned glyph_stride,
hb_position_t* advances,
unsigned advance_stride) {
// Batch the call to getWidths because its function entry cost is not
// cheap. getWidths accepts multiple glyphd ID, but not from a sparse
// array that copy them to a regular array.
Vector<Glyph, 256> glyph_array(count);
for (unsigned i = 0; i < count;
i++, glyphs = advance_by_byte_size(glyphs, glyph_stride)) {
glyph_array[i] = *glyphs;
}
Vector<SkScalar, 256> sk_width_array(count);
font.getWidths(glyph_array.data(), count, sk_width_array.data());
if (!font.isSubpixel()) {
for (unsigned i = 0; i < count; i++)
sk_width_array[i] = SkScalarRoundToInt(sk_width_array[i]);
}
// Copy the results back to the sparse array.
for (unsigned i = 0; i < count;
i++, advances = advance_by_byte_size(advances, advance_stride)) {
*advances = SkiaScalarToHarfBuzzPosition(sk_width_array[i]);
}
}
// HarfBuzz callback to retrieve glyph extents, mainly used by HarfBuzz for
// fallback mark positioning, i.e. the situation when the font does not have
// mark anchors or other mark positioning rules, but instead HarfBuzz is
// supposed to heuristically place combining marks around base glyphs. HarfBuzz
// does this by measuring "ink boxes" of glyphs, and placing them according to
// Unicode mark classes. Above, below, centered or left or right, etc.
void SkFontGetGlyphExtentsForHarfBuzz(const SkFont& font,
hb_codepoint_t codepoint,
hb_glyph_extents_t* extents) {
// We don't want to compute glyph extents for kUnmatchedVSGlyphId
// cases yet. Since we will do that during the second shaping pass,
// when VariationSelectorMode is set to kIgnoreVariationSelector.
if (codepoint == kUnmatchedVSGlyphId) {
return;
}
DCHECK_LE(codepoint, 0xFFFFu);
CHECK(extents);
SkRect sk_bounds;
uint16_t glyph = codepoint;
#if BUILDFLAG(IS_APPLE)
// TODO(drott): Remove this once we have better metrics bounds
// on Mac, https://bugs.chromium.org/p/skia/issues/detail?id=5328
SkPath path;
if (font.getPath(glyph, &path)) {
sk_bounds = path.getBounds();
} else {
font.getBounds(&glyph, 1, &sk_bounds, nullptr);
}
#else
font.getBounds(&glyph, 1, &sk_bounds, nullptr);
#endif
if (!font.isSubpixel()) {
// Use roundOut() rather than round() to avoid rendering glyphs
// outside the visual overflow rect. crbug.com/452914.
sk_bounds.set(sk_bounds.roundOut());
}
// Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be
// y-grows-up.
extents->x_bearing = SkiaScalarToHarfBuzzPosition(sk_bounds.fLeft);
extents->y_bearing = SkiaScalarToHarfBuzzPosition(-sk_bounds.fTop);
extents->width = SkiaScalarToHarfBuzzPosition(sk_bounds.width());
extents->height = SkiaScalarToHarfBuzzPosition(-sk_bounds.height());
}
void SkFontGetBoundsForGlyph(const SkFont& font, Glyph glyph, SkRect* bounds) {
#if BUILDFLAG(IS_APPLE)
// TODO(drott): Remove this once we have better metrics bounds
// on Mac, https://bugs.chromium.org/p/skia/issues/detail?id=5328
SkPath path;
if (font.getPath(glyph, &path)) {
*bounds = path.getBounds();
} else {
// Fonts like Apple Color Emoji have no paths, fall back to bounds here.
font.getBounds(&glyph, 1, bounds, nullptr);
}
#else
font.getBounds(&glyph, 1, bounds, nullptr);
#endif
if (!font.isSubpixel()) {
SkIRect ir;
bounds->roundOut(&ir);
bounds->set(ir);
}
}
void SkFontGetBoundsForGlyphs(const SkFont& font,
const Vector<Glyph, 256>& glyphs,
SkRect* bounds) {
#if BUILDFLAG(IS_APPLE)
for (unsigned i = 0; i < glyphs.size(); i++) {
SkFontGetBoundsForGlyph(font, glyphs[i], &bounds[i]);
}
#else
static_assert(sizeof(Glyph) == 2, "Skia expects 2 bytes glyph id.");
font.getBounds(glyphs.data(), glyphs.size(), bounds, nullptr);
if (!font.isSubpixel()) {
for (unsigned i = 0; i < glyphs.size(); i++) {
SkIRect ir;
bounds[i].roundOut(&ir);
bounds[i].set(ir);
}
}
#endif
}
float SkFontGetWidthForGlyph(const SkFont& font, Glyph glyph) {
SkScalar sk_width;
font.getWidths(&glyph, 1, &sk_width);
if (!font.isSubpixel())
sk_width = SkScalarRoundToInt(sk_width);
return SkScalarToFloat(sk_width);
}
hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) {
// We treat HarfBuzz hb_position_t as 16.16 fixed-point.
static const int kHbPosition1 = 1 << 16;
return ClampTo<int>(value * kHbPosition1);
}
} // namespace blink
|