Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 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 | |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 8 | #include "Resources.h" |
| 9 | #include "SkCanvas.h" |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 10 | #include "SkFont.h" |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 11 | #include "SkFontMgr.h" |
| 12 | #include "SkFontMgr_fontconfig.h" |
| 13 | #include "SkTypeface.h" |
| 14 | #include "Test.h" |
| 15 | |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 16 | #include <fontconfig/fontconfig.h> |
| 17 | |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 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 | |
| 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 | |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 51 | SkPaint paint; |
| 52 | paint.setColor(SK_ColorGRAY); |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 53 | |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 54 | constexpr float kTextSize = 20; |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 55 | |
| 56 | std::unique_ptr<SkStreamAsset> distortableStream( |
| 57 | GetResourceAsStream("fonts/Distortable.ttf")); |
| 58 | if (!distortableStream) { |
| 59 | return; |
| 60 | } |
| 61 | |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 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 | |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 73 | SkFont fontStream( |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 74 | fontMgr->makeFromStream(distortableStream->duplicate(), |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 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"; |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 84 | |
| 85 | canvasStream.drawColor(SK_ColorWHITE); |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 86 | canvasStream.drawString(text, point.fX, point.fY, fontStream, paint); |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 87 | |
| 88 | canvasClone.drawColor(SK_ColorWHITE); |
Hal Canary | 3560ea7 | 2019-01-08 13:01:58 -0500 | [diff] [blame] | 89 | canvasClone.drawString(text, point.fX, point.fY, fontClone, paint); |
Ben Wagner | 37a06c0 | 2018-06-22 21:11:00 +0000 | [diff] [blame] | 90 | |
| 91 | bool success = bitmap_compare(bitmapStream, bitmapClone); |
| 92 | REPORTER_ASSERT(reporter, success); |
| 93 | } |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 94 | } |