OLD | NEW |
1 SkPaint | 1 SkPaint |
2 ======= | 2 ======= |
3 | 3 |
4 *color, stroke, font, effects* | 4 *color, stroke, font, effects* |
5 | 5 |
6 <!-- Updated Jan 17, 2013 by humper@google.com --> | 6 <!-- Updated Jan 17, 2013 by humper@google.com --> |
7 | 7 |
8 Anytime you draw something in Skia, and want to specify what color it | 8 Anytime you draw something in Skia, and want to specify what color it |
9 is, or how it blends with the background, or what style or font to | 9 is, or how it blends with the background, or what style or font to |
10 draw it in, you specify those attributes in a paint. | 10 draw it in, you specify those attributes in a paint. |
11 | 11 |
12 Unlike `SkCanvas`, paints do not maintain an internal stack of state | 12 Unlike `SkCanvas`, paints do not maintain an internal stack of state |
13 (i.e. there is no save/restore on a paint). However, paints are | 13 (i.e. there is no save/restore on a paint). However, paints are |
14 relatively light-weight, so the client may create and maintain any | 14 relatively light-weight, so the client may create and maintain any |
15 number of paint objects, each setup for a particular use. Factoring | 15 number of paint objects, each setup for a particular use. Factoring |
16 all of these color and stylistic attribute out of the canvas state, | 16 all of these color and stylistic attribute out of the canvas state, |
17 and into (multiple) paint objects, allows canvas' save/restore to be | 17 and into (multiple) paint objects, allows canvas' save/restore to be |
18 that much more efficient, as all they have to do is maintain the stack | 18 that much more efficient, as all they have to do is maintain the stack |
19 of matrix and clip settings. | 19 of matrix and clip settings. |
20 | 20 |
21 <!--?prettify lang=cc?--> | 21 <!--?prettify lang=cc?--> |
22 | 22 |
23 SkPaint paint1, paint2, paint3; | 23 void draw(SkCanvas* canvas) { |
| 24 canvas->clear(SK_ColorWHITE); |
24 | 25 |
25 paint1.setColor(0xFFFF0000: | 26 SkPaint paint1, paint2, paint3; |
26 paint1.setStyle(SkPaint::kFill_Style); | |
27 | 27 |
28 paint2.setColor(0x8000FF00); | 28 paint1.setTextSize(64.0f); |
29 paint2.setStyle(SkPaint::kStroke_Style); | 29 paint1.setAntiAlias(true); |
30 paint2.setStrokeWidth(SkIntToScalar(3)); | 30 paint1.setColor(0xFFFF0000); |
| 31 paint1.setStyle(SkPaint::kFill_Style); |
31 | 32 |
32 paint3.setColor(0xFF888888); | 33 paint2.setTextSize(64.f); |
33 paint3.setTextSize(SkIntToScalar(24)); | 34 paint2.setAntiAlias(true); |
34 paint3.setTextScaleX(SkFloatToScalar(0.75f)); | 35 paint2.setColor(0xFF008800); |
| 36 paint2.setStyle(SkPaint::kStroke_Style); |
| 37 paint2.setStrokeWidth(SkIntToScalar(3)); |
35 | 38 |
| 39 paint3.setTextSize(64.0f); |
| 40 paint3.setAntiAlias(true); |
| 41 paint3.setColor(0xFF888888); |
| 42 paint3.setTextScaleX(SkFloatToScalar(1.5f)); |
| 43 |
| 44 const char text[] = "Skia!"; |
| 45 canvas->drawText(text, strlen(text), 20.0f, 64.0f, paint1); |
| 46 canvas->drawText(text, strlen(text), 20.0f, 144.0f, paint2); |
| 47 canvas->drawText(text, strlen(text), 20.0f, 224.0f, paint3); |
| 48 } |
| 49 |
| 50 <a href="https://fiddle.skia.org/c/b8e7991ede1ca88e5458aa1f0039caf9"> |
| 51 <img src="https://fiddle.skia.org/i/b8e7991ede1ca88e5458aa1f0039caf9_raster.png"
></a> |
36 | 52 |
37 This shows three different paints, each setup to draw in a different | 53 This shows three different paints, each setup to draw in a different |
38 style. Now the caller can intermix these paints freely, either using | 54 style. Now the caller can intermix these paints freely, either using |
39 them as is, or modifying them as the drawing proceeds. | 55 them as is, or modifying them as the drawing proceeds. |
40 | 56 |
41 <!--?prettify lang=cc?--> | 57 <!--?prettify lang=cc?--> |
42 | 58 |
43 canvas->drawRect(..., paint1); | 59 canvas->drawRect(..., paint1); |
44 canvas->drawRect(..., paint2); | 60 canvas->drawRect(..., paint2); |
45 | 61 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 a specific font style, to be used for measuring and drawing | 109 a specific font style, to be used for measuring and drawing |
94 text. Speaking of which, paints are used not only for drawing text, | 110 text. Speaking of which, paints are used not only for drawing text, |
95 but also for measuring it. | 111 but also for measuring it. |
96 | 112 |
97 <!--?prettify lang=cc?--> | 113 <!--?prettify lang=cc?--> |
98 | 114 |
99 paint.measureText(...); | 115 paint.measureText(...); |
100 paint.getTextBounds(...); | 116 paint.getTextBounds(...); |
101 paint.textToGlyphs(...); | 117 paint.textToGlyphs(...); |
102 paint.getFontMetrics(...); | 118 paint.getFontMetrics(...); |
OLD | NEW |