• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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/SkFont.h"
9 #include "src/core/SkAutoMalloc.h"
10 #include "src/core/SkFontPriv.h"
11 #include "src/core/SkReadBuffer.h"
12 #include "src/core/SkWriteBuffer.h"
13 #include "tests/Test.h"
14 
serialize_deserialize(const SkFont & font,skiatest::Reporter * reporter)15 static SkFont serialize_deserialize(const SkFont& font, skiatest::Reporter* reporter) {
16     SkBinaryWriteBuffer wb;
17     SkFontPriv::Flatten(font, wb);
18 
19     size_t size = wb.bytesWritten();
20     SkAutoMalloc storage(size);
21     wb.writeToMemory(storage.get());
22 
23     SkReadBuffer rb(storage.get(), size);
24 
25     SkFont clone;
26     REPORTER_ASSERT(reporter, SkFontPriv::Unflatten(&clone, rb));
27     return clone;
28 }
29 
30 enum {
31     kForceAutoHinting      = 1 << 0,
32     kEmbeddedBitmaps       = 1 << 1,
33     kSubpixel              = 1 << 2,
34     kLinearMetrics         = 1 << 3,
35     kEmbolden              = 1 << 4,
36     kBaselineSnap          = 1 << 5,
37 
38     kAllBits = 0x3F,
39 };
40 
apply_flags(SkFont * font,unsigned flags)41 static void apply_flags(SkFont* font, unsigned flags) {
42     font->setForceAutoHinting(SkToBool(flags & kForceAutoHinting));
43     font->setEmbeddedBitmaps( SkToBool(flags & kEmbeddedBitmaps));
44     font->setSubpixel(        SkToBool(flags & kSubpixel));
45     font->setLinearMetrics(   SkToBool(flags & kLinearMetrics));
46     font->setEmbolden(        SkToBool(flags & kEmbolden));
47     font->setBaselineSnap(    SkToBool(flags & kBaselineSnap));
48 }
49 
DEF_TEST(Font_flatten,reporter)50 DEF_TEST(Font_flatten, reporter) {
51     const float sizes[] = {0, 0.001f, 1, 10, 10.001f, 100, 100000, 100000.01f};
52     const float scales[] = {-5, -1, 0, 1, 5};
53     const float skews[] = {-5, -1, 0, 1, 5};
54     const SkFont::Edging edges[] = {
55         SkFont::Edging::kAlias, SkFont::Edging::kAntiAlias, SkFont::Edging::kSubpixelAntiAlias
56     };
57     const SkFontHinting hints[] = {
58         SkFontHinting::kNone, SkFontHinting::kSlight, SkFontHinting::kNormal, SkFontHinting::kFull
59     };
60 
61     SkFont font;
62     for (float size : sizes) {
63         font.setSize(size);
64         for (float scale : scales) {
65             font.setScaleX(scale);
66             for (float skew : skews) {
67                 font.setSkewX(skew);
68                 for (auto edge : edges) {
69                     font.setEdging(edge);
70                     for (auto hint : hints) {
71                         font.setHinting(hint);
72                         for (unsigned flags = 0; flags <= kAllBits; ++flags) {
73                             apply_flags(&font, flags);
74 
75                             SkFont clone = serialize_deserialize(font, reporter);
76                             REPORTER_ASSERT(reporter, font == clone);
77                         }
78                     }
79                 }
80             }
81         }
82     }
83 }
84