OLD | NEW |
---|---|
1 | |
2 /* | 1 /* |
3 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
4 * | 3 * |
5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
7 | |
8 #include "gm.h" | 8 #include "gm.h" |
9 #include "SkCanvas.h" | |
10 #include "SkPaint.h" | |
11 #include "SkPath.h" | |
12 #include "SkRect.h" | |
robertphillips
2013/08/13 12:12:32
put this in the skiagm namespace?
djsollen
2013/08/13 13:24:45
Done.
| |
9 | 13 |
robertphillips
2013/08/13 12:12:32
// This GM exercises the flags to save(). save() a
djsollen
2013/08/13 13:24:45
Done.
| |
10 #include "SkAnnotation.h" | 14 class CanvasStateGM : public skiagm::GM { |
11 #include "SkData.h" | 15 SkSize fSize; |
16 enum { | |
17 WIDTH = 150, | |
18 HEIGHT = 150, | |
19 }; | |
12 | 20 |
13 namespace skiagm { | 21 SkPaint fFillPaint; |
22 SkPaint fStrokePaint; | |
14 | 23 |
15 /** Draws two rectangles. In output formats that support internal links (PDF), | 24 SkPath fPath; |
16 * clicking the one labeled "Link to A" should take you to the one labeled | 25 |
17 * "Target A". Note that you'll need to zoom your PDF viewer in a fair bit in | 26 SkRect fInsetRect; |
18 * order for the scrolling to not be blocked by the edge of the document. | 27 SkRect fFullRect; |
19 */ | 28 |
20 class InternalLinksGM : public GM { | 29 |
21 public: | 30 public: |
22 InternalLinksGM() { | 31 CanvasStateGM() { |
23 this->setBGColor(0xFFDDDDDD); | 32 fSize.set(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT)); |
33 | |
34 fFillPaint.setColor(SK_ColorRED); | |
35 fFillPaint.setStyle(SkPaint::kFill_Style); | |
36 | |
37 fStrokePaint.setColor(SK_ColorBLUE); | |
38 fStrokePaint.setStyle(SkPaint::kStroke_Style); | |
39 fStrokePaint.setStrokeWidth(1); | |
40 | |
41 fPath.moveTo(25, 25); | |
42 fPath.lineTo(125, 25); | |
43 fPath.lineTo(75, 125); | |
44 fPath.close(); | |
45 | |
46 fInsetRect = SkRect::MakeXYWH(1, 1, WIDTH-2, HEIGHT-2); | |
robertphillips
2013/08/13 12:12:32
I'm surprised fFullRect isn't 0,0 -> WIDTH,HEIGHT
| |
47 fFullRect = SkRect::MakeXYWH(10, 10, WIDTH-20, HEIGHT-20); | |
24 } | 48 } |
25 | 49 |
26 protected: | 50 protected: |
robertphillips
2013/08/13 12:12:32
SK_OVERRIDE?
| |
27 virtual SkString onShortName() { | 51 virtual SkString onShortName() { |
28 return SkString("internal_links"); | 52 return SkString("canvas-state"); |
29 } | 53 } |
30 | 54 |
robertphillips
2013/08/13 12:12:32
SK_OVERRIDE?
| |
31 virtual SkISize onISize() { | 55 virtual SkISize onISize() { |
32 return make_isize(700, 500); | 56 return SkISize::Make(960, 1200); |
33 } | 57 } |
34 | 58 |
robertphillips
2013/08/13 12:12:32
SK_OVERRIDE?
| |
35 virtual void onDraw(SkCanvas* canvas) { | 59 virtual void onDraw(SkCanvas* canvas) { |
36 SkAutoTUnref<SkData> name(SkData::NewWithCString("target-a")); | |
37 | 60 |
38 canvas->save(); | 61 canvas->save(); |
39 canvas->translate(SkIntToScalar(100), SkIntToScalar(100)); | 62 |
40 drawLabeledRect(canvas, "Link to A", 0, 0); | 63 // [0,0] -- red triangle |
41 SkRect rect = SkRect::MakeXYWH(0, 0, SkIntToScalar(50), SkIntToScalar(20 )); | 64 // test that we don't drop the clip since we are not setting the clip bi t |
robertphillips
2013/08/13 12:12:32
Would it make sense to encapsulate all these in:
| |
42 SkAnnotateLinkToDestination(canvas, rect, name.get()); | 65 canvas->drawRect(fInsetRect, fStrokePaint); |
66 canvas->save(SkCanvas::kMatrix_SaveFlag); | |
67 canvas->clipPath(fPath); | |
43 canvas->restore(); | 68 canvas->restore(); |
69 canvas->drawRect(fFullRect, fFillPaint); | |
44 | 70 |
71 canvas->restore(); | |
45 canvas->save(); | 72 canvas->save(); |
46 canvas->translate(SkIntToScalar(200), SkIntToScalar(200)); | 73 canvas->translate(WIDTH, 0); |
47 SkPoint point = SkPoint::Make(SkIntToScalar(100), SkIntToScalar(50)); | 74 |
48 drawLabeledRect(canvas, "Target A", point.x(), point.y()); | 75 // [0,1] -- red box |
49 SkAnnotateNamedDestination(canvas, point, name.get()); | 76 // test that we drop the clip since we are setting the clip bit |
77 canvas->drawRect(fInsetRect, fStrokePaint); | |
78 canvas->save(SkCanvas::kClip_SaveFlag); | |
79 canvas->clipPath(fPath); | |
80 canvas->restore(); | |
81 canvas->drawRect(fFullRect, fFillPaint); | |
82 | |
83 canvas->restore(); | |
84 canvas->save(); | |
85 canvas->translate(0, HEIGHT); | |
86 | |
87 // [1,0] -- red box in uppper left | |
88 // test that we don't drop the matrix since we are not setting the matri x bit | |
89 canvas->drawRect(fInsetRect, fStrokePaint); | |
90 canvas->save(SkCanvas::kClip_SaveFlag); | |
91 canvas->scale(0.5, 0.5); | |
92 canvas->restore(); | |
93 canvas->drawRect(fFullRect, fFillPaint); | |
94 | |
95 canvas->restore(); | |
96 canvas->save(); | |
97 canvas->translate(WIDTH, HEIGHT); | |
98 | |
99 // [1,1] -- red box | |
100 // test that we drop the matrix since we are setting the matrix bit | |
101 canvas->drawRect(fInsetRect, fStrokePaint); | |
102 canvas->save(SkCanvas::kMatrix_SaveFlag); | |
103 canvas->scale(0.5, 0.5); | |
104 canvas->restore(); | |
105 canvas->drawRect(fFullRect, fFillPaint); | |
106 | |
107 canvas->restore(); | |
108 canvas->save(); | |
109 canvas->translate(0, 2*HEIGHT); | |
110 | |
111 // [2,0] -- red box in upper left | |
112 // test that we drop the clip but not matrix | |
113 canvas->drawRect(fInsetRect, fStrokePaint); | |
114 canvas->save(SkCanvas::kClip_SaveFlag); | |
115 canvas->clipPath(fPath); | |
116 canvas->scale(0.5, 0.5); | |
117 canvas->restore(); | |
118 canvas->drawRect(fFullRect, fFillPaint); | |
119 | |
120 canvas->restore(); | |
121 canvas->save(); | |
122 canvas->translate(WIDTH, 2*HEIGHT); | |
123 | |
124 // [2,1] -- red triangle | |
125 // test that we drop the matrix but not the clip | |
126 canvas->drawRect(fInsetRect, fStrokePaint); | |
127 canvas->save(SkCanvas::kMatrix_SaveFlag); | |
128 canvas->clipPath(fPath); | |
129 canvas->scale(0.5, 0.5); | |
130 canvas->restore(); | |
131 canvas->drawRect(fFullRect, fFillPaint); | |
132 | |
50 canvas->restore(); | 133 canvas->restore(); |
51 } | 134 } |
52 | 135 |
53 private: | 136 private: |
54 /** Draw an arbitrary rectangle at a given location and label it with some | 137 typedef skiagm::GM INHERITED; |
55 * text. */ | |
56 void drawLabeledRect(SkCanvas* canvas, const char* text, SkScalar x, SkScala r y) { | |
57 SkPaint paint; | |
58 paint.setColor(SK_ColorBLUE); | |
59 SkRect rect = SkRect::MakeXYWH(x, y, | |
60 SkIntToScalar(50), SkIntToScalar(20)); | |
61 canvas->drawRect(rect, paint); | |
62 | |
63 paint.setAntiAlias(true); | |
64 paint.setTextSize(SkIntToScalar(25)); | |
65 paint.setColor(SK_ColorBLACK); | |
66 canvas->drawText(text, strlen(text), x, y, paint); | |
67 } | |
68 | |
69 typedef GM INHERITED; | |
70 }; | 138 }; |
71 | 139 |
72 ////////////////////////////////////////////////////////////////////////////// | 140 ////////////////////////////////////////////////////////////////////////////// |
73 | 141 |
robertphillips
2013/08/13 12:12:32
DEF_GM?
| |
74 static GM* MyFactory(void*) { return SkNEW(InternalLinksGM); } | 142 static skiagm::GM* MyFactory(void*) { return new CanvasStateGM; } |
75 static GMRegistry reg(MyFactory); | 143 static skiagm::GMRegistry reg(MyFactory); |
76 | |
77 } | |
OLD | NEW |