Flutter Engine
The Flutter Engine
Functions
TypefaceTest.cpp File Reference
#include "include/core/SkData.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontArguments.h"
#include "include/core/SkFontMgr.h"
#include "include/core/SkFontParameters.h"
#include "include/core/SkFontStyle.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "include/private/base/SkFixed.h"
#include "include/private/base/SkTemplates.h"
#include "src/base/SkEndian.h"
#include "src/base/SkUTF.h"
#include "src/core/SkFontDescriptor.h"
#include "src/core/SkFontPriv.h"
#include "src/core/SkTypefaceCache.h"
#include "src/sfnt/SkOTTable_OS_2.h"
#include "src/sfnt/SkOTTable_OS_2_V0.h"
#include "src/sfnt/SkSFNTHeader.h"
#include "tests/Test.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include "tools/fonts/FontToolUtils.h"
#include "tools/fonts/TestEmptyTypeface.h"
#include <algorithm>
#include <array>
#include <cinttypes>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <utility>

Go to the source code of this file.

Functions

static void TypefaceStyle_test (skiatest::Reporter *reporter, uint16_t weight, uint16_t width, SkData *data)
 
 DEF_TEST (TypefaceStyle, reporter)
 
 DEF_TEST (TypefaceStyleVariable, reporter)
 
 DEF_TEST (TypefacePostScriptName, reporter)
 
 DEF_TEST (TypefaceRoundTrip, reporter)
 
 DEF_TEST (FontDescriptorNegativeVariationSerialize, reporter)
 
 DEF_TEST (TypefaceAxes, reporter)
 
 DEF_TEST (TypefaceVariationIndex, reporter)
 
 DEF_TEST (Typeface, reporter)
 
 DEF_TEST (TypefaceAxesParameters, reporter)
 
static bool count_proc (SkTypeface *face, void *ctx)
 
static int count (skiatest::Reporter *reporter, const SkTypefaceCache &cache)
 
 DEF_TEST (TypefaceCache, reporter)
 
static void check_serialize_behaviors (sk_sp< SkTypeface > tf, skiatest::Reporter *reporter)
 
 DEF_TEST (Typeface_serialize, reporter)
 
 DEF_TEST (Typeface_glyph_to_char, reporter)
 
 DEF_TEST (LegacyMakeTypeface, reporter)
 

Function Documentation

◆ check_serialize_behaviors()

static void check_serialize_behaviors ( sk_sp< SkTypeface tf,
skiatest::Reporter reporter 
)
static

Definition at line 619 of file TypefaceTest.cpp.

619 {
620 if (!tf) {
621 return;
622 }
623
625 bool serialize;
626 tf->getFontDescriptor(&desc, &serialize);
627
631
632 REPORTER_ASSERT(reporter, data0->size() >= data1->size());
633
634 if (serialize) {
635 REPORTER_ASSERT(reporter, data0->equals(data2.get()));
636 } else {
637 REPORTER_ASSERT(reporter, data1->equals(data2.get()));
638 }
639}
reporter
Definition: FontMgrTest.cpp:39
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
void getFontDescriptor(SkFontDescriptor *desc, bool *isLocal) const
Definition: SkTypeface.h:326
void serialize(SkWStream *, SerializeBehavior=SerializeBehavior::kIncludeDataIfLocal) const
Definition: SkTypeface.cpp:202

◆ count()

static int count ( skiatest::Reporter reporter,
const SkTypefaceCache cache 
)
static

Definition at line 591 of file TypefaceTest.cpp.

591 {
592 int count = 0;
593 sk_sp<SkTypeface> none = cache.findByProcAndRef(count_proc, &count);
594 REPORTER_ASSERT(reporter, none == nullptr);
595 return count;
596}
static int count(skiatest::Reporter *reporter, const SkTypefaceCache &cache)
static bool count_proc(SkTypeface *face, void *ctx)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets Path to the Flutter assets directory enable service port Allow the VM service to fallback to automatic port selection if binding to a specified port fails trace Trace early application lifecycle Automatically switches to an endless trace buffer trace skia Filters out all Skia trace event categories except those that are specified in this comma separated list dump skp on shader Automatically dump the skp that triggers new shader compilations This is useful for writing custom ShaderWarmUp to reduce jank By this is not enabled to reduce the overhead purge persistent cache
Definition: switches.h:191

◆ count_proc()

static bool count_proc ( SkTypeface face,
void *  ctx 
)
static

Definition at line 586 of file TypefaceTest.cpp.

586 {
587 int* count = static_cast<int*>(ctx);
588 *count = *count + 1;
589 return false;
590}

◆ DEF_TEST() [1/13]

DEF_TEST ( FontDescriptorNegativeVariationSerialize  ,
reporter   
)

Definition at line 240 of file TypefaceTest.cpp.

