2 * Copyright 2019 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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"
20 #include <initializer_list>
22 #ifdef SK_BUILD_FOR_MAC
24 #include "include/core/SkSurface.h"
26 #import <ApplicationServices/ApplicationServices.h>
28 static void std_cg_setup(CGContextRef ctx) {
29 CGContextSetAllowsFontSubpixelQuantization(ctx, false);
30 CGContextSetShouldSubpixelQuantizeFonts(ctx, false);
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);
38 CGContextSetAllowsFontSmoothing(ctx, true);
39 CGContextSetShouldAntialias(ctx, true);
41 CGContextSetTextDrawingMode(ctx, kCGTextFill);
43 // Draw black on white to create mask. (Special path exists to speed this up in CG.)
44 CGContextSetGrayFillColor(ctx, 0.0f, 1.0f);
47 static CGContextRef make_cg_ctx(const SkPixmap& pm) {
51 switch (pm.colorType()) {
52 case kRGBA_8888_SkColorType:
53 info = kCGBitmapByteOrder32Host | (CGBitmapInfo)kCGImageAlphaNoneSkipFirst;
54 cs = CGColorSpaceCreateDeviceRGB();
56 case kGray_8_SkColorType:
57 info = kCGImageAlphaNone;
58 cs = CGColorSpaceCreateDeviceGray();
60 case kAlpha_8_SkColorType:
61 info = kCGImageAlphaOnly;
67 auto ctx = CGBitmapContextCreate(pm.writable_addr(), pm.width(), pm.height(), 8, pm.rowBytes(),
73 static void test_mac_fonts(SkCanvas* canvas, SkScalar size, SkScalar xpos) {
77 canvas->scale(10, 10);
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);
84 surf->peekPixels(&pm);
85 CGContextRef ctx = make_cg_ctx(pm);
86 CGContextSelectFont(ctx, "Times", size, kCGEncodingMacRoman);
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);
94 surf->draw(canvas, x, y);
101 class MacAAFontsGM : public skiagm::GM {
107 ~MacAAFontsGM() override {}
110 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
111 test_mac_fonts(canvas, fSize, fXPos);
113 return DrawResult::kOk;
116 SkISize onISize() override { return { 1024, 768 }; }
118 SkString onShortName() override { return SkString("macaatest"); }
120 bool onChar(SkUnichar uni) override {
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;
131 DEF_GM(return new MacAAFontsGM;)
135 DEF_SIMPLE_GM(macaa_colors, canvas, 800, 500) {
136 const SkColor GRAY = 0xFF808080;
137 const SkColor colors[] = {
138 SK_ColorBLACK, SK_ColorWHITE,
140 SK_ColorWHITE, SK_ColorBLACK,
143 const SkScalar sizes[] = {10, 12, 15, 18, 24};
145 const SkScalar width = 200;
146 const SkScalar height = 500;
147 const char str[] = "Hamburgefons";
148 const size_t len = strlen(str);
151 font.setTypeface(SkTypeface::MakeFromName("Times", SkFontStyle()));
153 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); i += 2) {
157 paint.setColor(colors[i+1]);
158 canvas->drawRect({0, 0, width, height}, paint);
159 paint.setColor(colors[i]);
163 for (SkScalar ps : sizes) {
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}) {
171 y += font.getSpacing() + 2;
172 canvas->drawSimpleText(str, len, SkTextEncoding::kUTF8, x, y, font, paint);
178 canvas->translate(width, 0);