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.
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 using namespace blink;
17 // Minimal TestSimpleFontData implementation.
18 // Font has no glyphs, but that's okay.
19 class TestSimpleFontData : public SimpleFontData {
21 static PassRefPtr<TestSimpleFontData> create()
23 return adoptRef(new TestSimpleFontData);
27 TestSimpleFontData() : SimpleFontData(nullptr, 10, false, false) { }
29 bool fillGlyphPage(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength) const override
35 TEST(GlyphBufferTest, StartsEmpty)
37 GlyphBuffer glyphBuffer;
38 EXPECT_TRUE(glyphBuffer.isEmpty());
39 EXPECT_EQ(0u, glyphBuffer.size());
42 TEST(GlyphBufferTest, StoresGlyphs)
44 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
45 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
47 GlyphBuffer glyphBuffer;
48 glyphBuffer.add(42, font1.get(), 10);
49 glyphBuffer.add(43, font1.get(), 15);
50 glyphBuffer.add(44, font2.get(), 12);
52 EXPECT_FALSE(glyphBuffer.isEmpty());
53 EXPECT_FALSE(glyphBuffer.hasOffsets());
54 EXPECT_EQ(3u, glyphBuffer.size());
56 EXPECT_EQ(42, glyphBuffer.glyphAt(0));
57 EXPECT_EQ(43, glyphBuffer.glyphAt(1));
58 EXPECT_EQ(44, glyphBuffer.glyphAt(2));
60 const Glyph* glyphs = glyphBuffer.glyphs(0);
61 EXPECT_EQ(42, glyphs[0]);
62 EXPECT_EQ(43, glyphs[1]);
63 EXPECT_EQ(44, glyphs[2]);
66 TEST(GlyphBufferTest, StoresOffsets)
68 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
69 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
71 GlyphBuffer glyphBuffer;
72 glyphBuffer.add(42, font1.get(), FloatSize(10, 0), 0);
73 glyphBuffer.add(43, font1.get(), FloatSize(15, 0), 0);
74 glyphBuffer.add(44, font2.get(), FloatSize(12, 2), 0);
76 EXPECT_FALSE(glyphBuffer.isEmpty());
77 EXPECT_TRUE(glyphBuffer.hasOffsets());
78 EXPECT_EQ(3u, glyphBuffer.size());
80 const FloatSize* offsets = glyphBuffer.offsets(0);
81 EXPECT_EQ(FloatSize(10, 0), offsets[0]);
82 EXPECT_EQ(FloatSize(15, 0), offsets[1]);
83 EXPECT_EQ(FloatSize(12, 2), offsets[2]);
86 TEST(GlyphBufferTest, StoresAdvances)
88 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
89 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
91 GlyphBuffer glyphBuffer;
92 glyphBuffer.add(42, font1.get(), 10);
93 glyphBuffer.add(43, font1.get(), 15);
94 glyphBuffer.add(44, font2.get(), 20);
96 EXPECT_FALSE(glyphBuffer.isEmpty());
97 EXPECT_EQ(3u, glyphBuffer.size());
99 EXPECT_EQ(10, glyphBuffer.advanceAt(0));
100 EXPECT_EQ(15, glyphBuffer.advanceAt(1));
101 EXPECT_EQ(20, glyphBuffer.advanceAt(2));
103 const float* advances = glyphBuffer.advances(0);
104 EXPECT_EQ(10, advances[0]);
105 EXPECT_EQ(15, advances[1]);
106 EXPECT_EQ(20, advances[2]);
109 TEST(GlyphBufferTest, StoresSimpleFontData)
111 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
112 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
114 GlyphBuffer glyphBuffer;
115 glyphBuffer.add(42, font1.get(), 10);
116 glyphBuffer.add(43, font1.get(), 15);
117 glyphBuffer.add(44, font2.get(), 12);
119 EXPECT_FALSE(glyphBuffer.isEmpty());
120 EXPECT_EQ(3u, glyphBuffer.size());
122 EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(0));
123 EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(1));
124 EXPECT_EQ(font2.get(), glyphBuffer.fontDataAt(2));
127 TEST(GlyphBufferTest, GlyphArrayWithOffset)
129 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
130 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
132 GlyphBuffer glyphBuffer;
133 glyphBuffer.add(42, font1.get(), 10);
134 glyphBuffer.add(43, font1.get(), 15);
135 glyphBuffer.add(44, font2.get(), 12);
137 EXPECT_FALSE(glyphBuffer.isEmpty());
138 EXPECT_EQ(3u, glyphBuffer.size());
140 const Glyph* glyphs = glyphBuffer.glyphs(1);
141 EXPECT_EQ(43, glyphs[0]);
142 EXPECT_EQ(44, glyphs[1]);
145 TEST(GlyphBufferTest, AdvanceArrayWithOffset)
147 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
148 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
150 GlyphBuffer glyphBuffer;
151 glyphBuffer.add(42, font1.get(), 10);
152 glyphBuffer.add(43, font1.get(), 15);
153 glyphBuffer.add(43, font1.get(), 12);
155 EXPECT_FALSE(glyphBuffer.isEmpty());
156 EXPECT_EQ(3u, glyphBuffer.size());
158 const float* advances = glyphBuffer.advances(1);
159 EXPECT_EQ(15, advances[0]);
160 EXPECT_EQ(12, advances[1]);
163 TEST(GlyphBufferTest, Reverse)
165 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
166 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
168 GlyphBuffer glyphBuffer;
169 glyphBuffer.add(42, font1.get(), 10);
170 glyphBuffer.add(43, font1.get(), 15);
171 glyphBuffer.add(44, font2.get(), 20);
173 EXPECT_FALSE(glyphBuffer.isEmpty());
174 EXPECT_EQ(3u, glyphBuffer.size());
176 glyphBuffer.reverse();
178 EXPECT_FALSE(glyphBuffer.isEmpty());
179 EXPECT_EQ(3u, glyphBuffer.size());
180 EXPECT_EQ(44, glyphBuffer.glyphAt(0));
181 EXPECT_EQ(43, glyphBuffer.glyphAt(1));
182 EXPECT_EQ(42, glyphBuffer.glyphAt(2));
183 EXPECT_EQ(20, glyphBuffer.advanceAt(0));
184 EXPECT_EQ(15, glyphBuffer.advanceAt(1));
185 EXPECT_EQ(10, glyphBuffer.advanceAt(2));
186 EXPECT_EQ(font2.get(), glyphBuffer.fontDataAt(0));
187 EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(1));
188 EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(2));
191 TEST(GlyphBufferTest, ExpandLastAdvance)
193 RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
194 RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
196 GlyphBuffer glyphBuffer;
197 glyphBuffer.add(42, font1.get(), 10);
198 glyphBuffer.add(43, font1.get(), 15);
199 glyphBuffer.add(44, font2.get(), 12);
201 glyphBuffer.expandLastAdvance(20);
202 EXPECT_EQ(32, glyphBuffer.advanceAt(2));