Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / fonts / GlyphBufferTest.cpp
blobfa3a49ebb2ab133ba19b67bc39754c6279a07f1a
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "config.h"
6 #include "platform/fonts/GlyphBuffer.h"
8 #include "platform/fonts/SimpleFontData.h"
9 #include "wtf/PassRefPtr.h"
10 #include "wtf/RefPtr.h"
11 #include <gtest/gtest.h>
13 namespace blink {
15 namespace {
17 // Minimal TestSimpleFontData implementation.
18 // Font has no glyphs, but that's okay.
19 class TestSimpleFontData : public SimpleFontData {
20 public:
21 static PassRefPtr<TestSimpleFontData> create()
23 return adoptRef(new TestSimpleFontData);
26 private:
27 TestSimpleFontData() : SimpleFontData(nullptr, 10, false, false) { }
29 bool fillGlyphPage(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength) const override
31 return false;
35 } // anonymous namespace
37 TEST(GlyphBufferTest, StartsEmpty)
39 GlyphBuffer glyphBuffer;
40 EXPECT_TRUE(glyphBuffer.isEmpty());
41 EXPECT_EQ(0u, glyphBuffer.size());
44 TEST(GlyphBufferTest, StoresGlyphs)
46 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
47 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
49 GlyphBuffer glyphBuffer;
50 glyphBuffer.add(42, font1.get(), 10);
51 glyphBuffer.add(43, font1.get(), 15);
52 glyphBuffer.add(44, font2.get(), 22);
54 EXPECT_FALSE(glyphBuffer.isEmpty());
55 EXPECT_FALSE(glyphBuffer.hasVerticalOffsets());
56 EXPECT_EQ(3u, glyphBuffer.size());
58 EXPECT_EQ(42, glyphBuffer.glyphAt(0));
59 EXPECT_EQ(43, glyphBuffer.glyphAt(1));
60 EXPECT_EQ(44, glyphBuffer.glyphAt(2));
62 const Glyph* glyphs = glyphBuffer.glyphs(0);
63 EXPECT_EQ(42, glyphs[0]);
64 EXPECT_EQ(43, glyphs[1]);
65 EXPECT_EQ(44, glyphs[2]);
68 TEST(GlyphBufferTest, StoresVerticalOffsets)
70 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
71 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
73 GlyphBuffer glyphBuffer;
74 EXPECT_FALSE(glyphBuffer.hasVerticalOffsets());
76 glyphBuffer.add(42, font1.get(), FloatPoint(10, 0));
77 glyphBuffer.add(43, font1.get(), FloatPoint(15, 0));
78 glyphBuffer.add(44, font2.get(), FloatPoint(12, 2));
80 EXPECT_FALSE(glyphBuffer.isEmpty());
81 EXPECT_TRUE(glyphBuffer.hasVerticalOffsets());
82 EXPECT_EQ(3u, glyphBuffer.size());
84 const float* offsets = glyphBuffer.offsets(0);
85 EXPECT_EQ(10, glyphBuffer.xOffsetAt(0));
86 EXPECT_EQ(0, glyphBuffer.yOffsetAt(0));
87 EXPECT_EQ(15, glyphBuffer.xOffsetAt(1));
88 EXPECT_EQ(0, glyphBuffer.yOffsetAt(1));
89 EXPECT_EQ(12, glyphBuffer.xOffsetAt(2));
90 EXPECT_EQ(2, glyphBuffer.yOffsetAt(2));
92 EXPECT_EQ(10, offsets[0]);
93 EXPECT_EQ(0, offsets[1]);
94 EXPECT_EQ(15, offsets[2]);
95 EXPECT_EQ(0, offsets[3]);
96 EXPECT_EQ(12, offsets[4]);
97 EXPECT_EQ(2, offsets[5]);
100 TEST(GlyphBufferTest, StoresOffsets)
102 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
103 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
105 GlyphBuffer glyphBuffer;
106 glyphBuffer.add(42, font1.get(), 10);
107 glyphBuffer.add(43, font1.get(), 15);
108 glyphBuffer.add(44, font2.get(), 20);
110 EXPECT_FALSE(glyphBuffer.isEmpty());
111 EXPECT_FALSE(glyphBuffer.hasVerticalOffsets());
112 EXPECT_EQ(3u, glyphBuffer.size());
114 EXPECT_EQ(10, glyphBuffer.xOffsetAt(0));
115 EXPECT_EQ(15, glyphBuffer.xOffsetAt(1));
116 EXPECT_EQ(20, glyphBuffer.xOffsetAt(2));
118 const float* offsets = glyphBuffer.offsets(0);
119 EXPECT_EQ(10, offsets[0]);
120 EXPECT_EQ(15, offsets[1]);
121 EXPECT_EQ(20, offsets[2]);
124 TEST(GlyphBufferTest, StoresSimpleFontData)
126 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
127 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
129 GlyphBuffer glyphBuffer;
130 glyphBuffer.add(42, font1.get(), 10);
131 glyphBuffer.add(43, font1.get(), 15);
132 glyphBuffer.add(44, font2.get(), 12);
134 EXPECT_FALSE(glyphBuffer.isEmpty());
135 EXPECT_EQ(3u, glyphBuffer.size());
137 EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(0));
138 EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(1));
139 EXPECT_EQ(font2.get(), glyphBuffer.fontDataAt(2));
142 TEST(GlyphBufferTest, GlyphArrayWithOffset)
144 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
145 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
147 GlyphBuffer glyphBuffer;
148 glyphBuffer.add(42, font1.get(), 10);
149 glyphBuffer.add(43, font1.get(), 15);
150 glyphBuffer.add(44, font2.get(), 12);
152 EXPECT_FALSE(glyphBuffer.isEmpty());
153 EXPECT_EQ(3u, glyphBuffer.size());
155 const Glyph* glyphs = glyphBuffer.glyphs(1);
156 EXPECT_EQ(43, glyphs[0]);
157 EXPECT_EQ(44, glyphs[1]);
160 TEST(GlyphBufferTest, OffsetArrayWithNonZeroIndex)
162 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
163 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
166 GlyphBuffer glyphBuffer;
167 glyphBuffer.add(42, font1.get(), 10);
168 glyphBuffer.add(43, font1.get(), 15);
169 glyphBuffer.add(43, font2.get(), 12);
171 EXPECT_FALSE(glyphBuffer.isEmpty());
172 EXPECT_FALSE(glyphBuffer.hasVerticalOffsets());
173 EXPECT_EQ(3u, glyphBuffer.size());
175 const float* offsets = glyphBuffer.offsets(1);
176 EXPECT_EQ(15, offsets[0]);
177 EXPECT_EQ(12, offsets[1]);
181 GlyphBuffer glyphBuffer;
182 glyphBuffer.add(42, font1.get(), FloatPoint(10, 0));
183 glyphBuffer.add(43, font1.get(), FloatPoint(15, 0));
184 glyphBuffer.add(43, font2.get(), FloatPoint(12, 2));
186 EXPECT_FALSE(glyphBuffer.isEmpty());
187 EXPECT_TRUE(glyphBuffer.hasVerticalOffsets());
188 EXPECT_EQ(3u, glyphBuffer.size());
190 const float* offsets = glyphBuffer.offsets(1);
191 EXPECT_EQ(15, offsets[0]);
192 EXPECT_EQ(0, offsets[1]);
193 EXPECT_EQ(12, offsets[2]);
194 EXPECT_EQ(2, offsets[3]);
198 TEST(GlyphBufferTest, ReverseForSimpleRTL)
200 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
201 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
203 GlyphBuffer glyphBuffer;
204 glyphBuffer.add(42, font1.get(), 10);
205 glyphBuffer.add(43, font1.get(), 15);
206 glyphBuffer.add(44, font2.get(), 25);
208 EXPECT_FALSE(glyphBuffer.isEmpty());
209 EXPECT_EQ(3u, glyphBuffer.size());
211 glyphBuffer.reverseForSimpleRTL(30, 100);
213 EXPECT_FALSE(glyphBuffer.isEmpty());
214 EXPECT_EQ(3u, glyphBuffer.size());
215 EXPECT_EQ(44, glyphBuffer.glyphAt(0));
216 EXPECT_EQ(43, glyphBuffer.glyphAt(1));
217 EXPECT_EQ(42, glyphBuffer.glyphAt(2));
218 EXPECT_EQ(font2.get(), glyphBuffer.fontDataAt(0));
219 EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(1));
220 EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(2));
221 EXPECT_EQ(70, glyphBuffer.xOffsetAt(0));
222 EXPECT_EQ(75, glyphBuffer.xOffsetAt(1));
223 EXPECT_EQ(85, glyphBuffer.xOffsetAt(2));
226 } // namespace blink