From 1b8aa7226e24055db5e1e9d24b5686028dd77be1 Mon Sep 17 00:00:00 2001 From: Mike Reed Date: Thu, 21 Feb 2019 09:00:34 -0500 Subject: [PATCH] add test for mac aa on fonts Bug: skia: Change-Id: I93b18ee84345e27eb5cf3a94feccb35391ac0e71 Reviewed-on: https://skia-review.googlesource.com/c/193661 Reviewed-by: Mike Reed Commit-Queue: Mike Reed Auto-Submit: Mike Reed --- gm/mac_aa_explorer.cpp | 100 +++++++++++++++++++++++++++++++++++++++++ gn/gm.gni | 1 + 2 files changed, 101 insertions(+) create mode 100644 gm/mac_aa_explorer.cpp diff --git a/gm/mac_aa_explorer.cpp b/gm/mac_aa_explorer.cpp new file mode 100644 index 0000000000..3efec586c6 --- /dev/null +++ b/gm/mac_aa_explorer.cpp @@ -0,0 +1,100 @@ +/* + * Copyright 2019 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "gm.h" +#include "SkSurface.h" + +#ifdef SK_BUILD_FOR_MAC + +#import + +static CGContextRef make_cg_ctx(const SkPixmap& pm) { + CGBitmapInfo info; + CGColorSpaceRef cs; + + switch (pm.colorType()) { + case kRGBA_8888_SkColorType: + info = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast; + cs = CGColorSpaceCreateDeviceRGB(); + break; + case kGray_8_SkColorType: + info = kCGImageAlphaNone; + cs = CGColorSpaceCreateDeviceGray(); + break; + case kAlpha_8_SkColorType: + info = kCGImageAlphaOnly; + cs = nullptr; + break; + default: + return nullptr; + } + return CGBitmapContextCreate(pm.writable_addr(), pm.width(), pm.height(), 8, pm.rowBytes(), + cs, info); +} + +static void test_mac_fonts(SkCanvas* canvas, SkScalar size) { + int w = 32; + int h = 32; + bool shouldAA = true; + + canvas->scale(10, 10); + SkScalar y = 1; + + for (SkColorType ct : {kRGBA_8888_SkColorType, kGray_8_SkColorType, kAlpha_8_SkColorType}) { + SkImageInfo ii = SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType); + auto surf = SkSurface::MakeRaster(ii); + SkPixmap pm; + surf->peekPixels(&pm); + CGContextRef ctx = make_cg_ctx(pm); + CGContextSetAllowsFontSmoothing(ctx, true); + CGContextSetShouldAntialias(ctx, shouldAA); + CGContextSelectFont(ctx, "Helvetica", size, kCGEncodingMacRoman); + + SkScalar x = 1; + for (bool smooth : {false, true}) { + surf->getCanvas()->clear(ct == kGray_8_SkColorType ? 0xFFFFFFFF : 0); + CGContextSetShouldSmoothFonts(ctx, smooth); + CGContextShowTextAtPoint(ctx, 2, 2, "A", 1); + + surf->draw(canvas, x, y, nullptr); + x += pm.width(); + } + y += pm.height(); + } +} + +class MacAAFontsGM : public skiagm::GM { + SkScalar fSize = 16; +public: + MacAAFontsGM() {} + ~MacAAFontsGM() override {} + +protected: + DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { + test_mac_fonts(canvas, fSize); + + return DrawResult::kOk; + } + + SkISize onISize() override { return { 1024, 768 }; } + + SkString onShortName() override { return SkString("macaatest"); } + + bool onHandleKey(SkUnichar uni) override { + switch (uni) { + case 'i': fSize += 1; return true; + case 'k': fSize -= 1; return true; + default: break; + } + return false; + } +}; + +DEF_GM(return new MacAAFontsGM;) + +#endif + diff --git a/gn/gm.gni b/gn/gm.gni index 714fcb422e..5cda1915c5 100644 --- a/gn/gm.gni +++ b/gn/gm.gni @@ -221,6 +221,7 @@ gm_sources = [ "$_gm/localmatriximageshader.cpp", "$_gm/localmatrixshader.cpp", "$_gm/lumafilter.cpp", + "$_gm/mac_aa_explorer.cpp", "$_gm/makecolorspace.cpp", "$_gm/makeRasterImage.cpp", "$_gm/mandoline.cpp",