2 * Copyright 2015 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/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"
17 #include "include/core/SkRRect.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkShader.h"
21 #include "include/core/SkTextBlob.h"
22 #include "include/core/SkTileMode.h"
23 #include "include/core/SkTypeface.h"
24 #include "tools/ToolUtils.h"
28 static void rotated_checkerboard_shader(SkPaint* paint,
33 bm.allocN32Pixels(2 * size, 2 * size);
35 bm.eraseArea(SkIRect::MakeLTRB(0, 0, size, size), c2);
36 bm.eraseArea(SkIRect::MakeLTRB(size, size, 2 * size, 2 * size), c2);
38 matrix.setScale(0.75f, 0.75f);
39 matrix.preRotate(30.0f);
40 paint->setShader(bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
41 SkSamplingOptions(), matrix));
44 static void exercise_draw_pos_text(SkCanvas* canvas,
46 SkScalar x, SkScalar y,
47 const SkFont& font, const SkPaint& paint) {
48 const int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
49 SkTextBlobBuilder builder;
50 auto rec = builder.allocRunPos(font, count);
51 font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, rec.glyphs, count);
52 font.getPos(rec.glyphs, count, rec.points(), {x, y});
53 canvas->drawTextBlob(builder.make(), 0, 0, paint);
56 static void exercise_draw_pos_text_h(SkCanvas* canvas,
58 SkScalar x, SkScalar y,
59 const SkFont& font, const SkPaint& paint) {
60 const int count = font.countText(text, strlen(text), SkTextEncoding::kUTF8);
61 SkTextBlobBuilder builder;
62 auto rec = builder.allocRunPosH(font, count, 0);
63 font.textToGlyphs(text, strlen(text), SkTextEncoding::kUTF8, rec.glyphs, count);
64 font.getXPos(rec.glyphs, count, rec.pos);
65 canvas->drawTextBlob(builder.make(), x, y, paint);
68 static void test_text(SkCanvas* canvas, SkScalar size,
69 SkColor color, SkScalar Y) {
70 SkFont font(ToolUtils::create_portable_typeface(), 24);
71 font.setEdging(SkFont::Edging::kAlias);
74 const char text[] = "HELLO WORLD";
75 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 32, size / 2 + Y,
77 SkScalar lineSpacing = font.getSpacing();
78 exercise_draw_pos_text(canvas, text, 32, size / 2 + Y + lineSpacing, font, type);
79 exercise_draw_pos_text_h(canvas, text, 32,
80 size / 2 + Y + 2 * lineSpacing, font, type);
83 // If this GM works correctly, the cyan layer should be lined up with
84 // the objects below it.
85 DEF_SIMPLE_GM(skbug_257, canvas, 512, 512) {
86 const SkScalar size = 256;
87 SkAutoCanvasRestore autoCanvasRestore0(canvas, true);
88 const SkScalar scale = 1.00168f;
89 canvas->scale(scale, scale);
92 rotated_checkerboard_shader(&checker, SK_ColorWHITE, SK_ColorBLACK, 16);
93 checker.setAntiAlias(true);
95 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
96 canvas->clear(0xFFCECFCE);
97 SkScalar translate = 225364.0f;
98 canvas->translate(0, -translate);
101 SkRect rect = SkRect::MakeLTRB(8, 8 + translate, size - 8,
102 size - 8 + translate);
103 canvas->drawRect(rect, checker);
106 canvas->translate(size, 0);
108 SkVector radii[4] = {{40, 40}, {40, 40}, {40, 40}, {40, 40}};
109 rrect.setRectRadii(rect, radii);
110 canvas->drawRRect(rrect, checker);
113 canvas->translate(-size, size);
114 SkScalar delta = 1.0 / 64.0;
115 SkPoint points[8] = {{size / 2, 8 + translate},
116 {size / 2, 8 + translate + delta},
117 {8, size / 2 + translate},
118 {8, size / 2 + translate + delta},
119 {size / 2, size - 8 + translate},
120 {size / 2, size - 8 + translate + delta},
121 {size - 8, size / 2 + translate},
122 {size - 8, size / 2 + translate + delta}};
123 checker.setStyle(SkPaint::kStroke_Style);
124 checker.setStrokeWidth(8);
125 checker.setStrokeCap(SkPaint::kRound_Cap);
126 canvas->drawPoints(SkCanvas::kLines_PointMode, 8, points, checker);
129 canvas->translate(size, 0);
130 test_text(canvas, size, SK_ColorBLACK, translate);
132 // reference points (without the huge translations).
134 stroke.setStyle(SkPaint::kStroke_Style);
135 stroke.setStrokeWidth(5);
136 stroke.setColor(SK_ColorCYAN);
137 canvas->drawCircle(size / 2, size / 2, size / 2 - 10, stroke);
138 canvas->drawCircle(3 * size / 2, size / 2, size / 2 - 10, stroke);
139 canvas->drawCircle(size / 2, 384, size / 2 - 10, stroke);
140 canvas->translate(size, size);
141 test_text(canvas, size, SK_ColorCYAN, 0.0f);