• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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 "src/base/SkAutoMalloc.h"
9 #include "src/core/SkFontScanner.h"
10 #include "src/core/SkTHash.h"
11 #include "src/core/SkWriteBuffer.h"
12 #include "tests/Test.h"
13 #include "tools/Resources.h"
14 #include "tools/fonts/FontToolUtils.h"
15 
16 #ifdef SK_TYPEFACE_FACTORY_FREETYPE
17 #include "src/ports/SkTypeface_FreeType.h"
18 #endif
19 #ifdef SK_TYPEFACE_FACTORY_FONTATIONS
20 #include "src/ports/SkFontScanner_fontations.h"
21 #endif
22 
23 [[maybe_unused]]
FontScanner_VariableFont(skiatest::Reporter * reporter,SkFontScanner * scanner,bool defaultInstanceOnly)24 static void FontScanner_VariableFont(skiatest::Reporter* reporter,
25                                      SkFontScanner* scanner,
26                                      bool defaultInstanceOnly) {
27     SkString name = GetResourcePath("fonts/Variable.ttf");
28 
29     std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(name.c_str());
30     if (!stream) {
31          REPORTER_ASSERT(reporter, false, "Cannot open the font file %s\n", name.c_str());
32     }
33 
34     int numFaces;
35     if (!scanner->scanFile(stream.get(), &numFaces)) {
36          REPORTER_ASSERT(reporter, false, "Cannot scanFile\n");
37     }
38     REPORTER_ASSERT(reporter, numFaces == 1);
39 
40     skia_private::THashSet<SkFontStyle> uniqueStyles;
41     for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
42         int numInstances;
43         if (!scanner->scanFace(stream.get(), faceIndex, &numInstances)) {
44             REPORTER_ASSERT(reporter, false, "Cannot scanFace\n");
45             continue;
46         }
47         if (defaultInstanceOnly) {
48             REPORTER_ASSERT(reporter, numInstances == 1);
49             continue;
50         }
51         REPORTER_ASSERT(reporter, numInstances == 5);
52         for (int instanceIndex = 1; instanceIndex <= numInstances; ++instanceIndex) {
53             bool isFixedPitch;
54             SkString realname;
55             SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
56             if (!scanner->scanInstance(stream.get(),
57                                         faceIndex,
58                                         instanceIndex,
59                                         &realname,
60                                         &style,
61                                         &isFixedPitch,
62                                         nullptr)) {
63                 REPORTER_ASSERT(reporter,
64                                 false,
65                                 "Cannot scanInstance %s %d\n",
66                                 name.c_str(),
67                                 faceIndex);
68                 continue;
69             } else {
70                 if (uniqueStyles.find(style) == nullptr) {
71                     uniqueStyles.add(style);
72                 } else {
73                     REPORTER_ASSERT(
74                         reporter,
75                         false,
76                         "Font: %s (%d %d %d)\n",
77                         realname.c_str(), style.weight(), style.width(), style.slant());
78                 }
79             }
80         }
81         REPORTER_ASSERT(reporter, uniqueStyles.count() == numInstances);
82     }
83 }
84 
85 #ifdef SK_TYPEFACE_FACTORY_FREETYPE
DEF_TEST(FontScanner_FreeType_VariableFont,reporter)86 DEF_TEST(FontScanner_FreeType_VariableFont, reporter) {
87     SkFontScanner_FreeType free_type;
88     FontScanner_VariableFont(reporter, &free_type, /*defaultInstanceOnly=*/ false);
89 }
90 #endif
91 
92 #ifdef SK_TYPEFACE_FACTORY_FONTATIONS
DEF_TEST(FontScanner_Fontations_VariableFont,reporter)93 DEF_TEST(FontScanner_Fontations_VariableFont, reporter) {
94     SkFontScanner_Fontations fontations;
95     FontScanner_VariableFont(reporter, &fontations,/*defaultInstanceOnly=*/ true);
96 }
97 #endif
98