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 19 #include "sfntly/data/font_input_stream.h" 20 #include "sfntly/data/memory_byte_array.h" 21 #include "sfntly/font.h" 22 #include "sfntly/font_factory.h" 23 #include "sfntly/table/core/font_header_table.h" 24 #include "sfntly/table/table.h" 25 #include "sfntly/table/generic_table_builder.h" 26 #include "sfntly/table/table_based_table_builder.h" 27 #include "sfntly/tag.h" 28 #include "sfntly/port/file_input_stream.h" 29 #include "test/test_data.h" 30 #include "test/test_font_utils.h" 31 32 namespace sfntly { 33 34 bool TestFontParsing() { 35 ByteVector input_buffer; 36 LoadFile(SAMPLE_TTF_FILE, &input_buffer); 37 38 FontFactoryPtr factory; 39 factory.Attach(FontFactory::GetInstance()); 40 // File based 41 FontBuilderArray font_builder_array; 42 BuilderForFontFile(SAMPLE_TTF_FILE, factory, &font_builder_array); 43 FontBuilderPtr font_builder = font_builder_array[0]; 44 // Memory based 45 FontBuilderArray font_builder_array2; 46 factory->LoadFontsForBuilding(&input_buffer, &font_builder_array2); 47 FontBuilderPtr font_builder2 = font_builder_array2[0]; 48 49 for (size_t i = 0; i < SAMPLE_TTF_KNOWN_TAGS; ++i) { 50 EXPECT_TRUE(font_builder->HasTableBuilder(TTF_KNOWN_TAGS[i])); 51 EXPECT_TRUE(font_builder2->HasTableBuilder(TTF_KNOWN_TAGS[i])); 52 } 53 54 // Generic table 55 Ptr<GenericTableBuilder> gdef_builder = 56 down_cast<GenericTableBuilder*>(font_builder->GetTableBuilder(Tag::feat)); 57 HeaderPtr gdef_header = gdef_builder->header(); 58 EXPECT_EQ(gdef_header->length(), TTF_LENGTH[SAMPLE_TTF_FEAT]); 59 EXPECT_EQ(gdef_header->offset(), TTF_OFFSET[SAMPLE_TTF_FEAT]); 60 EXPECT_EQ(gdef_header->checksum(), TTF_CHECKSUM[SAMPLE_TTF_FEAT]); 61 EXPECT_TRUE(gdef_header->checksum_valid()); 62 63 WritableFontDataPtr wfd; 64 wfd.Attach(gdef_builder->Data()); 65 ByteVector b; 66 b.resize(TTF_LENGTH[SAMPLE_TTF_FEAT]); 67 wfd->ReadBytes(0, &(b[0]), 0, TTF_LENGTH[SAMPLE_TTF_FEAT]); 68 EXPECT_EQ(memcmp(&(b[0]), TTF_FEAT_DATA, TTF_LENGTH[SAMPLE_TTF_FEAT]), 0); 69 70 // Header table 71 FontHeaderTableBuilderPtr header_builder = 72 down_cast<FontHeaderTable::Builder*>( 73 font_builder->GetTableBuilder(Tag::head)); 74 HeaderPtr header_header = header_builder->header(); 75 EXPECT_EQ(header_header->length(), TTF_LENGTH[SAMPLE_TTF_HEAD]); 76 EXPECT_EQ(header_header->offset(), TTF_OFFSET[SAMPLE_TTF_HEAD]); 77 EXPECT_EQ(header_header->checksum(), TTF_CHECKSUM[SAMPLE_TTF_HEAD]); 78 EXPECT_TRUE(header_header->checksum_valid()); 79 80 // Data conformance 81 for (size_t i = 0; i < SAMPLE_TTF_KNOWN_TAGS; ++i) { 82 ByteVector b1, b2; 83 b1.resize(TTF_LENGTH[i]); 84 b2.resize(TTF_LENGTH[i]); 85 TableBuilderPtr builder1 = 86 font_builder->GetTableBuilder(TTF_KNOWN_TAGS[i]); 87 TableBuilderPtr builder2 = 88 font_builder2->GetTableBuilder(TTF_KNOWN_TAGS[i]); 89 WritableFontDataPtr wfd1; 90 wfd1.Attach(builder1->Data()); 91 WritableFontDataPtr wfd2; 92 wfd2.Attach(builder2->Data()); 93 wfd1->ReadBytes(0, &(b1[0]), 0, TTF_LENGTH[i]); 94 wfd2->ReadBytes(0, &(b2[0]), 0, TTF_LENGTH[i]); 95 EXPECT_EQ(memcmp(&(b1[0]), &(b2[0]), TTF_LENGTH[i]), 0); 96 } 97 98 return true; 99 } 100 101 bool TestTTFReadWrite() { 102 FontFactoryPtr factory; 103 factory.Attach(FontFactory::GetInstance()); 104 FontBuilderArray font_builder_array; 105 BuilderForFontFile(SAMPLE_TTF_FILE, factory, &font_builder_array); 106 FontBuilderPtr font_builder = font_builder_array[0]; 107 FontPtr font; 108 font.Attach(font_builder->Build()); 109 MemoryOutputStream output_stream; 110 factory->SerializeFont(font, &output_stream); 111 EXPECT_GE(output_stream.Size(), SAMPLE_TTF_SIZE); 112 113 return true; 114 } 115 116 bool TestTTFMemoryBasedReadWrite() { 117 ByteVector input_buffer; 118 LoadFile(SAMPLE_TTF_FILE, &input_buffer); 119 120 FontFactoryPtr factory; 121 factory.Attach(FontFactory::GetInstance()); 122 FontBuilderArray font_builder_array; 123 factory->LoadFontsForBuilding(&input_buffer, &font_builder_array); 124 FontBuilderPtr font_builder = font_builder_array[0]; 125 FontPtr font; 126 font.Attach(font_builder->Build()); 127 MemoryOutputStream output_stream; 128 factory->SerializeFont(font, &output_stream); 129 EXPECT_GE(output_stream.Size(), input_buffer.size()); 130 131 return true; 132 } 133 134 } // namespace sfntly 135 136 TEST(FontParsing, All) { 137 ASSERT_TRUE(sfntly::TestFontParsing()); 138 ASSERT_TRUE(sfntly::TestTTFReadWrite()); 139 ASSERT_TRUE(sfntly::TestTTFMemoryBasedReadWrite()); 140 } 141