OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * 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 |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gm.h" | 8 #include "gm.h" |
9 | 9 |
10 #include "Resources.h" | 10 #include "Resources.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 70 |
71 void onDraw(SkCanvas* canvas) override { | 71 void onDraw(SkCanvas* canvas) override { |
72 | 72 |
73 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); | 73 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); |
74 | 74 |
75 SkPaint paint; | 75 SkPaint paint; |
76 paint.setTypeface(emojiFont.typeface); | 76 paint.setTypeface(emojiFont.typeface); |
77 const char* text = emojiFont.text; | 77 const char* text = emojiFont.text; |
78 | 78 |
79 // draw text at different point sizes | 79 // draw text at different point sizes |
80 const int textSize[] = { 10, 30, 50, }; | 80 static constexpr SkScalar textSizes[] = { 10, 30, 50, }; |
81 const int textYOffset[] = { 10, 40, 100, }; | 81 SkPaint::FontMetrics metrics; |
82 SkASSERT(sizeof(textSize) == sizeof(textYOffset)); | 82 SkScalar y = 0; |
83 size_t y_offset = 0; | 83 for (const SkScalar& textSize : textSizes) { |
84 for (size_t y = 0; y < sizeof(textSize) / sizeof(int); y++) { | 84 paint.setTextSize(textSize); |
85 paint.setTextSize(SkIntToScalar(textSize[y])); | 85 paint.getFontMetrics(&metrics); |
86 canvas->drawText(text, strlen(text), 10, SkIntToScalar(textYOffset[y
]), paint); | 86 y += -metrics.fAscent; |
87 y_offset += textYOffset[y]; | 87 canvas->drawText(text, strlen(text), 10, y, paint); |
| 88 y += metrics.fDescent + metrics.fLeading; |
88 } | 89 } |
89 | 90 |
| 91 y += 20; |
| 92 SkScalar savedY = y; |
90 // draw with shaders and image filters | 93 // draw with shaders and image filters |
91 for (int makeLinear = 0; makeLinear < 2; makeLinear++) { | 94 for (int makeLinear = 0; makeLinear < 2; makeLinear++) { |
92 for (int makeBlur = 0; makeBlur < 2; makeBlur++) { | 95 for (int makeBlur = 0; makeBlur < 2; makeBlur++) { |
93 for (int makeGray = 0; makeGray < 2; makeGray++) { | 96 for (int makeGray = 0; makeGray < 2; makeGray++) { |
94 SkPaint shaderPaint; | 97 SkPaint shaderPaint; |
95 shaderPaint.setTypeface(sk_ref_sp(paint.getTypeface())); | 98 shaderPaint.setTypeface(sk_ref_sp(paint.getTypeface())); |
96 if (SkToBool(makeLinear)) { | 99 if (SkToBool(makeLinear)) { |
97 shaderPaint.setShader(MakeLinear()); | 100 shaderPaint.setShader(MakeLinear()); |
98 } | 101 } |
99 | 102 |
100 if (SkToBool(makeBlur) && SkToBool(makeGray)) { | 103 if (SkToBool(makeBlur) && SkToBool(makeGray)) { |
101 sk_sp<SkImageFilter> grayScale(make_grayscale(nullptr)); | 104 sk_sp<SkImageFilter> grayScale(make_grayscale(nullptr)); |
102 sk_sp<SkImageFilter> blur(make_blur(3.0f, std::move(gray
Scale))); | 105 sk_sp<SkImageFilter> blur(make_blur(3.0f, std::move(gray
Scale))); |
103 shaderPaint.setImageFilter(std::move(blur)); | 106 shaderPaint.setImageFilter(std::move(blur)); |
104 } else if (SkToBool(makeBlur)) { | 107 } else if (SkToBool(makeBlur)) { |
105 shaderPaint.setImageFilter(make_blur(3.0f, nullptr)); | 108 shaderPaint.setImageFilter(make_blur(3.0f, nullptr)); |
106 } else if (SkToBool(makeGray)) { | 109 } else if (SkToBool(makeGray)) { |
107 shaderPaint.setImageFilter(make_grayscale(nullptr)); | 110 shaderPaint.setImageFilter(make_grayscale(nullptr)); |
108 } | 111 } |
109 shaderPaint.setTextSize(30); | 112 shaderPaint.setTextSize(30); |
110 canvas->drawText(text, strlen(text), 380, SkIntToScalar(y_of
fset), | 113 shaderPaint.getFontMetrics(&metrics); |
111 shaderPaint); | 114 y += -metrics.fAscent; |
112 y_offset += 32; | 115 canvas->drawText(text, strlen(text), 380, y, shaderPaint); |
| 116 y += metrics.fDescent + metrics.fLeading; |
113 } | 117 } |
114 } | 118 } |
115 } | 119 } |
116 | |
117 // setup work needed to draw text with different clips | 120 // setup work needed to draw text with different clips |
118 canvas->translate(10, 160); | 121 canvas->translate(10, savedY); |
119 paint.setTextSize(40); | 122 paint.setTextSize(40); |
120 | 123 |
121 // compute the bounds of the text | 124 // compute the bounds of the text |
122 SkRect bounds; | 125 SkRect bounds; |
123 paint.measureText(text, strlen(text), &bounds); | 126 paint.measureText(text, strlen(text), &bounds); |
124 | 127 |
125 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf; | 128 const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf; |
126 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf; | 129 const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf; |
127 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf; | 130 const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf; |
128 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf; | 131 const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf; |
129 | 132 |
130 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(), | 133 SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(), |
131 boundsHalfWidth, boundsHalfHeigh
t); | 134 boundsHalfWidth, boundsHalfHeigh
t); |
132 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.center
Y(), | 135 SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.center
Y(), |
133 boundsHalfWidth, boundsHalfHeig
ht); | 136 boundsHalfWidth, boundsHalfHeig
ht); |
134 SkRect interiorClip = bounds; | 137 SkRect interiorClip = bounds; |
135 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight); | 138 interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight); |
136 | 139 |
137 const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, inte
riorClip }; | 140 static const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightCli
p, interiorClip }; |
138 | 141 |
139 SkPaint clipHairline; | 142 SkPaint clipHairline; |
140 clipHairline.setColor(SK_ColorWHITE); | 143 clipHairline.setColor(SK_ColorWHITE); |
141 clipHairline.setStyle(SkPaint::kStroke_Style); | 144 clipHairline.setStyle(SkPaint::kStroke_Style); |
142 | 145 |
143 for (size_t x = 0; x < sizeof(clipRects) / sizeof(SkRect); ++x) { | 146 for (const SkRect& clipRect : clipRects) { |
| 147 canvas->translate(0, bounds.height()); |
144 canvas->save(); | 148 canvas->save(); |
145 canvas->drawRect(clipRects[x], clipHairline); | 149 canvas->drawRect(clipRect, clipHairline); |
146 paint.setAlpha(0x20); | 150 paint.setAlpha(0x20); |
147 canvas->drawText(text, strlen(text), 0, 0, paint); | 151 canvas->drawText(text, strlen(text), 0, 0, paint); |
148 canvas->clipRect(clipRects[x]); | 152 canvas->clipRect(clipRect); |
149 paint.setAlpha(0xFF); | 153 paint.setAlpha(0xFF); |
150 canvas->drawText(text, strlen(text), 0, 0, paint); | 154 canvas->drawText(text, strlen(text), 0, 0, paint); |
151 canvas->restore(); | 155 canvas->restore(); |
152 canvas->translate(0, bounds.height() + SkIntToScalar(25)); | 156 canvas->translate(0, SkIntToScalar(25)); |
153 } | 157 } |
154 } | 158 } |
155 | 159 |
156 typedef GM INHERITED; | 160 typedef GM INHERITED; |
157 }; | 161 }; |
158 | 162 |
159 ////////////////////////////////////////////////////////////////////////////// | 163 ////////////////////////////////////////////////////////////////////////////// |
160 | 164 |
161 DEF_GM(return new ColorEmojiGM;) | 165 DEF_GM(return new ColorEmojiGM;) |
162 | 166 |
163 } | 167 } |
OLD | NEW |