Move more utils into SkFontPriv, IWYU

Fissioned from https://skia-review.googlesource.com/c/skia/+/185460

Bug: skia:
Change-Id: Iaf980e931c17196f917602fc58fc39ab51e3d248
Reviewed-on: https://skia-review.googlesource.com/c/185840
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Reed <reed@google.com>
Auto-Submit: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2019-01-22 16:30:40 -05:00 committed by Skia Commit-Bot
parent a4e1a63452
commit 77f94ea72b
25 changed files with 91 additions and 98 deletions

View File

@ -22,6 +22,7 @@
#include "SkBlendMode.h"
#include "SkClipOp.h"
#include "SkDeque.h"
#include "SkFontTypes.h"
#include "SkPaint.h"
#include "SkRasterHandleAllocator.h"
#include "SkSurfaceProps.h"

View File

@ -11,6 +11,7 @@
#include "SkMalloc.h"
#include "SkOnce.h"
#include "SkFont.h"
#include "SkFontMetrics.h"
#include "SkPoint.h"
#include "SkRefCnt.h"
#include "SkScalar.h"

View File

@ -122,7 +122,6 @@ public:
static RectType ComputeRectType(const SkPaint&, const SkMatrix&,
SkPoint* strokeSize);
static bool ShouldDrawTextAsPaths(const SkPaint&, const SkMatrix&, SkScalar sizeLimit = 1024);
static bool ShouldDrawTextAsPaths(const SkFont&, const SkPaint&, const SkMatrix&,
SkScalar sizeLimit = 1024);

View File

@ -14,22 +14,6 @@
#include "SkTextToPathIter.h"
#include "SkUtils.h"
bool SkDraw::ShouldDrawTextAsPaths(const SkPaint& paint, const SkMatrix& ctm, SkScalar sizeLimit) {
// hairline glyphs are fast enough so we don't need to cache them
if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) {
return true;
}
// we don't cache perspective
if (ctm.hasPerspective()) {
return true;
}
SkMatrix textM;
SkPaintPriv::MakeTextMatrix(&textM, paint);
return SkPaint::TooBigToUseCache(ctm, textM, sizeLimit);
}
bool SkDraw::ShouldDrawTextAsPaths(const SkFont& font, const SkPaint& paint,
const SkMatrix& ctm, SkScalar sizeLimit) {
// hairline glyphs are fast enough so we don't need to cache them
@ -42,7 +26,7 @@ bool SkDraw::ShouldDrawTextAsPaths(const SkFont& font, const SkPaint& paint,
return true;
}
return SkPaint::TooBigToUseCache(ctm, SkFontPriv::MakeTextMatrix(font), sizeLimit);
return SkFontPriv::TooBigToUseCache(ctm, SkFontPriv::MakeTextMatrix(font), sizeLimit);
}
// disable warning : local variable used without having been initialized

View File

