Commit cb99415f authored by drott's avatar drott Committed by Commit bot

Move UnicodeRangeSet to platform/fonts

Preparation for using UnicodeRangeSet in both CSSFontFace and
HarfBuzzFace for sharing character range set restriction information.

BUG=583450
R=eae

Review URL: https://codereview.chromium.org/1808853002

Cr-Commit-Position: refs/heads/master@{#381536}
parent cba00314
...@@ -3886,7 +3886,6 @@ ...@@ -3886,7 +3886,6 @@
'clipboard/DataObjectTest.cpp', 'clipboard/DataObjectTest.cpp',
'css/AffectedByFocusTest.cpp', 'css/AffectedByFocusTest.cpp',
'css/CSSCalculationValueTest.cpp', 'css/CSSCalculationValueTest.cpp',
'css/CSSFontFaceTest.cpp',
'css/CSSPageRuleTest.cpp', 'css/CSSPageRuleTest.cpp',
'css/CSSSelectorTest.cpp', 'css/CSSSelectorTest.cpp',
'css/CSSStyleDeclarationTest.cpp', 'css/CSSStyleDeclarationTest.cpp',
......
...@@ -195,67 +195,6 @@ void CSSFontFace::setLoadStatus(FontFace::LoadStatusType newStatus) ...@@ -195,67 +195,6 @@ void CSSFontFace::setLoadStatus(FontFace::LoadStatusType newStatus)
} }
} }
CSSFontFace::UnicodeRangeSet::UnicodeRangeSet(const Vector<UnicodeRange>& ranges)
: m_ranges(ranges)
{
if (m_ranges.isEmpty())
return;
std::sort(m_ranges.begin(), m_ranges.end());
// Unify overlapping ranges.
UChar32 from = m_ranges[0].from();
UChar32 to = m_ranges[0].to();
size_t targetIndex = 0;
for (size_t i = 1; i < m_ranges.size(); i++) {
if (to + 1 >= m_ranges[i].from()) {
to = std::max(to, m_ranges[i].to());
} else {
m_ranges[targetIndex++] = UnicodeRange(from, to);
from = m_ranges[i].from();
to = m_ranges[i].to();
}
}
m_ranges[targetIndex++] = UnicodeRange(from, to);
m_ranges.shrink(targetIndex);
}
bool CSSFontFace::UnicodeRangeSet::contains(UChar32 c) const
{
if (isEntireRange())
return true;
Vector<UnicodeRange>::const_iterator it = std::lower_bound(m_ranges.begin(), m_ranges.end(), c);
return it != m_ranges.end() && it->contains(c);
}
bool CSSFontFace::UnicodeRangeSet::contains(const FontDataRange& range) const
{
for (auto it = m_ranges.begin(); it != m_ranges.end(); ++it) {
if (*it == range)
return true;
}
return false;
}
bool CSSFontFace::UnicodeRangeSet::intersectsWith(const String& text) const
{
if (text.isEmpty())
return false;
if (isEntireRange())
return true;
if (text.is8Bit() && m_ranges[0].from() >= 0x100)
return false;
unsigned index = 0;
while (index < text.length()) {
UChar32 c = text.characterStartingAt(index);
index += U16_LENGTH(c);
if (contains(c))
return true;
}
return false;
}
DEFINE_TRACE(CSSFontFace) DEFINE_TRACE(CSSFontFace)
{ {
visitor->trace(m_segmentedFontFace); visitor->trace(m_segmentedFontFace);
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "core/css/CSSSegmentedFontFace.h" #include "core/css/CSSSegmentedFontFace.h"
#include "core/css/FontFace.h" #include "core/css/FontFace.h"
#include "platform/fonts/SegmentedFontData.h" #include "platform/fonts/SegmentedFontData.h"
#include "platform/fonts/UnicodeRangeSet.h"
#include "wtf/Deque.h" #include "wtf/Deque.h"
#include "wtf/Forward.h" #include "wtf/Forward.h"
#include "wtf/PassRefPtr.h" #include "wtf/PassRefPtr.h"
...@@ -46,9 +47,6 @@ class CORE_EXPORT CSSFontFace final : public NoBaseWillBeGarbageCollectedFinaliz ...@@ -46,9 +47,6 @@ class CORE_EXPORT CSSFontFace final : public NoBaseWillBeGarbageCollectedFinaliz
USING_FAST_MALLOC_WILL_BE_REMOVED(CSSFontFace); USING_FAST_MALLOC_WILL_BE_REMOVED(CSSFontFace);
WTF_MAKE_NONCOPYABLE(CSSFontFace); WTF_MAKE_NONCOPYABLE(CSSFontFace);
public: public:
struct UnicodeRange;
class UnicodeRangeSet;
CSSFontFace(FontFace* fontFace, Vector<UnicodeRange>& ranges) CSSFontFace(FontFace* fontFace, Vector<UnicodeRange>& ranges)
: m_ranges(ranges) : m_ranges(ranges)
, m_segmentedFontFace(nullptr) , m_segmentedFontFace(nullptr)
...@@ -74,40 +72,6 @@ public: ...@@ -74,40 +72,6 @@ public:
PassRefPtr<SimpleFontData> getFontData(const FontDescription&); PassRefPtr<SimpleFontData> getFontData(const FontDescription&);
struct UnicodeRange {
DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
UnicodeRange(UChar32 from, UChar32 to)
: m_from(from)
, m_to(to)
{
}
UChar32 from() const { return m_from; }
UChar32 to() const { return m_to; }
bool contains(UChar32 c) const { return m_from <= c && c <= m_to; }
bool operator<(const UnicodeRange& other) const { return m_from < other.m_from; }
bool operator<(UChar32 c) const { return m_to < c; }
bool operator==(const FontDataRange& fontDataRange) const { return fontDataRange.from() == m_from && fontDataRange.to() == m_to; };
private:
UChar32 m_from;
UChar32 m_to;
};
class CORE_EXPORT UnicodeRangeSet {
DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
public:
explicit UnicodeRangeSet(const Vector<UnicodeRange>&);
bool contains(UChar32) const;
bool contains(const FontDataRange&) const;
bool intersectsWith(const String&) const;
bool isEntireRange() const { return m_ranges.isEmpty(); }
size_t size() const { return m_ranges.size(); }
const UnicodeRange& rangeAt(size_t i) const { return m_ranges[i]; }
private:
Vector<UnicodeRange> m_ranges; // If empty, represents the whole code space.
};
FontFace::LoadStatusType loadStatus() const { return m_fontFace->loadStatus(); } FontFace::LoadStatusType loadStatus() const { return m_fontFace->loadStatus(); }
bool maybeScheduleFontLoad(const FontDescription&, UChar32); bool maybeScheduleFontLoad(const FontDescription&, UChar32);
bool maybeScheduleFontLoad(const FontDescription&, const FontDataRange&); bool maybeScheduleFontLoad(const FontDescription&, const FontDataRange&);
......
...@@ -106,7 +106,7 @@ void CSSSegmentedFontFace::removeFontFace(PassRefPtrWillBeRawPtr<FontFace> prpFo ...@@ -106,7 +106,7 @@ void CSSSegmentedFontFace::removeFontFace(PassRefPtrWillBeRawPtr<FontFace> prpFo
fontFace->cssFontFace()->clearSegmentedFontFace(); fontFace->cssFontFace()->clearSegmentedFontFace();
} }
static void appendFontData(SegmentedFontData* newFontData, PassRefPtr<SimpleFontData> prpFaceFontData, const CSSFontFace::UnicodeRangeSet& ranges) static void appendFontData(SegmentedFontData* newFontData, PassRefPtr<SimpleFontData> prpFaceFontData, const UnicodeRangeSet& ranges)
{ {
RefPtr<SimpleFontData> faceFontData = prpFaceFontData; RefPtr<SimpleFontData> faceFontData = prpFaceFontData;
unsigned numRanges = ranges.size(); unsigned numRanges = ranges.size();
......
...@@ -555,12 +555,12 @@ static FontDisplay CSSValueToFontDisplay(CSSValue* value) ...@@ -555,12 +555,12 @@ static FontDisplay CSSValueToFontDisplay(CSSValue* value)
static PassOwnPtrWillBeRawPtr<CSSFontFace> createCSSFontFace(FontFace* fontFace, CSSValue* unicodeRange) static PassOwnPtrWillBeRawPtr<CSSFontFace> createCSSFontFace(FontFace* fontFace, CSSValue* unicodeRange)
{ {
Vector<CSSFontFace::UnicodeRange> ranges; Vector<UnicodeRange> ranges;
if (CSSValueList* rangeList = toCSSValueList(unicodeRange)) { if (CSSValueList* rangeList = toCSSValueList(unicodeRange)) {
unsigned numRanges = rangeList->length(); unsigned numRanges = rangeList->length();
for (unsigned i = 0; i < numRanges; i++) { for (unsigned i = 0; i < numRanges; i++) {
CSSUnicodeRangeValue* range = toCSSUnicodeRangeValue(rangeList->item(i)); CSSUnicodeRangeValue* range = toCSSUnicodeRangeValue(rangeList->item(i));
ranges.append(CSSFontFace::UnicodeRange(range->from(), range->to())); ranges.append(UnicodeRange(range->from(), range->to()));
} }
} }
......
...@@ -450,6 +450,8 @@ ...@@ -450,6 +450,8 @@
'fonts/SymbolsIterator.cpp', 'fonts/SymbolsIterator.cpp',
'fonts/SymbolsIterator.h', 'fonts/SymbolsIterator.h',
'fonts/TextBlob.h', 'fonts/TextBlob.h',
'fonts/UnicodeRangeSet.cpp',
'fonts/UnicodeRangeSet.h',
'fonts/UTF16TextIterator.cpp', 'fonts/UTF16TextIterator.cpp',
'fonts/UTF16TextIterator.h', 'fonts/UTF16TextIterator.h',
'fonts/VDMXParser.cpp', 'fonts/VDMXParser.cpp',
...@@ -1153,6 +1155,7 @@ ...@@ -1153,6 +1155,7 @@
'fonts/ScriptRunIteratorTest.cpp', 'fonts/ScriptRunIteratorTest.cpp',
'fonts/SmallCapsIteratorTest.cpp', 'fonts/SmallCapsIteratorTest.cpp',
'fonts/SymbolsIteratorTest.cpp', 'fonts/SymbolsIteratorTest.cpp',
'fonts/UnicodeRangeSetTest.cpp',
'fonts/android/FontCacheAndroidTest.cpp', 'fonts/android/FontCacheAndroidTest.cpp',
'fonts/mac/FontFamilyMatcherMacTest.mm', 'fonts/mac/FontFamilyMatcherMacTest.mm',
'fonts/opentype/OpenTypeVerticalDataTest.cpp', 'fonts/opentype/OpenTypeVerticalDataTest.cpp',
......
/*
* Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "UnicodeRangeSet.h"
#include "wtf/text/WTFString.h"
namespace blink {
UnicodeRangeSet::UnicodeRangeSet(const Vector<UnicodeRange>& ranges)
: m_ranges(ranges)
{
if (m_ranges.isEmpty())
return;
std::sort(m_ranges.begin(), m_ranges.end());
// Unify overlapping ranges.
UChar32 from = m_ranges[0].from();
UChar32 to = m_ranges[0].to();
size_t targetIndex = 0;
for (size_t i = 1; i < m_ranges.size(); i++) {
if (to + 1 >= m_ranges[i].from()) {
to = std::max(to, m_ranges[i].to());
} else {
m_ranges[targetIndex++] = UnicodeRange(from, to);
from = m_ranges[i].from();
to = m_ranges[i].to();
}
}
m_ranges[targetIndex++] = UnicodeRange(from, to);
m_ranges.shrink(targetIndex);
}
bool UnicodeRangeSet::contains(UChar32 c) const
{
if (isEntireRange())
return true;
Vector<UnicodeRange>::const_iterator it = std::lower_bound(m_ranges.begin(), m_ranges.end(), c);
return it != m_ranges.end() && it->contains(c);
}
bool UnicodeRangeSet::contains(const FontDataRange& range) const
{
for (auto it = m_ranges.begin(); it != m_ranges.end(); ++it) {
if (*it == range)
return true;
}
return false;
}
bool UnicodeRangeSet::intersectsWith(const String& text) const
{
if (text.isEmpty())
return false;
if (isEntireRange())
return true;
if (text.is8Bit() && m_ranges[0].from() >= 0x100)
return false;
unsigned index = 0;
while (index < text.length()) {
UChar32 c = text.characterStartingAt(index);
index += U16_LENGTH(c);
if (contains(c))
return true;
}
return false;
}
}
/*
* Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UnicodeRangeSet_h
#define UnicodeRangeSet_h
#include "platform/PlatformExport.h"
#include "platform/fonts/FontDataRange.h"
#include "wtf/Allocator.h"
#include "wtf/text/Unicode.h"
namespace blink {
struct PLATFORM_EXPORT UnicodeRange {
DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
UnicodeRange(UChar32 from, UChar32 to)
: m_from(from)
, m_to(to)
{
}
UChar32 from() const { return m_from; }
UChar32 to() const { return m_to; }
bool contains(UChar32 c) const { return m_from <= c && c <= m_to; }
bool operator<(const UnicodeRange& other) const
{
return m_from < other.m_from;
}
bool operator<(UChar32 c) const { return m_to < c; }
bool operator==(const FontDataRange& fontDataRange) const
{
return fontDataRange.from() == m_from
&& fontDataRange.to() == m_to;
};
private:
UChar32 m_from;
UChar32 m_to;
};
class PLATFORM_EXPORT UnicodeRangeSet {
DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
public:
explicit UnicodeRangeSet(const Vector<UnicodeRange>&);
bool contains(UChar32) const;
bool contains(const FontDataRange&) const;
bool intersectsWith(const String&) const;
bool isEntireRange() const { return m_ranges.isEmpty(); }
size_t size() const { return m_ranges.size(); }
const UnicodeRange& rangeAt(size_t i) const { return m_ranges[i]; }
private:
Vector<UnicodeRange> m_ranges; // If empty, represents the whole code space.
};
} // namespace blink
#endif
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// 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 "core/css/CSSFontFace.h" #include "platform/fonts/UnicodeRangeSet.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -10,10 +10,10 @@ namespace blink { ...@@ -10,10 +10,10 @@ namespace blink {
static const UChar hiraganaA[2] = { 0x3042, 0 }; static const UChar hiraganaA[2] = { 0x3042, 0 };
TEST(CSSFontFace, UnicodeRangeSetEmpty) TEST(UnicodeRangeSet, Empty)
{ {
Vector<CSSFontFace::UnicodeRange> ranges; Vector<UnicodeRange> ranges;
CSSFontFace::UnicodeRangeSet set(ranges); UnicodeRangeSet set(ranges);
EXPECT_TRUE(set.isEntireRange()); EXPECT_TRUE(set.isEntireRange());
EXPECT_EQ(0u, set.size()); EXPECT_EQ(0u, set.size());
EXPECT_FALSE(set.intersectsWith(String())); EXPECT_FALSE(set.intersectsWith(String()));
...@@ -21,11 +21,11 @@ TEST(CSSFontFace, UnicodeRangeSetEmpty) ...@@ -21,11 +21,11 @@ TEST(CSSFontFace, UnicodeRangeSetEmpty)
EXPECT_TRUE(set.intersectsWith(String(hiraganaA))); EXPECT_TRUE(set.intersectsWith(String(hiraganaA)));
} }
TEST(CSSFontFace, UnicodeRangeSetSingleCharacter) TEST(UnicodeRangeSet, SingleCharacter)
{ {
Vector<CSSFontFace::UnicodeRange> ranges; Vector<UnicodeRange> ranges;
ranges.append(CSSFontFace::UnicodeRange('b', 'b')); ranges.append(UnicodeRange('b', 'b'));
CSSFontFace::UnicodeRangeSet set(ranges); UnicodeRangeSet set(ranges);
EXPECT_FALSE(set.isEntireRange()); EXPECT_FALSE(set.isEntireRange());
EXPECT_FALSE(set.intersectsWith(String())); EXPECT_FALSE(set.intersectsWith(String()));
EXPECT_FALSE(set.intersectsWith(String("a"))); EXPECT_FALSE(set.intersectsWith(String("a")));
...@@ -38,12 +38,12 @@ TEST(CSSFontFace, UnicodeRangeSetSingleCharacter) ...@@ -38,12 +38,12 @@ TEST(CSSFontFace, UnicodeRangeSetSingleCharacter)
EXPECT_EQ('b', set.rangeAt(0).to()); EXPECT_EQ('b', set.rangeAt(0).to());
} }
TEST(CSSFontFace, UnicodeRangeSetTwoRanges) TEST(UnicodeRangeSet, TwoRanges)
{ {
Vector<CSSFontFace::UnicodeRange> ranges; Vector<UnicodeRange> ranges;
ranges.append(CSSFontFace::UnicodeRange('6', '7')); ranges.append(UnicodeRange('6', '7'));
ranges.append(CSSFontFace::UnicodeRange('2', '4')); ranges.append(UnicodeRange('2', '4'));
CSSFontFace::UnicodeRangeSet set(ranges); UnicodeRangeSet set(ranges);
EXPECT_FALSE(set.isEntireRange()); EXPECT_FALSE(set.isEntireRange());
EXPECT_FALSE(set.intersectsWith(String())); EXPECT_FALSE(set.intersectsWith(String()));
EXPECT_FALSE(set.intersectsWith(String("1"))); EXPECT_FALSE(set.intersectsWith(String("1")));
...@@ -61,24 +61,24 @@ TEST(CSSFontFace, UnicodeRangeSetTwoRanges) ...@@ -61,24 +61,24 @@ TEST(CSSFontFace, UnicodeRangeSetTwoRanges)
EXPECT_EQ('7', set.rangeAt(1).to()); EXPECT_EQ('7', set.rangeAt(1).to());
} }
TEST(CSSFontFace, UnicodeRangeSetOverlap) TEST(UnicodeRangeSet, Overlap)
{ {
Vector<CSSFontFace::UnicodeRange> ranges; Vector<UnicodeRange> ranges;
ranges.append(CSSFontFace::UnicodeRange('0', '2')); ranges.append(UnicodeRange('0', '2'));
ranges.append(CSSFontFace::UnicodeRange('1', '1')); ranges.append(UnicodeRange('1', '1'));
ranges.append(CSSFontFace::UnicodeRange('3', '5')); ranges.append(UnicodeRange('3', '5'));
ranges.append(CSSFontFace::UnicodeRange('4', '6')); ranges.append(UnicodeRange('4', '6'));
CSSFontFace::UnicodeRangeSet set(ranges); UnicodeRangeSet set(ranges);
ASSERT_EQ(1u, set.size()); ASSERT_EQ(1u, set.size());
EXPECT_EQ('0', set.rangeAt(0).from()); EXPECT_EQ('0', set.rangeAt(0).from());
EXPECT_EQ('6', set.rangeAt(0).to()); EXPECT_EQ('6', set.rangeAt(0).to());
} }
TEST(CSSFontFace, UnicodeRangeSetNon8Bit) TEST(UnicodeRangeSet, Non8Bit)
{ {
Vector<CSSFontFace::UnicodeRange> ranges; Vector<UnicodeRange> ranges;
ranges.append(CSSFontFace::UnicodeRange(0x3042, 0x3042)); ranges.append(UnicodeRange(0x3042, 0x3042));
CSSFontFace::UnicodeRangeSet set(ranges); UnicodeRangeSet set(ranges);
ASSERT_EQ(1u, set.size()); ASSERT_EQ(1u, set.size());
EXPECT_EQ(0x3042, set.rangeAt(0).from()); EXPECT_EQ(0x3042, set.rangeAt(0).from());
EXPECT_EQ(0x3042, set.rangeAt(0).to()); EXPECT_EQ(0x3042, set.rangeAt(0).to());
......
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