skia2/gm/colrv1.cpp
Dominik Röttsches 2fa273eecf Reland: [COLRv1] Support retrieving ClipBox.
Reland after MSAN failure, initializing SkRect to empty in
computeColrV1GlyphBoundingBox().

After the discussion in [1] which was filed also in response to feedback
from Ben, the COLRv1 spec moved to not using a bounding box derived from
the `glyf` glyph for a give glyph id, but instead either use a ClipBox
found for a particular glyph id range from a ClipList array in the
COLRv1 table. If such a ClipBox is not found, perform a traversal of the
COLRv1 graph to compute the union of rectangles to compute a bounding
box.

[1] https://github.com/googlefonts/colr-gradients-spec/issues/251

Includes FreeType roll:
47b1a541cb..2c853b38a7

Fixed: skia:12297
Cq-Include-Trybots: luci.skia.skia.primary:Test-Android-Clang-GalaxyS6-GPU-MaliT760-arm64-Release-All-Android_NativeFonts, luci.skia.skia.primary:FM-Debian10-Clang-GCE-CPU-AVX2-x86_64-Release-All-MSAN
Change-Id: I165fb95c89045c4c7671af2cbe097af38ca65e84
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/437996
Commit-Queue: Ben Wagner <bungeman@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
Auto-Submit: Dominik Röttsches <drott@chromium.org>
2021-08-10 15:13:14 +00:00

179 lines
5.8 KiB
C++

/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontMetrics.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkTypeface.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include <string.h>
#include <initializer_list>
namespace skiagm {
class ColrV1GM : public GM {
public:
// TODO(drott): Consolidate test fonts.
enum ColrV1TestType {
kSkiaSampleFont,
kColorFontsRepoGradients,
kColorFontsRepoScaling,
kColorFontsRepoExtendMode,
kColorFontsRepoRotate,
kColorFontsRepoSkew,
kColorFontsRepoTransform,
kColorFontsRepoClipBox
};
ColrV1GM(ColrV1TestType testType, SkScalar skewX, SkScalar rotateDeg)
: fSkewX(skewX), fRotateDeg(rotateDeg), fTestType(testType) {}
protected:
static SkString testTypeToString(ColrV1TestType testType) {
switch (testType) {
case kSkiaSampleFont:
return SkString("skia");
case kColorFontsRepoGradients:
return SkString("gradients");
case kColorFontsRepoScaling:
return SkString("scaling");
case kColorFontsRepoExtendMode:
return SkString("extend_mode");
case kColorFontsRepoRotate:
return SkString("rotate");
case kColorFontsRepoSkew:
return SkString("skew");
case kColorFontsRepoTransform:
return SkString("transform");
case kColorFontsRepoClipBox:
return SkString("clipbox");
}
SkASSERT(false); /* not reached */
return SkString();
}
struct EmojiFont {
sk_sp<SkTypeface> fTypeface;
std::vector<uint16_t> fGlyphs;
size_t bytesize() { return fGlyphs.size() * sizeof(uint16_t); }
} fEmojiFont;
void onOnceBeforeDraw() override {
if (fTestType == kSkiaSampleFont) {
fEmojiFont.fTypeface = MakeResourceAsTypeface("fonts/colrv1_samples.ttf");
fEmojiFont.fGlyphs = {19, 33, 34, 35, 20, 21, 22, 23, 24, 25};
return;
}
fEmojiFont.fTypeface = MakeResourceAsTypeface("fonts/more_samples-glyf_colr_1.ttf");
switch (fTestType) {
case kSkiaSampleFont:
SkASSERT(false);
break;
case kColorFontsRepoGradients:
fEmojiFont.fGlyphs = {2, 5, 6, 7, 8};
break;
case kColorFontsRepoScaling:
fEmojiFont.fGlyphs = {9, 10, 11, 12, 13, 14};
break;
case kColorFontsRepoExtendMode:
fEmojiFont.fGlyphs = {15, 16, 17, 18, 19, 20};
break;
case kColorFontsRepoRotate:
fEmojiFont.fGlyphs = {21, 22, 23, 24};
break;
case kColorFontsRepoSkew:
fEmojiFont.fGlyphs = {25, 26, 27, 28, 29, 30};
break;
case kColorFontsRepoTransform:
fEmojiFont.fGlyphs = {31, 32, 33, 34};
break;
case kColorFontsRepoClipBox:
fEmojiFont.fGlyphs = {35, 36, 37, 38, 39};
break;
}
}
SkString onShortName() override {
SkString gm_name = SkStringPrintf("colrv1_%s_samples",
testTypeToString(fTestType).c_str());
if (fSkewX) {
gm_name.append("_skew");
}
if (fRotateDeg) {
gm_name.append("_rotate");
}
return gm_name;
}
SkISize onISize() override { return SkISize::Make(1400, 600); }
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
canvas->drawColor(SK_ColorWHITE);
SkPaint paint;
canvas->translate(200, 20);
if (!fEmojiFont.fTypeface) {
*errorMsg = "Did not recognize COLR v1 font format.";
return DrawResult::kSkip;
}
canvas->rotate(fRotateDeg);
canvas->skew(fSkewX, 0);
SkFont font(fEmojiFont.fTypeface);
SkFontMetrics metrics;
SkScalar y = 0;
for (SkScalar textSize : { 12, 18, 30, 120 }) {
font.setSize(textSize);
font.getMetrics(&metrics);
y += -metrics.fAscent;
canvas->drawSimpleText(fEmojiFont.fGlyphs.data(),
fEmojiFont.bytesize(),
SkTextEncoding::kGlyphID,
10, y, font, paint);
y += metrics.fDescent + metrics.fLeading;
}
return DrawResult::kOk;
}
private:
using INHERITED = GM;
SkScalar fSkewX;
SkScalar fRotateDeg;
ColrV1TestType fTestType;
};
DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, 0.f, 0.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, -0.5f, 0.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, 0.f, 20.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kSkiaSampleFont, -0.5f, 20.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoGradients, 0.f, 0.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoScaling, 0.f, 0.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoExtendMode, 0.f, 0.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoRotate, 0.f, 0.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoSkew, 0.f, 0.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoTransform, 0.f, 0.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoClipBox, 0.f, 0.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoClipBox, -0.5f, 20.f);)
} // namespace skiagm