OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2011 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 #include "gm.h" |
| 8 #include "SkSurface.h" |
| 9 #include "SkTypeface.h" |
| 10 |
| 11 namespace skiagm { |
| 12 |
| 13 class DFTextGM : public GM { |
| 14 public: |
| 15 DFTextGM() { |
| 16 this->setBGColor(0xFFFFFFFF); |
| 17 } |
| 18 |
| 19 virtual ~DFTextGM() { |
| 20 } |
| 21 |
| 22 protected: |
| 23 virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 24 return kGPUOnly_Flag; |
| 25 } |
| 26 |
| 27 virtual SkString onShortName() { |
| 28 return SkString("dftext"); |
| 29 } |
| 30 |
| 31 virtual SkISize onISize() { |
| 32 return SkISize::Make(1024, 768); |
| 33 } |
| 34 |
| 35 static void rotate_about(SkCanvas* canvas, |
| 36 SkScalar degrees, |
| 37 SkScalar px, SkScalar py) { |
| 38 canvas->translate(px, py); |
| 39 canvas->rotate(degrees); |
| 40 canvas->translate(-px, -py); |
| 41 } |
| 42 |
| 43 virtual void onDraw(SkCanvas* inputCanvas) { |
| 44 SkScalar textSizes[] = { 11.0f, 11.0f*2.0f, 11.0f*5.0f, 11.0f*2.0f*5.0f
}; |
| 45 SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f }; |
| 46 |
| 47 // set up offscreen rendering with distance field text |
| 48 #if SK_SUPPORT_GPU |
| 49 GrContext* ctx = inputCanvas->getGrContext(); |
| 50 SkImageInfo info = SkImageInfo::MakeN32Premul(onISize()); |
| 51 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, info, 0, |
| 52 SkSurface::kDistanceFiel
d_TextRenderMode)); |
| 53 SkCanvas* canvas = surface->getCanvas(); |
| 54 #else |
| 55 SkCanvas* canvas = inputCanvas; |
| 56 #endif |
| 57 canvas->clear(0xffffffff); |
| 58 |
| 59 SkPaint paint; |
| 60 paint.setAntiAlias(true); |
| 61 paint.setSubpixelText(true); |
| 62 #if !SK_SUPPORT_GPU |
| 63 paint.setDistanceFieldTextTEMP(true); |
| 64 #endif |
| 65 sk_tool_utils::set_portable_typeface(&paint, "Times New Roman", SkTypefa
ce::kNormal); |
| 66 |
| 67 const char* text = "Hamburgefons"; |
| 68 const size_t textLen = strlen(text); |
| 69 |
| 70 // check scaling up |
| 71 SkScalar x = SkIntToScalar(0); |
| 72 SkScalar y = SkIntToScalar(78); |
| 73 for (size_t i = 0; i < SK_ARRAY_COUNT(textSizes); ++i) { |
| 74 SkAutoCanvasRestore acr(canvas, true); |
| 75 canvas->translate(x, y); |
| 76 canvas->scale(scales[i], scales[i]); |
| 77 paint.setTextSize(textSizes[i]); |
| 78 canvas->drawText(text, textLen, 0, 0, paint); |
| 79 y += paint.getFontMetrics(NULL)*scales[i]; |
| 80 } |
| 81 |
| 82 // check rotation |
| 83 for (size_t i = 0; i < 5; ++i) { |
| 84 SkScalar rotX = SkIntToScalar(10); |
| 85 SkScalar rotY = y; |
| 86 |
| 87 SkAutoCanvasRestore acr(canvas, true); |
| 88 canvas->translate(SkIntToScalar(10 + i * 200), -80); |
| 89 rotate_about(canvas, SkIntToScalar(i * 5), rotX, rotY); |
| 90 for (int ps = 6; ps <= 32; ps += 3) { |
| 91 paint.setTextSize(SkIntToScalar(ps)); |
| 92 canvas->drawText(text, textLen, rotX, rotY, paint); |
| 93 rotY += paint.getFontMetrics(NULL); |
| 94 } |
| 95 } |
| 96 |
| 97 // check scaling down |
| 98 paint.setLCDRenderText(true); |
| 99 x = SkIntToScalar(700); |
| 100 y = SkIntToScalar(20); |
| 101 size_t arraySize = SK_ARRAY_COUNT(textSizes); |
| 102 for (size_t i = 0; i < arraySize; ++i) { |
| 103 SkAutoCanvasRestore acr(canvas, true); |
| 104 canvas->translate(x, y); |
| 105 SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]); |
| 106 canvas->scale(scaleFactor, scaleFactor); |
| 107 paint.setTextSize(textSizes[i]); |
| 108 canvas->drawText(text, textLen, 0, 0, paint); |
| 109 y += paint.getFontMetrics(NULL)*scaleFactor; |
| 110 } |
| 111 |
| 112 // check gamma-corrected blending |
| 113 const SkColor fg[] = { |
| 114 0xFFFFFFFF, |
| 115 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
| 116 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
| 117 0xFF000000, |
| 118 }; |
| 119 |
| 120 paint.setColor(0xFFF1F1F1); |
| 121 SkRect r = SkRect::MakeLTRB(690, 250, 840, 460); |
| 122 canvas->drawRect(r, paint); |
| 123 |
| 124 x = SkIntToScalar(700); |
| 125 y = SkIntToScalar(270); |
| 126 paint.setTextSize(SkIntToScalar(22)); |
| 127 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 128 paint.setColor(fg[i]); |
| 129 |
| 130 canvas->drawText(text, textLen, x, y, paint); |
| 131 y += paint.getFontMetrics(NULL); |
| 132 } |
| 133 |
| 134 paint.setColor(0xFF1F1F1F); |
| 135 r = SkRect::MakeLTRB(840, 250, 990, 460); |
| 136 canvas->drawRect(r, paint); |
| 137 |
| 138 x = SkIntToScalar(850); |
| 139 y = SkIntToScalar(270); |
| 140 paint.setTextSize(SkIntToScalar(22)); |
| 141 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 142 paint.setColor(fg[i]); |
| 143 |
| 144 canvas->drawText(text, textLen, x, y, paint); |
| 145 y += paint.getFontMetrics(NULL); |
| 146 } |
| 147 |
| 148 #if SK_SUPPORT_GPU |
| 149 // render offscreen buffer |
| 150 SkImage* image = surface->newImageSnapshot(); |
| 151 image->draw(inputCanvas, 0, 0, NULL); |
| 152 image->unref(); |
| 153 #endif |
| 154 } |
| 155 |
| 156 private: |
| 157 typedef GM INHERITED; |
| 158 }; |
| 159 |
| 160 ////////////////////////////////////////////////////////////////////////////// |
| 161 |
| 162 static GM* MyFactory(void*) { return new DFTextGM; } |
| 163 static GMRegistry reg(MyFactory); |
| 164 |
| 165 } |
OLD | NEW |