@ -8,6 +8,7 @@
#include "SkDraw.h"
#include "SkFontPriv.h"
#include "SkPaint.h"
#include "SkPaintDefaults.h"
#include "SkPath.h"
#include "SkScalerContext.h"
#include "SkStrike.h"
@ -18,10 +19,10 @@
#include "SkUTF.h"
#include "SkUtils.h"
#define kDefault_Size 12
#define kDefault_Size SkPaintDefaults_TextSize
#define kDefault_Flags 0
#define kDefault_Edging SkFont::Edging::kAntiAlias
#define kDefault_Hinting kNormal_SkFontHinting
#define kDefault_Hinting SkPaintDefaults_Hinting
static inline SkScalar valid_size(SkScalar size) {
return SkTMax<SkScalar>(0, size);
@ -128,8 +129,8 @@ SkScalar SkFont::setupForAsPaths(SkPaint* paint) {
paint->setPathEffect(nullptr);
}
SkScalar textSize = fSize;
this->setSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths));
return textSize / SkPaint::kCanonicalTextSizeForPaths;
this->setSize(SkIntToScalar(SkFontPriv::kCanonicalTextSizeForPaths));
return textSize / SkFontPriv::kCanonicalTextSizeForPaths;
}
bool SkFont::hasSomeAntiAliasing() const {
@ -481,7 +482,7 @@ SkScalar SkFont::getMetrics(SkFontMetrics* metrics) const {
*metrics = cache->getFontMetrics();
if (scale) {
SkPaintPriv::ScaleFontMetrics(metrics, scale);
SkFontPriv::ScaleFontMetrics(metrics, scale);
}
return metrics->fDescent - metrics->fAscent + metrics->fLeading;
}
@ -556,6 +557,38 @@ void SkFont::LEGACY_applyPaintFlags(uint32_t paintFlags) {
//////////////////////////////////////////////////////////////////////////////////////////////////
int SkFontPriv::ValidCountText(const void* text, size_t length, SkTextEncoding encoding) {
switch (encoding) {
case kUTF8_SkTextEncoding: return SkUTF::CountUTF8((const char*)text, length);
case kUTF16_SkTextEncoding: return SkUTF::CountUTF16((const uint16_t*)text, length);
case kUTF32_SkTextEncoding: return SkUTF::CountUTF32((const int32_t*)text, length);
case kGlyphID_SkTextEncoding:
if (!SkIsAlign2(intptr_t(text)) || !SkIsAlign2(length)) {
return -1;
}
return length >> 1;
}
return -1;
}
void SkFontPriv::ScaleFontMetrics(SkFontMetrics* metrics, SkScalar scale) {
metrics->fTop *= scale;
metrics->fAscent *= scale;
metrics->fDescent *= scale;
metrics->fBottom *= scale;
metrics->fLeading *= scale;
metrics->fAvgCharWidth *= scale;
metrics->fMaxCharWidth *= scale;
metrics->fXMin *= scale;
metrics->fXMax *= scale;
metrics->fXHeight *= scale;
metrics->fCapHeight *= scale;
metrics->fUnderlineThickness *= scale;
metrics->fUnderlinePosition *= scale;
metrics->fStrikeoutThickness *= scale;
metrics->fStrikeoutPosition *= scale;
}
SkRect SkFontPriv::GetFontBounds(const SkFont& font) {
SkMatrix m;
m.setScale(font.getSize() * font.getScaleX(), font.getSize());

View File

@ -19,6 +19,26 @@ class SkWriteBuffer;
class SkFontPriv {
public:
/* This is the size we use when we ask for a glyph's path. We then
* post-transform it as we draw to match the request.
* This is done to try to re-use cache entries for the path.
*
* This value is somewhat arbitrary. In theory, it could be 1, since
* we store paths as floats. However, we get the path from the font
* scaler, and it may represent its paths as fixed-point (or 26.6),
* so we shouldn't ask for something too big (might overflow 16.16)
* or too small (underflow 26.6).
*
* This value could track kMaxSizeForGlyphCache, assuming the above
* constraints, but since we ask for unhinted paths, the two values
* need not match per-se.
*/
static constexpr int kCanonicalTextSizeForPaths = 64;
static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM, SkScalar maxLimit);
static SkScalar MaxCacheSize2(SkScalar maxLimit);
/**
* Return a matrix that applies the paint's text values: size, scale, skew
*/

View File

@ -114,7 +114,7 @@ bool SkGlyphRunListPainter::ShouldDrawAsPath(
return true;
}
return SkPaint::TooBigToUseCache(matrix, SkFontPriv::MakeTextMatrix(font), 1024);
return SkFontPriv::TooBigToUseCache(matrix, SkFontPriv::MakeTextMatrix(font), 1024);
}
static bool check_glyph_position(SkPoint position) {

View File

@ -8,7 +8,7 @@
#ifndef SkPaintDefaults_DEFINED
#define SkPaintDefaults_DEFINED
#include "SkPaint.h"
#include "SkFontTypes.h"
/**
* Any of these can be specified by the build system (or SkUserConfig.h)

View File

@ -56,24 +56,6 @@ bool SkPaintPriv::Overwrites(const SkImage* image, const SkPaint* paint) {
: kNotOpaque_ShaderOverrideOpacity);
}
void SkPaintPriv::ScaleFontMetrics(SkFontMetrics* metrics, SkScalar scale) {
metrics->fTop *= scale;
metrics->fAscent *= scale;
metrics->fDescent *= scale;
metrics->fBottom *= scale;
metrics->fLeading *= scale;
metrics->fAvgCharWidth *= scale;
metrics->fMaxCharWidth *= scale;
metrics->fXMin *= scale;
metrics->fXMax *= scale;
metrics->fXHeight *= scale;
metrics->fCapHeight *= scale;
metrics->fUnderlineThickness *= scale;
metrics->fUnderlinePosition *= scale;
metrics->fStrikeoutThickness *= scale;
metrics->fStrikeoutPosition *= scale;
}
bool SkPaintPriv::ShouldDither(const SkPaint& p, SkColorType dstCT) {
// The paint dither flag can veto.
if (!p.isDither()) {
@ -89,18 +71,3 @@ bool SkPaintPriv::ShouldDither(const SkPaint& p, SkColorType dstCT) {
return p.getImageFilter() || p.getMaskFilter()
|| !p.getShader() || !as_SB(p.getShader())->isConstant();
}
int SkPaintPriv::ValidCountText(const void* text, size_t length, SkTextEncoding encoding) {
switch (encoding) {
case kUTF8_SkTextEncoding: return SkUTF::CountUTF8((const char*)text, length);
case kUTF16_SkTextEncoding: return SkUTF::CountUTF16((const uint16_t*)text, length);
case kUTF32_SkTextEncoding: return SkUTF::CountUTF32((const int32_t*)text, length);
case kGlyphID_SkTextEncoding:
if (!SkIsAlign2(intptr_t(text)) || !SkIsAlign2(length)) {
return -1;
}
return length >> 1;
}
return -1;
}

View File

@ -14,6 +14,7 @@
#include "SkTypeface.h"
class SkBitmap;
class SkFont;
class SkImage;
class SkReadBuffer;
class SkWriteBuffer;
@ -56,35 +57,8 @@ public:
*/
static bool Overwrites(const SkImage*, const SkPaint* paint);
static void ScaleFontMetrics(SkFontMetrics*, SkScalar);
/**
* Return a matrix that applies the paint's text values: size, scale, skew
*/
static void MakeTextMatrix(SkMatrix* matrix, SkScalar size, SkScalar scaleX, SkScalar skewX) {
matrix->setScale(size * scaleX, size);
if (skewX) {
matrix->postSkew(skewX, 0);
}
}
static void MakeTextMatrix(SkMatrix* matrix, const SkPaint& paint) {
MakeTextMatrix(matrix, paint.getTextSize(), paint.getTextScaleX(), paint.getTextSkewX());
}
static bool ShouldDither(const SkPaint&, SkColorType);
// returns -1 if buffer is invalid for specified encoding
static int ValidCountText(const void* text, size_t length, SkTextEncoding);
static SkTypeface* GetTypefaceOrDefault(const SkPaint& paint) {
return paint.getTypeface() ? paint.getTypeface() : SkTypeface::GetDefaultTypeface();
}
static sk_sp<SkTypeface> RefTypefaceOrDefault(const SkPaint& paint) {
return paint.getTypeface() ? paint.refTypeface() : SkTypeface::MakeDefault();
}
static SkTextEncoding GetEncoding(const SkPaint& paint) {
return paint.private_internal_getTextEncoding();
}

View File

@ -40,7 +40,7 @@ static bool tooBig(const SkMatrix& m, SkScalar ma2max) {
mag2(m[SkMatrix::kMSkewX], m[SkMatrix::kMScaleY]) > ma2max;
}
bool SkPaint::TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM, SkScalar maxLimit) {
bool SkFontPriv::TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM, SkScalar maxLimit) {
SkASSERT(!ctm.hasPerspective());
SkASSERT(!textM.hasPerspective());
@ -49,7 +49,7 @@ bool SkPaint::TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM, SkSca
return tooBig(matrix, MaxCacheSize2(maxLimit));
}
SkScalar SkPaint::MaxCacheSize2(SkScalar maxLimit) {
SkScalar SkFontPriv::MaxCacheSize2(SkScalar maxLimit) {
// we have a self-imposed maximum, just for memory-usage sanity
const int limit = SkMin32(SkGraphics::GetFontCachePointSizeLimit(), maxLimit);
const SkScalar maxSize = SkIntToScalar(limit);
@ -216,7 +216,8 @@ SkScalar SkPaint::setupForAsPaths() {
class SkCanonicalizePaint {
public:
SkCanonicalizePaint(const SkPaint& paint) : fPaint(&paint), fScale(0) {
if (paint.isLinearText() || SkDraw::ShouldDrawTextAsPaths(paint, SkMatrix::I())) {
const SkFont font = SkFont::LEGACY_ExtractFromPaint(paint);
if (paint.isLinearText() || SkDraw::ShouldDrawTextAsPaths(font, paint, SkMatrix::I())) {
SkPaint* p = fLazy.set(paint);
fScale = p->setupForAsPaths();
fPaint = p;
@ -438,8 +439,8 @@ SkTextBaseIter::SkTextBaseIter(const SkGlyphID glyphs[], int count, const SkFont
// can't use our canonical size if we need to apply patheffects
if (fPaint.getPathEffect() == nullptr) {
fScale = fFont.getSize() / SkPaint::kCanonicalTextSizeForPaths;
fFont.setSize(SkIntToScalar(SkPaint::kCanonicalTextSizeForPaths));
fScale = fFont.getSize() / SkFontPriv::kCanonicalTextSizeForPaths;
fFont.setSize(SkIntToScalar(SkFontPriv::kCanonicalTextSizeForPaths));
// Note: fScale can be zero here (even if it wasn't before the divide). It can also
// be very very small. We call sk_ieee_float_divide below to ensure IEEE divide behavior,
// since downstream we will check for the resulting coordinates being non-finite anyway.

View File

@ -8,6 +8,7 @@
#include "SkCanvas.h"
#include "SkCanvasPriv.h"
#include "SkDrawShadowInfo.h"
#include "SkFontPriv.h"
#include "SkPaintPriv.h"
#include "SkPatchUtils.h"
#include "SkPictureData.h"
@ -77,8 +78,8 @@ public:
if (fByteLength == 0) {
fCount = 0;
} else {
fCount = SkPaintPriv::ValidCountText(fText, fByteLength,
SkPaintPriv::GetEncoding(*paint));
fCount = SkFontPriv::ValidCountText(fText, fByteLength,
SkPaintPriv::GetEncoding(*paint));
reader->validate(fCount > 0);
}
}

View File

@ -13,6 +13,7 @@
#include "SkColorData.h"
#include "SkDescriptor.h"
#include "SkDraw.h"
#include "SkFontMetrics.h"
#include "SkFontPriv.h"
#include "SkGlyph.h"
#include "SkMakeUnique.h"
@ -635,7 +636,7 @@ void SkScalerContextRec::getMatrixFrom2x2(SkMatrix* dst) const {
}
void SkScalerContextRec::getLocalMatrix(SkMatrix* m) const {
SkPaintPriv::MakeTextMatrix(m, fTextSize, fPreScaleX, fPreSkewX);
*m = SkFontPriv::MakeTextMatrix(fTextSize, fPreScaleX, fPreSkewX);
}
void SkScalerContextRec::getSingleMatrix(SkMatrix* m) const {
@ -1092,7 +1093,7 @@ SkDescriptor* SkScalerContext::MakeDescriptorForPaths(SkFontID typefaceID,
SkScalerContextRec rec;
memset(&rec, 0, sizeof(rec));
rec.fFontID = typefaceID;
rec.fTextSize = SkPaint::kCanonicalTextSizeForPaths;
rec.fTextSize = SkFontPriv::kCanonicalTextSizeForPaths;
rec.fPreScaleX = rec.fPost2x2[0][0] = rec.fPost2x2[1][1] = SK_Scalar1;
return AutoDescriptorGivenRecAndEffects(rec, SkScalerContextEffects(), ad);
}

View File

@ -9,6 +9,8 @@
#include "SkArenaAlloc.h"
#include "SkDescriptor.h"
#include "SkFontMetrics.h"
#include "SkFontTypes.h"
#include "SkGlyph.h"
#include "SkGlyphRunPainter.h"
#include "SkPaint.h"

View File

@ -8,6 +8,7 @@
#include "SkAdvancedTypefaceMetrics.h"
#include "SkEndian.h"
#include "SkFontDescriptor.h"
#include "SkFontMetrics.h"
#include "SkFontMgr.h"
#include "SkMakeUnique.h"
#include "SkMutex.h"

View File

@ -14,6 +14,7 @@
#include "SkFDot6.h"
#include "SkFontDescriptor.h"
#include "SkFontHost_FreeType_common.h"
#include "SkFontMetrics.h"
#include "SkGlyph.h"
#include "SkMakeUnique.h"
#include "SkMalloc.h"

View File

@ -28,6 +28,7 @@
#include "SkEndian.h"
#include "SkFloatingPoint.h"
#include "SkFontDescriptor.h"
#include "SkFontMetrics.h"
#include "SkFontMgr.h"
#include "SkGlyph.h"
#include "SkMakeUnique.h"

View File

@ -14,6 +14,7 @@
#include "SkData.h"
#include "SkDescriptor.h"
#include "SkFontDescriptor.h"
#include "SkFontMetrics.h"
#include "SkGlyph.h"
#include "SkHRESULT.h"
#include "SkMacros.h"

View File

@ -16,6 +16,7 @@
#include "SkDWriteGeometrySink.h"
#include "SkDraw.h"
#include "SkEndian.h"
#include "SkFontMetrics.h"
#include "SkGlyph.h"
#include "SkHRESULT.h"
#include "SkMaskGamma.h"

View File

@ -99,7 +99,6 @@ DEF_TEST(SkPDF_tagged, r) {
SkPaint paint;
paint.setColor(SK_ColorBLACK);
paint.setHinting(kNo_SkFontHinting);
// First page.
SkCanvas* canvas =

View File

@ -402,7 +402,7 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRemoteGlyphCache_DrawTextAsPath, reporter,
SkPaint paint;
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(0);
REPORTER_ASSERT(reporter, SkDraw::ShouldDrawTextAsPaths(paint, SkMatrix::I()));
REPORTER_ASSERT(reporter, SkDraw::ShouldDrawTextAsPaths(SkFont(), paint, SkMatrix::I()));
// Server.
auto serverTf = SkTypeface::MakeFromName("monospace", SkFontStyle());

View File

@ -17,6 +17,7 @@
#include "SkData.h"
#include "SkEncodedImageFormat.h"
#include "SkFontDescriptor.h"
#include "SkFontPriv.h"
#include "SkFontStyle.h"
#include "SkGeometry.h"
#include "SkGlyph.h"
@ -253,7 +254,7 @@ protected:
void generateFontMetrics(SkFontMetrics* metrics) override {
this->geTestSVGTypeface()->getFontMetrics(metrics);
SkPaintPriv::ScaleFontMetrics(metrics, fMatrix.getScaleY());
SkFontPriv::ScaleFontMetrics(metrics, fMatrix.getScaleY());
}
private:

View File

@ -9,6 +9,7 @@
#define SkTestSVGTypeface_DEFINED
#include "SkFontArguments.h"
#include "SkFontMetrics.h"
#include "SkMutex.h"
#include "SkPaint.h"
#include "SkPoint.h"

View File

@ -9,6 +9,8 @@
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkFontDescriptor.h"
#include "SkFontMetrics.h"
#include "SkFontPriv.h"
#include "SkGlyph.h"
#include "SkImageInfo.h"
#include "SkMatrix.h"
@ -223,7 +225,7 @@ protected:
void generateFontMetrics(SkFontMetrics* metrics) override {
this->getTestTypeface()->getFontMetrics(metrics);
SkPaintPriv::ScaleFontMetrics(metrics, fMatrix.getScaleY());
SkFontPriv::ScaleFontMetrics(metrics, fMatrix.getScaleY());
}
private:

View File

@ -10,6 +10,7 @@
#include "SkFixed.h"
#include "SkFontArguments.h"
#include "SkFontMetrics.h"
#include "SkFontStyle.h"
#include "SkPaint.h"
#include "SkRefCnt.h"