From 37a06c063c9a9bb53f8840e8304706c3ff7fe141 Mon Sep 17 00:00:00 2001 From: Ben Wagner Date: Fri, 22 Jun 2018 21:11:00 +0000 Subject: Reland "Implement onMakeClone(const SkFontArguments& args) in class SkTypeface_fontconfig." This reverts commit cce82d2b7c6ce3addd72016191e1be73bc084953. Reason for revert: Just add suppression. Original change's description: > Revert "Implement onMakeClone(const SkFontArguments& args) in class SkTypeface_fontconfig." > > This reverts commit 96b1ecc25d00a3d52dcc51a788b0df0acdd1dad9. > > Reason for revert: Breaking Google3, I think > > Original change's description: > > Implement onMakeClone(const SkFontArguments& args) in class SkTypeface_fontconfig. > > > > Create FontMgrFontConfigTest.cpp file to test the above function. > > > > Change-Id: I7716355f702af3d6f25574305914f0b82a4147ce > > Reviewed-on: https://skia-review.googlesource.com/137133 > > Reviewed-by: Ben Wagner > > Commit-Queue: Ben Wagner > > TBR=bungeman@google.com,herb@google.com,brucewang@google.com > > Change-Id: I665de46d379f1b533358f0be644814aa4bfffb33 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Reviewed-on: https://skia-review.googlesource.com/137240 > Reviewed-by: Kevin Lubick > Commit-Queue: Kevin Lubick TBR=bungeman@google.com,herb@google.com,kjlubick@google.com,brucewang@google.com Change-Id: I41f0e4505ed0579a77a56212898e9a6caf6f1ed5 No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://skia-review.googlesource.com/137260 Commit-Queue: Ben Wagner Reviewed-by: Ben Wagner --- BUILD.gn | 3 ++ gn/tests.gni | 1 + public.bzl | 1 + src/ports/SkFontMgr_fontconfig.cpp | 15 ++++++ tests/FontMgrFontConfigTest.cpp | 95 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 tests/FontMgrFontConfigTest.cpp diff --git a/BUILD.gn b/BUILD.gn index f3f170ebfc..97cbf731f6 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1390,6 +1390,9 @@ if (skia_enable_tools) { if (!fontmgr_android_enabled) { sources -= [ "//tests/FontMgrAndroidParserTest.cpp" ] } + if (!(skia_use_freetype && skia_use_fontconfig)) { + sources -= [ "//tests/FontMgrFontConfigTest.cpp" ] + } deps = [ ":experimental_svg_model", ":flags", diff --git a/gn/tests.gni b/gn/tests.gni index 32c3337adc..2068ae527c 100644 --- a/gn/tests.gni +++ b/gn/tests.gni @@ -77,6 +77,7 @@ tests_sources = [ "$_tests/FontHostStreamTest.cpp", "$_tests/FontHostTest.cpp", "$_tests/FontMgrAndroidParserTest.cpp", + "$_tests/FontMgrFontConfigTest.cpp", "$_tests/FontMgrTest.cpp", "$_tests/FontNamesTest.cpp", "$_tests/FontObjTest.cpp", diff --git a/public.bzl b/public.bzl index e8fbf61cbe..b99c60c985 100644 --- a/public.bzl +++ b/public.bzl @@ -496,6 +496,7 @@ DM_SRCS_ALL = struct( exclude = [ "gm/cgms.cpp", "tests/FontMgrAndroidParserTest.cpp", # Android-only. + "tests/FontMgrFontConfigTest.cpp", # FontConfig-only. "tests/skia_test.cpp", # Old main. "tools/gpu/atlastext/*", "tools/gpu/gl/angle/*", diff --git a/src/ports/SkFontMgr_fontconfig.cpp b/src/ports/SkFontMgr_fontconfig.cpp index 3589ae8100..e5aed18c60 100644 --- a/src/ports/SkFontMgr_fontconfig.cpp +++ b/src/ports/SkFontMgr_fontconfig.cpp @@ -525,6 +525,21 @@ public: return info; } + sk_sp onMakeClone(const SkFontArguments& args) const override { + std::unique_ptr data = this->cloneFontData(args); + if (!data) { + return nullptr; + } + + SkString fFamilyName; + this->getFamilyName(&fFamilyName); + + return sk_make_sp(std::move(data), + fFamilyName, + this->fontStyle(), + this->isFixedPitch()); + } + ~SkTypeface_fontconfig() override { // Hold the lock while unrefing the pattern. FCLocker lock; diff --git a/tests/FontMgrFontConfigTest.cpp b/tests/FontMgrFontConfigTest.cpp new file mode 100644 index 0000000000..0ecca63034 --- /dev/null +++ b/tests/FontMgrFontConfigTest.cpp @@ -0,0 +1,95 @@ +/* + * Copyright 2014 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include +#include "Resources.h" +#include "SkCanvas.h" +#include "SkFontMgr.h" +#include "SkFontMgr_fontconfig.h" +#include "SkTypeface.h" +#include "Test.h" + +static bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) { + for (int y = 0; y < test.height(); ++y) { + for (int x = 0; x < test.width(); ++x) { + SkColor testColor = test.getColor(x, y); + SkColor refColor = ref.getColor(x, y); + if (refColor != testColor) { + return false; + } + } + } + return true; +} + +DEF_TEST(FontMgrFontConfig, reporter) { + FcConfig* config = FcConfigCreate(); + SkString distortablePath = GetResourcePath("fonts/Distortable.ttf"); + FcConfigAppFontAddFile( + config, reinterpret_cast(distortablePath.c_str())); + FcConfigBuildFonts(config); + + sk_sp fontMgr(SkFontMgr_New_FontConfig(config)); + sk_sp typeface(fontMgr->legacyMakeTypeface("Distortable", SkFontStyle())); + + SkBitmap bitmapStream; + bitmapStream.allocN32Pixels(64, 64); + SkCanvas canvasStream(bitmapStream); + canvasStream.drawColor(SK_ColorWHITE); + + SkBitmap bitmapClone; + bitmapClone.allocN32Pixels(64, 64); + SkCanvas canvasClone(bitmapClone); + canvasStream.drawColor(SK_ColorWHITE); + + SkPaint paintStream; + paintStream.setColor(SK_ColorGRAY); + paintStream.setTextSize(SkIntToScalar(20)); + paintStream.setAntiAlias(true); + paintStream.setLCDRenderText(true); + + SkPaint paintClone; + paintClone.setColor(SK_ColorGRAY); + paintClone.setTextSize(SkIntToScalar(20)); + paintClone.setAntiAlias(true); + paintClone.setLCDRenderText(true); + + std::unique_ptr distortableStream( + GetResourceAsStream("fonts/Distortable.ttf")); + if (!distortableStream) { + return; + } + + const char* text = "abc"; + const size_t textLen = strlen(text); + SkPoint point = SkPoint::Make(20.0f, 20.0f); + SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't'); + + for (int i = 0; i < 10; ++i) { + SkScalar styleValue = + SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10)); + SkFontArguments::VariationPosition::Coordinate + coordinates[] = {{tag, styleValue}}; + SkFontArguments::VariationPosition + position = {coordinates, SK_ARRAY_COUNT(coordinates)}; + + paintStream.setTypeface(sk_sp( + fontMgr->makeFromStream(distortableStream->duplicate(), + SkFontArguments().setVariationDesignPosition(position)))); + paintClone.setTypeface(sk_sp( + typeface->makeClone(SkFontArguments().setVariationDesignPosition(position)))); + + canvasStream.drawColor(SK_ColorWHITE); + canvasStream.drawText(text, textLen, point.fX, point.fY, paintStream); + + canvasClone.drawColor(SK_ColorWHITE); + canvasClone.drawText(text, textLen, point.fX, point.fY, paintClone); + + bool success = bitmap_compare(bitmapStream, bitmapClone); + REPORTER_ASSERT(reporter, success); + } +} \ No newline at end of file -- cgit v1.2.3