| 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 "tools/fonts/FontToolUtils.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkFont.h" |
| 13 |
#include "include/core/SkFontMgr.h" |
| 14 |
#include "include/core/SkFontScanner.h" |
| 15 |
#include "include/core/SkFontStyle.h" |
| 16 |
#include "include/core/SkFontTypes.h" |
| 17 |
#include "include/core/SkImage.h" |
| 18 |
#include "include/core/SkPaint.h" |
| 19 |
#include "include/core/SkPixelRef.h" // IWYU pragma: keep |
| 20 |
#include "include/core/SkStream.h" |
| 21 |
#include "include/core/SkTypeface.h" |
| 22 |
#if defined(SK_TYPEFACE_FACTORY_FONTATIONS) |
| 23 |
#include "include/ports/SkFontScanner_Fontations.h" |
| 24 |
#endif |
| 25 |
#if defined(SK_TYPEFACE_FACTORY_FREETYPE) |
| 26 |
#include "include/ports/SkFontScanner_FreeType.h" |
| 27 |
#endif |
| 28 |
#include "include/private/base/SkMutex.h" |
| 29 |
#include "include/utils/SkCustomTypeface.h" |
| 30 |
#include "src/base/SkUTF.h" |
| 31 |
#include "src/core/SkOSFile.h" |
| 32 |
#include "tools/Resources.h" |
| 33 |
#include "tools/flags/CommandLineFlags.h" |
| 34 |
#include "tools/fonts/TestFontMgr.h" |
| 35 |
|
| 36 |
#if defined(SK_BUILD_FOR_WIN) && (defined(SK_FONTMGR_GDI_AVAILABLE) || \ |
| 37 |
defined(SK_FONTMGR_DIRECTWRITE_AVAILABLE)) |
| 38 |
#include "include/ports/SkTypeface_win.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
#if defined(SK_BUILD_FOR_ANDROID) && defined(SK_FONTMGR_ANDROID_AVAILABLE) |
| 42 |
#include "include/ports/SkFontMgr_android.h" |
| 43 |
#include "include/ports/SkFontScanner_FreeType.h" |
| 44 |
#endif |
| 45 |
|
| 46 |
#if defined(SK_BUILD_FOR_ANDROID) && defined(SK_FONTMGR_ANDROID_NDK_AVAILABLE) |
| 47 |
#include "include/ports/SkFontMgr_android_ndk.h" |
| 48 |
#include "include/ports/SkFontScanner_FreeType.h" |
| 49 |
#endif |
| 50 |
|
| 51 |
#if defined(SK_FONTMGR_CORETEXT_AVAILABLE) && (defined(SK_BUILD_FOR_IOS) || \ |
| 52 |
defined(SK_BUILD_FOR_MAC)) |
| 53 |
#include "include/ports/SkFontMgr_mac_ct.h" |
| 54 |
#endif |
| 55 |
|
| 56 |
#if defined(SK_FONTMGR_FONTATIONS_AVAILABLE) |
| 57 |
#include "include/ports/SkFontMgr_Fontations.h" |
| 58 |
#endif |
| 59 |
|
| 60 |
#if defined(SK_BUILD_FOR_UNIX) && defined(SK_FONTMGR_FONTCONFIG_AVAILABLE) |
| 61 |
#include "include/ports/SkFontMgr_fontconfig.h" |
| 62 |
#include "include/ports/SkFontScanner_FreeType.h" |
| 63 |
#endif |
| 64 |
|
| 65 |
#if defined(SK_FONTMGR_FREETYPE_DIRECTORY_AVAILABLE) |
| 66 |
#include "include/ports/SkFontMgr_directory.h" |
| 67 |
#endif |
| 68 |
|
| 69 |
#if defined(SK_FONTMGR_FREETYPE_EMPTY_AVAILABLE) |
| 70 |
#include "include/ports/SkFontMgr_empty.h" |
| 71 |
#endif |
| 72 |
|
| 73 |
#if defined(SK_TYPEFACE_FACTORY_FREETYPE) |
| 74 |
#include "src/ports/SkTypeface_FreeType.h" |
| 75 |
#endif |
| 76 |
|
| 77 |
#if defined(SK_TYPEFACE_FACTORY_CORETEXT) |
| 78 |
#include "src/ports/SkTypeface_mac_ct.h" |
| 79 |
#endif |
| 80 |
|
| 81 |
#if defined(SK_TYPEFACE_FACTORY_DIRECTWRITE) |
| 82 |
#include "src/ports/SkTypeface_win_dw.h" |
| 83 |
#endif |
| 84 |
|
| 85 |
#if defined(SK_TYPEFACE_FACTORY_FONTATIONS) |
| 86 |
#include "include/ports/SkTypeface_fontations.h" |
| 87 |
#include "src/ports/SkTypeface_fontations_factory.h" |
| 88 |
#endif |
| 89 |
|
| 90 |
namespace ToolUtils { |
| 91 |
|
| 92 |
static DEFINE_bool(nativeFonts, |
| 93 |
true, |
| 94 |
"If true, use native font manager and rendering. " |
| 95 |
"If false, fonts will draw as portably as possible."); |
| 96 |
#if defined(SK_BUILD_FOR_WIN) |
| 97 |
static DEFINE_bool(gdi, false, "Use GDI instead of DirectWrite for font rendering."); |
| 98 |
#endif |
| 99 |
#if defined(SK_FONTMGR_FONTATIONS_AVAILABLE) || defined(SK_TYPEFACE_FACTORY_FONTATIONS) |
| 100 |
static DEFINE_bool(fontations, false, "Use Fontations for native font rendering."); |
| 101 |
#endif |
| 102 |
#if defined(SK_FONTMGR_ANDROID_NDK_AVAILABLE) |
| 103 |
static DEFINE_bool(androidndkfonts, false, "Use AndroidNDK for native font rendering."); |
| 104 |
#endif |
| 105 |
|
| 106 |
sk_sp<SkTypeface> PlanetTypeface() { |
| 107 |
static const sk_sp<SkTypeface> planetTypeface = []() { |
| 108 |
const char* filename; |
| 109 |
#if defined(SK_BUILD_FOR_WIN) |
| 110 |
filename = "fonts/planetcolr.ttf"; |
| 111 |
#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 112 |
filename = "fonts/planetsbix.ttf"; |
| 113 |
#else |
| 114 |
filename = "fonts/planetcbdt.ttf"; |
| 115 |
#endif |
| 116 |
sk_sp<SkTypeface> typeface = CreateTypefaceFromResource(filename); |
| 117 |
if (typeface) { |
| 118 |
return typeface; |
| 119 |
} |
| 120 |
return CreateTestTypeface("Planet", SkFontStyle()); |
| 121 |
}(); |
| 122 |
return planetTypeface; |
| 123 |
} |
| 124 |
|
| 125 |
EmojiTestSample EmojiSample() { |
| 126 |
static const EmojiTestSample emojiSample = []() { |
| 127 |
EmojiTestSample sample = {nullptr, ""}; |
| 128 |
#if defined(SK_BUILD_FOR_WIN) |
| 129 |
sample = EmojiSample(EmojiFontFormat::ColrV0); |
| 130 |
#elif defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 131 |
sample = EmojiSample(EmojiFontFormat::Sbix); |
| 132 |
#else |
| 133 |
sample = EmojiSample(EmojiFontFormat::Cbdt); |
| 134 |
#endif |
| 135 |
if (sample.typeface) { |
| 136 |
return sample; |
| 137 |
} |
| 138 |
return EmojiSample(EmojiFontFormat::Test); |
| 139 |
}(); |
| 140 |
return emojiSample; |
| 141 |
} |
| 142 |
|
| 143 |
EmojiTestSample EmojiSample(EmojiFontFormat format) { |
| 144 |
EmojiTestSample sample; |
| 145 |
sample.sampleText = "\U0001F600 \u2662"; // 😀 ♢ |
| 146 |
switch (format) { |
| 147 |
case EmojiFontFormat::Cbdt: |
| 148 |
sample.typeface = CreateTypefaceFromResource("fonts/cbdt.ttf"); |
| 149 |
break; |
| 150 |
case EmojiFontFormat::Sbix: |
| 151 |
sample.typeface = CreateTypefaceFromResource("fonts/sbix.ttf"); |
| 152 |
break; |
| 153 |
case EmojiFontFormat::ColrV0: |
| 154 |
sample.typeface = CreateTypefaceFromResource("fonts/colr.ttf"); |
| 155 |
break; |
| 156 |
case EmojiFontFormat::Svg: |
| 157 |
sample.typeface = CreateTypefaceFromResource("fonts/SampleSVG.ttf"); |
| 158 |
sample.sampleText = "abcdefghij"; |
| 159 |
break; |
| 160 |
case EmojiFontFormat::Test: |
| 161 |
sample.typeface = CreatePortableTypeface("Emoji", SkFontStyle()); |
| 162 |
} |
| 163 |
return sample; |
| 164 |
} |
| 165 |
|
| 166 |
SkString NameForFontFormat(EmojiFontFormat format) { |
| 167 |
switch (format) { |
| 168 |
case EmojiFontFormat::Cbdt: |
| 169 |
return SkString("cbdt"); |
| 170 |
case EmojiFontFormat::Sbix: |
| 171 |
return SkString("sbix"); |
| 172 |
case EmojiFontFormat::ColrV0: |
| 173 |
return SkString("colrv0"); |
| 174 |
case EmojiFontFormat::Test: |
| 175 |
return SkString("test"); |
| 176 |
case EmojiFontFormat::Svg: |
| 177 |
return SkString("svg"); |
| 178 |
} |
| 179 |
return SkString(); |
| 180 |
} |
| 181 |
|
| 182 |
sk_sp<SkTypeface> SampleUserTypeface() { |
| 183 |
SkCustomTypefaceBuilder builder; |
| 184 |
SkFont font; |
| 185 |
const float upem = 200; |
| 186 |
|
| 187 |
{ |
| 188 |
SkFontMetrics metrics; |
| 189 |
metrics.fFlags = 0; |
| 190 |
metrics.fTop = -200; |
| 191 |
metrics.fAscent = -150; |
| 192 |
metrics.fDescent = 50; |
| 193 |
metrics.fBottom = -75; |
| 194 |
metrics.fLeading = 10; |
| 195 |
metrics.fAvgCharWidth = 150; |
| 196 |
metrics.fMaxCharWidth = 300; |
| 197 |
metrics.fXMin = -20; |
| 198 |
metrics.fXMax = 290; |
| 199 |
metrics.fXHeight = -100; |
| 200 |
metrics.fCapHeight = 0; |
| 201 |
metrics.fUnderlineThickness = 5; |
| 202 |
metrics.fUnderlinePosition = 2; |
| 203 |
metrics.fStrikeoutThickness = 5; |
| 204 |
metrics.fStrikeoutPosition = -50; |
| 205 |
builder.setMetrics(metrics, 1.0f/upem); |
| 206 |
} |
| 207 |
builder.setFontStyle(SkFontStyle(367, 3, SkFontStyle::kOblique_Slant)); |
| 208 |
|
| 209 |
const SkMatrix scale = SkMatrix::Scale(1.0f/upem, 1.0f/upem); |
| 210 |
for (SkGlyphID index = 0; index <= 67; ++index) { |
| 211 |
SkScalar width; |
| 212 |
width = 100; |
| 213 |
|
| 214 |
builder.setGlyph(index, width/upem, SkPath::Circle(50, -50, 75).makeTransform(scale)); |
| 215 |
} |
| 216 |
|
| 217 |
return builder.detach(); |
| 218 |
} |
| 219 |
|
| 220 |
sk_sp<SkTypeface> CreatePortableTypeface(const char* name, SkFontStyle style) { |
| 221 |
static sk_sp<SkFontMgr> portableFontMgr = MakePortableFontMgr(); |
| 222 |
SkASSERT_RELEASE(portableFontMgr); |
| 223 |
sk_sp<SkTypeface> face = portableFontMgr->legacyMakeTypeface(name, style); |
| 224 |
SkASSERT_RELEASE(face); |
| 225 |
return face; |
| 226 |
} |
| 227 |
|
| 228 |
sk_sp<SkTypeface> DefaultPortableTypeface() { |
| 229 |
// At last check, the default typeface is a serif font. |
| 230 |
sk_sp<SkTypeface> face = CreatePortableTypeface(nullptr, SkFontStyle()); |
| 231 |
SkASSERT_RELEASE(face); |
| 232 |
return face; |
| 233 |
} |
| 234 |
|
| 235 |
SkFont DefaultPortableFont() { |
| 236 |
return SkFont(DefaultPortableTypeface(), 12); |
| 237 |
} |
| 238 |
|
| 239 |
SkBitmap CreateStringBitmap(int w, int h, SkColor c, int x, int y, int textSize, |
| 240 |
const char* str) { |
| 241 |
SkBitmap bitmap; |
| 242 |
bitmap.allocN32Pixels(w, h); |
| 243 |
SkCanvas canvas(bitmap); |
| 244 |
|
| 245 |
SkPaint paint; |
| 246 |
paint.setColor(c); |
| 247 |
|
| 248 |
SkFont font(DefaultPortableTypeface(), textSize); |
| 249 |
|
| 250 |
canvas.clear(0x00000000); |
| 251 |
canvas.drawSimpleText(str, |
| 252 |
strlen(str), |
| 253 |
SkTextEncoding::kUTF8, |
| 254 |
SkIntToScalar(x), |
| 255 |
SkIntToScalar(y), |
| 256 |
font, |
| 257 |
paint); |
| 258 |
|
| 259 |
// Tag data as sRGB (without doing any color space conversion). Color-space aware configs |
| 260 |
// will process this correctly but legacy configs will render as if this returned N32. |
| 261 |
SkBitmap result; |
| 262 |
result.setInfo(SkImageInfo::MakeS32(w, h, kPremul_SkAlphaType)); |
| 263 |
result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0); |
| 264 |
return result; |
| 265 |
} |
| 266 |
|
| 267 |
sk_sp<SkImage> CreateStringImage(int w, int h, SkColor c, int x, int y, int textSize, |
| 268 |
const char* str) { |
| 269 |
return CreateStringBitmap(w, h, c, x, y, textSize, str).asImage(); |
| 270 |
} |
| 271 |
|
| 272 |
#if defined(SK_TYPEFACE_FACTORY_FONTATIONS) || defined(SK_TYPEFACE_FACTORY_FREETYPE) |
| 273 |
#define SK_TYPEFACE_SCANNER_AVAILABLE |
| 274 |
#endif |
| 275 |
|
| 276 |
std::unique_ptr<SkFontScanner> TestFontScanner() { |
| 277 |
#if defined(SK_TYPEFACE_FACTORY_FONTATIONS) |
| 278 |
// If both are compiled in, use the flag to point to fontations or not. |
| 279 |
#if defined(SK_TYPEFACE_FACTORY_FONTATIONS) && defined(SK_TYPEFACE_FACTORY_FREETYPE) |
| 280 |
const bool useFontations = FLAGS_fontations; |
| 281 |
#else |
| 282 |
const bool useFontations = true; |
| 283 |
#endif |
| 284 |
if (useFontations) { |
| 285 |
auto result = SkFontScanner_Make_Fontations(); |
| 286 |
if (result) { |
| 287 |
return result; |
| 288 |
} |
| 289 |
} |
| 290 |
#endif // defined(SK_TYPEFACE_FACTORY_FONTATIONS) |
| 291 |
|
| 292 |
#if defined(SK_TYPEFACE_FACTORY_FREETYPE) |
| 293 |
return SkFontScanner_Make_FreeType(); |
| 294 |
#else |
| 295 |
SkDEBUGFAIL("No font scanner created - this may cause failures down the line."); |
| 296 |
return nullptr; |
| 297 |
#endif |
| 298 |
} |
| 299 |
|
| 300 |
sk_sp<SkFontMgr> TestFontMgr() { |
| 301 |
static sk_sp<SkFontMgr> mgr; |
| 302 |
static SkOnce once; |
| 303 |
once([] { |
| 304 |
if (!FLAGS_nativeFonts) { |
| 305 |
mgr = MakePortableFontMgr(); |
| 306 |
} |
| 307 |
#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTMGR_GDI_AVAILABLE) |
| 308 |
else if (FLAGS_gdi) { |
| 309 |
mgr = SkFontMgr_New_GDI(); |
| 310 |
} |
| 311 |
#endif |
| 312 |
#if defined(SK_BUILD_FOR_ANDROID) && defined(SK_FONTMGR_ANDROID_NDK_AVAILABLE) && defined(SK_TYPEFACE_SCANNER_AVAILABLE) |
| 313 |
else if (FLAGS_androidndkfonts) { |
| 314 |
mgr = SkFontMgr_New_AndroidNDK(false, TestFontScanner()); |
| 315 |
} |
| 316 |
#endif |
| 317 |
else { |
| 318 |
#if defined(SK_BUILD_FOR_ANDROID) && defined(SK_FONTMGR_ANDROID_AVAILABLE) && defined(SK_TYPEFACE_SCANNER_AVAILABLE) |
| 319 |
mgr = SkFontMgr_New_Android(nullptr, TestFontScanner()); |
| 320 |
#elif defined(SK_BUILD_FOR_WIN) && defined(SK_FONTMGR_DIRECTWRITE_AVAILABLE) |
| 321 |
mgr = SkFontMgr_New_DirectWrite(); |
| 322 |
#elif defined(SK_FONTMGR_CORETEXT_AVAILABLE) && (defined(SK_BUILD_FOR_IOS) || \ |
| 323 |
defined(SK_BUILD_FOR_MAC)) |
| 324 |
mgr = SkFontMgr_New_CoreText(nullptr); |
| 325 |
#elif defined(SK_FONTMGR_FONTCONFIG_AVAILABLE) && defined(SK_TYPEFACE_SCANNER_AVAILABLE) |
| 326 |
mgr = SkFontMgr_New_FontConfig(nullptr, TestFontScanner()); |
| 327 |
#elif defined(SK_FONTMGR_FREETYPE_DIRECTORY_AVAILABLE) |
| 328 |
# if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 329 |
static constexpr char kFontFilePrefix[] = "/System/Library/Fonts/"; |
| 330 |
# else |
| 331 |
static constexpr char kFontFilePrefix[] = "/usr/share/fonts/"; |
| 332 |
# endif |
| 333 |
// In particular, this is used on ChromeOS, which is Linux-like but doesn't have |
| 334 |
// FontConfig. |
| 335 |
mgr = SkFontMgr_New_Custom_Directory(kFontFilePrefix); |
| 336 |
#elif defined(SK_FONTMGR_FREETYPE_EMPTY_AVAILABLE) |
| 337 |
mgr = SkFontMgr_New_Custom_Empty(); |
| 338 |
#else |
| 339 |
mgr = SkFontMgr::RefEmpty(); |
| 340 |
#endif |
| 341 |
} |
| 342 |
SkASSERT_RELEASE(mgr); |
| 343 |
}); |
| 344 |
return mgr; |
| 345 |
} |
| 346 |
|
| 347 |
bool FontMgrIsGDI() { |
| 348 |
if (!FLAGS_nativeFonts) { |
| 349 |
return false; |
| 350 |
} |
| 351 |
#if defined(SK_BUILD_FOR_WIN) |
| 352 |
if (FLAGS_gdi) { |
| 353 |
return true; |
| 354 |
} |
| 355 |
#endif |
| 356 |
return false; |
| 357 |
} |
| 358 |
|
| 359 |
void UsePortableFontMgr() { FLAGS_nativeFonts = false; } |
| 360 |
|
| 361 |
sk_sp<SkTypeface> DefaultTypeface() { |
| 362 |
return CreateTestTypeface(nullptr, SkFontStyle()); |
| 363 |
} |
| 364 |
|
| 365 |
sk_sp<SkTypeface> CreateTestTypeface(const char* name, SkFontStyle style) { |
| 366 |
sk_sp<SkFontMgr> fm = TestFontMgr(); |
| 367 |
SkASSERT_RELEASE(fm); |
| 368 |
sk_sp<SkTypeface> face = fm->legacyMakeTypeface(name, style); |
| 369 |
if (face) { |
| 370 |
return face; |
| 371 |
} |
| 372 |
return CreatePortableTypeface(name, style); |
| 373 |
} |
| 374 |
|
| 375 |
sk_sp<SkTypeface> CreateTypefaceFromResource(const char* resource, int ttcIndex) { |
| 376 |
sk_sp<SkFontMgr> fm = TestFontMgr(); |
| 377 |
SkASSERT_RELEASE(fm); |
| 378 |
return fm->makeFromStream(GetResourceAsStream(resource), ttcIndex); |
| 379 |
} |
| 380 |
|
| 381 |
SkFont DefaultFont() { |
| 382 |
return SkFont(DefaultTypeface(), 12); |
| 383 |
} |
| 384 |
|
| 385 |
void RegisterAvailableTypefaceFactories() { |
| 386 |
#if defined(SK_TYPEFACE_FACTORY_CORETEXT) |
| 387 |
SkTypeface::Register(SkTypeface_Mac::FactoryId, SkTypeface_Mac::MakeFromStream); |
| 388 |
#endif |
| 389 |
#if defined(SK_TYPEFACE_FACTORY_DIRECTWRITE) |
| 390 |
SkTypeface::Register(DWriteFontTypeface::FactoryId, DWriteFontTypeface::MakeFromStream); |
| 391 |
#endif |
| 392 |
#if defined(SK_TYPEFACE_FACTORY_FREETYPE) |
| 393 |
SkTypeface::Register(SkTypeface_FreeType::FactoryId, SkTypeface_FreeType::MakeFromStream); |
| 394 |
#endif |
| 395 |
#if defined(SK_TYPEFACE_FACTORY_FONTATIONS) |
| 396 |
SkTypeface::Register(SkTypefaces::Fontations::FactoryId, SkTypeface_Make_Fontations); |
| 397 |
#endif |
| 398 |
} |
| 399 |
|
| 400 |
} // namespace ToolUtils |