240 {
243 desc.setStyle(style);
244 const char postscriptName[] = "postscript";
245 desc.setPostscriptName(postscriptName);
246 SkFontArguments::VariationPosition::Coordinate* variation = desc.setVariationCoordinates(1);
247 variation[0] = { 0, -1.0f };
248
250 desc.serialize(&stream);
251 SkFontDescriptor descD;
252 SkFontDescriptor::Deserialize(stream.detachAsStream().get(), &descD);
253
254 REPORTER_ASSERT(reporter, descD.getStyle() == style);
255 REPORTER_ASSERT(reporter, 0 == strcmp(desc.getPostscriptName(), postscriptName));
256 if (descD.getVariationCoordinateCount() != 1) {
257 REPORT_FAILURE(reporter, "descD.getVariationCoordinateCount() != 1", SkString());
258 return;
259 }
260
261 REPORTER_ASSERT(reporter, descD.getVariation()[0].value == -1.0f);
262}
#define REPORT_FAILURE(reporter, cond, message)
Definition: Test.h:90
SkFontStyle getStyle() const
const SkFontArguments::VariationPosition::Coordinate * getVariation() const
static bool Deserialize(SkStream *, SkFontDescriptor *result)
int getVariationCoordinateCount() const

◆ DEF_TEST() [2/13]

DEF_TEST ( LegacyMakeTypeface  ,
reporter   
)

Definition at line 690 of file TypefaceTest.cpp.

690 {
692 sk_sp<SkTypeface> typeface1 = fm->legacyMakeTypeface(nullptr, SkFontStyle::Italic());
693 sk_sp<SkTypeface> typeface2 = fm->legacyMakeTypeface(nullptr, SkFontStyle::Bold());
695
696 if (typeface1 || typeface2 || typeface3) {
697 REPORTER_ASSERT(reporter, typeface1 && typeface2 && typeface1);
698 }
699
700 if (typeface1) {
701 REPORTER_ASSERT(reporter, typeface1->isItalic());
702 REPORTER_ASSERT(reporter, !typeface1->isBold());
703 }
704 if (typeface2) {
705 REPORTER_ASSERT(reporter, !typeface2->isItalic());
706 REPORTER_ASSERT(reporter, typeface2->isBold());
707 }
708 if (typeface3) {
709 REPORTER_ASSERT(reporter, typeface3->isItalic());
710 REPORTER_ASSERT(reporter, typeface3->isBold());
711 }
712}
sk_sp< SkTypeface > legacyMakeTypeface(const char familyName[], SkFontStyle style) const
Definition: SkFontMgr.cpp:150
static constexpr SkFontStyle Italic()
Definition: SkFontStyle.h:72
static constexpr SkFontStyle BoldItalic()
Definition: SkFontStyle.h:75
static constexpr SkFontStyle Bold()
Definition: SkFontStyle.h:69
bool isBold() const
Definition: SkTypeface.h:60
bool isItalic() const
Definition: SkTypeface.h:63
sk_sp< SkFontMgr > TestFontMgr()

◆ DEF_TEST() [3/13]

DEF_TEST ( Typeface  ,
reporter   
)

Definition at line 433 of file TypefaceTest.cpp.

433 {
434
437
438 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), t2.get()));
439 REPORTER_ASSERT(reporter, SkTypeface::Equal(nullptr, nullptr));
440
441 REPORTER_ASSERT(reporter, !SkTypeface::Equal(nullptr, t1.get()));
442 REPORTER_ASSERT(reporter, !SkTypeface::Equal(nullptr, t2.get()));
443 REPORTER_ASSERT(reporter, !SkTypeface::Equal(t1.get(), nullptr));
444 REPORTER_ASSERT(reporter, !SkTypeface::Equal(t2.get(), nullptr));
445}
static bool Equal(const SkTypeface *facea, const SkTypeface *faceb)
Definition: SkTypeface.cpp:149
sk_sp< SkTypeface > DefaultTypeface()
sk_sp< SkTypeface > CreateTestTypeface(const char *name, SkFontStyle style)

◆ DEF_TEST() [4/13]

DEF_TEST ( Typeface_glyph_to_char  ,
reporter   
)

Definition at line 648 of file TypefaceTest.cpp.

