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 #include "gm.h" | 7 #include "gm.h" |
8 #include "SkSurface.h" | 8 #include "SkSurface.h" |
9 #include "SkTypeface.h" | 9 #include "SkTypeface.h" |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 SkScalar textSizes[] = { 11.0f, 11.0f*2.0f, 11.0f*5.0f, 11.0f*2.0f*5.0f
}; | 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 }; | 45 SkScalar scales[] = { 2.0f*5.0f, 5.0f, 2.0f, 1.0f }; |
46 | 46 |
47 // set up offscreen rendering with distance field text | 47 // set up offscreen rendering with distance field text |
48 #if SK_SUPPORT_GPU | 48 #if SK_SUPPORT_GPU |
49 GrContext* ctx = inputCanvas->getGrContext(); | 49 GrContext* ctx = inputCanvas->getGrContext(); |
50 SkImageInfo info = SkImageInfo::MakeN32Premul(onISize()); | 50 SkImageInfo info = SkImageInfo::MakeN32Premul(onISize()); |
51 SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, | 51 SkSurfaceProps props(SkSurfaceProps::kUseDistanceFieldFonts_Flag, |
52 SkSurfaceProps::kLegacyFontHost_InitType); | 52 SkSurfaceProps::kLegacyFontHost_InitType); |
53 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, info, 0,
&props)); | 53 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, info, 0,
&props)); |
54 SkCanvas* canvas = surface->getCanvas(); | 54 SkCanvas* canvas = surface.get() ? surface->getCanvas() : inputCanvas; |
55 #else | 55 #else |
56 SkCanvas* canvas = inputCanvas; | 56 SkCanvas* canvas = inputCanvas; |
57 #endif | 57 #endif |
| 58 |
| 59 // apply global scale to test glyph positioning |
| 60 canvas->scale(1.05f, 1.05f); |
58 canvas->clear(0xffffffff); | 61 canvas->clear(0xffffffff); |
59 | 62 |
60 SkPaint paint; | 63 SkPaint paint; |
61 paint.setAntiAlias(true); | 64 paint.setAntiAlias(true); |
62 paint.setSubpixelText(true); | 65 paint.setSubpixelText(true); |
63 #if !SK_SUPPORT_GPU | 66 #if !SK_SUPPORT_GPU |
64 paint.setDistanceFieldTextTEMP(true); | 67 paint.setDistanceFieldTextTEMP(true); |
65 #endif | 68 #endif |
66 sk_tool_utils::set_portable_typeface(&paint, "Times New Roman", SkTypefa
ce::kNormal); | 69 sk_tool_utils::set_portable_typeface(&paint, "Times New Roman", SkTypefa
ce::kNormal); |
67 | 70 |
(...skipping 22 matching lines...) Expand all Loading... |
90 rotate_about(canvas, SkIntToScalar(i * 5), rotX, rotY); | 93 rotate_about(canvas, SkIntToScalar(i * 5), rotX, rotY); |
91 for (int ps = 6; ps <= 32; ps += 3) { | 94 for (int ps = 6; ps <= 32; ps += 3) { |
92 paint.setTextSize(SkIntToScalar(ps)); | 95 paint.setTextSize(SkIntToScalar(ps)); |
93 canvas->drawText(text, textLen, rotX, rotY, paint); | 96 canvas->drawText(text, textLen, rotX, rotY, paint); |
94 rotY += paint.getFontMetrics(NULL); | 97 rotY += paint.getFontMetrics(NULL); |
95 } | 98 } |
96 } | 99 } |
97 | 100 |
98 // check scaling down | 101 // check scaling down |
99 paint.setLCDRenderText(true); | 102 paint.setLCDRenderText(true); |
100 x = SkIntToScalar(700); | 103 x = SkIntToScalar(680); |
101 y = SkIntToScalar(20); | 104 y = SkIntToScalar(20); |
102 size_t arraySize = SK_ARRAY_COUNT(textSizes); | 105 size_t arraySize = SK_ARRAY_COUNT(textSizes); |
103 for (size_t i = 0; i < arraySize; ++i) { | 106 for (size_t i = 0; i < arraySize; ++i) { |
104 SkAutoCanvasRestore acr(canvas, true); | 107 SkAutoCanvasRestore acr(canvas, true); |
105 canvas->translate(x, y); | 108 canvas->translate(x, y); |
106 SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]); | 109 SkScalar scaleFactor = SkScalarInvert(scales[arraySize - i - 1]); |
107 canvas->scale(scaleFactor, scaleFactor); | 110 canvas->scale(scaleFactor, scaleFactor); |
108 paint.setTextSize(textSizes[i]); | 111 paint.setTextSize(textSizes[i]); |
109 canvas->drawText(text, textLen, 0, 0, paint); | 112 canvas->drawText(text, textLen, 0, 0, paint); |
110 y += paint.getFontMetrics(NULL)*scaleFactor; | 113 y += paint.getFontMetrics(NULL)*scaleFactor; |
111 } | 114 } |
112 | 115 |
| 116 // check pos text |
| 117 { |
| 118 SkAutoCanvasRestore acr(canvas, true); |
| 119 |
| 120 canvas->scale(2.0f, 2.0f); |
| 121 |
| 122 SkAutoTArray<SkPoint> pos(textLen); |
| 123 SkAutoTArray<SkScalar> widths(textLen); |
| 124 paint.setTextSize(textSizes[0]); |
| 125 |
| 126 paint.getTextWidths(text, textLen, &widths[0]); |
| 127 |
| 128 SkScalar x = SkIntToScalar(340); |
| 129 SkScalar y = SkIntToScalar(75); |
| 130 for (unsigned int i = 0; i < textLen; ++i) { |
| 131 pos[i].set(x, y); |
| 132 x += widths[i]; |
| 133 } |
| 134 |
| 135 canvas->drawPosText(text, textLen, &pos[0], paint); |
| 136 } |
| 137 |
| 138 |
113 // check gamma-corrected blending | 139 // check gamma-corrected blending |
114 const SkColor fg[] = { | 140 const SkColor fg[] = { |
115 0xFFFFFFFF, | 141 0xFFFFFFFF, |
116 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, | 142 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
117 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, | 143 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
118 0xFF000000, | 144 0xFF000000, |
119 }; | 145 }; |
120 | 146 |
121 paint.setColor(0xFFF1F1F1); | 147 paint.setColor(0xFFF1F1F1); |
122 SkRect r = SkRect::MakeLTRB(690, 250, 840, 460); | 148 SkRect r = SkRect::MakeLTRB(670, 250, 820, 460); |
123 canvas->drawRect(r, paint); | 149 canvas->drawRect(r, paint); |
124 | 150 |
125 x = SkIntToScalar(700); | 151 x = SkIntToScalar(680); |
126 y = SkIntToScalar(270); | 152 y = SkIntToScalar(270); |
127 paint.setTextSize(SkIntToScalar(22)); | 153 paint.setTextSize(SkIntToScalar(22)); |
128 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { | 154 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
129 paint.setColor(fg[i]); | 155 paint.setColor(fg[i]); |
130 | 156 |
131 canvas->drawText(text, textLen, x, y, paint); | 157 canvas->drawText(text, textLen, x, y, paint); |
132 y += paint.getFontMetrics(NULL); | 158 y += paint.getFontMetrics(NULL); |
133 } | 159 } |
134 | 160 |
135 paint.setColor(0xFF1F1F1F); | 161 paint.setColor(0xFF1F1F1F); |
136 r = SkRect::MakeLTRB(840, 250, 990, 460); | 162 r = SkRect::MakeLTRB(820, 250, 970, 460); |
137 canvas->drawRect(r, paint); | 163 canvas->drawRect(r, paint); |
138 | 164 |
139 x = SkIntToScalar(850); | 165 x = SkIntToScalar(830); |
140 y = SkIntToScalar(270); | 166 y = SkIntToScalar(270); |
141 paint.setTextSize(SkIntToScalar(22)); | 167 paint.setTextSize(SkIntToScalar(22)); |
142 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { | 168 for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
143 paint.setColor(fg[i]); | 169 paint.setColor(fg[i]); |
144 | 170 |
145 canvas->drawText(text, textLen, x, y, paint); | 171 canvas->drawText(text, textLen, x, y, paint); |
146 y += paint.getFontMetrics(NULL); | 172 y += paint.getFontMetrics(NULL); |
147 } | 173 } |
148 | 174 |
149 #if SK_SUPPORT_GPU | 175 #if SK_SUPPORT_GPU |
150 // render offscreen buffer | 176 // render offscreen buffer |
151 SkImage* image = surface->newImageSnapshot(); | 177 if (surface) { |
152 image->draw(inputCanvas, 0, 0, NULL); | 178 SkImage* image = surface->newImageSnapshot(); |
153 image->unref(); | 179 image->draw(inputCanvas, 0, 0, NULL); |
| 180 image->unref(); |
| 181 } |
154 #endif | 182 #endif |
155 } | 183 } |
156 | 184 |
157 private: | 185 private: |
158 typedef GM INHERITED; | 186 typedef GM INHERITED; |
159 }; | 187 }; |
160 | 188 |
161 ////////////////////////////////////////////////////////////////////////////// | 189 ////////////////////////////////////////////////////////////////////////////// |
162 | 190 |
163 static GM* MyFactory(void*) { return new DFTextGM; } | 191 static GM* MyFactory(void*) { return new DFTextGM; } |
164 static GMRegistry reg(MyFactory); | 192 static GMRegistry reg(MyFactory); |
165 | 193 |
166 } | 194 } |
OLD | NEW |