skia2/gm/colrv1.cpp
Dominik Röttsches 2e04df9a06 Support PaintTranslate in COLRv1 rasterisation
Found during implementation of COLRv1 fuzzer for FreeType that translate
operation was still missing.

Added a test glyph containing two squares filled with a radial gradient
shifted by dx 128, dy -128 and dx -308, dy 307
combined using a PaintComposite.

Bug: skia:11790
Change-Id: I4cbfe34a111a450777dc2f8fa9d32a405f808a89
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/389116
Commit-Queue: Ben Wagner <bungeman@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
2021-03-25 20:14:21 +00:00

98 lines
2.7 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:
ColrV1GM(SkScalar skewX, SkScalar rotateDeg) : fSkewX(skewX), fRotateDeg(rotateDeg) { }
protected:
struct EmojiFont {
sk_sp<SkTypeface> fTypeface;
const uint16_t fGlyphs[10] = { 19, 33, 34, 35, 20, 21, 22, 23, 24, 25 };
const size_t fGlyphs_bytesize =
SK_ARRAY_COUNT(fGlyphs) * sizeof(uint16_t);
} fEmojiFont;
void onOnceBeforeDraw() override {
fEmojiFont.fTypeface =
MakeResourceAsTypeface("fonts/colrv1_samples.ttf");
}
SkString onShortName() override {
if (fSkewX != 0 || fRotateDeg != 0) {
return SkStringPrintf("colrv1_skew_%g_rotate_%g",
fSkewX,
fRotateDeg);
}
return SkString("colrv1");
}
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,
fEmojiFont.fGlyphs_bytesize,
SkTextEncoding::kGlyphID,
10, y, font, paint);
y += metrics.fDescent + metrics.fLeading;
}
return DrawResult::kOk;
}
private:
using INHERITED = GM;
SkScalar fSkewX;
SkScalar fRotateDeg;
};
DEF_GM(return new ColrV1GM(0.f, 0.f);)
DEF_GM(return new ColrV1GM(-0.5f, 0.f);)
DEF_GM(return new ColrV1GM(0.f, 20.f);)
DEF_GM(return new ColrV1GM(-0.5f, 20.f);)
} // namespace skiagm