blob: 74f50a921885fc1e7e5d2f39075de0b11a284efb [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"
Hal Canary3560ea72019-01-08 13:01:58 -050010#include "SkFont.h"
Ben Wagner37a06c02018-06-22 21:11:00 +000011#include "SkFontMgr.h"
12#include "SkFontMgr_fontconfig.h"
13#include "SkTypeface.h"
14#include "Test.h"
15
Hal Canary8a001442018-09-19 11:31:27 -040016#include <fontconfig/fontconfig.h>
17
Ben Wagner37a06c02018-06-22 21:11:00 +000018static 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
31DEF_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
Hal Canary3560ea72019-01-08 13:01:58 -050051 SkPaint paint;
52 paint.setColor(SK_ColorGRAY);
Ben Wagner37a06c02018-06-22 21:11:00 +000053
Hal Canary3560ea72019-01-08 13:01:58 -050054 constexpr float kTextSize = 20;
Ben Wagner37a06c02018-06-22 21:11:00 +000055
56 std::unique_ptr<SkStreamAsset> distortableStream(
57 GetResourceAsStream("fonts/Distortable.ttf"));
58 if (!distortableStream) {
59 return;
60 }
61
Ben Wagner37a06c02018-06-22 21:11:00 +000062 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
Hal Canary3560ea72019-01-08 13:01:58 -050073 SkFont fontStream(
Ben Wagner37a06c02018-06-22 21:11:00 +000074 fontMgr->makeFromStream(distortableStream->duplicate(),
Hal Canary3560ea72019-01-08 13:01:58 -050075 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";
Ben Wagner37a06c02018-06-22 21:11:00 +000084
85 canvasStream.drawColor(SK_ColorWHITE);
Hal Canary3560ea72019-01-08 13:01:58 -050086 canvasStream.drawString(text, point.fX, point.fY, fontStream, paint);
Ben Wagner37a06c02018-06-22 21:11:00 +000087
88 canvasClone.drawColor(SK_ColorWHITE);
Hal Canary3560ea72019-01-08 13:01:58 -050089 canvasClone.drawString(text, point.fX, point.fY, fontClone, paint);
Ben Wagner37a06c02018-06-22 21:11:00 +000090
91 bool success = bitmap_compare(bitmapStream, bitmapClone);
92 REPORTER_ASSERT(reporter, success);
93 }
Hal Canary8a001442018-09-19 11:31:27 -040094}