• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Resources.h"
9 #include "SkCanvas.h"
10 #include "SkFont.h"
11 #include "SkFontMgr.h"
12 #include "SkFontMgr_fontconfig.h"
13 #include "SkTypeface.h"
14 #include "Test.h"
15 
16 #include <fontconfig/fontconfig.h>
17 
bitmap_compare(const SkBitmap & ref,const SkBitmap & test)18 static bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) {
19     for (int y = 0; y < test.height(); ++y) {
20         for (int x = 0; x < test.width(); ++x) {
21             SkColor testColor = test.getColor(x, y);
22             SkColor refColor = ref.getColor(x, y);
23             if (refColor != testColor) {
24                 return false;
25             }
26         }
27     }
28     return true;
29 }
30 
DEF_TEST(FontMgrFontConfig,reporter)31 DEF_TEST(FontMgrFontConfig, reporter) {
32     FcConfig* config = FcConfigCreate();
33     SkString distortablePath = GetResourcePath("fonts/Distortable.ttf");
34     FcConfigAppFontAddFile(
35         config, reinterpret_cast<const FcChar8*>(distortablePath.c_str()));
36     FcConfigBuildFonts(config);
37 
38     sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_FontConfig(config));
39     sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("Distortable", SkFontStyle()));
40 
41     SkBitmap bitmapStream;
42     bitmapStream.allocN32Pixels(64, 64);
43     SkCanvas canvasStream(bitmapStream);
44     canvasStream.drawColor(SK_ColorWHITE);
45 
46     SkBitmap bitmapClone;
47     bitmapClone.allocN32Pixels(64, 64);
48     SkCanvas canvasClone(bitmapClone);
49     canvasStream.drawColor(SK_ColorWHITE);
50 
51     SkPaint paint;
52     paint.setColor(SK_ColorGRAY);
53 
54     constexpr float kTextSize = 20;
55 
56     std::unique_ptr<SkStreamAsset> distortableStream(
57         GetResourceAsStream("fonts/Distortable.ttf"));
58     if (!distortableStream) {
59         return;
60     }
61 
62     SkPoint point = SkPoint::Make(20.0f, 20.0f);
63     SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't');
64 
65     for (int i = 0; i < 10; ++i) {
66         SkScalar styleValue =
67             SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10));
68         SkFontArguments::VariationPosition::Coordinate
69             coordinates[] = {{tag, styleValue}};
70         SkFontArguments::VariationPosition
71             position = {coordinates, SK_ARRAY_COUNT(coordinates)};
72 
73         SkFont fontStream(
74             fontMgr->makeFromStream(distortableStream->duplicate(),
75                                     SkFontArguments().setVariationDesignPosition(position)),
76             kTextSize);
77         fontStream.setEdging(SkFont::Edging::kSubpixelAntiAlias);
78 
79         SkFont fontClone(
80             typeface->makeClone(SkFontArguments().setVariationDesignPosition(position)), kTextSize);
81         fontClone.setEdging(SkFont::Edging::kSubpixelAntiAlias);
82 
83         constexpr char text[] = "abc";
84 
85         canvasStream.drawColor(SK_ColorWHITE);
86         canvasStream.drawString(text, point.fX, point.fY, fontStream, paint);
87 
88         canvasClone.drawColor(SK_ColorWHITE);
89         canvasClone.drawString(text, point.fX, point.fY, fontClone, paint);
90 
91         bool success = bitmap_compare(bitmapStream, bitmapClone);
92         REPORTER_ASSERT(reporter, success);
93     }
94 }
95