caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
reed | 39a9a50 | 2015-05-12 09:50:04 -0700 | [diff] [blame] | 8 | #include "SkAdvancedTypefaceMetrics.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkCanvas.h" |
Jim Van Verth | af78a94 | 2018-03-23 14:39:42 +0000 | [diff] [blame^] | 11 | #include "SkDescriptor.h" |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 12 | #include "SkFontDescriptor.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 13 | #include "SkGlyph.h" |
Jim Van Verth | af78a94 | 2018-03-23 14:39:42 +0000 | [diff] [blame^] | 14 | #include "SkMakeUnique.h" |
| 15 | #include "SkMask.h" |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 16 | #include "SkOTUtils.h" |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 17 | #include "SkPaintPriv.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 18 | #include "SkScalerContext.h" |
Jim Van Verth | af78a94 | 2018-03-23 14:39:42 +0000 | [diff] [blame^] | 19 | #include "SkTestScalerContext.h" |
| 20 | #include "SkTypefaceCache.h" |
Mike Klein | 71a38d9 | 2017-11-10 13:42:29 -0500 | [diff] [blame] | 21 | #include "SkUtils.h" |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 22 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 23 | SkTestFont::SkTestFont(const SkTestFontData& fontData) |
| 24 | : INHERITED() |
| 25 | , fCharCodes(fontData.fCharCodes) |
jvanverth | c7027ab | 2016-06-16 09:52:35 -0700 | [diff] [blame] | 26 | , fCharCodesCount(fontData.fCharCodes ? fontData.fCharCodesCount : 0) |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 27 | , fWidths(fontData.fWidths) |
| 28 | , fMetrics(fontData.fMetrics) |
| 29 | , fName(fontData.fName) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 30 | , fPaths(nullptr) |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 31 | { |
| 32 | init(fontData.fPoints, fontData.fVerbs); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | SkTestFont::~SkTestFont() { |
| 36 | for (unsigned index = 0; index < fCharCodesCount; ++index) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 37 | delete fPaths[index]; |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 38 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 39 | delete[] fPaths; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 40 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 41 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 42 | int SkTestFont::codeToIndex(SkUnichar charCode) const { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 43 | for (unsigned index = 0; index < fCharCodesCount; ++index) { |
| 44 | if (fCharCodes[index] == (unsigned) charCode) { |
| 45 | return (int) index; |
| 46 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 47 | } |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 48 | return 0; |
| 49 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 50 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 51 | void SkTestFont::init(const SkScalar* pts, const unsigned char* verbs) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 52 | fPaths = new SkPath* [fCharCodesCount]; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 53 | for (unsigned index = 0; index < fCharCodesCount; ++index) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 54 | SkPath* path = new SkPath; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 55 | SkPath::Verb verb; |
| 56 | while ((verb = (SkPath::Verb) *verbs++) != SkPath::kDone_Verb) { |
| 57 | switch (verb) { |
| 58 | case SkPath::kMove_Verb: |
| 59 | path->moveTo(pts[0], pts[1]); |
| 60 | pts += 2; |
| 61 | break; |
| 62 | case SkPath::kLine_Verb: |
| 63 | path->lineTo(pts[0], pts[1]); |
| 64 | pts += 2; |
| 65 | break; |
| 66 | case SkPath::kQuad_Verb: |
| 67 | path->quadTo(pts[0], pts[1], pts[2], pts[3]); |
| 68 | pts += 4; |
| 69 | break; |
| 70 | case SkPath::kCubic_Verb: |
| 71 | path->cubicTo(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]); |
| 72 | pts += 6; |
| 73 | break; |
| 74 | case SkPath::kClose_Verb: |
| 75 | path->close(); |
| 76 | break; |
| 77 | default: |
| 78 | SkDEBUGFAIL("bad verb"); |
| 79 | return; |
| 80 | } |
| 81 | } |
mtklein | 6f6264f | 2015-10-05 07:29:22 -0700 | [diff] [blame] | 82 | // This should make SkPath::getBounds() queries threadsafe. |
| 83 | path->updateBoundsCache(); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 84 | fPaths[index] = path; |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 85 | } |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 86 | } |
bungeman | a4c4a2d | 2014-10-20 13:33:19 -0700 | [diff] [blame] | 87 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 88 | SkTestTypeface::SkTestTypeface(sk_sp<SkTestFont> testFont, const SkFontStyle& style) |
bungeman | e3aea10 | 2016-07-13 05:16:58 -0700 | [diff] [blame] | 89 | : SkTypeface(style, false) |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 90 | , fTestFont(std::move(testFont)) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 91 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 92 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 93 | void SkTestTypeface::getAdvance(SkGlyph* glyph) { |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 94 | // TODO(benjaminwagner): Update users to use floats. |
| 95 | glyph->fAdvanceX = SkFixedToFloat(fTestFont->fWidths[glyph->getGlyphID()]); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 96 | glyph->fAdvanceY = 0; |
| 97 | } |
| 98 | |
| 99 | void SkTestTypeface::getFontMetrics(SkPaint::FontMetrics* metrics) { |
| 100 | *metrics = fTestFont->fMetrics; |
| 101 | } |
| 102 | |
| 103 | void SkTestTypeface::getMetrics(SkGlyph* glyph) { |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 104 | SkGlyphID glyphID = glyph->getGlyphID(); |
| 105 | glyphID = glyphID < fTestFont->fCharCodesCount ? glyphID : 0; |
| 106 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 107 | // TODO(benjaminwagner): Update users to use floats. |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 108 | glyph->fAdvanceX = SkFixedToFloat(fTestFont->fWidths[glyphID]); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 109 | glyph->fAdvanceY = 0; |
| 110 | } |
| 111 | |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 112 | void SkTestTypeface::getPath(SkGlyphID glyphID, SkPath* path) { |
| 113 | glyphID = glyphID < fTestFont->fCharCodesCount ? glyphID : 0; |
| 114 | *path = *fTestFont->fPaths[glyphID]; |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void SkTestTypeface::onFilterRec(SkScalerContextRec* rec) const { |
| 118 | rec->setHinting(SkPaint::kNo_Hinting); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Hal Canary | 209e4b1 | 2017-05-04 14:23:55 -0400 | [diff] [blame] | 121 | std::unique_ptr<SkAdvancedTypefaceMetrics> SkTestTypeface::onGetAdvancedMetrics() const { // pdf only |
| 122 | std::unique_ptr<SkAdvancedTypefaceMetrics> info(new SkAdvancedTypefaceMetrics); |
caryclark | 0edd965 | 2015-06-11 06:46:21 -0700 | [diff] [blame] | 123 | info->fFontName.set(fTestFont->fName); |
halcanary | 3d01c62 | 2016-08-31 12:52:35 -0700 | [diff] [blame] | 124 | int glyphCount = this->onCountGlyphs(); |
halcanary | 3d01c62 | 2016-08-31 12:52:35 -0700 | [diff] [blame] | 125 | |
| 126 | SkTDArray<SkUnichar>& toUnicode = info->fGlyphToUnicode; |
| 127 | toUnicode.setCount(glyphCount); |
| 128 | SkASSERT(glyphCount == SkToInt(fTestFont->fCharCodesCount)); |
| 129 | for (int gid = 0; gid < glyphCount; ++gid) { |
| 130 | toUnicode[gid] = SkToS32(fTestFont->fCharCodes[gid]); |
| 131 | } |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 132 | return info; |
| 133 | } |
| 134 | |
| 135 | void SkTestTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const { |
| 136 | desc->setFamilyName(fTestFont->fName); |
bungeman | b811378 | 2016-07-25 16:54:59 -0700 | [diff] [blame] | 137 | desc->setStyle(this->fontStyle()); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 138 | *isLocal = false; |
| 139 | } |
| 140 | |
| 141 | int SkTestTypeface::onCharsToGlyphs(const void* chars, Encoding encoding, |
Hal Canary | 58d2a72 | 2017-01-11 13:26:43 -0500 | [diff] [blame] | 142 | uint16_t glyphs[], int glyphCount) const { |
Mike Klein | 71a38d9 | 2017-11-10 13:42:29 -0500 | [diff] [blame] | 143 | auto utf8 = (const char*)chars; |
| 144 | auto utf16 = (const uint16_t*)chars; |
| 145 | auto utf32 = (const SkUnichar*)chars; |
| 146 | |
| 147 | for (int i = 0; i < glyphCount; i++) { |
| 148 | SkUnichar ch; |
| 149 | switch (encoding) { |
| 150 | case kUTF8_Encoding: ch = SkUTF8_NextUnichar(&utf8 ); break; |
| 151 | case kUTF16_Encoding: ch = SkUTF16_NextUnichar(&utf16); break; |
| 152 | case kUTF32_Encoding: ch = *utf32++; break; |
| 153 | } |
Brian Salomon | 0c1c2b3 | 2017-11-20 13:13:01 -0500 | [diff] [blame] | 154 | if (glyphs) { |
| 155 | glyphs[i] = fTestFont->codeToIndex(ch); |
| 156 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 157 | } |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 158 | return glyphCount; |
| 159 | } |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 160 | |
bungeman | b374d6a | 2014-09-17 07:48:59 -0700 | [diff] [blame] | 161 | void SkTestTypeface::onGetFamilyName(SkString* familyName) const { |
| 162 | *familyName = fTestFont->fName; |
| 163 | } |
| 164 | |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 165 | SkTypeface::LocalizedStrings* SkTestTypeface::onCreateFamilyNameIterator() const { |
| 166 | SkString familyName(fTestFont->fName); |
| 167 | SkString language("und"); //undetermined |
Mike Klein | cb6940b | 2017-11-09 13:45:10 -0500 | [diff] [blame] | 168 | return new SkOTUtils::LocalizedStrings_SingleName(familyName, language); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | class SkTestScalerContext : public SkScalerContext { |
| 172 | public: |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 173 | SkTestScalerContext(sk_sp<SkTestTypeface> face, const SkScalerContextEffects& effects, |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 174 | const SkDescriptor* desc) |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 175 | : SkScalerContext(std::move(face), effects, desc) |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 176 | { |
| 177 | fRec.getSingleMatrix(&fMatrix); |
| 178 | this->forceGenerateImageFromPath(); |
| 179 | } |
| 180 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 181 | protected: |
| 182 | SkTestTypeface* getTestTypeface() const { |
| 183 | return static_cast<SkTestTypeface*>(this->getTypeface()); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 184 | } |
| 185 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 186 | unsigned generateGlyphCount() override { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 187 | return this->getTestTypeface()->onCountGlyphs(); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 188 | } |
| 189 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 190 | uint16_t generateCharToGlyph(SkUnichar uni) override { |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 191 | uint16_t glyph; |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 192 | (void) this->getTestTypeface()->onCharsToGlyphs((const void *) &uni, |
Hal Canary | 58d2a72 | 2017-01-11 13:26:43 -0500 | [diff] [blame] | 193 | SkTypeface::kUTF32_Encoding, &glyph, 1); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 194 | return glyph; |
| 195 | } |
| 196 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 197 | void generateAdvance(SkGlyph* glyph) override { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 198 | this->getTestTypeface()->getAdvance(glyph); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 199 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 200 | const SkVector advance = fMatrix.mapXY(SkFloatToScalar(glyph->fAdvanceX), |
| 201 | SkFloatToScalar(glyph->fAdvanceY)); |
| 202 | glyph->fAdvanceX = SkScalarToFloat(advance.fX); |
| 203 | glyph->fAdvanceY = SkScalarToFloat(advance.fY); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 204 | } |
| 205 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 206 | void generateMetrics(SkGlyph* glyph) override { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 207 | this->getTestTypeface()->getMetrics(glyph); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 208 | |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame] | 209 | const SkVector advance = fMatrix.mapXY(SkFloatToScalar(glyph->fAdvanceX), |
| 210 | SkFloatToScalar(glyph->fAdvanceY)); |
| 211 | glyph->fAdvanceX = SkScalarToFloat(advance.fX); |
| 212 | glyph->fAdvanceY = SkScalarToFloat(advance.fY); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 213 | |
| 214 | SkPath path; |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 215 | this->getTestTypeface()->getPath(glyph->getGlyphID(), &path); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 216 | path.transform(fMatrix); |
| 217 | |
| 218 | SkRect storage; |
| 219 | const SkPaint paint; |
| 220 | const SkRect& newBounds = paint.doComputeFastBounds(path.getBounds(), |
| 221 | &storage, |
| 222 | SkPaint::kFill_Style); |
| 223 | SkIRect ibounds; |
| 224 | newBounds.roundOut(&ibounds); |
| 225 | glyph->fLeft = ibounds.fLeft; |
| 226 | glyph->fTop = ibounds.fTop; |
| 227 | glyph->fWidth = ibounds.width(); |
| 228 | glyph->fHeight = ibounds.height(); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 229 | } |
| 230 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 231 | void generateImage(const SkGlyph& glyph) override { |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 232 | SkPath path; |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 233 | this->getTestTypeface()->getPath(glyph.getGlyphID(), &path); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 234 | |
| 235 | SkBitmap bm; |
| 236 | bm.installPixels(SkImageInfo::MakeN32Premul(glyph.fWidth, glyph.fHeight), |
| 237 | glyph.fImage, glyph.rowBytes()); |
| 238 | bm.eraseColor(0); |
| 239 | |
| 240 | SkCanvas canvas(bm); |
| 241 | canvas.translate(-SkIntToScalar(glyph.fLeft), |
| 242 | -SkIntToScalar(glyph.fTop)); |
| 243 | canvas.concat(fMatrix); |
| 244 | SkPaint paint; |
| 245 | paint.setAntiAlias(true); |
| 246 | canvas.drawPath(path, paint); |
| 247 | } |
| 248 | |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 249 | void generatePath(SkGlyphID glyph, SkPath* path) override { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 250 | this->getTestTypeface()->getPath(glyph, path); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 251 | path->transform(fMatrix); |
| 252 | } |
| 253 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 254 | void generateFontMetrics(SkPaint::FontMetrics* metrics) override { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 255 | this->getTestTypeface()->getFontMetrics(metrics); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 256 | SkPaintPriv::ScaleFontMetrics(metrics, fMatrix.getScaleY()); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | private: |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 260 | SkMatrix fMatrix; |
| 261 | }; |
| 262 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 263 | SkScalerContext* SkTestTypeface::onCreateScalerContext( |
| 264 | const SkScalerContextEffects& effects, const SkDescriptor* desc) const |
| 265 | { |
| 266 | return new SkTestScalerContext(sk_ref_sp(const_cast<SkTestTypeface*>(this)), effects, desc); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 267 | } |