2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkGradientShader.h"
12 #include "SkTypeface.h"
14 static SkShader* make_heatGradient(const SkPoint pts[2]) {
16 const SkColor colors[] = {
17 SK_ColorBLACK, SK_ColorBLUE, SK_ColorCYAN, SK_ColorGREEN,
18 SK_ColorYELLOW, SK_ColorRED, SK_ColorWHITE
21 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE };
23 return SkGradientShader::CreateLinear(pts, bw, NULL,
25 SkShader::kClamp_TileMode);
28 static bool setFont(SkPaint* paint, const char name[]) {
29 SkTypeface* tf = sk_tool_utils::create_portable_typeface(name, SkTypeface::kNormal);
31 paint->setTypeface(tf)->unref();
37 #ifdef SK_BUILD_FOR_MAC
38 #import <ApplicationServices/ApplicationServices.h>
39 #define BITMAP_INFO_RGB (kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host)
41 static CGContextRef makeCG(const SkImageInfo& info, const void* addr,
43 if (kN32_SkColorType != info.colorType() || NULL == addr) {
46 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
47 CGContextRef cg = CGBitmapContextCreate((void*)addr, info.width(), info.height(),
48 8, rowBytes, space, BITMAP_INFO_RGB);
51 CGContextSetAllowsFontSubpixelQuantization(cg, false);
52 CGContextSetShouldSubpixelQuantizeFonts(cg, false);
57 extern CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face);
59 static CGFontRef typefaceToCGFont(const SkTypeface* face) {
64 CTFontRef ct = SkTypeface_GetCTFontRef(face);
65 return CTFontCopyGraphicsFont(ct, NULL);
68 static void cgSetPaintForText(CGContextRef cg, const SkPaint& paint) {
69 SkColor c = paint.getColor();
71 SkColorGetB(c) / 255.0f,
72 SkColorGetG(c) / 255.0f,
73 SkColorGetR(c) / 255.0f,
74 SkColorGetA(c) / 255.0f,
76 CGContextSetRGBFillColor(cg, rgba[0], rgba[1], rgba[2], rgba[3]);
78 CGContextSetTextDrawingMode(cg, kCGTextFill);
79 CGContextSetFont(cg, typefaceToCGFont(paint.getTypeface()));
80 CGContextSetFontSize(cg, SkScalarToFloat(paint.getTextSize()));
82 CGContextSetAllowsFontSubpixelPositioning(cg, paint.isSubpixelText());
83 CGContextSetShouldSubpixelPositionFonts(cg, paint.isSubpixelText());
85 CGContextSetShouldAntialias(cg, paint.isAntiAlias());
86 CGContextSetShouldSmoothFonts(cg, paint.isLCDRenderText());
89 static void cgDrawText(CGContextRef cg, const void* text, size_t len,
90 float x, float y, const SkPaint& paint) {
92 cgSetPaintForText(cg, paint);
95 int count = paint.textToGlyphs(text, len, glyphs);
97 CGContextShowGlyphsAtPoint(cg, x, y, glyphs, count);
103 Test a set of clipping problems discovered while writing blitAntiRect,
104 and test all the code paths through the clipping blitters.
105 Each region should show as a blue center surrounded by a 2px green
111 class GammaTextGM : public skiagm::GM {
118 virtual SkString onShortName() {
119 return SkString("gammatext");
122 virtual SkISize onISize() {
123 return SkISize::Make(1024, HEIGHT);
126 static void drawGrad(SkCanvas* canvas) {
127 SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } };
129 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE };
130 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
132 SkShader* s = make_heatGradient(pts);
135 canvas->clear(SK_ColorRED);
137 paint.setShader(s)->unref();
138 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) };
139 canvas->drawRect(r, paint);
142 virtual void onDraw(SkCanvas* canvas) {
143 #ifdef SK_BUILD_FOR_MAC
148 const void* addr = canvas->peekPixels(&info, &rowBytes);
150 cg = makeCG(info, addr, rowBytes);
157 const SkColor fg[] = {
159 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF,
160 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
164 const char* text = "Hamburgefons";
165 size_t len = strlen(text);
168 setFont(&paint, "Times");
169 paint.setTextSize(SkIntToScalar(16));
170 paint.setAntiAlias(true);
171 paint.setLCDRenderText(true);
173 SkScalar x = SkIntToScalar(10);
174 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) {
175 paint.setColor(fg[i]);
177 SkScalar y = SkIntToScalar(40);
178 SkScalar stopy = SkIntToScalar(HEIGHT);
181 canvas->drawText(text, len, x, y, paint);
183 #ifdef SK_BUILD_FOR_MAC
185 cgDrawText(cg, text, len, SkScalarToFloat(x),
186 static_cast<float>(HEIGHT) - SkScalarToFloat(y),
190 y += paint.getTextSize() * 2;
192 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg);
194 #ifdef SK_BUILD_FOR_MAC
195 CGContextRelease(cg);
200 typedef skiagm::GM INHERITED;
203 DEF_GM( return new GammaTextGM; )
205 //////////////////////////////////////////////////////////////////////////////
207 static SkShader* make_gradient(SkColor c) {
208 const SkPoint pts[] = { { 0, 0 }, { 240, 0 } };
211 colors[1] = SkColorSetA(c, 0);
212 return SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode);
215 static void set_face(SkPaint* paint) {
216 SkTypeface* face = SkTypeface::CreateFromName("serif", SkTypeface::kItalic);
217 SkSafeUnref(paint->setTypeface(face));
220 static void draw_pair(SkCanvas* canvas, SkPaint* paint, SkShader* shader) {
221 const char text[] = "Now is the time for all good";
222 const size_t len = strlen(text);
224 paint->setShader(NULL);
225 canvas->drawText(text, len, 10, 20, *paint);
226 paint->setShader(SkShader::CreateColorShader(paint->getColor()))->unref();
227 canvas->drawText(text, len, 10, 40, *paint);
228 paint->setShader(shader);
229 canvas->drawText(text, len, 10, 60, *paint);
232 class GammaShaderTextGM : public skiagm::GM {
233 SkShader* fShaders[3];
237 GammaShaderTextGM() {
238 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE };
239 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
241 fColors[i] = colors[i];
245 virtual ~GammaShaderTextGM() {
246 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
247 SkSafeUnref(fShaders[i]);
252 virtual SkString onShortName() {
253 return SkString("gammagradienttext");
256 virtual SkISize onISize() {
257 return SkISize::Make(300, 300);
260 virtual void onOnceBeforeDraw() {
261 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
262 fShaders[i] = make_gradient(fColors[i]);
266 virtual void onDraw(SkCanvas* canvas) {
268 paint.setAntiAlias(true);
269 paint.setLCDRenderText(true);
270 paint.setTextSize(18);
273 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) {
274 paint.setColor(fColors[i]);
275 draw_pair(canvas, &paint, fShaders[i]);
276 canvas->translate(0, 80);
281 typedef skiagm::GM INHERITED;
284 DEF_GM( return new GammaShaderTextGM; )