648 {
650 SkFont font(emojiSample.typeface, 12);
651 SkASSERT(font.getTypeface());
652 char const * text = emojiSample.sampleText;
653 size_t const textLen = strlen(text);
654 SkString familyName;
655 font.getTypeface()->getFamilyName(&familyName);
656
657 size_t const codepointCount = SkUTF::CountUTF8(text, textLen);
658 char const * const textEnd = text + textLen;
659 std::unique_ptr<SkUnichar[]> originalCodepoints(new SkUnichar[codepointCount]);
660 for (size_t i = 0; i < codepointCount; ++i) {
661 originalCodepoints[i] = SkUTF::NextUTF8(&text, textEnd);
662 }
663 std::unique_ptr<SkGlyphID[]> glyphs(new SkGlyphID[codepointCount]);
664 font.unicharsToGlyphs(originalCodepoints.get(), codepointCount, glyphs.get());
665 if (std::any_of(glyphs.get(), glyphs.get()+codepointCount, [](SkGlyphID g){ return g == 0;})) {
666 ERRORF(reporter, "Unexpected typeface \"%s\". Expected full support for emoji_sample_text.",
667 familyName.c_str());
668 return;
669 }
670
671 std::unique_ptr<SkUnichar[]> newCodepoints(new SkUnichar[codepointCount]);
672 SkFontPriv::GlyphsToUnichars(font, glyphs.get(), codepointCount, newCodepoints.get());
673
674 for (size_t i = 0; i < codepointCount; ++i) {
675 // GDI does not support character to glyph mapping outside BMP.
676 if (ToolUtils::FontMgrIsGDI() && 0xFFFF < originalCodepoints[i] && newCodepoints[i] == 0) {
677 continue;
678 }
679 // If two codepoints map to the same glyph then this assert is not valid.
680 // However, the emoji test font should never have multiple characters map to the same glyph.
681 REPORTER_ASSERT(reporter, originalCodepoints[i] == newCodepoints[i],
682 "name:%s i:%zu original:%d new:%d glyph:%d", familyName.c_str(), i,
683 originalCodepoints[i], newCodepoints[i], glyphs[i]);
684 }
685}
uint16_t glyphs[5]
Definition: FontMgrTest.cpp:46
#define SkASSERT(cond)
Definition: SkAssert.h:116
int32_t SkUnichar
Definition: SkTypes.h:175
uint16_t SkGlyphID
Definition: SkTypes.h:179
#define ERRORF(r,...)
Definition: Test.h:293
static void GlyphsToUnichars(const SkFont &, const uint16_t glyphs[], int count, SkUnichar[])
Definition: SkFont.cpp:396
Definition: SkFont.h:35
const char * c_str() const
Definition: SkString.h:133
std::u16string text
SK_SPI SkUnichar NextUTF8(const char **ptr, const char *end)
Definition: SkUTF.cpp:118
SK_SPI int CountUTF8(const char *utf8, size_t byteLength)
Definition: SkUTF.cpp:47
bool FontMgrIsGDI()
EmojiTestSample EmojiSample()
font
Font Metadata and Metrics.
sk_sp< SkTypeface > typeface
Definition: FontToolUtils.h:48

◆ DEF_TEST() [5/13]

DEF_TEST ( Typeface_serialize  ,
reporter   
)

Definition at line 641 of file TypefaceTest.cpp.

641 {
644 ToolUtils::TestFontMgr()->makeFromStream(GetResourceAsStream("fonts/Distortable.ttf")),
645 reporter);
646}
std::unique_ptr< SkStreamAsset > GetResourceAsStream(const char *resource, bool useFileStream)
Definition: Resources.cpp:31
static void check_serialize_behaviors(sk_sp< SkTypeface > tf, skiatest::Reporter *reporter)

◆ DEF_TEST() [6/13]

DEF_TEST ( TypefaceAxes  ,
reporter   
)

Definition at line 264 of file TypefaceTest.cpp.

