Home | History | Annotate | Download | only in fonts
      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.
      4 
      5 #include "config.h"
      6 #include "platform/fonts/GlyphBuffer.h"
      7 
      8 #include "platform/fonts/SimpleFontData.h"
      9 #include "wtf/PassRefPtr.h"
     10 #include "wtf/RefPtr.h"
     11 #include <gtest/gtest.h>
     12 
     13 using namespace blink;
     14 
     15 namespace {
     16 
     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()
     22     {
     23         return adoptRef(new TestSimpleFontData);
     24     }
     25 
     26 private:
     27     TestSimpleFontData() : SimpleFontData(nullptr, 10, false, false) { }
     28 
     29     bool fillGlyphPage(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength) const OVERRIDE
     30     {
     31         return false;
     32     }
     33 };
     34 
     35 TEST(GlyphBufferTest, StartsEmpty)
     36 {
     37     GlyphBuffer glyphBuffer;
     38     EXPECT_TRUE(glyphBuffer.isEmpty());
     39     EXPECT_EQ(0u, glyphBuffer.size());
     40 }
     41 
     42 TEST(GlyphBufferTest, StoresGlyphs)
     43 {
     44     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
     45     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
     46 
     47     GlyphBuffer glyphBuffer;
     48     glyphBuffer.add(42, font1.get(), 10);
     49     glyphBuffer.add(43, font1.get(), 15);
     50     glyphBuffer.add(44, font2.get(), FloatSize(12, 2));
     51 
     52     EXPECT_FALSE(glyphBuffer.isEmpty());
     53     EXPECT_EQ(3u, glyphBuffer.size());
     54 
     55     EXPECT_EQ(42, glyphBuffer.glyphAt(0));
     56     EXPECT_EQ(43, glyphBuffer.glyphAt(1));
     57     EXPECT_EQ(44, glyphBuffer.glyphAt(2));
     58 
     59     const Glyph* glyphs = glyphBuffer.glyphs(0);
     60     EXPECT_EQ(42, glyphs[0]);
     61     EXPECT_EQ(43, glyphs[1]);
     62     EXPECT_EQ(44, glyphs[2]);
     63 }
     64 
     65 TEST(GlyphBufferTest, StoresAdvances)
     66 {
     67     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
     68     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
     69 
     70     GlyphBuffer glyphBuffer;
     71     glyphBuffer.add(42, font1.get(), 10);
     72     glyphBuffer.add(43, font1.get(), 15);
     73     glyphBuffer.add(44, font2.get(), FloatSize(12, 2));
     74 
     75     EXPECT_FALSE(glyphBuffer.isEmpty());
     76     EXPECT_EQ(3u, glyphBuffer.size());
     77 
     78     EXPECT_EQ(FloatSize(10, 0), glyphBuffer.advanceAt(0));
     79     EXPECT_EQ(FloatSize(15, 0), glyphBuffer.advanceAt(1));
     80     EXPECT_EQ(FloatSize(12, 2), glyphBuffer.advanceAt(2));
     81 
     82     const FloatSize* advances = glyphBuffer.advances(0);
     83     EXPECT_EQ(FloatSize(10, 0), advances[0]);
     84     EXPECT_EQ(FloatSize(15, 0), advances[1]);
     85     EXPECT_EQ(FloatSize(12, 2), advances[2]);
     86 }
     87 
     88 TEST(GlyphBufferTest, StoresSimpleFontData)
     89 {
     90     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
     91     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
     92 
     93     GlyphBuffer glyphBuffer;
     94     glyphBuffer.add(42, font1.get(), 10);
     95     glyphBuffer.add(43, font1.get(), 15);
     96     glyphBuffer.add(44, font2.get(), FloatSize(12, 2));
     97 
     98     EXPECT_FALSE(glyphBuffer.isEmpty());
     99     EXPECT_EQ(3u, glyphBuffer.size());
    100 
    101     EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(0));
    102     EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(1));
    103     EXPECT_EQ(font2.get(), glyphBuffer.fontDataAt(2));
    104 }
    105 
    106 TEST(GlyphBufferTest, GlyphArrayWithOffset)
    107 {
    108     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
    109     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
    110 
    111     GlyphBuffer glyphBuffer;
    112     glyphBuffer.add(42, font1.get(), 10);
    113     glyphBuffer.add(43, font1.get(), 15);
    114     glyphBuffer.add(44, font2.get(), FloatSize(12, 2));
    115 
    116     EXPECT_FALSE(glyphBuffer.isEmpty());
    117     EXPECT_EQ(3u, glyphBuffer.size());
    118 
    119     const Glyph* glyphs = glyphBuffer.glyphs(1);
    120     EXPECT_EQ(43, glyphs[0]);
    121     EXPECT_EQ(44, glyphs[1]);
    122 }
    123 
    124 TEST(GlyphBufferTest, AdvanceArrayWithOffset)
    125 {
    126     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
    127     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
    128 
    129     GlyphBuffer glyphBuffer;
    130     glyphBuffer.add(42, font1.get(), 10);
    131     glyphBuffer.add(43, font1.get(), 15);
    132     glyphBuffer.add(44, font2.get(), FloatSize(12, 2));
    133 
    134     EXPECT_FALSE(glyphBuffer.isEmpty());
    135     EXPECT_EQ(3u, glyphBuffer.size());
    136 
    137     const FloatSize* advances = glyphBuffer.advances(1);
    138     EXPECT_EQ(FloatSize(15, 0), advances[0]);
    139     EXPECT_EQ(FloatSize(12, 2), advances[1]);
    140 }
    141 
    142 TEST(GlyphBufferTest, Clear)
    143 {
    144     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
    145     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
    146 
    147     GlyphBuffer glyphBuffer;
    148     glyphBuffer.add(42, font1.get(), 10);
    149     glyphBuffer.add(43, font1.get(), 15);
    150     glyphBuffer.add(44, font2.get(), FloatSize(12, 2));
    151 
    152     EXPECT_FALSE(glyphBuffer.isEmpty());
    153     EXPECT_EQ(3u, glyphBuffer.size());
    154 
    155     glyphBuffer.clear();
    156 
    157     EXPECT_TRUE(glyphBuffer.isEmpty());
    158     EXPECT_EQ(0u, glyphBuffer.size());
    159 }
    160 
    161 TEST(GlyphBufferTest, TracksVerticalAdvances)
    162 {
    163     RefPtr<SimpleFontData> font = TestSimpleFontData::create();
    164     GlyphBuffer glyphBuffer;
    165     EXPECT_FALSE(glyphBuffer.hasVerticalAdvances());
    166     glyphBuffer.add(42, font.get(), 10);
    167     EXPECT_FALSE(glyphBuffer.hasVerticalAdvances());
    168     glyphBuffer.add(43, font.get(), FloatSize(15, 0));
    169     EXPECT_FALSE(glyphBuffer.hasVerticalAdvances());
    170     glyphBuffer.add(44, font.get(), FloatSize(10, 5));
    171     EXPECT_TRUE(glyphBuffer.hasVerticalAdvances());
    172     glyphBuffer.clear();
    173     EXPECT_FALSE(glyphBuffer.hasVerticalAdvances());
    174 }
    175 
    176 TEST(GlyphBufferTest, Reverse)
    177 {
    178     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
    179     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
    180 
    181     GlyphBuffer glyphBuffer;
    182     glyphBuffer.add(42, font1.get(), 10);
    183     glyphBuffer.add(43, font1.get(), 15);
    184     glyphBuffer.add(44, font2.get(), FloatSize(12, 2));
    185 
    186     EXPECT_FALSE(glyphBuffer.isEmpty());
    187     EXPECT_EQ(3u, glyphBuffer.size());
    188     EXPECT_TRUE(glyphBuffer.hasVerticalAdvances());
    189 
    190     glyphBuffer.reverse();
    191 
    192     EXPECT_FALSE(glyphBuffer.isEmpty());
    193     EXPECT_EQ(3u, glyphBuffer.size());
    194     EXPECT_TRUE(glyphBuffer.hasVerticalAdvances());
    195     EXPECT_EQ(44, glyphBuffer.glyphAt(0));
    196     EXPECT_EQ(43, glyphBuffer.glyphAt(1));
    197     EXPECT_EQ(42, glyphBuffer.glyphAt(2));
    198     EXPECT_EQ(FloatSize(12, 2), glyphBuffer.advanceAt(0));
    199     EXPECT_EQ(FloatSize(15, 0), glyphBuffer.advanceAt(1));
    200     EXPECT_EQ(FloatSize(10, 0), glyphBuffer.advanceAt(2));
    201     EXPECT_EQ(font2.get(), glyphBuffer.fontDataAt(0));
    202     EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(1));
    203     EXPECT_EQ(font1.get(), glyphBuffer.fontDataAt(2));
    204 }
    205 
    206 TEST(GlyphBufferTest, SetAdvanceWidth)
    207 {
    208     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
    209     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
    210 
    211     GlyphBuffer glyphBuffer;
    212     glyphBuffer.add(42, font1.get(), 10);
    213     glyphBuffer.add(43, font1.get(), 15);
    214     glyphBuffer.add(44, font2.get(), FloatSize(12, 2));
    215 
    216     glyphBuffer.setAdvanceWidth(1, 20);
    217     EXPECT_EQ(FloatSize(20, 0), glyphBuffer.advanceAt(1));
    218 
    219     glyphBuffer.setAdvanceWidth(2, 10);
    220     EXPECT_EQ(FloatSize(10, 2), glyphBuffer.advanceAt(2));
    221 }
    222 
    223 TEST(GlyphBufferTest, ExpandLastAdvance)
    224 {
    225     RefPtr<SimpleFontData> font1 = TestSimpleFontData::create();
    226     RefPtr<SimpleFontData> font2 = TestSimpleFontData::create();
    227 
    228     GlyphBuffer glyphBuffer;
    229     glyphBuffer.add(42, font1.get(), 10);
    230     glyphBuffer.add(43, font1.get(), 15);
    231     glyphBuffer.add(44, font2.get(), FloatSize(12, 2));
    232 
    233     glyphBuffer.expandLastAdvance(20);
    234     EXPECT_EQ(FloatSize(32, 2), glyphBuffer.advanceAt(2));
    235 }
    236 
    237 } // namespace
    238