Debian Codegraph @

gm/mac_aa_explorer.cpp

raw
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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkFontTypes.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkTypeface.h"
17 #include "include/core/SkTypes.h"
18 #include "src/utils/mac/SkUniqueCFRef.h"
19 #include "tools/fonts/FontToolUtils.h"
20
21 #include <string.h>
22 #include <initializer_list>
23
24 #ifdef SK_BUILD_FOR_MAC
25
26 #include "include/core/SkSurface.h"
27
28 #import <ApplicationServices/ApplicationServices.h>
29
30 static void std_cg_setup(CGContextRef ctx) {
31 CGContextSetAllowsFontSubpixelQuantization(ctx, false);
32 CGContextSetShouldSubpixelQuantizeFonts(ctx, false);
33
34 // Because CG always draws from the horizontal baseline,
35 // if there is a non-integral translation from the horizontal origin to the vertical origin,
36 // then CG cannot draw the glyph in the correct location without subpixel positioning.
37 CGContextSetAllowsFontSubpixelPositioning(ctx, true);
38 CGContextSetShouldSubpixelPositionFonts(ctx, true);
39
40 CGContextSetAllowsFontSmoothing(ctx, true);
41 CGContextSetShouldAntialias(ctx, true);
42
43 CGContextSetTextDrawingMode(ctx, kCGTextFill);
44
45 // Draw black on white to create mask. (Special path exists to speed this up in CG.)
46 CGContextSetGrayFillColor(ctx, 0.0f, 1.0f);
47 }
48
49 static CGContextRef make_cg_ctx(const SkPixmap& pm) {
50 CGBitmapInfo info;
51 CGColorSpaceRef cs;
52
53 switch (pm.colorType()) {
54 case kRGBA_8888_SkColorType:
55 info = kCGBitmapByteOrder32Host | (CGBitmapInfo)kCGImageAlphaNoneSkipFirst;
56 cs = CGColorSpaceCreateDeviceRGB();
57 break;
58 case kGray_8_SkColorType:
59 info = kCGImageAlphaNone;
60 cs = CGColorSpaceCreateDeviceGray();
61 break;
62 case kAlpha_8_SkColorType:
63 info = kCGImageAlphaOnly;
64 cs = nullptr;
65 break;
66 default:
67 return nullptr;
68 }
69 auto ctx = CGBitmapContextCreate(pm.writable_addr(), pm.width(), pm.height(), 8, pm.rowBytes(),
70 cs, info);
71 std_cg_setup(ctx);
72 return ctx;
73 }
74
75 static void test_mac_fonts(SkCanvas* canvas, SkScalar size, SkScalar xpos) {
76 int w = 32;
77 int h = 32;
78
79 canvas->scale(10, 10);
80 SkScalar y = 1;
81
82 for (SkColorType ct : {kRGBA_8888_SkColorType, kGray_8_SkColorType, kAlpha_8_SkColorType}) {
83 SkImageInfo ii = SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType);
84 auto surf = SkSurfaces::Raster(ii);
85 SkPixmap pm;
86 surf->peekPixels(&pm);
87 SkUniqueCFRef<CGContextRef> ctx(make_cg_ctx(pm));
88 SkUniqueCFRef<CTFontRef> ctFont(CTFontCreateWithName(CFSTR("Times"), size, nullptr));
89 UniChar uni = 'A';
90 CGGlyph glyph;
91 CTFontGetGlyphsForCharacters(ctFont.get(), &uni, &glyph, 1);
92
93 SkScalar x = 1;
94 for (bool smooth : {false, true}) {
95 surf->getCanvas()->clear(ct == kAlpha_8_SkColorType ? 0 : 0xFFFFFFFF);
96 CGContextSetShouldSmoothFonts(ctx.get(), smooth);
97 CGPoint point = {2 + xpos, 2};
98 CTFontDrawGlyphs(ctFont.get(), &glyph, &point, 1, ctx.get());
99
100 surf->draw(canvas, x, y);
101 x += pm.width();
102 }
103 y += pm.height();
104 }
105 }
106
107 class MacAAFontsGM : public skiagm::GM {
108 SkScalar fSize = 16;
109 SkScalar fXPos = 0;
110
111 public:
112 MacAAFontsGM() {}
113 ~MacAAFontsGM() override {}
114
115 protected:
116 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
117 test_mac_fonts(canvas, fSize, fXPos);
118
119 return DrawResult::kOk;
120 }
121
122 SkISize getISize() override { return {1024, 768}; }
123
124 SkString getName() const override { return SkString("macaatest"); }
125
126 bool onChar(SkUnichar uni) override {
127 switch (uni) {
128 case 'i': fSize += 1; return true;
129 case 'k': fSize -= 1; return true;
130 case 'j': fXPos -= 1.0f/16; return true;
131 case 'l': fXPos += 1.0f/16; return true;
132 default: break;
133 }
134 return false;
135 }
136 };
137 DEF_GM(return new MacAAFontsGM;)
138
139 #endif
140
141 DEF_SIMPLE_GM(macaa_colors, canvas, 800, 500) {
142 const SkColor GRAY = 0xFF808080;
143 const SkColor colors[] = {
144 SK_ColorBLACK, SK_ColorWHITE,
145 SK_ColorBLACK, GRAY,
146 SK_ColorWHITE, SK_ColorBLACK,
147 SK_ColorWHITE, GRAY,
148 };
149 const SkScalar sizes[] = {10, 12, 15, 18, 24};
150
151 const SkScalar width = 200;
152 const SkScalar height = 500;
153 const char str[] = "Hamburgefons";
154 const size_t len = strlen(str);
155
156 sk_sp<SkTypeface> face = ToolUtils::CreateTestTypeface("Times", SkFontStyle());
157 if (!face) {
158 face = ToolUtils::DefaultPortableTypeface();
159 }
160 SkFont font(face, 12);
161
162 for (size_t i = 0; i < std::size(colors); i += 2) {
163 canvas->save();
164
165 SkPaint paint;
166 paint.setColor(colors[i+1]);
167 canvas->drawRect({0, 0, width, height}, paint);
168 paint.setColor(colors[i]);
169
170 SkScalar y = 10;
171 SkScalar x = 10;
172 for (SkScalar ps : sizes) {
173 font.setSize(ps);
174 for (bool lcd : {false, true}) {
175 font.setEdging(lcd ? SkFont::Edging::kSubpixelAntiAlias
176 : SkFont::Edging::kAntiAlias);
177 for (auto h : {SkFontHinting::kNone, SkFontHinting::kNormal}) {
178 font.setHinting(h);
179
180 y += font.getSpacing() + 2;
181 canvas->drawSimpleText(str, len, SkTextEncoding::kUTF8, x, y, font, paint);
182 }
183 }
184 y += 8;
185 }
186 canvas->restore();
187 canvas->translate(width, 0);
188 }
189 }