264 {
265 using Variation = SkFontArguments::VariationPosition;
266 // In DWrite in at least up to 1901 18363.1198 IDWriteFontFace5::GetFontAxisValues and
267 // GetFontAxisValueCount along with IDWriteFontResource::GetFontAxisAttributes and
268 // GetFontAxisCount (and related) seem to incorrectly collapse multiple axes with the same tag.
269 // Since this is a limitation of the underlying implementation, for now allow the test to pass
270 // with the axis tag count (as opposed to the axis count). Eventually all implementations should
271 // pass this test without 'alsoAcceptedAxisTagCount'.
272 auto test = [&](SkTypeface* typeface, const Variation& expected, int alsoAcceptedAxisTagCount) {
273 if (!typeface) {
274 return; // Not all SkFontMgr can makeFromStream().
275 }
276
277 int actualCount = typeface->getVariationDesignPosition(nullptr, 0);
278 if (actualCount == -1) {
279 return; // The number of axes is unknown.
280 }
281 REPORTER_ASSERT(reporter, actualCount == expected.coordinateCount ||
282 actualCount == alsoAcceptedAxisTagCount);
283
284 // Variable font conservative bounds don't vary, so ensure they aren't reported.
286
287 std::unique_ptr<Variation::Coordinate[]> actual(new Variation::Coordinate[actualCount]);
288 actualCount = typeface->getVariationDesignPosition(actual.get(), actualCount);
289 if (actualCount == -1) {
290 return; // The position cannot be determined.
291 }
292 REPORTER_ASSERT(reporter, actualCount == expected.coordinateCount ||
293 actualCount == alsoAcceptedAxisTagCount);
294
295 // Every actual must be expected.
296 std::unique_ptr<bool[]> expectedUsed(new bool[expected.coordinateCount]());
297 for (int actualIdx = 0; actualIdx < actualCount; ++actualIdx) {
298 bool actualFound = false;
299 for (int expectedIdx = 0; expectedIdx < expected.coordinateCount; ++expectedIdx) {
300 if (expectedUsed[expectedIdx]) {
301 continue;
302 }
303
304 if (actual[actualIdx].axis != expected.coordinates[expectedIdx].axis) {
305 continue;
306 }
307
308 // Convert to fixed for "almost equal".
309 SkFixed fixedRead = SkScalarToFixed(actual[actualIdx].value);
310 SkFixed fixedOriginal = SkScalarToFixed(expected.coordinates[expectedIdx].value);
311 if (!(SkTAbs(fixedRead - fixedOriginal) < 2)) {
312 continue;
313 }
314
315 // This actual matched an unused expected.
316 actualFound = true;
317 expectedUsed[expectedIdx] = true;
318 break;
319 }
320 REPORTER_ASSERT(reporter, actualFound,
321 "Actual axis '%c%c%c%c' with value '%f' not expected",
322 (char)((actual[actualIdx].axis >> 24) & 0xFF),
323 (char)((actual[actualIdx].axis >> 16) & 0xFF),
324 (char)((actual[actualIdx].axis >> 8) & 0xFF),
325 (char)((actual[actualIdx].axis ) & 0xFF),
326 SkScalarToDouble(actual[actualIdx].value));
327 }
328 };
329
331
332 // Not specifying a position should produce the default.
333 {
334 std::unique_ptr<SkStreamAsset> variable(GetResourceAsStream("fonts/Variable.ttf"));
335 if (!variable) {
336 REPORT_FAILURE(reporter, "variable", SkString());
337 return;
338 }
339 const Variation::Coordinate defaultPosition[] = {
340 { SkSetFourByteTag('w','g','h','t'), 400.0f },
341 { SkSetFourByteTag('w','d','t','h'), 100.0f },
342 };
343 sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(variable), 0);
344 test(typeface.get(), Variation{&defaultPosition[0], 2}, -1);
345 }
346
347 // Multiple axes with the same tag (and min, max, default) works.
348 {
349 std::unique_ptr<SkStreamAsset> dupTags(GetResourceAsStream("fonts/VaryAlongQuads.ttf"));
350 if (!dupTags) {
351 REPORT_FAILURE(reporter, "dupTags", SkString());
352 return;
353 }
354
355 // The position may be over specified. If there are multiple values for a given axis,
356 // ensure the last one since that's what css-fonts-4 requires.
357 const Variation::Coordinate position[] = {
358 { SkSetFourByteTag('w','g','h','t'), 700.0f },
359 { SkSetFourByteTag('w','g','h','t'), 600.0f },
360 { SkSetFourByteTag('w','g','h','t'), 600.0f },
361 };
363 params.setVariationDesignPosition({position, std::size(position)});
364 sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(dupTags), params);
365 test(typeface.get(), Variation{&position[1], 2}, 1);
366 }
367
368 // Overspecifying an axis tag value applies the last one in the list.
369 {
370 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
371 if (!distortable) {
372 REPORT_FAILURE(reporter, "distortable", SkString());
373 return;
374 }
375
376 // The position may be over specified. If there are multiple values for a given axis,
377 // ensure the last one since that's what css-fonts-4 requires.
378 const Variation::Coordinate position[] = {
379 { SkSetFourByteTag('w','g','h','t'), 1.618033988749895f },
380 { SkSetFourByteTag('w','g','h','t'), SK_ScalarSqrt2 },
381 };
383 params.setVariationDesignPosition({position, std::size(position)});
384 sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(distortable), params);
385 test(typeface.get(), Variation{&position[1], 1}, -1);
386
387 if (typeface) {
388 // Cloning without specifying any parameters should produce an equivalent variation.
389 sk_sp<SkTypeface> clone = typeface->makeClone(SkFontArguments());
390 test(clone.get(), Variation{&position[1], 1}, -1);
391 }
392 }
393}
#define test(name)
int32_t SkFixed
Definition: SkFixed.h:25
#define SkScalarToFixed(x)
Definition: SkFixed.h:125
#define SkScalarToDouble(x)
Definition: SkScalar.h:63
#define SK_ScalarSqrt2
Definition: SkScalar.h:20
static T SkTAbs(T value)
Definition: SkTemplates.h:43
static constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d)
Definition: SkTypes.h:167
sk_sp< SkTypeface > makeFromStream(std::unique_ptr< SkStreamAsset >, int ttcIndex=0) const
Definition: SkFontMgr.cpp:127
SkRect getBounds() const
Definition: SkTypeface.cpp:507
int getVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const
Definition: SkTypeface.cpp:289
sk_sp< SkTypeface > makeClone(const SkFontArguments &) const
Definition: SkTypeface.cpp:190
T * get() const
Definition: SkRefCnt.h:303
const EmbeddedViewParams * params
uint8_t value
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
bool isEmpty() const
Definition: SkRect.h:693

