| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkPath.h" | 10 #include "SkPath.h" |
| 11 #include "SkGradientShader.h" | 11 #include "SkGradientShader.h" |
| 12 #include "SkTypeface.h" | 12 #include "SkTypeface.h" |
| 13 | 13 |
| 14 static SkShader* make_heatGradient(const SkPoint pts[2]) { | 14 static SkShader* make_heatGradient(const SkPoint pts[2]) { |
| 15 #if 0 // UNUSED | |
| 16 const SkColor colors[] = { | |
| 17 SK_ColorBLACK, SK_ColorBLUE, SK_ColorCYAN, SK_ColorGREEN, | |
| 18 SK_ColorYELLOW, SK_ColorRED, SK_ColorWHITE | |
| 19 }; | |
| 20 #endif | |
| 21 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE }; | 15 const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE }; |
| 22 | 16 |
| 23 return SkGradientShader::CreateLinear(pts, bw, NULL, | 17 return SkGradientShader::CreateLinear(pts, bw, NULL, |
| 24 SK_ARRAY_COUNT(bw), | 18 SK_ARRAY_COUNT(bw), |
| 25 SkShader::kClamp_TileMode); | 19 SkShader::kClamp_TileMode); |
| 26 } | 20 } |
| 27 | 21 |
| 28 static bool setFont(SkPaint* paint, const char name[]) { | 22 static bool setFont(SkPaint* paint, const char name[]) { |
| 29 SkTypeface* tf = sk_tool_utils::create_portable_typeface(name, SkTypeface::k
Normal); | 23 SkTypeface* tf = sk_tool_utils::create_portable_typeface(name, SkTypeface::k
Normal); |
| 30 if (tf) { | 24 if (tf) { |
| 31 paint->setTypeface(tf)->unref(); | 25 paint->setTypeface(tf)->unref(); |
| 32 return true; | 26 return true; |
| 33 } | 27 } |
| 34 return false; | 28 return false; |
| 35 } | 29 } |
| 36 | 30 |
| 37 #ifdef SK_BUILD_FOR_MAC | |
| 38 #import <ApplicationServices/ApplicationServices.h> | |
| 39 #define BITMAP_INFO_RGB (kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Ho
st) | |
| 40 | |
| 41 static CGContextRef makeCG(const SkImageInfo& info, const void* addr, | |
| 42 size_t rowBytes) { | |
| 43 if (kN32_SkColorType != info.colorType() || NULL == addr) { | |
| 44 return NULL; | |
| 45 } | |
| 46 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); | |
| 47 CGContextRef cg = CGBitmapContextCreate((void*)addr, info.width(), info.heig
ht(), | |
| 48 8, rowBytes, space, BITMAP_INFO_RGB)
; | |
| 49 CFRelease(space); | |
| 50 | |
| 51 CGContextSetAllowsFontSubpixelQuantization(cg, false); | |
| 52 CGContextSetShouldSubpixelQuantizeFonts(cg, false); | |
| 53 | |
| 54 return cg; | |
| 55 } | |
| 56 | |
| 57 extern CTFontRef SkTypeface_GetCTFontRef(const SkTypeface* face); | |
| 58 | |
| 59 static CGFontRef typefaceToCGFont(const SkTypeface* face) { | |
| 60 if (NULL == face) { | |
| 61 return 0; | |
| 62 } | |
| 63 | |
| 64 CTFontRef ct = SkTypeface_GetCTFontRef(face); | |
| 65 return CTFontCopyGraphicsFont(ct, NULL); | |
| 66 } | |
| 67 | |
| 68 static void cgSetPaintForText(CGContextRef cg, const SkPaint& paint) { | |
| 69 SkColor c = paint.getColor(); | |
| 70 CGFloat rgba[] = { | |
| 71 SkColorGetB(c) / 255.0f, | |
| 72 SkColorGetG(c) / 255.0f, | |
| 73 SkColorGetR(c) / 255.0f, | |
| 74 SkColorGetA(c) / 255.0f, | |
| 75 }; | |
| 76 CGContextSetRGBFillColor(cg, rgba[0], rgba[1], rgba[2], rgba[3]); | |
| 77 | |
| 78 CGContextSetTextDrawingMode(cg, kCGTextFill); | |
| 79 CGContextSetFont(cg, typefaceToCGFont(paint.getTypeface())); | |
| 80 CGContextSetFontSize(cg, SkScalarToFloat(paint.getTextSize())); | |
| 81 | |
| 82 CGContextSetAllowsFontSubpixelPositioning(cg, paint.isSubpixelText()); | |
| 83 CGContextSetShouldSubpixelPositionFonts(cg, paint.isSubpixelText()); | |
| 84 | |
| 85 CGContextSetShouldAntialias(cg, paint.isAntiAlias()); | |
| 86 CGContextSetShouldSmoothFonts(cg, paint.isLCDRenderText()); | |
| 87 } | |
| 88 | |
| 89 static void cgDrawText(CGContextRef cg, const void* text, size_t len, | |
| 90 float x, float y, const SkPaint& paint) { | |
| 91 if (cg) { | |
| 92 cgSetPaintForText(cg, paint); | |
| 93 | |
| 94 uint16_t glyphs[200]; | |
| 95 int count = paint.textToGlyphs(text, len, glyphs); | |
| 96 | |
| 97 CGContextShowGlyphsAtPoint(cg, x, y, glyphs, count); | |
| 98 } | |
| 99 } | |
| 100 #endif | |
| 101 | |
| 102 /** | 31 /** |
| 103 Test a set of clipping problems discovered while writing blitAntiRect, | 32 Test a set of clipping problems discovered while writing blitAntiRect, |
| 104 and test all the code paths through the clipping blitters. | 33 and test all the code paths through the clipping blitters. |
| 105 Each region should show as a blue center surrounded by a 2px green | 34 Each region should show as a blue center surrounded by a 2px green |
| 106 border, with no red. | 35 border, with no red. |
| 107 */ | 36 */ |
| 108 | 37 |
| 109 #define HEIGHT 480 | 38 #define HEIGHT 480 |
| 110 | 39 |
| 111 class GammaTextGM : public skiagm::GM { | 40 class GammaTextGM : public skiagm::GM { |
| 112 public: | |
| 113 GammaTextGM() { | |
| 114 | |
| 115 } | |
| 116 | |
| 117 protected: | 41 protected: |
| 118 virtual SkString onShortName() { | 42 SkString onShortName() SK_OVERRIDE { |
| 119 return SkString("gammatext"); | 43 return SkString("gammatext"); |
| 120 } | 44 } |
| 121 | 45 |
| 122 virtual SkISize onISize() { | 46 SkISize onISize() SK_OVERRIDE { |
| 123 return SkISize::Make(1024, HEIGHT); | 47 return SkISize::Make(1024, HEIGHT); |
| 124 } | 48 } |
| 125 | 49 |
| 126 static void drawGrad(SkCanvas* canvas) { | 50 static void drawGrad(SkCanvas* canvas) { |
| 127 SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } }; | 51 SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } }; |
| 128 #if 0 | |
| 129 const SkColor colors[] = { SK_ColorBLACK, SK_ColorWHITE }; | |
| 130 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkSha
der::kClamp_TileMode); | |
| 131 #else | |
| 132 SkShader* s = make_heatGradient(pts); | 52 SkShader* s = make_heatGradient(pts); |
| 133 #endif | |
| 134 | 53 |
| 135 canvas->clear(SK_ColorRED); | 54 canvas->clear(SK_ColorRED); |
| 136 SkPaint paint; | 55 SkPaint paint; |
| 137 paint.setShader(s)->unref(); | 56 paint.setShader(s)->unref(); |
| 138 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) }; | 57 SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) }; |
| 139 canvas->drawRect(r, paint); | 58 canvas->drawRect(r, paint); |
| 140 } | 59 } |
| 141 | 60 |
| 142 virtual void onDraw(SkCanvas* canvas) { | 61 void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 143 #ifdef SK_BUILD_FOR_MAC | |
| 144 CGContextRef cg = 0; | |
| 145 { | |
| 146 SkImageInfo info; | |
| 147 size_t rowBytes; | |
| 148 const void* addr = canvas->peekPixels(&info, &rowBytes); | |
| 149 if (addr) { | |
| 150 cg = makeCG(info, addr, rowBytes); | |
| 151 } | |
| 152 } | |
| 153 #endif | |
| 154 | |
| 155 drawGrad(canvas); | 62 drawGrad(canvas); |
| 156 | 63 |
| 157 const SkColor fg[] = { | 64 const SkColor fg[] = { |
| 158 0xFFFFFFFF, | 65 0xFFFFFFFF, |
| 159 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, | 66 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
| 160 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, | 67 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
| 161 0xFF000000, | 68 0xFF000000, |
| 162 }; | 69 }; |
| 163 | 70 |
| 164 const char* text = "Hamburgefons"; | 71 const char* text = "Hamburgefons"; |
| 165 size_t len = strlen(text); | 72 size_t len = strlen(text); |
| 166 | 73 |
| 167 SkPaint paint; | 74 SkPaint paint; |
| 168 setFont(&paint, "Times"); | 75 setFont(&paint, "Times"); |
| 169 paint.setTextSize(SkIntToScalar(16)); | 76 paint.setTextSize(SkIntToScalar(16)); |
| 170 paint.setAntiAlias(true); | 77 paint.setAntiAlias(true); |
| 171 paint.setLCDRenderText(true); | 78 paint.setLCDRenderText(true); |
| 172 | 79 |
| 173 SkScalar x = SkIntToScalar(10); | 80 SkScalar x = SkIntToScalar(10); |
| 174 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { | 81 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 175 paint.setColor(fg[i]); | 82 paint.setColor(fg[i]); |
| 176 | 83 |
| 177 SkScalar y = SkIntToScalar(40); | 84 SkScalar y = SkIntToScalar(40); |
| 178 SkScalar stopy = SkIntToScalar(HEIGHT); | 85 SkScalar stopy = SkIntToScalar(HEIGHT); |
| 179 while (y < stopy) { | 86 while (y < stopy) { |
| 180 if (true) { | 87 canvas->drawText(text, len, x, y, paint); |
| 181 canvas->drawText(text, len, x, y, paint); | |
| 182 } | |
| 183 #ifdef SK_BUILD_FOR_MAC | |
| 184 else { | |
| 185 cgDrawText(cg, text, len, SkScalarToFloat(x), | |
| 186 static_cast<float>(HEIGHT) - SkScalarToFloat(y), | |
| 187 paint); | |
| 188 } | |
| 189 #endif | |
| 190 y += paint.getTextSize() * 2; | 88 y += paint.getTextSize() * 2; |
| 191 } | 89 } |
| 192 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg); | 90 x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg); |
| 193 } | 91 } |
| 194 #ifdef SK_BUILD_FOR_MAC | |
| 195 CGContextRelease(cg); | |
| 196 #endif | |
| 197 } | 92 } |
| 198 | 93 |
| 199 private: | 94 private: |
| 200 typedef skiagm::GM INHERITED; | 95 typedef skiagm::GM INHERITED; |
| 201 }; | 96 }; |
| 202 | 97 |
| 203 DEF_GM( return new GammaTextGM; ) | 98 DEF_GM( return new GammaTextGM; ) |
| 204 | 99 |
| 205 ////////////////////////////////////////////////////////////////////////////// | 100 ////////////////////////////////////////////////////////////////////////////// |
| 206 | 101 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 235 | 130 |
| 236 public: | 131 public: |
| 237 GammaShaderTextGM() { | 132 GammaShaderTextGM() { |
| 238 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE }; | 133 const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE }; |
| 239 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { | 134 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 240 fShaders[i] = NULL; | 135 fShaders[i] = NULL; |
| 241 fColors[i] = colors[i]; | 136 fColors[i] = colors[i]; |
| 242 } | 137 } |
| 243 } | 138 } |
| 244 | 139 |
| 245 virtual ~GammaShaderTextGM() { | 140 virtual ~GammaShaderTextGM() SK_OVERRIDE { |
| 246 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { | 141 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 247 SkSafeUnref(fShaders[i]); | 142 SkSafeUnref(fShaders[i]); |
| 248 } | 143 } |
| 249 } | 144 } |
| 250 | 145 |
| 251 protected: | 146 protected: |
| 252 virtual SkString onShortName() { | 147 SkString onShortName() SK_OVERRIDE { |
| 253 return SkString("gammagradienttext"); | 148 return SkString("gammagradienttext"); |
| 254 } | 149 } |
| 255 | 150 |
| 256 virtual SkISize onISize() { | 151 SkISize onISize() SK_OVERRIDE { |
| 257 return SkISize::Make(300, 300); | 152 return SkISize::Make(300, 300); |
| 258 } | 153 } |
| 259 | 154 |
| 260 virtual void onOnceBeforeDraw() { | 155 void onOnceBeforeDraw() SK_OVERRIDE { |
| 261 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { | 156 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 262 fShaders[i] = make_gradient(fColors[i]); | 157 fShaders[i] = make_gradient(fColors[i]); |
| 263 } | 158 } |
| 264 } | 159 } |
| 265 | 160 |
| 266 virtual void onDraw(SkCanvas* canvas) { | 161 void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 267 SkPaint paint; | 162 SkPaint paint; |
| 268 paint.setAntiAlias(true); | 163 paint.setAntiAlias(true); |
| 269 paint.setLCDRenderText(true); | 164 paint.setLCDRenderText(true); |
| 270 paint.setTextSize(18); | 165 paint.setTextSize(18); |
| 271 set_face(&paint); | 166 set_face(&paint); |
| 272 | 167 |
| 273 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { | 168 for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 274 paint.setColor(fColors[i]); | 169 paint.setColor(fColors[i]); |
| 275 draw_pair(canvas, &paint, fShaders[i]); | 170 draw_pair(canvas, &paint, fShaders[i]); |
| 276 canvas->translate(0, 80); | 171 canvas->translate(0, 80); |
| 277 } | 172 } |
| 278 } | 173 } |
| 279 | 174 |
| 280 private: | 175 private: |
| 281 typedef skiagm::GM INHERITED; | 176 typedef skiagm::GM INHERITED; |
| 282 }; | 177 }; |
| 283 | 178 |
| 284 DEF_GM( return new GammaShaderTextGM; ) | 179 DEF_GM( return new GammaShaderTextGM; ) |
| 285 | 180 |
| OLD | NEW |