1 /*
2  * Copyright 2016 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"
9 #include "sk_tool_utils.h"
10 #include "SkBlurMask.h"
11 #include "SkCanvas.h"
12 #include "SkMaskFilter.h"
13 #include "SkTextBlob.h"
14 
15 #define WIDTH 800
16 #define HEIGHT 800
17 
draw_text(SkCanvas * canvas,sk_sp<SkTextBlob> blob,const SkPaint & paint,const SkPaint & blurPaint,const SkPaint & clearPaint)18 static void draw_text(SkCanvas* canvas, sk_sp<SkTextBlob> blob,
19                       const SkPaint& paint, const SkPaint& blurPaint,
20                       const SkPaint& clearPaint) {
21     canvas->save();
22     canvas->clipRect(SkRect::MakeLTRB(0, 0, 1081, 665));
23     canvas->drawRect(SkRect::MakeLTRB(0, 0, 1081, 665), clearPaint);
24     // draw as blurred to push glyph to be too large for atlas
25     canvas->drawTextBlob(blob, 0, 256, blurPaint);
26     canvas->drawTextBlob(blob, 0, 477, paint);
27     canvas->restore();
28 }
29 
30 // This test ensures that glyphs that are too large for the atlas
31 // are both translated and clipped correctly.
32 class ClipErrorGM : public skiagm::GM {
33 public:
ClipErrorGM()34     ClipErrorGM() {}
35 
36 protected:
onShortName()37     SkString onShortName() override { return SkString("cliperror"); }
38 
onISize()39     SkISize onISize() override { return SkISize::Make(WIDTH, HEIGHT); }
40 
onDraw(SkCanvas * canvas)41     void onDraw(SkCanvas* canvas) override {
42         SkPaint paint;
43         paint.setAntiAlias(true);
44 
45         SkFont font(sk_tool_utils::create_portable_typeface(), 256);
46 
47         // setup up maskfilter
48         const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(50));
49 
50         SkPaint blurPaint(paint);
51         blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma));
52 
53         const char text[] = "hambur";
54         auto blob = SkTextBlob::MakeFromText(text, strlen(text), font);
55 
56         SkPaint clearPaint(paint);
57         clearPaint.setColor(SK_ColorWHITE);
58 
59         canvas->save();
60         canvas->translate(0, 0);
61         canvas->clipRect(SkRect::MakeLTRB(0, 0, WIDTH, 256));
62         draw_text(canvas, blob, paint, blurPaint, clearPaint);
63         canvas->restore();
64 
65         canvas->save();
66         canvas->translate(0, 256);
67         canvas->clipRect(SkRect::MakeLTRB(0, 256, WIDTH, 510));
68         draw_text(canvas, blob, paint, blurPaint, clearPaint);
69         canvas->restore();
70     }
71 
72 private:
73     typedef skiagm::GM INHERITED;
74 };
75 
76 DEF_GM(return new ClipErrorGM;)
77