blob: ad3a93a634be0f47eedd481291b2896b9e514b1a [file] [log] [blame]
halcanary05b48e22015-11-16 10:51:21 -08001/*
2 * Copyright 2015 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/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkFontTypes.h"
14#include "include/core/SkMatrix.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkRect.h"
19#include "include/core/SkScalar.h"
20#include "include/core/SkShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/core/SkTextBlob.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "include/core/SkTileMode.h"
23#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "tools/ToolUtils.h"
halcanary05b48e22015-11-16 10:51:21 -080025
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include <string.h>
27
halcanary05b48e22015-11-16 10:51:21 -080028static void rotated_checkerboard_shader(SkPaint* paint,
29 SkColor c1,
30 SkColor c2,
31 int size) {
32 SkBitmap bm;
33 bm.allocN32Pixels(2 * size, 2 * size);
34 bm.eraseColor(c1);
35 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
36 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
37 SkMatrix matrix;
38 matrix.setScale(0.75f, 0.75f);
39 matrix.preRotate(30.0f);
Mike Reed50acf8f2019-04-08 13:20:23 -040040 paint->setShader(bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &matrix));
halcanary05b48e22015-11-16 10:51:21 -080041}
42
43static void exercise_draw_pos_text(SkCanvas* canvas,
44 const char* text,
45 SkScalar x, SkScalar y,
Mike Reed088b74e2018-12-24 14:52:46 -050046 const SkFont& font, const SkPaint& paint) {
Ben Wagner51e15a62019-05-07 15:38:46 -040047 const int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
Mike Reed088b74e2018-12-24 14:52:46 -050048 SkTextBlobBuilder builder;
49 auto rec = builder.allocRunPos(font, count);
Ben Wagner51e15a62019-05-07 15:38:46 -040050 font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, rec.glyphs, count);
Mike Reed22451cc2019-01-01 15:40:28 -050051 font.getPos(rec.glyphs, count, rec.points(), {x, y});
Mike Reed088b74e2018-12-24 14:52:46 -050052 canvas->drawTextBlob(builder.make(), 0, 0, paint);
halcanary05b48e22015-11-16 10:51:21 -080053}
54
55static void exercise_draw_pos_text_h(SkCanvas* canvas,
56 const char* text,
57 SkScalar x, SkScalar y,
Mike Reed088b74e2018-12-24 14:52:46 -050058 const SkFont& font, const SkPaint& paint) {
Ben Wagner51e15a62019-05-07 15:38:46 -040059 const int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
Mike Reed088b74e2018-12-24 14:52:46 -050060 SkTextBlobBuilder builder;
61 auto rec = builder.allocRunPosH(font, count, 0);
Ben Wagner51e15a62019-05-07 15:38:46 -040062 font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, rec.glyphs, count);
Mike Reed088b74e2018-12-24 14:52:46 -050063 font.getXPos(rec.glyphs, count, rec.pos);
64 canvas->drawTextBlob(builder.make(), x, y, paint);
halcanary05b48e22015-11-16 10:51:21 -080065}
66
67static void test_text(SkCanvas* canvas, SkScalar size,
68 SkColor color, SkScalar Y) {
Mike Kleinea3f0142019-03-20 11:12:10 -050069 SkFont font(ToolUtils::create_portable_typeface(), 24);
Mike Reed088b74e2018-12-24 14:52:46 -050070 font.setEdging(SkFont::Edging::kAlias);
halcanary05b48e22015-11-16 10:51:21 -080071 SkPaint type;
halcanary05b48e22015-11-16 10:51:21 -080072 type.setColor(color);
73 const char text[] = "HELLO WORLD";
Ben Wagner51e15a62019-05-07 15:38:46 -040074 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 32, size / 2 + Y,
Mike Reed088b74e2018-12-24 14:52:46 -050075 font, type);
76 SkScalar lineSpacing = font.getSpacing();
77 exercise_draw_pos_text(canvas, text, 32, size / 2 + Y + lineSpacing, font, type);
halcanary05b48e22015-11-16 10:51:21 -080078 exercise_draw_pos_text_h(canvas, text, 32,
Mike Reed088b74e2018-12-24 14:52:46 -050079 size / 2 + Y + 2 * lineSpacing, font, type);
halcanary05b48e22015-11-16 10:51:21 -080080}
81
82// If this GM works correctly, the cyan layer should be lined up with
83// the objects below it.
84DEF_SIMPLE_GM(skbug_257, canvas, 512, 512) {
85 const SkScalar size = 256;
86 SkAutoCanvasRestore autoCanvasRestore0(canvas, true);
87 const SkScalar scale = 1.00168f;
88 canvas->scale(scale, scale);
89 {
90 SkPaint checker;
91 rotated_checkerboard_shader(&checker, SK_ColorWHITE, SK_ColorBLACK, 16);
92 checker.setAntiAlias(true);
93
94 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
95 canvas->clear(0xFFCECFCE);
96 SkScalar translate = 225364.0f;
97 canvas->translate(0, -translate);
98
99 // Test rects
100 SkRect rect = SkRect::MakeLTRB(8, 8 + translate, size - 8,
101 size - 8 + translate);
102 canvas->drawRect(rect, checker);
103
104 // Test Paths
105 canvas->translate(size, 0);
106 SkRRect rrect;
107 SkVector radii[4] = {{40, 40}, {40, 40}, {40, 40}, {40, 40}};
108 rrect.setRectRadii(rect, radii);
109 canvas->drawRRect(rrect, checker);
110
111 // Test Points
112 canvas->translate(-size, size);
113 SkScalar delta = 1.0 / 64.0;
114 SkPoint points[8] = {{size / 2, 8 + translate},
115 {size / 2, 8 + translate + delta},
116 {8, size / 2 + translate},
117 {8, size / 2 + translate + delta},
118 {size / 2, size - 8 + translate},
119 {size / 2, size - 8 + translate + delta},
120 {size - 8, size / 2 + translate},
121 {size - 8, size / 2 + translate + delta}};
122 checker.setStyle(SkPaint::kStroke_Style);
123 checker.setStrokeWidth(8);
124 checker.setStrokeCap(SkPaint::kRound_Cap);
125 canvas->drawPoints(SkCanvas::kLines_PointMode, 8, points, checker);
126
127 // Test Text
128 canvas->translate(size, 0);
129 test_text(canvas, size, SK_ColorBLACK, translate);
130 }
131 // reference points (without the huge translations).
132 SkPaint stroke;
133 stroke.setStyle(SkPaint::kStroke_Style);
134 stroke.setStrokeWidth(5);
135 stroke.setColor(SK_ColorCYAN);
136 canvas->drawCircle(size / 2, size / 2, size / 2 - 10, stroke);
137 canvas->drawCircle(3 * size / 2, size / 2, size / 2 - 10, stroke);
138 canvas->drawCircle(size / 2, 384, size / 2 - 10, stroke);
139 canvas->translate(size, size);
140 test_text(canvas, size, SK_ColorCYAN, 0.0f);
141}