| 1 |
/* |
| 2 |
* Copyright 2014 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkFont.h" |
| 9 |
|
| 10 |
#include "include/core/SkFontMetrics.h" |
| 11 |
#include "include/core/SkFontTypes.h" |
| 12 |
#include "include/core/SkMatrix.h" |
| 13 |
#include "include/core/SkPaint.h" |
| 14 |
#include "include/core/SkPath.h" |
| 15 |
#include "include/core/SkPathEffect.h" |
| 16 |
#include "include/core/SkPoint.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkRefCnt.h" |
| 19 |
#include "include/core/SkScalar.h" |
| 20 |
#include "include/core/SkTypeface.h" |
| 21 |
#include "include/core/SkTypes.h" |
| 22 |
#include "include/private/base/SkDebug.h" |
| 23 |
#include "include/private/base/SkFloatingPoint.h" |
| 24 |
#include "include/private/base/SkTemplates.h" |
| 25 |
#include "include/private/base/SkTo.h" |
| 26 |
#include "src/base/SkUTF.h" |
| 27 |
#include "src/base/SkZip.h" |
| 28 |
#include "src/core/SkFontPriv.h" |
| 29 |
#include "src/core/SkGlyph.h" |
| 30 |
#include "src/core/SkMatrixPriv.h" |
| 31 |
#include "src/core/SkPaintDefaults.h" |
| 32 |
#include "src/core/SkStrike.h" |
| 33 |
#include "src/core/SkStrikeSpec.h" |
| 34 |
|
| 35 |
#include <algorithm> |
| 36 |
#include <cstddef> |
| 37 |
#include <cstdint> |
| 38 |
#include <utility> |
| 39 |
|
| 40 |
using namespace skia_private; |
| 41 |
|
| 42 |
#define kDefault_Size SkPaintDefaults_TextSize |
| 43 |
#define kDefault_Flags SkFont::kBaselineSnap_PrivFlag |
| 44 |
#define kDefault_Edging SkFont::Edging::kAntiAlias |
| 45 |
#define kDefault_Hinting SkPaintDefaults_Hinting |
| 46 |
|
| 47 |
static inline SkScalar valid_size(SkScalar size) { |
| 48 |
return std::max<SkScalar>(0, size); |
| 49 |
} |
| 50 |
|
| 51 |
SkFont::SkFont(sk_sp<SkTypeface> face, SkScalar size, SkScalar scaleX, SkScalar skewX) |
| 52 |
: fTypeface(std::move(face)) |
| 53 |
, fSize(valid_size(size)) |
| 54 |
, fScaleX(scaleX) |
| 55 |
, fSkewX(skewX) |
| 56 |
, fFlags(kDefault_Flags) |
| 57 |
, fEdging(static_cast<unsigned>(kDefault_Edging)) |
| 58 |
, fHinting(static_cast<unsigned>(kDefault_Hinting)) { |
| 59 |
if (!fTypeface) { |
| 60 |
fTypeface = SkTypeface::MakeEmpty(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
SkFont::SkFont(sk_sp<SkTypeface> face, SkScalar size) : SkFont(std::move(face), size, 1, 0) {} |
| 65 |
|
| 66 |
SkFont::SkFont(sk_sp<SkTypeface> face) : SkFont(std::move(face), kDefault_Size, 1, 0) {} |
| 67 |
|
| 68 |
SkFont::SkFont() : SkFont(nullptr, kDefault_Size) {} |
| 69 |
|
| 70 |
bool SkFont::operator==(const SkFont& b) const { |
| 71 |
return fTypeface.get() == b.fTypeface.get() && |
| 72 |
fSize == b.fSize && |
| 73 |
fScaleX == b.fScaleX && |
| 74 |
fSkewX == b.fSkewX && |
| 75 |
fFlags == b.fFlags && |
| 76 |
fEdging == b.fEdging && |
| 77 |
fHinting == b.fHinting; |
| 78 |
} |
| 79 |
|
| 80 |
void SkFont::dump() const { |
| 81 |
SkDebugf("typeface %p\n", fTypeface.get()); |
| 82 |
SkDebugf("size %g\n", fSize); |
| 83 |
SkDebugf("skewx %g\n", fSkewX); |
| 84 |
SkDebugf("scalex %g\n", fScaleX); |
| 85 |
SkDebugf("flags 0x%X\n", fFlags); |
| 86 |
SkDebugf("edging %u\n", (unsigned)fEdging); |
| 87 |
SkDebugf("hinting %u\n", (unsigned)fHinting); |
| 88 |
} |
| 89 |
|
| 90 |
void SkFont::setTypeface(sk_sp<SkTypeface> tf) { |
| 91 |
fTypeface = std::move(tf); |
| 92 |
if (!fTypeface) { |
| 93 |
fTypeface = SkTypeface::MakeEmpty(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 98 |
|
| 99 |
static inline uint32_t set_clear_mask(uint32_t bits, bool cond, uint32_t mask) { |
| 100 |
return cond ? bits | mask : bits & ~mask; |
| 101 |
} |
| 102 |
|
| 103 |
void SkFont::setForceAutoHinting(bool predicate) { |
| 104 |
fFlags = set_clear_mask(fFlags, predicate, kForceAutoHinting_PrivFlag); |
| 105 |
} |
| 106 |
void SkFont::setEmbeddedBitmaps(bool predicate) { |
| 107 |
fFlags = set_clear_mask(fFlags, predicate, kEmbeddedBitmaps_PrivFlag); |
| 108 |
} |
| 109 |
void SkFont::setSubpixel(bool predicate) { |
| 110 |
fFlags = set_clear_mask(fFlags, predicate, kSubpixel_PrivFlag); |
| 111 |
} |
| 112 |
void SkFont::setLinearMetrics(bool predicate) { |
| 113 |
fFlags = set_clear_mask(fFlags, predicate, kLinearMetrics_PrivFlag); |
| 114 |
} |
| 115 |
void SkFont::setEmbolden(bool predicate) { |
| 116 |
fFlags = set_clear_mask(fFlags, predicate, kEmbolden_PrivFlag); |
| 117 |
} |
| 118 |
void SkFont::setBaselineSnap(bool predicate) { |
| 119 |
fFlags = set_clear_mask(fFlags, predicate, kBaselineSnap_PrivFlag); |
| 120 |
} |
| 121 |
void SkFont::setEdging(Edging e) { |
| 122 |
fEdging = SkToU8(e); |
| 123 |
} |
| 124 |
|
| 125 |
void SkFont::setHinting(SkFontHinting h) { |
| 126 |
fHinting = SkToU8(h); |
| 127 |
} |
| 128 |
|
| 129 |
void SkFont::setSize(SkScalar size) { |
| 130 |
fSize = valid_size(size); |
| 131 |
} |
| 132 |
void SkFont::setScaleX(SkScalar scale) { |
| 133 |
fScaleX = scale; |
| 134 |
} |
| 135 |
void SkFont::setSkewX(SkScalar skew) { |
| 136 |
fSkewX = skew; |
| 137 |
} |
| 138 |
|
| 139 |
SkFont SkFont::makeWithSize(SkScalar newSize) const { |
| 140 |
SkFont font = *this; |
| 141 |
font.setSize(newSize); |
| 142 |
return font; |
| 143 |
} |
| 144 |
|
| 145 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 146 |
|
| 147 |
SkScalar SkFont::setupForAsPaths(SkPaint* paint) { |
| 148 |
constexpr uint32_t flagsToIgnore = kEmbeddedBitmaps_PrivFlag | |
| 149 |
kForceAutoHinting_PrivFlag; |
| 150 |
|
| 151 |
fFlags = (fFlags & ~flagsToIgnore) | kSubpixel_PrivFlag; |
| 152 |
this->setHinting(SkFontHinting::kNone); |
| 153 |
|
| 154 |
if (this->getEdging() == Edging::kSubpixelAntiAlias) { |
| 155 |
this->setEdging(Edging::kAntiAlias); |
| 156 |
} |
| 157 |
|
| 158 |
if (paint) { |
| 159 |
paint->setStyle(SkPaint::kFill_Style); |
| 160 |
paint->setPathEffect(nullptr); |
| 161 |
} |
| 162 |
SkScalar textSize = fSize; |
| 163 |
this->setSize(SkIntToScalar(SkFontPriv::kCanonicalTextSizeForPaths)); |
| 164 |
return textSize / SkFontPriv::kCanonicalTextSizeForPaths; |
| 165 |
} |
| 166 |
|
| 167 |
bool SkFont::hasSomeAntiAliasing() const { |
| 168 |
Edging edging = this->getEdging(); |
| 169 |
return edging == SkFont::Edging::kAntiAlias |
| 170 |
|| edging == SkFont::Edging::kSubpixelAntiAlias; |
| 171 |
} |
| 172 |
|
| 173 |
SkGlyphID SkFont::unicharToGlyph(SkUnichar uni) const { |
| 174 |
return this->getTypeface()->unicharToGlyph(uni); |
| 175 |
} |
| 176 |
|
| 177 |
void SkFont::unicharsToGlyphs(SkSpan<const SkUnichar> unis, SkSpan<SkGlyphID> glyphs) const { |
| 178 |
this->getTypeface()->unicharsToGlyphs(unis, glyphs); |
| 179 |
} |
| 180 |
|
| 181 |
size_t SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding, |
| 182 |
SkSpan<SkGlyphID> glyphs) const { |
| 183 |
return this->getTypeface()->textToGlyphs(text, byteLength, encoding, glyphs); |
| 184 |
} |
| 185 |
|
| 186 |
SkScalar SkFont::measureText(const void* text, size_t length, SkTextEncoding encoding, |
| 187 |
SkRect* bounds, const SkPaint* paint) const { |
| 188 |
|
| 189 |
SkAutoToGlyphs atg(*this, text, length, encoding); |
| 190 |
const SkSpan<const SkGlyphID> glyphIDs = atg.glyphs(); |
| 191 |
|
| 192 |
if (glyphIDs.size() == 0) { |
| 193 |
if (bounds) { |
| 194 |
bounds->setEmpty(); |
| 195 |
} |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
auto [strikeSpec, strikeToSourceScale] = SkStrikeSpec::MakeCanonicalized(*this, paint); |
| 200 |
SkBulkGlyphMetrics metrics{strikeSpec}; |
| 201 |
SkSpan<const SkGlyph*> glyphs = metrics.glyphs(glyphIDs); |
| 202 |
|
| 203 |
SkScalar width = 0; |
| 204 |
if (bounds) { |
| 205 |
*bounds = glyphs[0]->rect(); |
| 206 |
width = glyphs[0]->advanceX(); |
| 207 |
for (size_t i = 1; i < glyphIDs.size(); ++i) { |
| 208 |
SkRect r = glyphs[i]->rect(); |
| 209 |
r.offset(width, 0); |
| 210 |
bounds->join(r); |
| 211 |
width += glyphs[i]->advanceX(); |
| 212 |
} |
| 213 |
} else { |
| 214 |
for (auto glyph : glyphs) { |
| 215 |
width += glyph->advanceX(); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if (strikeToSourceScale != 1) { |
| 220 |
width *= strikeToSourceScale; |
| 221 |
if (bounds) { |
| 222 |
bounds->fLeft *= strikeToSourceScale; |
| 223 |
bounds->fTop *= strikeToSourceScale; |
| 224 |
bounds->fRight *= strikeToSourceScale; |
| 225 |
bounds->fBottom *= strikeToSourceScale; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return width; |
| 230 |
} |
| 231 |
|
| 232 |
static inline SkRect scale_pos(SkRect r, SkScalar s) { |
| 233 |
SkASSERT(s >= 0); // so we don't have to worry about swapping the rect to stay valid |
| 234 |
return { |
| 235 |
r.fLeft * s, r.fTop * s, r.fRight * s, r.fBottom * s, |
| 236 |
}; |
| 237 |
} |
| 238 |
void SkFont::getWidthsBounds(SkSpan<const SkGlyphID> glyphIDs, |
| 239 |
SkSpan<SkScalar> widths, |
| 240 |
SkSpan<SkRect> bounds, |
| 241 |
const SkPaint* paint) const { |
| 242 |
auto [strikeSpec, strikeToSourceScale] = SkStrikeSpec::MakeCanonicalized(*this, paint); |
| 243 |
SkBulkGlyphMetrics metrics{strikeSpec}; |
| 244 |
SkSpan<const SkGlyph*> glyphs = metrics.glyphs(glyphIDs); |
| 245 |
|
| 246 |
if (bounds.size()) { |
| 247 |
const auto n = std::min(bounds.size(), glyphs.size()); |
| 248 |
for (auto [bound, glyph] : SkMakeZip(bounds.first(n), glyphs.first(n))) { |
| 249 |
bound = scale_pos(glyph->rect(), strikeToSourceScale); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
if (widths.size()) { |
| 254 |
const auto n = std::min(widths.size(), glyphs.size()); |
| 255 |
for (auto [width, glyph] : SkMakeZip(widths.first(n), glyphs.first(n))) { |
| 256 |
width = glyph->advanceX() * strikeToSourceScale; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
void SkFont::getPos(SkSpan<const SkGlyphID> glyphIDs, SkSpan<SkPoint> pos, SkPoint origin) const { |
| 262 |
auto [strikeSpec, strikeToSourceScale] = SkStrikeSpec::MakeCanonicalized(*this); |
| 263 |
SkBulkGlyphMetrics metrics{strikeSpec}; |
| 264 |
SkSpan<const SkGlyph*> glyphs = metrics.glyphs(glyphIDs); |
| 265 |
|
| 266 |
SkPoint sum = origin; |
| 267 |
const auto n = std::min(pos.size(), glyphs.size()); |
| 268 |
for (auto [position, glyph] : SkMakeZip(pos.first(n), glyphs.first(n))) { |
| 269 |
position = sum; |
| 270 |
sum += glyph->advanceVector() * strikeToSourceScale; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
void SkFont::getXPos(SkSpan<const SkGlyphID> gIDs, SkSpan<SkScalar> xpos, SkScalar origin) const { |
| 275 |
auto [strikeSpec, strikeToSourceScale] = SkStrikeSpec::MakeCanonicalized(*this); |
| 276 |
SkBulkGlyphMetrics metrics{strikeSpec}; |
| 277 |
SkSpan<const SkGlyph*> glyphs = metrics.glyphs(gIDs); |
| 278 |
|
| 279 |
SkScalar loc = origin; |
| 280 |
const auto n = std::min(xpos.size(), glyphs.size()); |
| 281 |
for (auto [xposition, glyph] : SkMakeZip(xpos.first(n), glyphs.first(n))) { |
| 282 |
xposition = loc; |
| 283 |
loc += glyph->advanceX() * strikeToSourceScale; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
void SkFont::getPaths(SkSpan<const SkGlyphID> glyphIDs, |
| 288 |
void (*proc)(const SkPath*, const SkMatrix&, void*), void* ctx) const { |
| 289 |
SkFont font(*this); |
| 290 |
SkScalar scale = font.setupForAsPaths(nullptr); |
| 291 |
const SkMatrix mx = SkMatrix::Scale(scale, scale); |
| 292 |
|
| 293 |
SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(font); |
| 294 |
SkBulkGlyphMetricsAndPaths paths{strikeSpec}; |
| 295 |
SkSpan<const SkGlyph*> glyphs = paths.glyphs(glyphIDs); |
| 296 |
|
| 297 |
for (auto glyph : glyphs) { |
| 298 |
proc(glyph->path(), mx, ctx); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
std::optional<SkPath> SkFont::getPath(SkGlyphID glyphID) const { |
| 303 |
std::optional<SkPath> result; |
| 304 |
|
| 305 |
this->getPaths({&glyphID, 1}, [](const SkPath* path, const SkMatrix& mx, void* ctx) { |
| 306 |
if (path) { |
| 307 |
auto* result = static_cast<std::optional<SkPath>*>(ctx); |
| 308 |
*result = path->tryMakeTransform(mx); |
| 309 |
} |
| 310 |
}, &result); |
| 311 |
|
| 312 |
return result; |
| 313 |
} |
| 314 |
|
| 315 |
SkScalar SkFont::getMetrics(SkFontMetrics* metrics) const { |
| 316 |
|
| 317 |
auto [strikeSpec, strikeToSourceScale] = SkStrikeSpec::MakeCanonicalized(*this, nullptr); |
| 318 |
|
| 319 |
SkFontMetrics storage; |
| 320 |
if (nullptr == metrics) { |
| 321 |
metrics = &storage; |
| 322 |
} |
| 323 |
|
| 324 |
auto cache = strikeSpec.findOrCreateStrike(); |
| 325 |
*metrics = cache->getFontMetrics(); |
| 326 |
|
| 327 |
if (strikeToSourceScale != 1) { |
| 328 |
SkFontPriv::ScaleFontMetrics(metrics, strikeToSourceScale); |
| 329 |
} |
| 330 |
return metrics->fDescent - metrics->fAscent + metrics->fLeading; |
| 331 |
} |
| 332 |
|
| 333 |
////////////////////////////////////////////////////////////////////////////////////////////////// |
| 334 |
|
| 335 |
void SkFontPriv::ScaleFontMetrics(SkFontMetrics* metrics, SkScalar scale) { |
| 336 |
metrics->fTop *= scale; |
| 337 |
metrics->fAscent *= scale; |
| 338 |
metrics->fDescent *= scale; |
| 339 |
metrics->fBottom *= scale; |
| 340 |
metrics->fLeading *= scale; |
| 341 |
metrics->fAvgCharWidth *= scale; |
| 342 |
metrics->fMaxCharWidth *= scale; |
| 343 |
metrics->fXMin *= scale; |
| 344 |
metrics->fXMax *= scale; |
| 345 |
metrics->fXHeight *= scale; |
| 346 |
metrics->fCapHeight *= scale; |
| 347 |
metrics->fUnderlineThickness *= scale; |
| 348 |
metrics->fUnderlinePosition *= scale; |
| 349 |
metrics->fStrikeoutThickness *= scale; |
| 350 |
metrics->fStrikeoutPosition *= scale; |
| 351 |
} |
| 352 |
|
| 353 |
SkRect SkFontPriv::GetFontBounds(const SkFont& font) { |
| 354 |
SkMatrix m; |
| 355 |
m.setScale(font.getSize() * font.getScaleX(), font.getSize()); |
| 356 |
m.postSkew(font.getSkewX(), 0); |
| 357 |
|
| 358 |
SkTypeface* typeface = font.getTypeface(); |
| 359 |
|
| 360 |
SkRect bounds; |
| 361 |
m.mapRect(&bounds, typeface->getBounds()); |
| 362 |
return bounds; |
| 363 |
} |
| 364 |
|
| 365 |
SkScalar SkFontPriv::ApproximateTransformedTextSize(const SkFont& font, const SkMatrix& matrix, |
| 366 |
const SkPoint& textLocation) { |
| 367 |
if (!matrix.hasPerspective()) { |
| 368 |
return font.getSize() * matrix.getMaxScale(); |
| 369 |
} else { |
| 370 |
// approximate the scale since we can't get it directly from the matrix |
| 371 |
SkScalar maxScaleSq = SkMatrixPriv::DifferentialAreaScale(matrix, textLocation); |
| 372 |
if (SkIsFinite(maxScaleSq) && !SkScalarNearlyZero(maxScaleSq)) { |
| 373 |
return font.getSize() * SkScalarSqrt(maxScaleSq); |
| 374 |
} else { |
| 375 |
return -font.getSize(); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
size_t SkFontPriv::CountTextElements(const void* text, size_t byteLength, SkTextEncoding encoding) { |
| 381 |
switch (encoding) { |
| 382 |
case SkTextEncoding::kUTF8: |
| 383 |
return SkUTF::CountUTF8(reinterpret_cast<const char*>(text), byteLength); |
| 384 |
case SkTextEncoding::kUTF16: |
| 385 |
return SkUTF::CountUTF16(reinterpret_cast<const uint16_t*>(text), byteLength); |
| 386 |
case SkTextEncoding::kUTF32: |
| 387 |
return byteLength >> 2; |
| 388 |
case SkTextEncoding::kGlyphID: |
| 389 |
return byteLength >> 1; |
| 390 |
} |
| 391 |
SkASSERT(false); |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
void SkFontPriv::GlyphsToUnichars(const SkFont& font, const SkGlyphID glyphs[], int count, |
| 396 |
SkUnichar text[]) { |
| 397 |
if (count <= 0) { |
| 398 |
return; |
| 399 |
} |
| 400 |
|
| 401 |
auto typeface = font.getTypeface(); |
| 402 |
const unsigned numGlyphsInTypeface = typeface->countGlyphs(); |
| 403 |
AutoTArray<SkUnichar> unichars(static_cast<size_t>(numGlyphsInTypeface)); |
| 404 |
typeface->getGlyphToUnicodeMap(unichars); |
| 405 |
|
| 406 |
for (int i = 0; i < count; ++i) { |
| 407 |
unsigned id = glyphs[i]; |
| 408 |
text[i] = (id < numGlyphsInTypeface) ? unichars[id] : 0xFFFD; |
| 409 |
} |
| 410 |
} |