blob: 572ddf59046d4860d8e0f15ca3faba5d94f576f8 [file] [log] [blame]
Mike Reed1b8aa722019-02-21 09:00:34 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#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>
Mike Reed1b8aa722019-02-21 09:00:34 -050021
22#ifdef SK_BUILD_FOR_MAC
23
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "include/core/SkSurface.h"
25
Mike Reed1b8aa722019-02-21 09:00:34 -050026#import <ApplicationServices/ApplicationServices.h>
27
Mike Reed11bfaf12019-02-21 10:42:07 -050028static 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
Mike Reed1b8aa722019-02-21 09:00:34 -050047static CGContextRef make_cg_ctx(const SkPixmap& pm) {
48 CGBitmapInfo info;
49 CGColorSpaceRef cs;
50
51 switch (pm.colorType()) {
52 case kRGBA_8888_SkColorType:
John Stiles637838d2020-06-12 12:10:54 -040053 info = kCGBitmapByteOrder32Host | (CGBitmapInfo)kCGImageAlphaNoneSkipFirst;
Mike Reed1b8aa722019-02-21 09:00:34 -050054 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 }
Mike Reed11bfaf12019-02-21 10:42:07 -050067 auto ctx = CGBitmapContextCreate(pm.writable_addr(), pm.width(), pm.height(), 8, pm.rowBytes(),
68 cs, info);
69 std_cg_setup(ctx);
70 return ctx;
Mike Reed1b8aa722019-02-21 09:00:34 -050071}
72
Mike Reed11bfaf12019-02-21 10:42:07 -050073static void test_mac_fonts(SkCanvas* canvas, SkScalar size, SkScalar xpos) {
Mike Reed1b8aa722019-02-21 09:00:34 -050074 int w = 32;
75 int h = 32;
Mike Reed1b8aa722019-02-21 09:00:34 -050076
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);
Mike Reed0ac90992019-02-21 15:43:55 -050086 CGContextSelectFont(ctx, "Times", size, kCGEncodingMacRoman);
Mike Reed1b8aa722019-02-21 09:00:34 -050087
88 SkScalar x = 1;
89 for (bool smooth : {false, true}) {
Mike Reed0ac90992019-02-21 15:43:55 -050090 surf->getCanvas()->clear(ct == kAlpha_8_SkColorType ? 0 : 0xFFFFFFFF);
Mike Reed1b8aa722019-02-21 09:00:34 -050091 CGContextSetShouldSmoothFonts(ctx, smooth);
Mike Reed11bfaf12019-02-21 10:42:07 -050092 CGContextShowTextAtPoint(ctx, 2 + xpos, 2, "A", 1);
Mike Reed1b8aa722019-02-21 09:00:34 -050093
94 surf->draw(canvas, x, y, nullptr);
95 x += pm.width();
96 }
97 y += pm.height();
98 }
99}
100
101class MacAAFontsGM : public skiagm::GM {
102 SkScalar fSize = 16;
Mike Reed11bfaf12019-02-21 10:42:07 -0500103 SkScalar fXPos = 0;
104
Mike Reed1b8aa722019-02-21 09:00:34 -0500105public:
106 MacAAFontsGM() {}
107 ~MacAAFontsGM() override {}
108
109protected:
110 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
Mike Reed11bfaf12019-02-21 10:42:07 -0500111 test_mac_fonts(canvas, fSize, fXPos);
Mike Reed1b8aa722019-02-21 09:00:34 -0500112
113 return DrawResult::kOk;
114 }
115
116 SkISize onISize() override { return { 1024, 768 }; }
117
118 SkString onShortName() override { return SkString("macaatest"); }
119
Hal Canaryc74a5502019-07-08 14:55:15 -0400120 bool onChar(SkUnichar uni) override {
Mike Reed1b8aa722019-02-21 09:00:34 -0500121 switch (uni) {
122 case 'i': fSize += 1; return true;
123 case 'k': fSize -= 1; return true;
Mike Reed11bfaf12019-02-21 10:42:07 -0500124 case 'j': fXPos -= 1.0f/16; return true;
125 case 'l': fXPos += 1.0f/16; return true;
Mike Reed1b8aa722019-02-21 09:00:34 -0500126 default: break;
127 }
128 return false;
129 }
130};
Mike Reed1b8aa722019-02-21 09:00:34 -0500131DEF_GM(return new MacAAFontsGM;)
132
Mike Reed0ac90992019-02-21 15:43:55 -0500133#endif
134
Mike Reed651446c2019-02-21 12:12:52 -0500135DEF_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
Mike Reed651446c2019-02-21 12:12:52 -0500161 SkScalar y = 10;
Mike Reedc6461682019-02-22 08:24:15 -0500162 SkScalar x = 10;
Mike Reed651446c2019-02-21 12:12:52 -0500163 for (SkScalar ps : sizes) {
164 font.setSize(ps);
165 for (bool lcd : {false, true}) {
166 font.setEdging(lcd ? SkFont::Edging::kSubpixelAntiAlias
Mike Reedc6461682019-02-22 08:24:15 -0500167 : SkFont::Edging::kAntiAlias);
Ben Wagner5785e4a2019-05-07 16:50:29 -0400168 for (auto h : {SkFontHinting::kNone, SkFontHinting::kNormal}) {
Mike Reedc6461682019-02-22 08:24:15 -0500169 font.setHinting(h);
Mike Reed651446c2019-02-21 12:12:52 -0500170
Mike Reedc6461682019-02-22 08:24:15 -0500171 y += font.getSpacing() + 2;
Ben Wagner51e15a62019-05-07 15:38:46 -0400172 canvas->drawSimpleText(str, len, SkTextEncoding::kUTF8, x, y, font, paint);
Mike Reedc6461682019-02-22 08:24:15 -0500173 }
Mike Reed651446c2019-02-21 12:12:52 -0500174 }
Mike Reedc6461682019-02-22 08:24:15 -0500175 y += 8;
Mike Reed651446c2019-02-21 12:12:52 -0500176 }
Mike Reed651446c2019-02-21 12:12:52 -0500177 canvas->restore();
178 canvas->translate(width, 0);
179 }
180}