Commit 0afd0147 authored by Walter Korman's avatar Walter Korman Committed by Commit Bot

Add initial ToString for FontDescription and affiliated types.

Will be useful for folks working on font support in out-of-process
raster. For more see:

https: //docs.google.com/document/d/1vsJsk1zGSagxxBggd8j5G9u0rMzD1NTaQWgEbUabEvY/preview#heading=h.xzi2kl5b59mm
Change-Id: I77797cb295495b9adcef1a620754d71882dee79f
Reviewed-on: https://chromium-review.googlesource.com/683463
Commit-Queue: Walter Korman <wkorman@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#504284}
parent 8a218af5
...@@ -749,6 +749,7 @@ jumbo_component("platform") { ...@@ -749,6 +749,7 @@ jumbo_component("platform") {
"fonts/SymbolsIterator.cpp", "fonts/SymbolsIterator.cpp",
"fonts/SymbolsIterator.h", "fonts/SymbolsIterator.h",
"fonts/TextRenderingMode.h", "fonts/TextRenderingMode.h",
"fonts/TypesettingFeatures.cpp",
"fonts/TypesettingFeatures.h", "fonts/TypesettingFeatures.h",
"fonts/UTF16TextIterator.cpp", "fonts/UTF16TextIterator.cpp",
"fonts/UTF16TextIterator.h", "fonts/UTF16TextIterator.h",
...@@ -1787,6 +1788,7 @@ test("blink_platform_unittests") { ...@@ -1787,6 +1788,7 @@ test("blink_platform_unittests") {
"fonts/AcceptLanguagesResolverTest.cpp", "fonts/AcceptLanguagesResolverTest.cpp",
"fonts/FontCacheTest.cpp", "fonts/FontCacheTest.cpp",
"fonts/FontDescriptionTest.cpp", "fonts/FontDescriptionTest.cpp",
"fonts/FontFamilyTest.cpp",
"fonts/FontPlatformDataTest.cpp", "fonts/FontPlatformDataTest.cpp",
"fonts/FontSelectionTypesTest.cpp", "fonts/FontSelectionTypesTest.cpp",
"fonts/FontTest.cpp", "fonts/FontTest.cpp",
...@@ -1796,6 +1798,7 @@ test("blink_platform_unittests") { ...@@ -1796,6 +1798,7 @@ test("blink_platform_unittests") {
"fonts/ScriptRunIteratorTest.cpp", "fonts/ScriptRunIteratorTest.cpp",
"fonts/SmallCapsIteratorTest.cpp", "fonts/SmallCapsIteratorTest.cpp",
"fonts/SymbolsIteratorTest.cpp", "fonts/SymbolsIteratorTest.cpp",
"fonts/TypesettingFeaturesTest.cpp",
"fonts/UnicodeRangeSetTest.cpp", "fonts/UnicodeRangeSetTest.cpp",
"fonts/android/FontCacheAndroidTest.cpp", "fonts/android/FontCacheAndroidTest.cpp",
"fonts/mac/FontFamilyMatcherMacTest.mm", "fonts/mac/FontFamilyMatcherMacTest.mm",
......
...@@ -347,4 +347,29 @@ SkFontStyle FontDescription::SkiaFontStyle() const { ...@@ -347,4 +347,29 @@ SkFontStyle FontDescription::SkiaFontStyle() const {
return SkFontStyle(skia_weight, skia_width, slant); return SkFontStyle(skia_weight, skia_width, slant);
} }
String FontDescription::ToString() const {
// TODO(wkorman): Incorporate BitFields.
return String::Format(
"family_list=[%s], feature_settings=[%s], variation_settings=[%s], "
"locale=%s, "
"specified_size=%f, computed_size=%f, adjusted_size=%f, "
"size_adjust=%f, letter_spacing=%f, word_spacing=%f, "
"font_selection_request=[%s], "
"typesetting_features=[%s]",
family_list_.ToString().Ascii().data(),
(feature_settings_ ? feature_settings_->ToString().Ascii().data() : ""),
(variation_settings_ ? variation_settings_->ToString().Ascii().data()
: ""),
// TODO(wkorman): Locale has additional internal fields such as
// hyphenation and script. Consider adding a more detailed
// string method.
(locale_ ? locale_->LocaleString().Ascii().data() : ""), specified_size_,
computed_size_, adjusted_size_, size_adjust_, letter_spacing_,
word_spacing_, font_selection_request_.ToString().Ascii().data(),
blink::ToString(
static_cast<TypesettingFeatures>(fields_.typesetting_features_))
.Ascii()
.data());
}
} // namespace blink } // namespace blink
...@@ -325,6 +325,8 @@ class PLATFORM_EXPORT FontDescription { ...@@ -325,6 +325,8 @@ class PLATFORM_EXPORT FontDescription {
SkFontStyle SkiaFontStyle() const; SkFontStyle SkiaFontStyle() const;
String ToString() const;
private: private:
FontFamily family_list_; // The list of font families to be used. FontFamily family_list_; // The list of font families to be used.
RefPtr<FontFeatureSettings> feature_settings_; RefPtr<FontFeatureSettings> feature_settings_;
......
...@@ -62,4 +62,50 @@ TEST(FontDescriptionTest, TestHashCollision) { ...@@ -62,4 +62,50 @@ TEST(FontDescriptionTest, TestHashCollision) {
} }
} }
TEST(FontDescriptionTest, ToString) {
FontDescription description;
FontFamily family;
family.SetFamily("A");
RefPtr<SharedFontFamily> b_family = SharedFontFamily::Create();
b_family->SetFamily("B");
family.AppendFamily(b_family);
description.SetFamily(family);
description.SetLocale(LayoutLocale::Get("no"));
RefPtr<FontVariationSettings> variation_settings =
FontVariationSettings::Create();
variation_settings->Append(FontVariationAxis{"a", 42});
variation_settings->Append(FontVariationAxis{"b", 8118});
description.SetVariationSettings(variation_settings);
RefPtr<FontFeatureSettings> feature_settings = FontFeatureSettings::Create();
feature_settings->Append(FontFeature{"c", 76});
feature_settings->Append(FontFeature{"d", 94});
description.SetFeatureSettings(feature_settings);
description.SetSpecifiedSize(1.1f);
description.SetComputedSize(2.2f);
description.SetAdjustedSize(3.3f);
description.SetSizeAdjust(4.4f);
description.SetLetterSpacing(5.5f);
description.SetWordSpacing(6.6f);
description.SetStyle(FontSelectionValue(31.5));
description.SetWeight(FontSelectionValue(32.6));
description.SetStretch(FontSelectionValue(33.7));
description.SetTextRendering(kOptimizeLegibility);
EXPECT_EQ(
"family_list=[A,B], feature_settings=[c=76,d=94], "
"variation_settings=[a=42,b=8118], locale=no, specified_size=1.100000, "
"computed_size=2.200000, adjusted_size=3.300000, size_adjust=4.400000, "
"letter_spacing=5.500000, word_spacing=6.600000, "
"font_selection_request=[weight=32.500000, width=33.500000, "
"slope=31.500000], typesetting_features=[Kerning,Ligatures]",
description.ToString());
}
} // namespace blink } // namespace blink
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#include "platform/fonts/FontFamily.h" #include "platform/fonts/FontFamily.h"
#include "platform/wtf/text/StringBuilder.h"
namespace blink { namespace blink {
bool operator==(const FontFamily& a, const FontFamily& b) { bool operator==(const FontFamily& a, const FontFamily& b) {
...@@ -42,4 +44,16 @@ bool operator==(const FontFamily& a, const FontFamily& b) { ...@@ -42,4 +44,16 @@ bool operator==(const FontFamily& a, const FontFamily& b) {
return true; return true;
} }
String FontFamily::ToString() const {
StringBuilder builder;
builder.Append(family_);
const FontFamily* current = Next();
while (current) {
builder.Append(",");
builder.Append(current->Family());
current = current->Next();
}
return builder.ToString();
}
} // namespace blink } // namespace blink
...@@ -51,6 +51,10 @@ class PLATFORM_EXPORT FontFamily { ...@@ -51,6 +51,10 @@ class PLATFORM_EXPORT FontFamily {
void AppendFamily(RefPtr<SharedFontFamily>); void AppendFamily(RefPtr<SharedFontFamily>);
RefPtr<SharedFontFamily> ReleaseNext(); RefPtr<SharedFontFamily> ReleaseNext();
// Returns this font family's name followed by all subsequent linked
// families delimited by commas.
String ToString() const;
private: private:
AtomicString family_; AtomicString family_;
RefPtr<SharedFontFamily> next_; RefPtr<SharedFontFamily> next_;
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "platform/fonts/FontFamily.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
namespace {
FontFamily* CreateAndAppendFamily(FontFamily& parent, const char* name) {
RefPtr<SharedFontFamily> family = SharedFontFamily::Create();
family->SetFamily(name);
parent.AppendFamily(family);
return family.Get();
}
} // namespace
TEST(FontFamilyTest, ToString) {
{
FontFamily family;
EXPECT_EQ("", family.ToString());
}
{
FontFamily family;
family.SetFamily("A");
CreateAndAppendFamily(family, "B");
EXPECT_EQ("A,B", family.ToString());
}
{
FontFamily family;
family.SetFamily("A");
FontFamily* b_family = CreateAndAppendFamily(family, "B");
CreateAndAppendFamily(*b_family, "C");
EXPECT_EQ("A,B,C", family.ToString());
}
}
} // namespace blink
...@@ -71,4 +71,14 @@ unsigned FontSelectionCapabilitiesHash::GetHash( ...@@ -71,4 +71,14 @@ unsigned FontSelectionCapabilitiesHash::GetHash(
return hasher.hash(); return hasher.hash();
} }
String FontSelectionValue::ToString() const {
return String::Format("%f", (float)*this);
}
String FontSelectionRequest::ToString() const {
return String::Format(
"weight=%s, width=%s, slope=%s", weight.ToString().Ascii().data(),
width.ToString().Ascii().data(), slope.ToString().Ascii().data());
}
} // namespace blink } // namespace blink
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "platform/wtf/HashTraits.h" #include "platform/wtf/HashTraits.h"
#include "platform/wtf/MathExtras.h" #include "platform/wtf/MathExtras.h"
#include "platform/wtf/StdLibExtras.h" #include "platform/wtf/StdLibExtras.h"
#include "platform/wtf/text/WTFString.h"
namespace blink { namespace blink {
...@@ -38,7 +39,7 @@ namespace blink { ...@@ -38,7 +39,7 @@ namespace blink {
// font variations. Sixteen bits in total, one sign bit, two fractional bits, // font variations. Sixteen bits in total, one sign bit, two fractional bits,
// means the smallest positive representable value is 0.25, the maximum // means the smallest positive representable value is 0.25, the maximum
// representable value is 8191.75, and the minimum representable value is -8192. // representable value is 8191.75, and the minimum representable value is -8192.
class FontSelectionValue { class PLATFORM_EXPORT FontSelectionValue {
public: public:
FontSelectionValue() = default; FontSelectionValue() = default;
...@@ -71,6 +72,8 @@ class FontSelectionValue { ...@@ -71,6 +72,8 @@ class FontSelectionValue {
int16_t RawValue() const { return backing_; } int16_t RawValue() const { return backing_; }
String ToString() const;
static const FontSelectionValue& MaximumValue() { static const FontSelectionValue& MaximumValue() {
DEFINE_THREAD_SAFE_STATIC_LOCAL( DEFINE_THREAD_SAFE_STATIC_LOCAL(
const FontSelectionValue, maximumValue, const FontSelectionValue, maximumValue,
...@@ -335,6 +338,8 @@ struct PLATFORM_EXPORT FontSelectionRequest { ...@@ -335,6 +338,8 @@ struct PLATFORM_EXPORT FontSelectionRequest {
return !operator==(other); return !operator==(other);
} }
String ToString() const;
FontSelectionValue weight; FontSelectionValue weight;
FontSelectionValue width; FontSelectionValue width;
FontSelectionValue slope; FontSelectionValue slope;
......
...@@ -2,9 +2,8 @@ ...@@ -2,9 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "platform/fonts/Font.h"
#include "platform/fonts/FontSelectionTypes.h" #include "platform/fonts/FontSelectionTypes.h"
#include "platform/wtf/HashSet.h" #include "platform/wtf/HashSet.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -31,4 +30,26 @@ TEST(FontSelectionTypesTest, HashCollisions) { ...@@ -31,4 +30,26 @@ TEST(FontSelectionTypesTest, HashCollisions) {
ASSERT_EQ(hashes.size(), weights.size() * slopes.size() * widths.size()); ASSERT_EQ(hashes.size(), weights.size() * slopes.size() * widths.size());
} }
TEST(FontSelectionTypesTest, ValueToString) {
{
FontSelectionValue value(42);
EXPECT_EQ("42.000000", value.ToString());
}
{
FontSelectionValue value(42.81f);
EXPECT_EQ("42.750000", value.ToString());
}
{
FontSelectionValue value(42.923456789123456789);
EXPECT_EQ("42.750000", value.ToString());
}
}
TEST(FontSelectionTypesTest, RequestToString) {
FontSelectionRequest request(FontSelectionValue(42), FontSelectionValue(43),
FontSelectionValue(44));
EXPECT_EQ("weight=42.000000, width=43.000000, slope=44.000000",
request.ToString());
}
} // namespace blink } // namespace blink
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "platform/fonts/TypesettingFeatures.h"
#include "platform/wtf/text/StringBuilder.h"
#include "platform/wtf/text/WTFString.h"
namespace blink {
namespace {
const char* kFeatureNames[kMaxTypesettingFeatureIndex + 1] = {
"Kerning", "Ligatures", "Caps"};
} // namespace
String ToString(TypesettingFeatures features) {
StringBuilder builder;
int featureCount = 0;
for (int i = 0; i <= kMaxTypesettingFeatureIndex; i++) {
if (features & (1 << i)) {
if (featureCount++ > 0)
builder.Append(",");
builder.Append(kFeatureNames[i]);
}
}
return builder.ToString();
}
} // namespace blink
...@@ -26,16 +26,24 @@ ...@@ -26,16 +26,24 @@
#ifndef TypesettingFeatures_h #ifndef TypesettingFeatures_h
#define TypesettingFeatures_h #define TypesettingFeatures_h
#include "platform/PlatformExport.h"
#include "platform/wtf/Forward.h"
namespace blink { namespace blink {
constexpr int kMaxTypesettingFeatureIndex = 2;
enum TypesettingFeature { enum TypesettingFeature {
kKerning = 1 << 0, kKerning = 1 << 0,
kLigatures = 1 << 1, kLigatures = 1 << 1,
kCaps = 1 << 2 kCaps = 1 << kMaxTypesettingFeatureIndex
// When adding new features be sure to increment kMaxFeatureIndex
// and update kFeatureNames.
}; };
typedef unsigned TypesettingFeatures; typedef unsigned TypesettingFeatures;
PLATFORM_EXPORT String ToString(TypesettingFeatures);
} // namespace blink } // namespace blink
#endif // TypesettingFeatures_h #endif // TypesettingFeatures_h
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "platform/fonts/TypesettingFeatures.h"
#include "platform/wtf/text/WTFString.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
TEST(TypesettingFeaturesTest, ToString) {
{
TypesettingFeatures features = 0;
EXPECT_EQ("", ToString(features));
}
{
TypesettingFeatures features = kKerning | kLigatures;
EXPECT_EQ("Kerning,Ligatures", ToString(features));
}
{
TypesettingFeatures features = kKerning | kLigatures | kCaps;
EXPECT_EQ("Kerning,Ligatures,Caps", ToString(features));
}
}
} // namespace blink
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "platform/wtf/RefPtr.h" #include "platform/wtf/RefPtr.h"
#include "platform/wtf/Vector.h" #include "platform/wtf/Vector.h"
#include "platform/wtf/text/AtomicString.h" #include "platform/wtf/text/AtomicString.h"
#include "platform/wtf/text/StringBuilder.h"
namespace blink { namespace blink {
...@@ -47,6 +48,19 @@ class FontSettings { ...@@ -47,6 +48,19 @@ class FontSettings {
bool operator==(const FontSettings& other) const { bool operator==(const FontSettings& other) const {
return list_ == other.list_; return list_ == other.list_;
}; };
String ToString() const {
StringBuilder builder;
size_t num_features = size();
for (size_t i = 0; i < num_features; ++i) {
if (i > 0)
builder.Append(",");
const AtomicString& tag = at(i).Tag();
builder.Append(tag);
builder.Append("=");
builder.AppendNumber(at(i).Value());
}
return builder.ToString();
}
protected: protected:
FontSettings(){}; FontSettings(){};
......
...@@ -9,27 +9,31 @@ ...@@ -9,27 +9,31 @@
namespace blink { namespace blink {
RefPtr<FontVariationSettings> MakeFontVariationSettings( namespace {
std::initializer_list<FontVariationAxis> variation_axes) {
RefPtr<FontVariationSettings> variation_settings =
FontVariationSettings::Create();
for (auto axis = variation_axes.begin(); axis != variation_axes.end(); template <typename T, typename U>
++axis) { RefPtr<T> MakeSettings(std::initializer_list<U> items) {
variation_settings->Append(*axis); RefPtr<T> settings = T::Create();
for (auto item = items.begin(); item != items.end(); ++item) {
settings->Append(*item);
} }
return variation_settings; return settings;
} }
} // namespace
TEST(FontSettingsTest, HashTest) { TEST(FontSettingsTest, HashTest) {
RefPtr<FontVariationSettings> one_axis_a = RefPtr<FontVariationSettings> one_axis_a =
MakeFontVariationSettings({FontVariationAxis{"a ", 0}}); MakeSettings<FontVariationSettings, FontVariationAxis>(
{FontVariationAxis{"a ", 0}});
RefPtr<FontVariationSettings> one_axis_b = RefPtr<FontVariationSettings> one_axis_b =
MakeFontVariationSettings({FontVariationAxis{"b ", 0}}); MakeSettings<FontVariationSettings, FontVariationAxis>(
RefPtr<FontVariationSettings> two_axes = MakeFontVariationSettings( {FontVariationAxis{"b ", 0}});
{FontVariationAxis{"a ", 0}, FontVariationAxis{"b ", 0}}); RefPtr<FontVariationSettings> two_axes =
MakeSettings<FontVariationSettings, FontVariationAxis>(
{FontVariationAxis{"a ", 0}, FontVariationAxis{"b ", 0}});
RefPtr<FontVariationSettings> two_axes_different_value = RefPtr<FontVariationSettings> two_axes_different_value =
MakeFontVariationSettings( MakeSettings<FontVariationSettings, FontVariationAxis>(
{FontVariationAxis{"a ", 0}, FontVariationAxis{"b ", 1}}); {FontVariationAxis{"a ", 0}, FontVariationAxis{"b ", 1}});
RefPtr<FontVariationSettings> empty_variation_settings = RefPtr<FontVariationSettings> empty_variation_settings =
...@@ -42,4 +46,19 @@ TEST(FontSettingsTest, HashTest) { ...@@ -42,4 +46,19 @@ TEST(FontSettingsTest, HashTest) {
CHECK_EQ(empty_variation_settings->GetHash(), 0u); CHECK_EQ(empty_variation_settings->GetHash(), 0u);
}; };
TEST(FontSettingsTest, ToString) {
{
RefPtr<FontVariationSettings> settings =
MakeSettings<FontVariationSettings, FontVariationAxis>(
{FontVariationAxis{"a", 42}, FontVariationAxis{"b", 8118}});
EXPECT_EQ("a=42,b=8118", settings->ToString());
}
{
RefPtr<FontFeatureSettings> settings =
MakeSettings<FontFeatureSettings, FontFeature>(
{FontFeature{"a", 42}, FontFeature{"b", 8118}});
EXPECT_EQ("a=42,b=8118", settings->ToString());
}
}
} // namespace blink } // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment