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, |
| 29 | const char* fontName, const char* bpc47, |
| 30 | const SkFontStyle& fontStyle) { |
| 31 | // find typeface containing the requested character and draw it |
| 32 | SkString ch; |
| 33 | ch.appendUnichar(character); |
| 34 | SkTypeface* typeface = fm->matchFamilyStyleCharacter(fontName, fontStyle, bpc47, character); |
| 35 | SkSafeUnref(paint.setTypeface(typeface)); |
| 36 | x = drawString(canvas, ch, x, y, paint) + 20; |
| 37 | |
| 38 | if (NULL == typeface) { |
| 39 | return x; |
| 40 | } |
| 41 | |
| 42 | // repeat the process, but this time use the family name of the typeface |
| 43 | // from the first pass. This emulates the behavior in Blink where it |
| 44 | // it expects to get the same glyph when following this pattern. |
| 45 | SkString familyName; |
| 46 | typeface->getFamilyName(&familyName); |
| 47 | SkTypeface* typefaceCopy = fm->legacyCreateTypeface(familyName.c_str(), typeface->style()); |
| 48 | SkSafeUnref(paint.setTypeface(typefaceCopy)); |
| 49 | return drawString(canvas, ch, x, y, paint) + 20; |
| 50 | } |
| 51 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 52 | class FontMgrGM : public skiagm::GM { |
| 53 | public: |
bungeman@google.com | 6eddc77 | 2014-03-31 19:18:07 +0000 | [diff] [blame] | 54 | FontMgrGM(SkFontMgr* fontMgr = NULL) { |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 55 | SkGraphics::SetFontCacheLimit(16 * 1024 * 1024); |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 56 | |
| 57 | fName.set("fontmgr_iter"); |
bungeman@google.com | 6eddc77 | 2014-03-31 19:18:07 +0000 | [diff] [blame] | 58 | if (fontMgr) { |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 59 | fName.append("_factory"); |
bungeman@google.com | 6eddc77 | 2014-03-31 19:18:07 +0000 | [diff] [blame] | 60 | fFM.reset(fontMgr); |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 61 | } else { |
| 62 | fFM.reset(SkFontMgr::RefDefault()); |
| 63 | } |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | protected: |
| 67 | virtual SkString onShortName() { |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 68 | return fName; |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | virtual SkISize onISize() { |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 72 | return SkISize::Make(1536, 768); |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 76 | SkScalar y = 20; |
| 77 | SkPaint paint; |
| 78 | paint.setAntiAlias(true); |
| 79 | paint.setLCDRenderText(true); |
| 80 | paint.setSubpixelText(true); |
| 81 | paint.setTextSize(17); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 82 | |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 83 | SkFontMgr* fm = fFM; |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 84 | int count = SkMin32(fm->countFamilies(), MAX_FAMILIES); |
| 85 | |
| 86 | for (int i = 0; i < count; ++i) { |
| 87 | SkString fname; |
| 88 | fm->getFamilyName(i, &fname); |
| 89 | paint.setTypeface(NULL); |
| 90 | (void)drawString(canvas, fname, 20, y, paint); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 91 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 92 | SkScalar x = 220; |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 93 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 94 | SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i)); |
| 95 | for (int j = 0; j < set->count(); ++j) { |
| 96 | SkString sname; |
| 97 | SkFontStyle fs; |
| 98 | set->getStyle(j, &fs, &sname); |
reed@google.com | 5111648 | 2013-04-24 19:14:39 +0000 | [diff] [blame] | 99 | 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] | 100 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 101 | SkSafeUnref(paint.setTypeface(set->createTypeface(j))); |
| 102 | x = drawString(canvas, sname, x, y, paint) + 20; |
djsollen | 0d393a9 | 2014-08-27 07:03:13 -0700 | [diff] [blame] | 103 | |
| 104 | // check to see that we get different glyphs in japanese and chinese |
| 105 | x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), "zh", fs); |
| 106 | x = drawCharacter(canvas, 0x5203, x, y, paint, fm, fName.c_str(), "ja", fs); |
| 107 | // check that emoji characters are found |
| 108 | x = drawCharacter(canvas, 0x1f601, x, y, paint, fm, fName.c_str(), NULL, fs); |
skia.committer@gmail.com | 6acd09e | 2013-03-29 07:01:22 +0000 | [diff] [blame] | 109 | } |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 110 | y += 24; |
| 111 | } |
| 112 | } |
| 113 | |
reed@google.com | 6518daf | 2013-03-28 15:03:22 +0000 | [diff] [blame] | 114 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 115 | // fontdescriptors (and therefore serialization) don't yet understand |
| 116 | // these new styles, so skip tests that exercise that for now. |
bungeman@google.com | 7103344 | 2013-05-01 14:21:20 +0000 | [diff] [blame] | 117 | |
| 118 | // If certain fonts are picked up (e.g. Microsoft Jhenghei 20MB for Regular, 12MB for Bold), |
| 119 | // the resulting pdf can be ~700MB and crashes Chrome's PDF viewer. |
| 120 | |
| 121 | return kSkipPicture_Flag | kSkipPipe_Flag | kSkipPDF_Flag; |
reed@google.com | 6518daf | 2013-03-28 15:03:22 +0000 | [diff] [blame] | 122 | } |
| 123 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 124 | private: |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 125 | SkAutoTUnref<SkFontMgr> fFM; |
| 126 | SkString fName; |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 127 | typedef GM INHERITED; |
| 128 | }; |
| 129 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 130 | class FontMgrMatchGM : public skiagm::GM { |
| 131 | SkAutoTUnref<SkFontMgr> fFM; |
| 132 | |
| 133 | public: |
| 134 | FontMgrMatchGM() : fFM(SkFontMgr::RefDefault()) { |
| 135 | SkGraphics::SetFontCacheLimit(16 * 1024 * 1024); |
| 136 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 137 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 138 | protected: |
| 139 | virtual SkString onShortName() { |
| 140 | return SkString("fontmgr_match"); |
| 141 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 142 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 143 | virtual SkISize onISize() { |
| 144 | return SkISize::Make(640, 1024); |
| 145 | } |
| 146 | |
| 147 | void iterateFamily(SkCanvas* canvas, const SkPaint& paint, |
| 148 | SkFontStyleSet* fset) { |
| 149 | SkPaint p(paint); |
| 150 | SkScalar y = 0; |
| 151 | |
| 152 | for (int j = 0; j < fset->count(); ++j) { |
| 153 | SkString sname; |
| 154 | SkFontStyle fs; |
| 155 | fset->getStyle(j, &fs, &sname); |
| 156 | |
| 157 | sname.appendf(" [%d %d]", fs.weight(), fs.width()); |
| 158 | |
| 159 | SkSafeUnref(p.setTypeface(fset->createTypeface(j))); |
| 160 | (void)drawString(canvas, sname, 0, y, p); |
| 161 | y += 24; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void exploreFamily(SkCanvas* canvas, const SkPaint& paint, |
| 166 | SkFontStyleSet* fset) { |
| 167 | SkPaint p(paint); |
| 168 | SkScalar y = 0; |
| 169 | |
| 170 | for (int weight = 100; weight <= 900; weight += 200) { |
| 171 | for (int width = 1; width <= 9; width += 2) { |
| 172 | SkFontStyle fs(weight, width, SkFontStyle::kUpright_Slant); |
| 173 | SkTypeface* face = fset->matchStyle(fs); |
| 174 | if (face) { |
| 175 | SkString str; |
| 176 | str.printf("request [%d %d]", fs.weight(), fs.width()); |
| 177 | p.setTypeface(face)->unref(); |
| 178 | (void)drawString(canvas, str, 0, y, p); |
| 179 | y += 24; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 184 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 185 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 186 | SkPaint paint; |
| 187 | paint.setAntiAlias(true); |
| 188 | paint.setLCDRenderText(true); |
| 189 | paint.setSubpixelText(true); |
| 190 | paint.setTextSize(17); |
| 191 | |
| 192 | static const char* gNames[] = { |
| 193 | "Helvetica Neue", "Arial" |
| 194 | }; |
| 195 | |
bungeman@google.com | 9fc5c68 | 2013-11-12 15:25:29 +0000 | [diff] [blame] | 196 | SkAutoTUnref<SkFontStyleSet> fset; |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 197 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) { |
bungeman@google.com | 9fc5c68 | 2013-11-12 15:25:29 +0000 | [diff] [blame] | 198 | fset.reset(fFM->matchFamily(gNames[i])); |
| 199 | if (fset->count() > 0) { |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 200 | break; |
| 201 | } |
| 202 | } |
bungeman@google.com | 9fc5c68 | 2013-11-12 15:25:29 +0000 | [diff] [blame] | 203 | if (NULL == fset.get()) { |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 204 | return; |
| 205 | } |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 206 | |
| 207 | canvas->translate(20, 40); |
| 208 | this->exploreFamily(canvas, paint, fset); |
| 209 | canvas->translate(150, 0); |
| 210 | this->iterateFamily(canvas, paint, fset); |
| 211 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 212 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 213 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 214 | // fontdescriptors (and therefore serialization) don't yet understand |
| 215 | // these new styles, so skip tests that exercise that for now. |
| 216 | return kSkipPicture_Flag | kSkipPipe_Flag; |
| 217 | } |
skia.committer@gmail.com | d55846d | 2013-03-30 07:01:27 +0000 | [diff] [blame] | 218 | |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 219 | private: |
| 220 | typedef GM INHERITED; |
| 221 | }; |
| 222 | |
reed@google.com | af0fa6a | 2013-03-28 13:39:35 +0000 | [diff] [blame] | 223 | ////////////////////////////////////////////////////////////////////////////// |
| 224 | |
| 225 | DEF_GM( return SkNEW(FontMgrGM); ) |
reed@google.com | 964988f | 2013-03-29 14:57:22 +0000 | [diff] [blame] | 226 | DEF_GM( return SkNEW(FontMgrMatchGM); ) |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 227 | |
| 228 | #ifdef SK_BUILD_FOR_WIN |
bungeman@google.com | 6eddc77 | 2014-03-31 19:18:07 +0000 | [diff] [blame] | 229 | DEF_GM( return SkNEW_ARGS(FontMgrGM, (SkFontMgr_New_DirectWrite())); ) |
bungeman@google.com | bfc6cc4 | 2013-08-21 15:20:43 +0000 | [diff] [blame] | 230 | #endif |