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