summaryrefslogtreecommitdiff
path: root/test/serialization_test.cc
blob: 0f2f489f4b2a8673f5a3193c992463b8cf6883cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "gtest/gtest.h"
#include "sfntly/font.h"
#include "sfntly/font_factory.h"
#include "sfntly/port/memory_input_stream.h"
#include "sfntly/port/memory_output_stream.h"
#include "test/test_data.h"
#include "test/test_font_utils.h"
#include "test/serialization_test.h"

namespace sfntly {

bool TestSerialization() {
  FontFactoryPtr factory1, factory2, factory3;
  factory1.Attach(FontFactory::GetInstance());
  FontArray font_array;
  LoadFont(SAMPLE_TTF_FILE, factory1, &font_array);
  FontPtr original = font_array[0];

  factory2.Attach(FontFactory::GetInstance());
  FontBuilderArray font_builder_array;
  BuilderForFontFile(SAMPLE_TTF_FILE, factory2, &font_builder_array);
  FontBuilderPtr font_builder = font_builder_array[0];

  FontPtr intermediate;
  intermediate.Attach(font_builder->Build());
  MemoryOutputStream os;
  factory2->SerializeFont(intermediate, &os);

  factory3.Attach(FontFactory::GetInstance());
  FontArray new_font_array;
  MemoryInputStream is;
  is.Attach(os.Get(), os.Size());
  factory3->LoadFonts(&is, &new_font_array);
  FontPtr serialized = new_font_array[0];

  // Check number of tables
  EXPECT_EQ(original->num_tables(), serialized->num_tables());

  // Check if same set of tables
  const TableMap* original_tables = original->GetTableMap();
  const TableMap* serialized_tables = serialized->GetTableMap();
  EXPECT_EQ(original_tables->size(), serialized_tables->size());
  TableMap::const_iterator not_found = serialized_tables->end();
  for (TableMap::const_iterator b = original_tables->begin(),
                                e = original_tables->end(); b != e; ++b) {
    EXPECT_TRUE((serialized_tables->find(b->first) != not_found));
  }

  // TODO(arthurhsu): check cmap equivalence
  // Check checksum equivalence
  for (size_t i = 0; i < SAMPLE_TTF_KNOWN_TAGS; ++i) {
      TablePtr original_table = original->GetTable(TTF_KNOWN_TAGS[i]);
      TablePtr serialized_table = serialized->GetTable(TTF_KNOWN_TAGS[i]);
    EXPECT_EQ(original_table->CalculatedChecksum(),
              serialized_table->CalculatedChecksum());
    EXPECT_EQ(original_table->DataLength(), serialized_table->DataLength());

    if (TTF_KNOWN_TAGS[i] == Tag::hhea) {
      EXPECT_TRUE(VerifyHHEA(original_table, serialized_table));
    } else if (TTF_KNOWN_TAGS[i] == Tag::glyf) {
        EXPECT_TRUE(VerifyGLYF(original_table, serialized_table));
    } else if (TTF_KNOWN_TAGS[i] == Tag::hmtx) {
        EXPECT_TRUE(VerifyHMTX(original_table, serialized_table));
    } else if (TTF_KNOWN_TAGS[i] == Tag::loca) {
        EXPECT_TRUE(VerifyLOCA(original_table, serialized_table));
    } else if (TTF_KNOWN_TAGS[i] == Tag::maxp) {
        EXPECT_TRUE(VerifyMAXP(original_table, serialized_table));
    } else if (TTF_KNOWN_TAGS[i] == Tag::name) {
        EXPECT_TRUE(VerifyNAME(original_table, serialized_table));
    } else if (TTF_KNOWN_TAGS[i] == Tag::OS_2) {
        EXPECT_TRUE(VerifyOS_2(original_table, serialized_table));
    }
  }

  return true;
}

bool TestSerializationBitmap() {
  FontFactoryPtr factory1, factory2, factory3;
  factory1.Attach(FontFactory::GetInstance());
  FontArray font_array;
  LoadFont(SAMPLE_BITMAP_FONT, factory1, &font_array);
  FontPtr original = font_array[0];

  factory2.Attach(FontFactory::GetInstance());
  FontBuilderArray font_builder_array;
  BuilderForFontFile(SAMPLE_BITMAP_FONT, factory2, &font_builder_array);
  FontBuilderPtr font_builder = font_builder_array[0];

  FontPtr intermediate;
  intermediate.Attach(font_builder->Build());
  MemoryOutputStream os;
  factory2->SerializeFont(intermediate, &os);

  factory3.Attach(FontFactory::GetInstance());
  FontArray new_font_array;
  MemoryInputStream is;
  is.Attach(os.Get(), os.Size());
  factory3->LoadFonts(&is, &new_font_array);
  FontPtr serialized = new_font_array[0];

  // Check number of tables
  EXPECT_EQ(original->num_tables(), serialized->num_tables());

  // Check if same set of tables
  const TableMap* original_tables = original->GetTableMap();
  const TableMap* serialized_tables = serialized->GetTableMap();
  EXPECT_EQ(original_tables->size(), serialized_tables->size());
  TableMap::const_iterator not_found = serialized_tables->end();
  for (TableMap::const_iterator b = original_tables->begin(),
                                e = original_tables->end(); b != e; ++b) {
    EXPECT_TRUE((serialized_tables->find(b->first) != not_found));
  }

  // Check checksum equivalence
  for (size_t i = 0; i < SAMPLE_BITMAP_KNOWN_TAGS; ++i) {
      TablePtr original_table = original->GetTable(BITMAP_KNOWN_TAGS[i]);
      TablePtr serialized_table = serialized->GetTable(BITMAP_KNOWN_TAGS[i]);
    EXPECT_EQ(original_table->CalculatedChecksum(),
              serialized_table->CalculatedChecksum());
    EXPECT_EQ(original_table->DataLength(), serialized_table->DataLength());
  }

  return true;
}

}  // namespace sfntly

TEST(Serialization, Simple) {
  ASSERT_TRUE(sfntly::TestSerialization());
}

TEST(Serialization, Bitmap) {
  ASSERT_TRUE(sfntly::TestSerializationBitmap());
}