◆ DEF_TEST() [7/13]

DEF_TEST ( TypefaceAxesParameters  ,
reporter   
)

Definition at line 447 of file TypefaceTest.cpp.

447 {
449
450 // In DWrite in at least up to 1901 18363.1198 IDWriteFontFace5::GetFontAxisValues and
451 // GetFontAxisValueCount along with IDWriteFontResource::GetFontAxisAttributes and
452 // GetFontAxisCount (and related) seem to incorrectly collapse multiple axes with the same tag.
453 // Since this is a limitation of the underlying implementation, for now allow the test to pass
454 // with the axis tag count (as opposed to the axis count). Eventually all implementations should
455 // pass this test without 'alsoAcceptedAxisTagCount'.
456 auto test = [&](SkTypeface* typeface, const Axis* expected, int expectedCount,
457 int alsoAcceptedAxisTagCount)
458 {
459 if (!typeface) {
460 return; // Not all SkFontMgr can makeFromStream().
461 }
462
463 int actualCount = typeface->getVariationDesignParameters(nullptr, 0);
464 if (actualCount == -1) {
465 return; // The number of axes is unknown.
466 }
467 REPORTER_ASSERT(reporter, actualCount == expectedCount ||
468 actualCount == alsoAcceptedAxisTagCount);
469
470 std::unique_ptr<Axis[]> actual(new Axis[actualCount]);
471 actualCount = typeface->getVariationDesignParameters(actual.get(), actualCount);
472 if (actualCount == -1) {
473 return; // The position cannot be determined.
474 }
475 REPORTER_ASSERT(reporter, actualCount == expectedCount ||
476 actualCount == alsoAcceptedAxisTagCount);
477
478 // Every actual must be expected.
479 std::unique_ptr<bool[]> expectedUsed(new bool[expectedCount]());
480 for (int actualIdx = 0; actualIdx < actualCount; ++actualIdx) {
481 bool actualFound = false;
482 for (int expectedIdx = 0; expectedIdx < expectedCount; ++expectedIdx) {
483 if (expectedUsed[expectedIdx]) {
484 continue;
485 }
486
487 if (actual[actualIdx].tag != expected[expectedIdx].tag) {
488 continue;
489 }
490
491 // Convert to fixed for "almost equal".
492 SkFixed fixedActualMin = SkScalarToFixed(actual[actualIdx].min);
493 SkFixed fixedExpectedMin = SkScalarToFixed(expected[expectedIdx].min);
494 if (!(SkTAbs(fixedActualMin - fixedExpectedMin) < 2)) {
495 continue;
496 }
497
498 SkFixed fixedActualMax = SkScalarToFixed(actual[actualIdx].max);
499 SkFixed fixedExpectedMax = SkScalarToFixed(expected[expectedIdx].max);
500 if (!(SkTAbs(fixedActualMax - fixedExpectedMax) < 2)) {
501 continue;
502 }
503
504 SkFixed fixedActualDefault = SkScalarToFixed(actual[actualIdx].def);
505 SkFixed fixedExpectedDefault = SkScalarToFixed(expected[expectedIdx].def);
506 if (!(SkTAbs(fixedActualDefault - fixedExpectedDefault) < 2)) {
507 continue;
508 }
509
510 // This seems silly, but allows MSAN to ensure that isHidden is initialized.
511 // In GDI or before macOS 10.12, Win10, or FreeType 2.8.1 API for hidden is missing.
512 if (actual[actualIdx].isHidden() &&
513 actual[actualIdx].isHidden() != expected[expectedIdx].isHidden())
514 {
515 continue;
516 }
517
518 // This actual matched an unused expected.
519 actualFound = true;
520 expectedUsed[expectedIdx] = true;
521 break;
522 }
523 REPORTER_ASSERT(reporter, actualFound,
524 "Actual axis '%c%c%c%c' with min %f max %f default %f hidden %s not expected",
525 (char)((actual[actualIdx].tag >> 24) & 0xFF),
526 (char)((actual[actualIdx].tag >> 16) & 0xFF),
527 (char)((actual[actualIdx].tag >> 8) & 0xFF),
528 (char)((actual[actualIdx].tag ) & 0xFF),
529 actual[actualIdx].min,
530 actual[actualIdx].def,
531 actual[actualIdx].max,
532 actual[actualIdx].isHidden() ? "true" : "false");
533 }
534 };
535
537
538 // Two axis OpenType variable font.
539 {
540 std::unique_ptr<SkStreamAsset> variable(GetResourceAsStream("fonts/Variable.ttf"));
541 if (!variable) {
542 REPORT_FAILURE(reporter, "variable", SkString());
543 return;
544 }
545 constexpr Axis expected[] = {
546 Axis(SkSetFourByteTag('w','g','h','t'), 100.0f, 400.0f, 900.0f, true ),
547 Axis(SkSetFourByteTag('w','d','t','h'), 50.0f, 100.0f, 200.0f, false),
548 };
549 sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(variable), 0);
550 test(typeface.get(), &expected[0], std::size(expected), -1);
551 }
552
553 // Multiple axes with the same tag (and min, max, default) works.
554 {
555 std::unique_ptr<SkStreamAsset> dupTags(GetResourceAsStream("fonts/VaryAlongQuads.ttf"));
556 if (!dupTags) {
557 REPORT_FAILURE(reporter, "dupTags", SkString());
558 return;
559 }
560
561 // The position may be over specified. If there are multiple values for a given axis,
562 // ensure the last one since that's what css-fonts-4 requires.
563 constexpr Axis expected[] = {
564 Axis(SkSetFourByteTag('w','g','h','t'), 100.0f, 400.0f, 900.0f, false),
565 Axis(SkSetFourByteTag('w','g','h','t'), 100.0f, 400.0f, 900.0f, false),
566 };
567 sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(dupTags), 0);
568 test(typeface.get(), &expected[0], std::size(expected), 1);
569 }
570
571 // Simple single axis GX variable font.
572 {
573 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
574 if (!distortable) {
575 REPORT_FAILURE(reporter, "distortable", SkString());
576 return;
577 }
578 constexpr Axis expected[] = {
579 Axis(SkSetFourByteTag('w','g','h','t'), 0.5f, 1.0f, 2.0f, true),
580 };
581 sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(distortable), 0);
582 test(typeface.get(), &expected[0], std::size(expected), -1);
583 }
584}
int getVariationDesignParameters(SkFontParameters::Variation::Axis parameters[], int parameterCount) const
Definition: SkTypeface.cpp:295
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48

