skia2/gm/colrv1.cpp

198 lines
6.7 KiB
C++
Raw Normal View History

Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
/*
* Copyright 2021 Google Inc.
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
*
* 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,
kColorFontsRepoComposite,
kColorFontsRepoForeground
};
ColrV1GM(ColrV1TestType testType, SkScalar skewX, SkScalar rotateDeg)
: fSkewX(skewX), fRotateDeg(rotateDeg), fTestType(testType) {}
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
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");
case kColorFontsRepoComposite:
return SkString("composite");
case kColorFontsRepoForeground:
return SkString("foreground");
}
SkASSERT(false); /* not reached */
return SkString();
}
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
struct EmojiFont {
sk_sp<SkTypeface> fTypeface;
std::vector<uint16_t> fGlyphs;
size_t bytesize() { return fGlyphs.size() * sizeof(uint16_t); }
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
} 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, 55};
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;
case kColorFontsRepoComposite:
fEmojiFont.fGlyphs = {40, 41, 42, 43, 44, 45, 46};
break;
case kColorFontsRepoForeground:
fEmojiFont.fGlyphs = {47, 48, 49, 50, 51, 52, 53, 54};
break;
}
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
}
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");
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
}
return gm_name;
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
}
SkISize onISize() override { return SkISize::Make(1400, 600); }
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
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;
std::vector<SkColor> paint_colors = {
SK_ColorBLACK, SK_ColorGREEN, SK_ColorRED, SK_ColorBLUE};
auto paint_color_iterator = paint_colors.begin();
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
for (SkScalar textSize : { 12, 18, 30, 120 }) {
font.setSize(textSize);
font.getMetrics(&metrics);
y += -metrics.fAscent;
paint.setColor(*paint_color_iterator);
canvas->drawSimpleText(fEmojiFont.fGlyphs.data(),
fEmojiFont.bytesize(),
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
SkTextEncoding::kGlyphID,
10, y, font, paint);
y += metrics.fDescent + metrics.fLeading;
paint_color_iterator++;
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
}
return DrawResult::kOk;
}
private:
using INHERITED = GM;
SkScalar fSkewX;
SkScalar fRotateDeg;
ColrV1TestType fTestType;
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
};
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);)
DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoComposite, 0.f, 0.f);)
DEF_GM(return new ColrV1GM(ColrV1GM::kColorFontsRepoForeground, 0.f, 0.f);)
Support COLRv1 color fonts Implement support for COLRv1 color gradient vector forms. COLR v1 is an extension to the OpenType COLR tables that introduces support for gradients, transforms and compositing. [1] COLRv1 fonts contain an OpenType COLR table with format v1. In a COLR v1 table, glyphs are defined by a directed, acyclic graph of Paint operations. The task for the Skia implementation is to extract the COLR v1 information from the font using FreeType's COLRv1 API, then traverse the glyph graph and translate information from the font into Skia operations on SkCanvas. Care needs to be taken for transformations to work correctly. FreeType sends a top level transform that includes the scaling from font units to actual glyph size, as well as optional transforms configured on FreeType using FT_Set_Transform. This is set up as the starting transform at the beginning of drawing a COLRv1 glyph. At the stage of setting a clip for a PaintGlyph operation, the glyph outline is retrieved from FreeType unscaled and untransformed. Since the starting transformation is applied to the SkCanvas, the unscaled glyph is properly scaled and positioned. Similarly, computing the initial bounding box for the COLRv1 glyph needs to consider transformations. COLRv1 glyphs have a base glyph entry in the glyf table which can use a minimum of two points for defining a bounding box. This bounding box is an imaged rectangle enclosing those two points at the identity transform. We need to compute the bounding box scaled, but at identity transform, then make a rectangle from it, that we then transform so that we get a large enough area to paint in. If those two points from the glyf table would be transformed first, then we would compute the bounding box, the resulting bounding box ends up too small. As a test font, add a version of the samples-glyf_colr_1.ttf from [2] with one glyph added for testing PaintColrGlyph functionality and compositions. Add a basic GM test that produces color glyphs untransformed, with skew and with rotation and combinations of those. [1] https://github.com/googlefonts/colr-gradients-spec/ [2] https://github.com/googlefonts/color-fonts Bug: skia:11264 Change-Id: I8357cd0c7ac0039bb2eac18f21fa343bf61871eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/300558 Commit-Queue: Dominik Röttsches <drott@chromium.org> Commit-Queue: Ben Wagner <bungeman@google.com> Auto-Submit: Dominik Röttsches <drott@chromium.org> Reviewed-by: Ben Wagner <bungeman@google.com>
2021-02-11 19:08:44 +00:00
} // namespace skiagm