reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkFontMgr.h" |
| 11 | #include "SkGraphics.h" |
| 12 | #include "SkTypeface.h" |
| 13 | |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 14 | #ifdef SK_BUILD_FOR_WIN |
reed@google.com | d1bcfc9 | 2013-08-28 20:31:58 +0000 | [diff] [blame] | 15 | #include "SkTypeface_win.h" |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 16 | #endif |
| 17 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 18 | // limit this just so we don't take too long to draw |
| 19 | #define MAX_FAMILIES 30 |
| 20 | |
| 21 | static SkScalar drawString(SkCanvas* canvas, const SkString& text, SkScalar x, |
| 22 | SkScalar y, const SkPaint& paint) { |
| 23 | canvas->drawText(text.c_str(), text.size(), x, y, paint); |
| 24 | return x + paint.measureText(text.c_str(), text.size()); |
| 25 | } |
| 26 | |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 27 | static SkScalar drawCharacter(SkCanvas* canvas, uint32_t character, SkScalar x, |
| 28 | SkScalar y, SkPaint& paint, SkFontMgr* fm, |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 29 | const char* fontName, const char* bcp47[], int bcp47Count, |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 30 | const SkFontStyle& fontStyle) { |
| 31 | // find typeface containing the requested character and draw it |
| 32 | SkString ch; |
| 33 | ch.appendUnichar(character); |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 34 | SkTypeface* typeface = fm->matchFamilyStyleCharacter(fontName, fontStyle, |
| 35 | bcp47, bcp47Count, character); |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 36 | SkSafeUnref(paint.setTypeface(typeface)); |
| 37 | x = drawString(canvas, ch, x, y, paint) + 20; |
| 38 | |
| 39 | if (NULL == typeface) { |
| 40 | return x; |
| 41 | } |
| 42 | |
| 43 | // repeat the process, but this time use the family name of the typeface |
| 44 | // from the first pass. This emulates the behavior in Blink where it |
| 45 | // it expects to get the same glyph when following this pattern. |
| 46 | SkString familyName; |
| 47 | typeface->getFamilyName(&familyName); |
| 48 | SkTypeface* typefaceCopy = fm->legacyCreateTypeface(familyName.c_str(), typeface->style()); |
| 49 | SkSafeUnref(paint.setTypeface(typefaceCopy)); |
| 50 | return drawString(canvas, ch, x, y, paint) + 20; |
| 51 | } |
| 52 | |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 53 | static const char* zh = "zh"; |
| 54 | static const char* ja = "ja"; |
| 55 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 56 | class FontMgrGM : public skiagm::GM { |
| 57 | public: |
bungeman@google.com | 6eddc77 | 2014-03-31 19:18:07 +0000 | [diff] [blame] | 58 | FontMgrGM(SkFontMgr* fontMgr = NULL) { |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 59 | SkGraphics::SetFontCacheLimit(16 * 1024 * 1024); |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 60 | |
| 61 | fName.set("fontmgr_iter"); |
bungeman@google.com | 6eddc77 | 2014-03-31 19:18:07 +0000 | [diff] [blame] | 62 | if (fontMgr) { |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 63 | fName.append("_factory"); |
bungeman@google.com | 6eddc77 | 2014-03-31 19:18:07 +0000 | [diff] [blame] | 64 | fFM.reset(fontMgr); |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 65 | } else { |
| 66 | fFM.reset(SkFontMgr::RefDefault()); |
| 67 | } |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 71 | SkString onShortName() SK_OVERRIDE { |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 72 | return fName; |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 73 | } |
| 74 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 75 | SkISize onISize() SK_OVERRIDE { |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 76 | return SkISize::Make(1536, 768); |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 77 | } |
| 78 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 79 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 80 | SkScalar y = 20; |
| 81 | SkPaint paint; |
| 82 | paint.setAntiAlias(true); |
| 83 | paint.setLCDRenderText(true); |
| 84 | paint.setSubpixelText(true); |
| 85 | paint.setTextSize(17); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 86 | |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 87 | SkFontMgr* fm = fFM; |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 88 | int count = SkMin32(fm->countFamilies(), MAX_FAMILIES); |
| 89 | |
| 90 | for (int i = 0; i < count; ++i) { |
| 91 | SkString fname; |
| 92 | fm->getFamilyName(i, &fname); |
| 93 | paint.setTypeface(NULL); |
| 94 | (void)drawString(canvas, fname, 20, y, paint); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 95 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 96 | SkScalar x = 220; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 97 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 98 | SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i)); |
| 99 | for (int j = 0; j < set->count(); ++j) { |
| 100 | SkString sname; |
| 101 | SkFontStyle fs; |
| 102 | set->getStyle(j, &fs, &sname); |
reed@google.com | 5111648 | 2013-04-24 19:14:39 +0000 | [diff] [blame] | 103 | sname.appendf(" [%d %d %d]", fs.weight(), fs.width(), fs.isItalic()); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 104 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 105 | SkSafeUnref(paint.setTypeface(set->createTypeface(j))); |
| 106 | x = drawString(canvas, sname, x, y, paint) + 20; |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 107 | |
| 108 | // check to see that we get different glyphs in japanese and chinese |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 109 | x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), &zh, 1, fs); |
| 110 | x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), &ja, 1, fs); |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 111 | // check that emoji characters are found |
bungeman | c9232dc | 2014-11-10 13:29:33 -0800 | [diff] [blame] | 112 | x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, fName.c_str(), NULL, 0, fs); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 113 | } |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 114 | y += 24; |
| 115 | } |
| 116 | } |
| 117 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 118 | private: |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 119 | SkAutoTUnref<SkFontMgr> fFM; |
| 120 | SkString fName; |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 121 | typedef GM INHERITED; |
| 122 | }; |
| 123 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 124 | class FontMgrMatchGM : public skiagm::GM { |
| 125 | SkAutoTUnref<SkFontMgr> fFM; |
| 126 | |
| 127 | public: |
| 128 | FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) { |
| 129 | SkGraphics::SetFontCacheLimit(16 * 1024 * 1024); |
| 130 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 131 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 132 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 133 | SkString onShortName() SK_OVERRIDE { |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 134 | return SkString("fontmgr_match"); |
| 135 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 136 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 137 | SkISize onISize() SK_OVERRIDE { |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 138 | return SkISize::Make(640, 1024); |
| 139 | } |
| 140 | |
| 141 | void iterateFamily(SkCanvas* canvas, const SkPaint& paint, |
| 142 | SkFontStyleSet* fset) { |
| 143 | SkPaint p(paint); |
| 144 | SkScalar y = 0; |
| 145 | |
| 146 | for (int j = 0; j < fset->count(); ++j) { |
| 147 | SkString sname; |
| 148 | SkFontStyle fs; |
| 149 | fset->getStyle(j, &fs, &sname); |
| 150 | |
| 151 | sname.appendf(" [%d %d]", fs.weight(), fs.width()); |
| 152 | |
| 153 | SkSafeUnref(p.setTypeface(fset->createTypeface(j))); |
| 154 | (void)drawString(canvas, sname, 0, y, p); |
| 155 | y += 24; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void exploreFamily(SkCanvas* canvas, const SkPaint& paint, |
| 160 | SkFontStyleSet* fset) { |
| 161 | SkPaint p(paint); |
| 162 | SkScalar y = 0; |
| 163 | |
| 164 | for (int weight = 100; weight <= 900; weight += 200) { |
| 165 | for (int width = 1; width <= 9; width += 2) { |
| 166 | SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant); |
| 167 | SkTypeface* face = fset->matchStyle(fs); |
| 168 | if (face) { |
| 169 | SkString str; |
| 170 | str.printf("request [%d %d]", fs.weight(), fs.width()); |
| 171 | p.setTypeface(face)->unref(); |
| 172 | (void)drawString(canvas, str, 0, y, p); |
| 173 | y += 24; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 178 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 179 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 180 | SkPaint paint; |
| 181 | paint.setAntiAlias(true); |
| 182 | paint.setLCDRenderText(true); |
| 183 | paint.setSubpixelText(true); |
| 184 | paint.setTextSize(17); |
| 185 | |
| 186 | static const char* gNames[] = { |
| 187 | "Helvetica Neue", "Arial" |
| 188 | }; |
| 189 | |
bungeman@google.com | 9fc5c68 | 2013-11-12 15:25:29 +0000 | [diff] [blame] | 190 | SkAutoTUnref<SkFontStyleSet> fset; |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 191 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) { |
bungeman@google.com | 9fc5c68 | 2013-11-12 15:25:29 +0000 | [diff] [blame] | 192 | fset.reset(fFM->matchFamily(gNames[i])); |
| 193 | if (fset->count() > 0) { |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | } |
bungeman@google.com | 9fc5c68 | 2013-11-12 15:25:29 +0000 | [diff] [blame] | 197 | if (NULL == fset.get()) { |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 198 | return; |
| 199 | } |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 200 | |
| 201 | canvas->translate(20, 40); |
| 202 | this->exploreFamily(canvas, paint, fset); |
| 203 | canvas->translate(150, 0); |
| 204 | this->iterateFamily(canvas, paint, fset); |
| 205 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 206 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 207 | private: |
| 208 | typedef GM INHERITED; |
| 209 | }; |
| 210 | |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 211 | class FontMgrBoundsGM : public skiagm::GM { |
| 212 | public: |
reed | 8893e5f | 2014-12-15 13:27:26 -0800 | [diff] [blame] | 213 | FontMgrBoundsGM(double scale, double skew) |
| 214 | : fScaleX(SkDoubleToScalar(scale)) |
| 215 | , fSkewX(SkDoubleToScalar(skew)) |
| 216 | { |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 217 | fName.set("fontmgr_bounds"); |
reed | 8893e5f | 2014-12-15 13:27:26 -0800 | [diff] [blame] | 218 | if (scale != 1 || skew != 0) { |
| 219 | fName.appendf("_%g_%g", scale, skew); |
| 220 | } |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 221 | fFM.reset(SkFontMgr::RefDefault()); |
| 222 | } |
| 223 | |
| 224 | static void show_bounds(SkCanvas* canvas, const SkPaint& paint, SkScalar x, SkScalar y, |
| 225 | SkColor boundsColor) { |
| 226 | const char str[] = "jyHO[]{}@-_&%$"; |
| 227 | |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 228 | for (int i = 0; str[i]; ++i) { |
| 229 | canvas->drawText(&str[i], 1, x, y, paint); |
| 230 | } |
tfarina | aa458fb | 2015-01-05 17:18:51 -0800 | [diff] [blame] | 231 | |
reed | 8893e5f | 2014-12-15 13:27:26 -0800 | [diff] [blame] | 232 | SkRect r = paint.getFontBounds(); |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 233 | r.offset(x, y); |
| 234 | SkPaint p(paint); |
| 235 | p.setColor(boundsColor); |
| 236 | canvas->drawRect(r, p); |
| 237 | } |
| 238 | |
| 239 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 240 | SkString onShortName() SK_OVERRIDE { |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 241 | return fName; |
| 242 | } |
tfarina | aa458fb | 2015-01-05 17:18:51 -0800 | [diff] [blame] | 243 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 244 | SkISize onISize() SK_OVERRIDE { |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 245 | return SkISize::Make(1024, 850); |
| 246 | } |
tfarina | aa458fb | 2015-01-05 17:18:51 -0800 | [diff] [blame] | 247 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 248 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 249 | SkPaint paint; |
| 250 | paint.setAntiAlias(true); |
| 251 | paint.setSubpixelText(true); |
| 252 | paint.setTextSize(100); |
| 253 | paint.setStyle(SkPaint::kStroke_Style); |
reed | 8893e5f | 2014-12-15 13:27:26 -0800 | [diff] [blame] | 254 | paint.setTextScaleX(fScaleX); |
| 255 | paint.setTextSkewX(fSkewX); |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 256 | |
| 257 | const SkColor boundsColors[2] = { SK_ColorRED, SK_ColorBLUE }; |
| 258 | |
| 259 | SkFontMgr* fm = fFM; |
| 260 | int count = SkMin32(fm->countFamilies(), 32); |
| 261 | |
| 262 | int index = 0; |
| 263 | SkScalar x = 0, y = 0; |
| 264 | |
| 265 | canvas->translate(80, 120); |
| 266 | |
| 267 | for (int i = 0; i < count; ++i) { |
| 268 | SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i)); |
| 269 | for (int j = 0; j < set->count(); ++j) { |
| 270 | SkSafeUnref(paint.setTypeface(set->createTypeface(j))); |
| 271 | if (paint.getTypeface()) { |
| 272 | show_bounds(canvas, paint, x, y, boundsColors[index & 1]); |
| 273 | index += 1; |
| 274 | x += 160; |
| 275 | if (0 == (index % 6)) { |
| 276 | x = 0; |
| 277 | y += 160; |
| 278 | } |
| 279 | if (index >= 30) { |
| 280 | return; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
mtklein | 1c40292 | 2015-01-23 11:07:07 -0800 | [diff] [blame] | 286 | |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 287 | private: |
| 288 | SkAutoTUnref<SkFontMgr> fFM; |
| 289 | SkString fName; |
reed | 8893e5f | 2014-12-15 13:27:26 -0800 | [diff] [blame] | 290 | SkScalar fScaleX, fSkewX; |
reed | a0c814c | 2014-10-22 13:20:58 -0700 | [diff] [blame] | 291 | typedef GM INHERITED; |
| 292 | }; |
| 293 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 294 | ////////////////////////////////////////////////////////////////////////////// |
| 295 | |
| 296 | DEF_GM( return SkNEW(FontMgrGM); ) |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 297 | DEF_GM( return SkNEW(FontMgrMatchGM); ) |
reed | 8893e5f | 2014-12-15 13:27:26 -0800 | [diff] [blame] | 298 | DEF_GM( return SkNEW(FontMgrBoundsGM(1.0, 0)); ) |
| 299 | DEF_GM( return SkNEW(FontMgrBoundsGM(0.75, 0)); ) |
| 300 | DEF_GM( return SkNEW(FontMgrBoundsGM(1.0, -0.25)); ) |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 301 | |
| 302 | #ifdef SK_BUILD_FOR_WIN |
bungeman@google.com | 6eddc77 | 2014-03-31 19:18:07 +0000 | [diff] [blame] | 303 | DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); ) |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 304 | #endif |