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