• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "gm/gm.h"
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"
25 
26 #include <string.h>
27 
rotated_checkerboard_shader(SkPaint * paint,SkColor c1,SkColor c2,int size)28 static 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);
40     paint->setShader(bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
41                                    SkSamplingOptions(), matrix));
42 }
43 
exercise_draw_pos_text(SkCanvas * canvas,const char * text,SkScalar x,SkScalar y,const SkFont & font,const SkPaint & paint)44 static void exercise_draw_pos_text(SkCanvas* canvas,
45                                    const char* text,
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);
54 }
55 
exercise_draw_pos_text_h(SkCanvas * canvas,const char * text,SkScalar x,SkScalar y,const SkFont & font,const SkPaint & paint)56 static void exercise_draw_pos_text_h(SkCanvas* canvas,
57                                      const char* text,
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);
66 }
67 
test_text(SkCanvas * canvas,SkScalar size,SkColor color,SkScalar Y)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);
72     SkPaint type;
73     type.setColor(color);
74     const char text[] = "HELLO WORLD";
75     canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 32, size / 2 + Y,
76                            font, type);
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);
81 }
82 
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);
90     {
91         SkPaint checker;
92         rotated_checkerboard_shader(&checker, SK_ColorWHITE, SK_ColorBLACK, 16);
93         checker.setAntiAlias(true);
94 
95         SkAutoCanvasRestore autoCanvasRestore(canvas, true);
96         canvas->clear(0xFFCECFCE);
97         SkScalar translate = 225364.0f;
98         canvas->translate(0, -translate);
99 
100         // Test rects
101         SkRect rect = SkRect::MakeLTRB(8, 8 + translate, size - 8,
102                                        size - 8 + translate);
103         canvas->drawRect(rect, checker);
104 
105         // Test Paths
106         canvas->translate(size, 0);
107         SkRRect rrect;
108         SkVector radii[4] = {{40, 40}, {40, 40}, {40, 40}, {40, 40}};
109         rrect.setRectRadii(rect, radii);
110         canvas->drawRRect(rrect, checker);
111 
112         // Test Points
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);
127 
128         // Test Text
129         canvas->translate(size, 0);
130         test_text(canvas, size, SK_ColorBLACK, translate);
131     }
132     // reference points (without the huge translations).
133     SkPaint stroke;
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);
142 }
143