Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright 2011 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "gtest/gtest.h"
     18 #include "sfntly/font.h"
     19 #include "sfntly/font_factory.h"
     20 #include "sfntly/port/memory_input_stream.h"
     21 #include "sfntly/port/memory_output_stream.h"
     22 #include "test/test_data.h"
     23 #include "test/test_font_utils.h"
     24 #include "test/serialization_test.h"
     25 
     26 namespace sfntly {
     27 
     28 bool TestSerialization() {
     29   FontFactoryPtr factory1, factory2, factory3;
     30   factory1.Attach(FontFactory::GetInstance());
     31   FontArray font_array;
     32   LoadFont(SAMPLE_TTF_FILE, factory1, &font_array);
     33   FontPtr original = font_array[0];
     34 
     35   factory2.Attach(FontFactory::GetInstance());
     36   FontBuilderArray font_builder_array;
     37   BuilderForFontFile(SAMPLE_TTF_FILE, factory2, &font_builder_array);
     38   FontBuilderPtr font_builder = font_builder_array[0];
     39 
     40   FontPtr intermediate;
     41   intermediate.Attach(font_builder->Build());
     42   MemoryOutputStream os;
     43   factory2->SerializeFont(intermediate, &os);
     44 
     45   factory3.Attach(FontFactory::GetInstance());
     46   FontArray new_font_array;
     47   MemoryInputStream is;
     48   is.Attach(os.Get(), os.Size());
     49   factory3->LoadFonts(&is, &new_font_array);
     50   FontPtr serialized = new_font_array[0];
     51 
     52   // Check number of tables
     53   EXPECT_EQ(original->num_tables(), serialized->num_tables());
     54 
     55   // Check if same set of tables
     56   const TableMap* original_tables = original->GetTableMap();
     57   const TableMap* serialized_tables = serialized->GetTableMap();
     58   EXPECT_EQ(original_tables->size(), serialized_tables->size());
     59   TableMap::const_iterator not_found = serialized_tables->end();
     60   for (TableMap::const_iterator b = original_tables->begin(),
     61                                 e = original_tables->end(); b != e; ++b) {
     62     EXPECT_TRUE((serialized_tables->find(b->first) != not_found));
     63   }
     64 
     65   // TODO(arthurhsu): check cmap equivalence
     66   // Check checksum equivalence
     67   for (size_t i = 0; i < SAMPLE_TTF_KNOWN_TAGS; ++i) {
     68       TablePtr original_table = original->GetTable(TTF_KNOWN_TAGS[i]);
     69       TablePtr serialized_table = serialized->GetTable(TTF_KNOWN_TAGS[i]);
     70     EXPECT_EQ(original_table->CalculatedChecksum(),
     71               serialized_table->CalculatedChecksum());
     72     EXPECT_EQ(original_table->DataLength(), serialized_table->DataLength());
     73 
     74     if (TTF_KNOWN_TAGS[i] == Tag::hhea) {
     75       EXPECT_TRUE(VerifyHHEA(original_table, serialized_table));
     76     } else if (TTF_KNOWN_TAGS[i] == Tag::glyf) {
     77         EXPECT_TRUE(VerifyGLYF(original_table, serialized_table));
     78     } else if (TTF_KNOWN_TAGS[i] == Tag::hmtx) {
     79         EXPECT_TRUE(VerifyHMTX(original_table, serialized_table));
     80     } else if (TTF_KNOWN_TAGS[i] == Tag::loca) {
     81         EXPECT_TRUE(VerifyLOCA(original_table, serialized_table));
     82     } else if (TTF_KNOWN_TAGS[i] == Tag::maxp) {
     83         EXPECT_TRUE(VerifyMAXP(original_table, serialized_table));
     84     } else if (TTF_KNOWN_TAGS[i] == Tag::name) {
     85         EXPECT_TRUE(VerifyNAME(original_table, serialized_table));
     86     } else if (TTF_KNOWN_TAGS[i] == Tag::OS_2) {
     87         EXPECT_TRUE(VerifyOS_2(original_table, serialized_table));
     88     }
     89   }
     90 
     91   return true;
     92 }
     93 
     94 bool TestSerializationBitmap() {
     95   FontFactoryPtr factory1, factory2, factory3;
     96   factory1.Attach(FontFactory::GetInstance());
     97   FontArray font_array;
     98   LoadFont(SAMPLE_BITMAP_FONT, factory1, &font_array);
     99   FontPtr original = font_array[0];
    100 
    101   factory2.Attach(FontFactory::GetInstance());
    102   FontBuilderArray font_builder_array;
    103   BuilderForFontFile(SAMPLE_BITMAP_FONT, factory2, &font_builder_array);
    104   FontBuilderPtr font_builder = font_builder_array[0];
    105 
    106   FontPtr intermediate;
    107   intermediate.Attach(font_builder->Build());
    108   MemoryOutputStream os;
    109   factory2->SerializeFont(intermediate, &os);
    110 
    111   factory3.Attach(FontFactory::GetInstance());
    112   FontArray new_font_array;
    113   MemoryInputStream is;
    114   is.Attach(os.Get(), os.Size());
    115   factory3->LoadFonts(&is, &new_font_array);
    116   FontPtr serialized = new_font_array[0];
    117 
    118   // Check number of tables
    119   EXPECT_EQ(original->num_tables(), serialized->num_tables());
    120 
    121   // Check if same set of tables
    122   const TableMap* original_tables = original->GetTableMap();
    123   const TableMap* serialized_tables = serialized->GetTableMap();
    124   EXPECT_EQ(original_tables->size(), serialized_tables->size());
    125   TableMap::const_iterator not_found = serialized_tables->end();
    126   for (TableMap::const_iterator b = original_tables->begin(),
    127                                 e = original_tables->end(); b != e; ++b) {
    128     EXPECT_TRUE((serialized_tables->find(b->first) != not_found));
    129   }
    130 
    131   // Check checksum equivalence
    132   for (size_t i = 0; i < SAMPLE_BITMAP_KNOWN_TAGS; ++i) {
    133       TablePtr original_table = original->GetTable(BITMAP_KNOWN_TAGS[i]);
    134       TablePtr serialized_table = serialized->GetTable(BITMAP_KNOWN_TAGS[i]);
    135     EXPECT_EQ(original_table->CalculatedChecksum(),
    136               serialized_table->CalculatedChecksum());
    137     EXPECT_EQ(original_table->DataLength(), serialized_table->DataLength());
    138   }
    139 
    140   return true;
    141 }
    142 
    143 }  // namespace sfntly
    144 
    145 TEST(Serialization, Simple) {
    146   ASSERT_TRUE(sfntly::TestSerialization());
    147 }
    148 
    149 TEST(Serialization, Bitmap) {
    150   ASSERT_TRUE(sfntly::TestSerializationBitmap());
    151 }
    152