Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
mac_aa_explorer.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 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 "gm/gm.h"
11#include "include/core/SkFont.h"
19
20#include <string.h>
21#include <initializer_list>
22
23#ifdef SK_BUILD_FOR_MAC
24
26
27#import <ApplicationServices/ApplicationServices.h>
28
29static void std_cg_setup(CGContextRef ctx) {
30 CGContextSetAllowsFontSubpixelQuantization(ctx, false);
31 CGContextSetShouldSubpixelQuantizeFonts(ctx, false);
32
33 // Because CG always draws from the horizontal baseline,
34 // if there is a non-integral translation from the horizontal origin to the vertical origin,
35 // then CG cannot draw the glyph in the correct location without subpixel positioning.
36 CGContextSetAllowsFontSubpixelPositioning(ctx, true);
37 CGContextSetShouldSubpixelPositionFonts(ctx, true);
38
39 CGContextSetAllowsFontSmoothing(ctx, true);
40 CGContextSetShouldAntialias(ctx, true);
41
42 CGContextSetTextDrawingMode(ctx, kCGTextFill);
43
44 // Draw black on white to create mask. (Special path exists to speed this up in CG.)
45 CGContextSetGrayFillColor(ctx, 0.0f, 1.0f);
46}
47
48static CGContextRef make_cg_ctx(const SkPixmap& pm) {
49 CGBitmapInfo info;
50 CGColorSpaceRef cs;
51
52 switch (pm.colorType()) {
54 info = kCGBitmapByteOrder32Host | (CGBitmapInfo)kCGImageAlphaNoneSkipFirst;
55 cs = CGColorSpaceCreateDeviceRGB();
56 break;
58 info = kCGImageAlphaNone;
59 cs = CGColorSpaceCreateDeviceGray();
60 break;
62 info = kCGImageAlphaOnly;
63 cs = nullptr;
64 break;
65 default:
66 return nullptr;
67 }
68 auto ctx = CGBitmapContextCreate(pm.writable_addr(), pm.width(), pm.height(), 8, pm.rowBytes(),
69 cs, info);
70 std_cg_setup(ctx);
71 return ctx;
72}
73
74static void test_mac_fonts(SkCanvas* canvas, SkScalar size, SkScalar xpos) {
75 int w = 32;
76 int h = 32;
77
78 canvas->scale(10, 10);
79 SkScalar y = 1;
80
83 auto surf = SkSurfaces::Raster(ii);
84 SkPixmap pm;
85 surf->peekPixels(&pm);
86 CGContextRef ctx = make_cg_ctx(pm);
87 CGContextSelectFont(ctx, "Times", size, kCGEncodingMacRoman);
88
89 SkScalar x = 1;
90 for (bool smooth : {false, true}) {
91 surf->getCanvas()->clear(ct == kAlpha_8_SkColorType ? 0 : 0xFFFFFFFF);
92 CGContextSetShouldSmoothFonts(ctx, smooth);
93 CGContextShowTextAtPoint(ctx, 2 + xpos, 2, "A", 1);
94
95 surf->draw(canvas, x, y);
96 x += pm.width();
97 }
98 y += pm.height();
99 }
100}
101
102class MacAAFontsGM : public skiagm::GM {
103 SkScalar fSize = 16;
104 SkScalar fXPos = 0;
105
106public:
107 MacAAFontsGM() {}
108 ~MacAAFontsGM() override {}
109
110protected:
111 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
112 test_mac_fonts(canvas, fSize, fXPos);
113
114 return DrawResult::kOk;
115 }
116
117 SkISize getISize() override { return {1024, 768}; }
118
119 SkString getName() const override { return SkString("macaatest"); }
120
121 bool onChar(SkUnichar uni) override {
122 switch (uni) {
123 case 'i': fSize += 1; return true;
124 case 'k': fSize -= 1; return true;
125 case 'j': fXPos -= 1.0f/16; return true;
126 case 'l': fXPos += 1.0f/16; return true;
127 default: break;
128 }
129 return false;
130 }
131};
132DEF_GM(return new MacAAFontsGM;)
133
134#endif
135
136DEF_SIMPLE_GM(macaa_colors, canvas, 800, 500) {
137 const SkColor GRAY = 0xFF808080;
138 const SkColor colors[] = {
140 SK_ColorBLACK, GRAY,
142 SK_ColorWHITE, GRAY,
143 };
144 const SkScalar sizes[] = {10, 12, 15, 18, 24};
145
146 const SkScalar width = 200;
147 const SkScalar height = 500;
148 const char str[] = "Hamburgefons";
149 const size_t len = strlen(str);
150
152 if (!face) {
154 }
155 SkFont font(face, 12);
156
157 for (size_t i = 0; i < std::size(colors); i += 2) {
158 canvas->save();
159
161 paint.setColor(colors[i+1]);
162 canvas->drawRect({0, 0, width, height}, paint);
163 paint.setColor(colors[i]);
164
165 SkScalar y = 10;
166 SkScalar x = 10;
167 for (SkScalar ps : sizes) {
168 font.setSize(ps);
169 for (bool lcd : {false, true}) {
170 font.setEdging(lcd ? SkFont::Edging::kSubpixelAntiAlias
173 font.setHinting(h);
174
175 y += font.getSpacing() + 2;
176 canvas->drawSimpleText(str, len, SkTextEncoding::kUTF8, x, y, font, paint);
177 }
178 }
179 y += 8;
180 }
181 canvas->restore();
182 canvas->translate(width, 0);
183 }
184}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
SkColorType
Definition SkColorType.h:19
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition SkColorType.h:21
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition SkColorType.h:35
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
@ kNormal
glyph outlines modified to improve constrast
@ kNone
glyph outlines unchanged
@ kUTF8
uses bytes to represent UTF-8 or ASCII
int32_t SkUnichar
Definition SkTypes.h:175
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
int save()
Definition SkCanvas.cpp:451
void scale(SkScalar sx, SkScalar sy)
@ kAntiAlias
may have transparent pixels on glyph edges
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
size_t rowBytes() const
Definition SkPixmap.h:145
int width() const
Definition SkPixmap.h:160
SkColorType colorType() const
Definition SkPixmap.h:173
void * writable_addr() const
Definition SkPixmap.h:483
int height() const
Definition SkPixmap.h:166
virtual bool onChar(SkUnichar)
Definition gm.cpp:171
virtual SkISize getISize()=0
virtual SkString getName() const =0
virtual DrawResult onDraw(SkCanvas *, SkString *errorMsg)
Definition gm.cpp:139
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
double y
double x
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkTypeface > CreateTestTypeface(const char *name, SkFontStyle style)
DrawResult
Definition gm.h:104
SkScalar w
SkScalar h
int32_t height
int32_t width
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)