◆ DEF_TEST() [8/13]

DEF_TEST ( TypefaceCache  ,
reporter   
)

Definition at line 598 of file TypefaceTest.cpp.

598 {
600 {
603 {
605 cache.add(t0);
607 cache.add(t1);
609 cache.purgeAll();
611 }
613 cache.purgeAll();
615 }
616 REPORTER_ASSERT(reporter, t1->unique());
617}
static sk_sp< SkTypeface > Make()

◆ DEF_TEST() [9/13]

DEF_TEST ( TypefacePostScriptName  ,
reporter   
)

Definition at line 209 of file TypefaceTest.cpp.

209 {
211 if (!typeface) {
212 // Not all SkFontMgr can MakeFromStream().
213 return;
214 }
215
216 SkString postScriptName;
217 bool hasName = typeface->getPostScriptName(&postScriptName);
218 bool hasName2 = typeface->getPostScriptName(nullptr);
219 REPORTER_ASSERT(reporter, hasName == hasName2);
220 if (hasName) {
221 REPORTER_ASSERT(reporter, postScriptName == SkString("Em"));
222 }
223}
sk_sp< SkTypeface > CreateTypefaceFromResource(const char *resource, int ttcIndex)

◆ DEF_TEST() [10/13]

DEF_TEST ( TypefaceRoundTrip  ,
reporter   
)

Definition at line 225 of file TypefaceTest.cpp.

225 {
227 if (!typeface) {
228 // Not all SkFontMgr can MakeFromStream().
229 return;
230 }
231
232 int fontIndex;
233 std::unique_ptr<SkStreamAsset> stream = typeface->openStream(&fontIndex);
234
235 sk_sp<SkTypeface> typeface2 =
236 ToolUtils::TestFontMgr()->makeFromStream(std::move(stream), fontIndex);
237 REPORTER_ASSERT(reporter, typeface2);
238}

◆ DEF_TEST() [11/13]

DEF_TEST ( TypefaceStyle  ,
reporter   
)

Definition at line 103 of file TypefaceTest.cpp.

103 {
104 std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream("fonts/Em.ttf"));
105 if (!stream) {
106 REPORT_FAILURE(reporter, "fonts/Em.ttf", SkString("Cannot load resource"));
107 return;
108 }
110
111 using SkFS = SkFontStyle;
112 for (int weight = SkFS::kInvisible_Weight; weight <= SkFS::kExtraBlack_Weight; ++weight) {
113 TypefaceStyle_test(reporter, weight, 5, data.get());
114 }
115 for (int width = SkFS::kUltraCondensed_Width; width <= SkFS::kUltraExpanded_Width; ++width) {
117 }
118}
static void TypefaceStyle_test(skiatest::Reporter *reporter, uint16_t weight, uint16_t width, SkData *data)
static sk_sp< SkData > MakeFromStream(SkStream *, size_t size)
Definition: SkData.cpp:208
int32_t width
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ DEF_TEST() [12/13]

