mac_aa_explorer.cpp 5.4 KB

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