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
TestOTFBasicEditing()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 if (font_builder_array.size() != 1) {
37 EXPECT_TRUE(false);
38 return false;
39 }
40 FontBuilderPtr font_builder = font_builder_array[0];
41
42 // ensure the builder is not bogus
43 if (!font_builder) {
44 EXPECT_TRUE(false);
45 return false;
46 }
47 TableBuilderMap* builder_map = font_builder->table_builders();
48 if (!builder_map) {
49 EXPECT_TRUE(false);
50 return false;
51 }
52 IntegerSet builder_tags;
53 for (TableBuilderMap::iterator i = builder_map->begin(),
54 e = builder_map->end(); i != e; ++i) {
55 if (!i->second) {
56 EXPECT_TRUE(false);
57 char tag[5] = {0};
58 int32_t value = ToBE32(i->first);
59 memcpy(tag, &value, 4);
60 fprintf(stderr, "tag %s does not have valid builder\n", tag);
61 continue;
62 }
63 builder_tags.insert(i->first);
64 }
65
66 FontHeaderTableBuilderPtr header_builder =
67 down_cast<FontHeaderTable::Builder*>(
68 font_builder->GetTableBuilder(Tag::head));
69 int64_t mod_date = header_builder->Modified();
70 EXPECT_EQ(3397043097, mod_date);
71 header_builder->SetModified(mod_date + 1);
72 FontPtr font;
73 font.Attach(font_builder->Build());
74
75 // ensure every table had a builder
76 const TableMap* table_map = font->GetTableMap();
77 for (TableMap::const_iterator i = table_map->begin(), e = table_map->end();
78 i != e; ++i) {
79 TablePtr table = i->second;
80 HeaderPtr header = table->header();
81 size_t erased = builder_tags.erase(header->tag());
82 EXPECT_EQ(1U, erased);
83 }
84 EXPECT_TRUE(builder_tags.empty());
85
86 FontHeaderTablePtr header =
87 down_cast<FontHeaderTable*>(font->GetTable(Tag::head));
88 int64_t after_mod_date = header->Modified();
89 EXPECT_EQ(mod_date + 1, after_mod_date);
90
91 // Checksum correctness of builder.
92 TablePtr post = font->GetTable(Tag::post);
93 EXPECT_EQ(TTF_CHECKSUM[SAMPLE_TTF_POST], post->CalculatedChecksum());
94 return true;
95 }
96
97 } // namespace sfntly
98
TEST(OTFBasicEditing,All)99 TEST(OTFBasicEditing, All) {
100 ASSERT_TRUE(sfntly::TestOTFBasicEditing());
101 }
102