DEF_TEST ( TypefaceStyleVariable  ,
reporter   
)

Definition at line 120 of file TypefaceTest.cpp.

120 {
121 using Variation = SkFontArguments::VariationPosition;
123
124 std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream("fonts/Variable.ttf"));
125 if (!stream) {
126 REPORT_FAILURE(reporter, "fonts/Variable.ttf", SkString("Cannot load resource"));
127 return;
128 }
129 sk_sp<SkTypeface> typeface(ToolUtils::TestFontMgr()->makeFromStream(stream->duplicate()));
130 if (!typeface) {
131 // Not all SkFontMgr can MakeFromStream().
132 return;
133 }
134
135 // Creating Variable.ttf without any extra parameters should have a normal font style.
136 SkFontStyle fs = typeface->fontStyle();
138 "fs: %d %d %d", fs.weight(), fs.width(), fs.slant());
139
140 // Ensure that the font supports variable stuff
141 Variation::Coordinate varPos[2];
142 int numAxes = typeface->getVariationDesignPosition(varPos, std::size(varPos));
143 if (numAxes <= 0) {
144 // Not all SkTypeface can get the variation.
145 return;
146 }
147 if (numAxes != 2) {
148 // Variable.ttf has two axes.
149 REPORTER_ASSERT(reporter, numAxes == 2);
150 return;
151 }
152
153 // If a fontmgr or typeface can do variations, ensure the variation affects the reported style.
154 struct TestCase {
155 std::vector<Variation::Coordinate> position;
156 SkFontStyle expected;
157
158 // On Mac10.15 and earlier, the wdth affected the style using the old gx ranges.
159 // On macOS 11 and later, the wdth affects the style using the new OpenType ranges.
160 // Allow old CoreText to report the wrong width values.
161 SkFontStyle mac1015expected;
162 } testCases[] = {
163 // In range but non-default
164 { {{ SkSetFourByteTag('w','g','h','t'), 200.0f },
165 { SkSetFourByteTag('w','d','t','h'), 75.0f }},
168
169 // Out of range low, should clamp
170 { {{ SkSetFourByteTag('w','g','h','t'), 0.0f },
171 { SkSetFourByteTag('w','d','t','h'), 75.0f }},
174
175 // Out of range high, should clamp
176 { {{ SkSetFourByteTag('w','g','h','t'), 10000.0f },
177 { SkSetFourByteTag('w','d','t','h'), 75.0f }},
180 };
181
182 auto runTest = [&fm, &typeface, &stream, &reporter](TestCase& test){
183 static const constexpr bool isMac =
184#if defined(SK_BUILD_FOR_MAC)
185 true;
186#else
187 false;
188#endif
190 args.setVariationDesignPosition(Variation{test.position.data(), (int)test.position.size()});
191
192 sk_sp<SkTypeface> nonDefaultTypeface = fm->makeFromStream(stream->duplicate(), args);
193 SkFontStyle ndfs = nonDefaultTypeface->fontStyle();
194 REPORTER_ASSERT(reporter, ndfs == test.expected || (isMac && ndfs == test.mac1015expected),
195 "ndfs: %d %d %d", ndfs.weight(), ndfs.width(), ndfs.slant());
196
197 sk_sp<SkTypeface> cloneTypeface = typeface->makeClone(args);
198 SkFontStyle cfs = cloneTypeface->fontStyle();
199 REPORTER_ASSERT(reporter, cfs == test.expected || (isMac && cfs == test.mac1015expected),
200 "cfs: %d %d %d", cfs.weight(), cfs.width(), cfs.slant());
201
202 };
203
204 for (auto&& testCase : testCases) {
205 runTest(testCase);
206 }
207}
Slant slant() const
Definition: SkFontStyle.h:64
int width() const
Definition: SkFontStyle.h:63
int weight() const
Definition: SkFontStyle.h:62
static constexpr SkFontStyle Normal()
Definition: SkFontStyle.h:66
SkFontStyle fontStyle() const
Definition: SkTypeface.h:55
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args

◆ DEF_TEST() [13/13]

DEF_TEST ( TypefaceVariationIndex  ,
reporter   
)

Definition at line 395 of file TypefaceTest.cpp.

395 {
396 std::unique_ptr<SkStreamAsset> distortable(GetResourceAsStream("fonts/Distortable.ttf"));
397 if (!distortable) {
398 REPORT_FAILURE(reporter, "distortable", SkString());
399 return;
400 }
401
404 // The first named variation position in Distortable is 'Thin'.
405 params.setCollectionIndex(0x00010000);
406 sk_sp<SkTypeface> typeface = fm->makeFromStream(std::move(distortable), params);
407 if (!typeface) {
408 // FreeType is the only weird thing that supports this, Skia just needs to make sure if it
409 // gets one of these things make sense.
410 return;
411 }
412
413 int count = typeface->getVariationDesignPosition(nullptr, 0);
414 if (!(count == 1)) {
415 REPORT_FAILURE(reporter, "count == 1", SkString());
416 return;
417 }
418
420 count = typeface->getVariationDesignPosition(positionRead, std::size(positionRead));
421 if (count == -1) {
422 return;
423 }
424 if (!(count == 1)) {
425 REPORT_FAILURE(reporter, "count == 1", SkString());
426 return;
427 }
428 REPORTER_ASSERT(reporter, positionRead[0].axis == SkSetFourByteTag('w','g','h','t'));
429 REPORTER_ASSERT(reporter, positionRead[0].value == 0.5,
430 "positionRead[0].value: %f", positionRead[0].value);
431}

◆ TypefaceStyle_test()

static void TypefaceStyle_test ( skiatest::Reporter reporter,
uint16_t  weight,
uint16_t  width,
SkData data 
)
static

Definition at line 46 of file TypefaceTest.cpp.

48{
49 sk_sp<SkData> dataCopy;
50 if (!data->unique()) {
51 dataCopy = SkData::MakeWithCopy(data->data(), data->size());
52 data = dataCopy.get();
53 }
54 SkSFNTHeader* sfntHeader = static_cast<SkSFNTHeader*>(data->writable_data());
55
57 SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
58 SkSFNTHeader::TableDirectoryEntry* os2TableEntry = nullptr;
59 int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
60 for (int tableEntryIndex = 0; tableEntryIndex < numTables; ++tableEntryIndex) {
61 if (SkOTTableOS2::TAG == tableEntry[tableEntryIndex].tag) {
62 os2TableEntry = tableEntry + tableEntryIndex;
63 break;
64 }
65 }
66 SkASSERT_RELEASE(os2TableEntry);
67
68 size_t os2TableOffset = SkEndian_SwapBE32(os2TableEntry->offset);
69 SkOTTableOS2_V0* os2Table = SkTAddOffset<SkOTTableOS2_V0>(sfntHeader, os2TableOffset);
70 os2Table->usWeightClass.value = SkEndian_SwapBE16(weight);
71 using WidthType = SkOTTableOS2_V0::WidthClass::Value;
72 os2Table->usWidthClass.value = static_cast<WidthType>(SkEndian_SwapBE16(width));
73
74 sk_sp<SkTypeface> newTypeface(ToolUtils::TestFontMgr()->makeFromData(sk_ref_sp(data)));
75 if (!newTypeface) {
76 // Not all SkFontMgr can MakeFromStream().
77 return;
78 }
79
80 SkFontStyle newStyle = newTypeface->fontStyle();
81
82 //printf("%d, %f\n", weight, (newStyle.weight() - (float)0x7FFF) / (float)0x7FFF);
83 //printf("%d, %f\n", width , (newStyle.width() - (float)0x7F) / (float)0x7F);
84 //printf("%d, %d\n", weight, newStyle.weight());
85 //printf("%d, %d\n", width , newStyle.width());
86
87 // Some back-ends (CG, GDI, DW) support OS/2 version A which uses 0 - 10 (but all differently).
89 newStyle.weight() == weight ||
90 (weight <= 10 && newStyle.weight() == 100 * weight) ||
91 (weight == 4 && newStyle.weight() == 350) || // GDI weirdness
92 (weight == 5 && newStyle.weight() == 400) || // GDI weirdness
93 (weight == 0 && newStyle.weight() == 1) || // DW weirdness
94 (weight == 1000 && newStyle.weight() == 999) // DW weirdness
95 );
96
97 // Some back-ends (GDI) don't support width, ensure these always report 'normal'.
100 newStyle.width() == width || newStyle.width() == SkFontStyle::Width::kNormal_Width,
101 "newStyle.width(): %d width: %" PRIu16, newStyle.width(), width);
102}
#define SkASSERT_RELEASE(cond)
Definition: SkAssert.h:100
#define SkEndian_SwapBE32(n)
Definition: SkEndian.h:136
#define SkEndian_SwapBE16(n)
Definition: SkEndian.h:135
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition: SkData.cpp:111
enum SkOTTableOS2_V0::WidthClass::Value value
struct SkOTTableOS2_V0::WidthClass usWidthClass
struct SkOTTableOS2_V0::WeightClass usWeightClass
static constexpr SK_OT_ULONG TAG
SK_SFNT_USHORT numTables
Definition: SkSFNTHeader.h:51