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/table/core/font_header_table.h"
     21 #include "sfntly/tag.h"
     22 #include "sfntly/data/memory_byte_array.h"
     23 #include "sfntly/port/endian.h"
     24 #include "sfntly/port/file_input_stream.h"
     25 #include "sfntly/port/memory_output_stream.h"
     26 #include "test/test_data.h"
     27 #include "test/test_font_utils.h"
     28 
     29 namespace sfntly {
     30 
     31 bool TestOTFBasicEditing() {
     32   FontFactoryPtr factory;
     33   factory.Attach(FontFactory::GetInstance());
     34   FontBuilderArray font_builder_array;
     35   BuilderForFontFile(SAMPLE_TTF_FILE, factory, &font_builder_array);
     36   FontBuilderPtr font_builder = font_builder_array[0];
     37 
     38   // ensure the builder is not bogus
     39   EXPECT_TRUE(font_builder != NULL);
     40   TableBuilderMap* builder_map = font_builder->table_builders();
     41   EXPECT_TRUE(builder_map != NULL);
     42   IntegerSet builder_tags;
     43   for (TableBuilderMap::iterator i = builder_map->begin(),
     44                                  e = builder_map->end(); i != e; ++i) {
     45     EXPECT_TRUE(i->second != NULL);
     46     if (i->second == NULL) {
     47       char tag[5] = {0};
     48       int32_t value = ToBE32(i->first);
     49       memcpy(tag, &value, 4);
     50       fprintf(stderr, "tag %s does not have valid builder\n", tag);
     51     } else {
     52       builder_tags.insert(i->first);
     53     }
     54   }
     55 
     56   FontHeaderTableBuilderPtr header_builder =
     57       down_cast<FontHeaderTable::Builder*>(
     58           font_builder->GetTableBuilder(Tag::head));
     59   int64_t mod_date = header_builder->Modified();
     60   header_builder->SetModified(mod_date + 1);
     61   FontPtr font;
     62   font.Attach(font_builder->Build());
     63 
     64   // ensure every table had a builder
     65   const TableMap* table_map = font->GetTableMap();
     66   for (TableMap::const_iterator i = table_map->begin(), e = table_map->end();
     67                                 i != e; ++i) {
     68     TablePtr table = (*i).second;
     69     HeaderPtr header = table->header();
     70     EXPECT_TRUE(builder_tags.find(header->tag()) != builder_tags.end());
     71     builder_tags.erase(header->tag());
     72   }
     73   EXPECT_TRUE(builder_tags.empty());
     74 
     75   FontHeaderTablePtr header =
     76       down_cast<FontHeaderTable*>(font->GetTable(Tag::head));
     77   int64_t after_mod_date = header->Modified();
     78   EXPECT_EQ(mod_date + 1, after_mod_date);
     79 
     80   // Checksum correctness of builder.
     81   TablePtr post = font->GetTable(Tag::post);
     82   EXPECT_EQ(post->CalculatedChecksum(), TTF_CHECKSUM[SAMPLE_TTF_POST]);
     83   return true;
     84 }
     85 
     86 }  // namespace sfntly
     87 
     88 TEST(OTFBasicEditing, All) {
     89   ASSERT_TRUE(sfntly::TestOTFBasicEditing());
     90 }
     91