scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 10 | #include "Resources.h" |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 12 | #include "SkFontStyle.h" |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 13 | #include "SkString.h" |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 14 | #include "SkSurfaceProps.h" |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 15 | #include "SkTypeface.h" |
| 16 | #include "SkTypes.h" |
| 17 | |
reed@google.com | 35fe737 | 2013-10-30 15:07:03 +0000 | [diff] [blame] | 18 | static void getGlyphPositions(const SkPaint& paint, const uint16_t glyphs[], |
| 19 | int count, SkScalar x, SkScalar y, SkPoint pos[]) { |
| 20 | SkASSERT(SkPaint::kGlyphID_TextEncoding == paint.getTextEncoding()); |
| 21 | |
| 22 | SkAutoSTMalloc<128, SkScalar> widthStorage(count); |
| 23 | SkScalar* widths = widthStorage.get(); |
| 24 | paint.getTextWidths(glyphs, count * sizeof(uint16_t), widths); |
skia.committer@gmail.com | 7ed98df | 2013-10-31 07:01:53 +0000 | [diff] [blame] | 25 | |
reed@google.com | 35fe737 | 2013-10-30 15:07:03 +0000 | [diff] [blame] | 26 | for (int i = 0; i < count; ++i) { |
| 27 | pos[i].set(x, y); |
| 28 | x += widths[i]; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | static void applyKerning(SkPoint pos[], const int32_t adjustments[], int count, |
| 33 | const SkPaint& paint) { |
| 34 | SkScalar scale = paint.getTextSize() / paint.getTypeface()->getUnitsPerEm(); |
| 35 | |
| 36 | SkScalar globalAdj = 0; |
| 37 | for (int i = 0; i < count - 1; ++i) { |
| 38 | globalAdj += adjustments[i] * scale; |
| 39 | pos[i + 1].fX += globalAdj; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static void drawKernText(SkCanvas* canvas, const void* text, size_t len, |
| 44 | SkScalar x, SkScalar y, const SkPaint& paint) { |
| 45 | SkTypeface* face = paint.getTypeface(); |
| 46 | if (!face) { |
| 47 | canvas->drawText(text, len, x, y, paint); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | SkAutoSTMalloc<128, uint16_t> glyphStorage(len); |
| 52 | uint16_t* glyphs = glyphStorage.get(); |
| 53 | int glyphCount = paint.textToGlyphs(text, len, glyphs); |
| 54 | if (glyphCount < 1) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | SkAutoSTMalloc<128, int32_t> adjustmentStorage(glyphCount - 1); |
| 59 | int32_t* adjustments = adjustmentStorage.get(); |
| 60 | if (!face->getKerningPairAdjustments(glyphs, glyphCount, adjustments)) { |
| 61 | canvas->drawText(text, len, x, y, paint); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | SkPaint glyphPaint(paint); |
| 66 | glyphPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 67 | |
| 68 | SkAutoSTMalloc<128, SkPoint> posStorage(glyphCount); |
| 69 | SkPoint* pos = posStorage.get(); |
| 70 | getGlyphPositions(glyphPaint, glyphs, glyphCount, x, y, pos); |
| 71 | |
| 72 | applyKerning(pos, adjustments, glyphCount, glyphPaint); |
| 73 | canvas->drawPosText(glyphs, glyphCount * sizeof(uint16_t), pos, glyphPaint); |
| 74 | } |
| 75 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 76 | constexpr struct { |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 77 | const char* fName; |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 78 | SkFontStyle fStyle; |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 79 | } gFaceStyles[] = { |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 80 | { "sans-serif", SkFontStyle::Normal() }, |
| 81 | { "sans-serif", SkFontStyle::Bold() }, |
| 82 | { "sans-serif", SkFontStyle::Italic() }, |
| 83 | { "sans-serif", SkFontStyle::BoldItalic() }, |
| 84 | { "serif", SkFontStyle::Normal() }, |
| 85 | { "serif", SkFontStyle::Bold() }, |
| 86 | { "serif", SkFontStyle::Italic() }, |
| 87 | { "serif", SkFontStyle::BoldItalic() }, |
| 88 | { "monospace", SkFontStyle::Normal() }, |
| 89 | { "monospace", SkFontStyle::Bold() }, |
| 90 | { "monospace", SkFontStyle::Italic() }, |
| 91 | { "monospace", SkFontStyle::BoldItalic() }, |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 92 | }; |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 93 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 94 | constexpr int gFaceStylesCount = SK_ARRAY_COUNT(gFaceStyles); |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 95 | |
| 96 | class TypefaceStylesGM : public skiagm::GM { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 97 | sk_sp<SkTypeface> fFaces[gFaceStylesCount]; |
reed@google.com | 35fe737 | 2013-10-30 15:07:03 +0000 | [diff] [blame] | 98 | bool fApplyKerning; |
skia.committer@gmail.com | 12eea2b | 2013-02-27 07:10:10 +0000 | [diff] [blame] | 99 | |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 100 | public: |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 101 | TypefaceStylesGM(bool applyKerning) |
| 102 | : fApplyKerning(applyKerning) { |
| 103 | memset(fFaces, 0, sizeof(fFaces)); |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 104 | } |
skia.committer@gmail.com | 12eea2b | 2013-02-27 07:10:10 +0000 | [diff] [blame] | 105 | |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 106 | protected: |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 107 | void onOnceBeforeDraw() override { |
| 108 | for (int i = 0; i < gFaceStylesCount; i++) { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 109 | fFaces[i] = SkTypeface::MakeFromName( |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 110 | sk_tool_utils::platform_font_name(gFaceStyles[i].fName), gFaceStyles[i].fStyle); |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 114 | SkString onShortName() override { |
reed@google.com | 35fe737 | 2013-10-30 15:07:03 +0000 | [diff] [blame] | 115 | SkString name("typefacestyles"); |
| 116 | if (fApplyKerning) { |
| 117 | name.append("_kerning"); |
| 118 | } |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 119 | name.append(sk_tool_utils::major_platform_os_name()); |
reed@google.com | 35fe737 | 2013-10-30 15:07:03 +0000 | [diff] [blame] | 120 | return name; |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 121 | } |
skia.committer@gmail.com | 12eea2b | 2013-02-27 07:10:10 +0000 | [diff] [blame] | 122 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 123 | SkISize onISize() override { |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 124 | return SkISize::Make(640, 480); |
| 125 | } |
skia.committer@gmail.com | 12eea2b | 2013-02-27 07:10:10 +0000 | [diff] [blame] | 126 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 127 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 128 | SkPaint paint; |
| 129 | paint.setAntiAlias(true); |
| 130 | paint.setTextSize(SkIntToScalar(30)); |
skia.committer@gmail.com | 12eea2b | 2013-02-27 07:10:10 +0000 | [diff] [blame] | 131 | |
reed@google.com | 35fe737 | 2013-10-30 15:07:03 +0000 | [diff] [blame] | 132 | const char* text = fApplyKerning ? "Type AWAY" : "Hamburgefons"; |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 133 | const size_t textLen = strlen(text); |
skia.committer@gmail.com | 12eea2b | 2013-02-27 07:10:10 +0000 | [diff] [blame] | 134 | |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 135 | SkScalar x = SkIntToScalar(10); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 136 | SkScalar dy = paint.getFontMetrics(nullptr); |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 137 | SkScalar y = dy; |
skia.committer@gmail.com | 12eea2b | 2013-02-27 07:10:10 +0000 | [diff] [blame] | 138 | |
reed@google.com | 35fe737 | 2013-10-30 15:07:03 +0000 | [diff] [blame] | 139 | if (fApplyKerning) { |
| 140 | paint.setSubpixelText(true); |
| 141 | } else { |
| 142 | paint.setLinearText(true); |
| 143 | } |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 144 | for (int i = 0; i < gFaceStylesCount; i++) { |
| 145 | paint.setTypeface(fFaces[i]); |
| 146 | canvas->drawText(text, textLen, x, y, paint); |
reed@google.com | 35fe737 | 2013-10-30 15:07:03 +0000 | [diff] [blame] | 147 | if (fApplyKerning) { |
| 148 | drawKernText(canvas, text, textLen, x + 240, y, paint); |
| 149 | } |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 150 | y += dy; |
| 151 | } |
| 152 | } |
skia.committer@gmail.com | 12eea2b | 2013-02-27 07:10:10 +0000 | [diff] [blame] | 153 | |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 154 | private: |
| 155 | typedef skiagm::GM INHERITED; |
| 156 | }; |
skia.committer@gmail.com | 12eea2b | 2013-02-27 07:10:10 +0000 | [diff] [blame] | 157 | |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 158 | DEF_GM( return new TypefaceStylesGM(false); ) |
| 159 | DEF_GM( return new TypefaceStylesGM(true); ) |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 160 | |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 161 | //////////////////////////////////////////////////////////////////////////////// |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 162 | |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 163 | static void draw_typeface_rendering_gm(SkCanvas* canvas, sk_sp<SkTypeface> face, |
| 164 | char character = 'A') { |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 165 | struct AliasType { |
| 166 | bool antiAlias; |
| 167 | bool subpixelAntitalias; |
| 168 | bool inLayer; |
| 169 | } constexpr aliasTypes[] { |
bungeman | f382b48 | 2016-07-13 14:00:39 -0700 | [diff] [blame] | 170 | #ifndef SK_BUILD_FOR_IOS |
| 171 | // This gm crashes on iOS when drawing an embedded bitmap when requesting aliased rendering. |
| 172 | // The crash looks like |
| 173 | // libTrueTypeScaler.dylib`<redacted> + 80 |
| 174 | // stop reason = EXC_BAD_ACCESS (code=EXC_ARM_DA_ALIGN, address=...) |
| 175 | // -> 0x330b19d0 <+80>: strd r2, r3, [r5, #36] |
| 176 | // 0x330b19d4 <+84>: movs r3, #0x0 |
| 177 | // 0x330b19d6 <+86>: add r2, sp, #0x28 |
| 178 | // 0x330b19d8 <+88>: ldr r0, [r4, #0x4] |
| 179 | // Disable testing embedded bitmaps on iOS for now. |
| 180 | // See https://bug.skia.org/5530 . |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 181 | { false, false, false }, // aliased |
bungeman | f382b48 | 2016-07-13 14:00:39 -0700 | [diff] [blame] | 182 | #endif |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 183 | { true, false, false }, // anti-aliased |
| 184 | { true, true , false }, // subpixel anti-aliased |
| 185 | { true, false, true }, // anti-aliased in layer (flat pixel geometry) |
| 186 | { true, true , true }, // subpixel anti-aliased in layer (flat pixel geometry) |
| 187 | }; |
| 188 | |
| 189 | // The hintgasp.ttf is designed for the following sizes to be different. |
| 190 | // GASP_DOGRAY 0x0002 0<=ppem<=10 |
| 191 | // GASP_SYMMETRIC_SMOOTHING 0x0008 0<=ppem<=10 |
| 192 | // GASP_GRIDFIT 0x0001 11<=ppem<=12 |
| 193 | // GASP_SYMMETRIC_GRIDFIT 0x0004 11<=ppem<=12 |
| 194 | // GASP_DOGRAY|GASP_GRIDFIT 0x0003 13<=ppem<=14 |
| 195 | // GASP_SYMMETRIC_SMOOTHING|GASP_SYMMETRIC_GRIDFIT 0x000C 13<=ppem<=14 |
| 196 | // (neither) 0x0000 15<=ppem |
bungeman | 5dba301 | 2016-07-12 06:55:25 -0700 | [diff] [blame] | 197 | // Odd sizes have embedded bitmaps. |
| 198 | constexpr SkScalar textSizes[] = { 9, 10, 11, 12, 13, 14, 15, 16 }; |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 199 | |
| 200 | constexpr SkPaint::Hinting hintingTypes[] = { SkPaint::kNo_Hinting, |
| 201 | SkPaint::kSlight_Hinting, |
| 202 | SkPaint::kNormal_Hinting, |
| 203 | SkPaint::kFull_Hinting }; |
| 204 | |
| 205 | struct SubpixelType { |
| 206 | bool requested; |
| 207 | SkVector offset; |
| 208 | } constexpr subpixelTypes[] = { |
| 209 | { false, { 0.00, 0.00 } }, |
| 210 | { true , { 0.00, 0.00 } }, |
| 211 | { true , { 0.25, 0.00 } }, |
| 212 | { true , { 0.25, 0.25 } }, |
| 213 | }; |
| 214 | |
| 215 | constexpr bool rotateABitTypes[] = { false, true }; |
| 216 | |
| 217 | SkPaint paint; |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 218 | paint.setTypeface(face); |
bungeman | 5dba301 | 2016-07-12 06:55:25 -0700 | [diff] [blame] | 219 | paint.setEmbeddedBitmapText(true); |
| 220 | |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 221 | SkScalar x = 0; |
| 222 | SkScalar xMax = x; |
| 223 | SkScalar xBase = 0; |
| 224 | SkScalar y = 0; // The baseline of the previous output |
| 225 | for (const SubpixelType subpixel : subpixelTypes) { |
| 226 | y = 0; |
| 227 | paint.setSubpixelText(subpixel.requested); |
| 228 | |
| 229 | for (const AliasType& alias : aliasTypes) { |
| 230 | paint.setAntiAlias(alias.antiAlias); |
| 231 | paint.setLCDRenderText(alias.subpixelAntitalias); |
| 232 | SkAutoCanvasRestore acr(canvas, false); |
| 233 | if (alias.inLayer) { |
| 234 | canvas->saveLayer(nullptr, &paint); |
| 235 | } |
| 236 | |
| 237 | for (const SkScalar& textSize : textSizes) { |
| 238 | x = xBase + 5; |
| 239 | paint.setTextSize(textSize); |
| 240 | |
| 241 | SkScalar dy = SkScalarCeilToScalar(paint.getFontMetrics(nullptr)); |
| 242 | y += dy; |
| 243 | for (const SkPaint::Hinting& hinting : hintingTypes) { |
| 244 | paint.setHinting(hinting); |
| 245 | |
| 246 | for (const bool& rotateABit : rotateABitTypes) { |
| 247 | SkAutoCanvasRestore acr(canvas, true); |
| 248 | if (rotateABit) { |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 249 | canvas->rotate(2, x + subpixel.offset.x(), |
| 250 | y + subpixel.offset.y()); |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 251 | } |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 252 | canvas->drawText(&character, 1, |
| 253 | x + subpixel.offset.x(), |
| 254 | y + subpixel.offset.y(), paint); |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 255 | |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 256 | SkScalar dx = SkScalarCeilToScalar( |
| 257 | paint.measureText(&character, 1)) + 5; |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 258 | x += dx; |
| 259 | xMax = SkTMax(x, xMax); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | y += 10; |
| 264 | } |
| 265 | xBase = xMax; |
| 266 | } |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | DEF_SIMPLE_GM_BG_NAME(typefacerendering, canvas, 640, 680, SK_ColorWHITE, |
| 270 | SkStringPrintf("typefacerendering%s", |
| 271 | sk_tool_utils::major_platform_os_name().c_str())) { |
| 272 | if (sk_sp<SkTypeface> face = MakeResourceAsTypeface("/fonts/hintgasp.ttf")) { |
| 273 | draw_typeface_rendering_gm(canvas, std::move(face)); |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 274 | } |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 275 | } |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 276 | |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 277 | // Type1 fonts don't currently work in Skia on Windows. |
| 278 | #ifndef SK_BUILD_FOR_WIN |
bungeman | 61457a6 | 2016-07-06 11:55:05 -0700 | [diff] [blame] | 279 | |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 280 | DEF_SIMPLE_GM_BG_NAME(typefacerendering_pfa, canvas, 640, 680, SK_ColorWHITE, |
| 281 | SkStringPrintf("typefacerendering_pfa%s", |
| 282 | sk_tool_utils::major_platform_os_name().c_str())) { |
| 283 | if (sk_sp<SkTypeface> face = MakeResourceAsTypeface("fonts/Roboto2-Regular.pfa")) { |
| 284 | // This subsetted typeface doesn't have the character 'A'. |
| 285 | draw_typeface_rendering_gm(canvas, std::move(face), 'O'); |
| 286 | } |
| 287 | } |
reed@google.com | 92abe48 | 2013-02-26 16:57:16 +0000 | [diff] [blame] | 288 | |
halcanary | 4ecf0d4 | 2016-09-20 13:11:01 -0700 | [diff] [blame] | 289 | DEF_SIMPLE_GM_BG_NAME(typefacerendering_pfb, canvas, 640, 680, SK_ColorWHITE, |
| 290 | SkStringPrintf("typefacerendering_pfb%s", |
| 291 | sk_tool_utils::major_platform_os_name().c_str())) { |
| 292 | if (sk_sp<SkTypeface> face = MakeResourceAsTypeface("fonts/Roboto2-Regular.pfb")) { |
| 293 | draw_typeface_rendering_gm(canvas, std::move(face), 'O'); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | #endif |