| 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 |
|
| 8 |
#include "include/core/SkBitmap.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkFontMetrics.h" |
| 11 |
#include "include/core/SkImageInfo.h" |
| 12 |
#include "include/core/SkMatrix.h" |
| 13 |
#include "include/core/SkPathBuilder.h" |
| 14 |
#include "include/core/SkPoint.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkString.h" |
| 17 |
#include "include/private/base/SkTDArray.h" |
| 18 |
#include "include/private/base/SkTo.h" |
| 19 |
#include "src/base/SkUtils.h" |
| 20 |
#include "src/core/SkAdvancedTypefaceMetrics.h" |
| 21 |
#include "src/core/SkFontDescriptor.h" |
| 22 |
#include "src/core/SkFontPriv.h" |
| 23 |
#include "src/core/SkGlyph.h" |
| 24 |
#include "src/core/SkPaintPriv.h" |
| 25 |
#include "src/core/SkScalerContext.h" |
| 26 |
#include "src/sfnt/SkOTUtils.h" |
| 27 |
#include "tools/fonts/TestTypeface.h" |
| 28 |
|
| 29 |
#include <utility> |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
#include "tools/fonts/test_font_monospace.inc" |
| 34 |
#include "tools/fonts/test_font_sans_serif.inc" |
| 35 |
#include "tools/fonts/test_font_serif.inc" |
| 36 |
|
| 37 |
#include "tools/fonts/test_font_index.inc" |
| 38 |
|
| 39 |
} // namespace |
| 40 |
|
| 41 |
class SkDescriptor; |
| 42 |
|
| 43 |
const TestTypeface::List& TestTypeface::Typefaces() { |
| 44 |
static List list = []() -> List { |
| 45 |
TestTypeface::List list; |
| 46 |
for (const auto& sub : gSubFonts) { |
| 47 |
List::Family* existingFamily = nullptr; |
| 48 |
for (auto& family : list.families) { |
| 49 |
if (strcmp(family.name, sub.fFamilyName) == 0) { |
| 50 |
existingFamily = &family; |
| 51 |
break; |
| 52 |
} |
| 53 |
} |
| 54 |
if (!existingFamily) { |
| 55 |
existingFamily = &list.families.emplace_back(); |
| 56 |
existingFamily->name = sub.fFamilyName; |
| 57 |
} |
| 58 |
|
| 59 |
auto font = sk_make_sp<SkTestFont>(sub.fFont); |
| 60 |
sk_sp<SkTypeface> typeface(new TestTypeface(std::move(font), sub.fStyle)); |
| 61 |
bool isDefault = (&sub - gSubFonts == gDefaultFontIndex); |
| 62 |
existingFamily->faces.emplace_back( |
| 63 |
List::Family::Face{std::move(typeface), sub.fStyleName, isDefault}); |
| 64 |
} |
| 65 |
return list; |
| 66 |
}(); |
| 67 |
return list; |
| 68 |
} |
| 69 |
|
| 70 |
SkTestFont::SkTestFont(const SkTestFontData& fontData) |
| 71 |
: INHERITED() |
| 72 |
, fCharCodes(fontData.fCharCodes) |
| 73 |
, fCharCodesCount(fontData.fCharCodes ? fontData.fCharCodesCount : 0) |
| 74 |
, fWidths(fontData.fWidths) |
| 75 |
, fMetrics(fontData.fMetrics) |
| 76 |
, fName(fontData.fName) |
| 77 |
, fPaths(nullptr) { |
| 78 |
init(fontData.fPoints, fontData.fVerbs); |
| 79 |
} |
| 80 |
|
| 81 |
SkTestFont::~SkTestFont() { |
| 82 |
delete[] fPaths; |
| 83 |
} |
| 84 |
|
| 85 |
SkGlyphID SkTestFont::glyphForUnichar(SkUnichar charCode) const { |
| 86 |
for (size_t index = 0; index < fCharCodesCount; ++index) { |
| 87 |
if (fCharCodes[index] == charCode) { |
| 88 |
return SkTo<SkGlyphID>(index); |
| 89 |
} |
| 90 |
} |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
void SkTestFont::init(const SkScalar* pts, const unsigned char* verbs) { |
| 95 |
fPaths = new SkPath[fCharCodesCount]; |
| 96 |
for (unsigned index = 0; index < fCharCodesCount; ++index) { |
| 97 |
SkPathBuilder b; |
| 98 |
SkPath::Verb verb; |
| 99 |
while ((verb = (SkPath::Verb)*verbs++) != SkPath::kDone_Verb) { |
| 100 |
switch (verb) { |
| 101 |
case SkPath::kMove_Verb: |
| 102 |
b.moveTo(pts[0], pts[1]); |
| 103 |
pts += 2; |
| 104 |
break; |
| 105 |
case SkPath::kLine_Verb: |
| 106 |
b.lineTo(pts[0], pts[1]); |
| 107 |
pts += 2; |
| 108 |
break; |
| 109 |
case SkPath::kQuad_Verb: |
| 110 |
b.quadTo(pts[0], pts[1], pts[2], pts[3]); |
| 111 |
pts += 4; |
| 112 |
break; |
| 113 |
case SkPath::kCubic_Verb: |
| 114 |
b.cubicTo(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]); |
| 115 |
pts += 6; |
| 116 |
break; |
| 117 |
case SkPath::kClose_Verb: |
| 118 |
b.close(); |
| 119 |
break; |
| 120 |
default: |
| 121 |
SK_ABORT("bad verb"); |
| 122 |
} |
| 123 |
} |
| 124 |
fPaths[index] = b.detach(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
TestTypeface::TestTypeface(sk_sp<SkTestFont> testFont, const SkFontStyle& style) |
| 129 |
: SkTypeface(style, false), fTestFont(std::move(testFont)) {} |
| 130 |
|
| 131 |
SkVector TestTypeface::getAdvance(SkGlyphID glyphID) const { |
| 132 |
glyphID = glyphID < fTestFont->fCharCodesCount ? glyphID : 0; |
| 133 |
|
| 134 |
// TODO(benjaminwagner): Update users to use floats. |
| 135 |
return {SkFixedToFloat(fTestFont->fWidths[glyphID]), 0}; |
| 136 |
} |
| 137 |
|
| 138 |
void TestTypeface::getFontMetrics(SkFontMetrics* metrics) { *metrics = fTestFont->fMetrics; } |
| 139 |
|
| 140 |
SkPath TestTypeface::getPath(SkGlyphID glyphID) { |
| 141 |
glyphID = glyphID < fTestFont->fCharCodesCount ? glyphID : 0; |
| 142 |
return fTestFont->fPaths[glyphID]; |
| 143 |
} |
| 144 |
|
| 145 |
void TestTypeface::onFilterRec(SkScalerContextRec* rec) const { |
| 146 |
rec->useStrokeForFakeBold(); |
| 147 |
rec->setHinting(SkFontHinting::kNone); |
| 148 |
} |
| 149 |
|
| 150 |
void TestTypeface::getGlyphToUnicodeMap(SkSpan<SkUnichar> glyphToUnicode) const { |
| 151 |
unsigned glyphCount = std::min(fTestFont->fCharCodesCount, glyphToUnicode.size()); |
| 152 |
for (unsigned gid = 0; gid < glyphCount; ++gid) { |
| 153 |
glyphToUnicode[gid] = SkTo<SkUnichar>(fTestFont->fCharCodes[gid]); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
std::unique_ptr<SkAdvancedTypefaceMetrics> TestTypeface::onGetAdvancedMetrics() const { // pdf only |
| 158 |
std::unique_ptr<SkAdvancedTypefaceMetrics>info(new SkAdvancedTypefaceMetrics); |
| 159 |
info->fPostScriptName.set(fTestFont->fName); |
| 160 |
return info; |
| 161 |
} |
| 162 |
|
| 163 |
static constexpr const char gHeaderString[] = "SkTestTypeface01"; |
| 164 |
static constexpr const size_t kHeaderSize = sizeof(gHeaderString); |
| 165 |
|
| 166 |
std::unique_ptr<SkStreamAsset> TestTypeface::onOpenStream(int* ttcIndex) const { |
| 167 |
SkDynamicMemoryWStream wstream; |
| 168 |
wstream.write(gHeaderString, kHeaderSize); |
| 169 |
|
| 170 |
SkString name; |
| 171 |
this->getFamilyName(&name); |
| 172 |
SkFontStyle style = this->fontStyle(); |
| 173 |
|
| 174 |
wstream.writePackedUInt(name.size()); |
| 175 |
wstream.write(name.c_str(), name.size()); |
| 176 |
wstream.writeScalar(style.weight()); |
| 177 |
wstream.writeScalar(style.width()); |
| 178 |
wstream.writePackedUInt(style.slant()); |
| 179 |
|
| 180 |
*ttcIndex = 0; |
| 181 |
return wstream.detachAsStream(); |
| 182 |
} |
| 183 |
|
| 184 |
sk_sp<SkTypeface> TestTypeface::MakeFromStream(std::unique_ptr<SkStreamAsset> stream, |
| 185 |
const SkFontArguments&) { |
| 186 |
char header[kHeaderSize]; |
| 187 |
if (stream->read(header, kHeaderSize) != kHeaderSize || |
| 188 |
0 != memcmp(header, gHeaderString, kHeaderSize)) |
| 189 |
{ |
| 190 |
return nullptr; |
| 191 |
} |
| 192 |
|
| 193 |
size_t familyNameSize; |
| 194 |
SkString familyName; |
| 195 |
if (!stream->readPackedUInt(&familyNameSize)) { return nullptr; } |
| 196 |
familyName.resize(familyNameSize); |
| 197 |
if (!stream->read(familyName.data(), familyNameSize)) { return nullptr; } |
| 198 |
|
| 199 |
SkScalar weight; |
| 200 |
SkScalar width; |
| 201 |
size_t slant; |
| 202 |
if (!stream->readScalar(&weight)) { return nullptr; } |
| 203 |
if (!stream->readScalar(&width)) { return nullptr; } |
| 204 |
if (!stream->readPackedUInt(&slant)) { return nullptr; } |
| 205 |
SkFontStyle style(weight, width, (SkFontStyle::Slant)slant); |
| 206 |
|
| 207 |
auto&& list = TestTypeface::Typefaces(); |
| 208 |
for (auto&& family : list.families) { |
| 209 |
if (familyName.equals(family.name)) { |
| 210 |
for (auto&& face : family.faces) { |
| 211 |
if (face.typeface->fontStyle() == style) { |
| 212 |
return face.typeface; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
return nullptr; |
| 218 |
} |
| 219 |
|
| 220 |
void TestTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const { |
| 221 |
desc->setFamilyName(fTestFont->fName); |
| 222 |
desc->setStyle(this->fontStyle()); |
| 223 |
desc->setFactoryId(FactoryId); |
| 224 |
*serialize = true; |
| 225 |
} |
| 226 |
|
| 227 |
TestTypeface::Register::Register() { |
| 228 |
SkTypeface::Register(TestTypeface::FactoryId, &TestTypeface::MakeFromStream); |
| 229 |
} |
| 230 |
static TestTypeface::Register registerer; |
| 231 |
|
| 232 |
void TestTypeface::onCharsToGlyphs(SkSpan<const SkUnichar> uni, SkSpan<SkGlyphID> glyphs) const { |
| 233 |
SkASSERT(uni.size() == glyphs.size()); |
| 234 |
for (size_t i = 0; i < uni.size(); ++i) { |
| 235 |
glyphs[i] = fTestFont->glyphForUnichar(uni[i]); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void TestTypeface::onGetFamilyName(SkString* familyName) const { *familyName = fTestFont->fName; } |
| 240 |
|
| 241 |
bool TestTypeface::onGetPostScriptName(SkString*) const { return false; } |
| 242 |
|
| 243 |
SkTypeface::LocalizedStrings* TestTypeface::onCreateFamilyNameIterator() const { |
| 244 |
SkString familyName(fTestFont->fName); |
| 245 |
SkString language("und"); // undetermined |
| 246 |
return new SkOTUtils::LocalizedStrings_SingleName(familyName, language); |
| 247 |
} |
| 248 |
|
| 249 |
class SkTestScalerContext : public SkScalerContext { |
| 250 |
public: |
| 251 |
SkTestScalerContext(TestTypeface& face, |
| 252 |
const SkScalerContextEffects& effects, |
| 253 |
const SkDescriptor* desc) |
| 254 |
: SkScalerContext(face, effects, desc) |
| 255 |
, fMatrix(fRec.getSingleMatrix()) |
| 256 |
{} |
| 257 |
|
| 258 |
protected: |
| 259 |
TestTypeface* getTestTypeface() const { |
| 260 |
return static_cast<TestTypeface*>(this->getTypeface()); |
| 261 |
} |
| 262 |
|
| 263 |
GlyphMetrics generateMetrics(const SkGlyph& glyph, SkArenaAlloc*) override { |
| 264 |
GlyphMetrics mx(glyph.maskFormat()); |
| 265 |
|
| 266 |
SkPoint advance = this->getTestTypeface()->getAdvance(glyph.getGlyphID()); |
| 267 |
mx.advance = fMatrix.mapPoint(advance); |
| 268 |
|
| 269 |
// Always generates from paths, so SkScalerContext::makeGlyph will figure the bounds. |
| 270 |
mx.computeFromPath = true; |
| 271 |
return mx; |
| 272 |
} |
| 273 |
|
| 274 |
void generateImage(const SkGlyph& glyph, void* imageBuffer) override { |
| 275 |
this->generateImageFromPath(glyph, imageBuffer); |
| 276 |
} |
| 277 |
|
| 278 |
std::optional<GeneratedPath> generatePath(const SkGlyph& glyph) override { |
| 279 |
return {{ |
| 280 |
this->getTestTypeface()->getPath(glyph.getGlyphID()).makeTransform(fMatrix), |
| 281 |
false |
| 282 |
}}; |
| 283 |
} |
| 284 |
|
| 285 |
void generateFontMetrics(SkFontMetrics* metrics) override { |
| 286 |
this->getTestTypeface()->getFontMetrics(metrics); |
| 287 |
SkFontPriv::ScaleFontMetrics(metrics, fMatrix.getScaleY()); |
| 288 |
} |
| 289 |
|
| 290 |
private: |
| 291 |
const SkMatrix fMatrix; |
| 292 |
}; |
| 293 |
|
| 294 |
std::unique_ptr<SkScalerContext> TestTypeface::onCreateScalerContext( |
| 295 |
const SkScalerContextEffects& effects, const SkDescriptor* desc) const |
| 296 |
{ |
| 297 |
return std::make_unique<SkTestScalerContext>(*const_cast<TestTypeface*>(this), effects, desc); |
| 298 |
} |