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