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 <map>
18 #include <algorithm>
19 
20 #include "sfntly/font.h"
21 #include "sfntly/font_factory.h"
22 #include "sfntly/table/core/font_header_table.h"
23 #include "sfntly/tag.h"
24 #include "sfntly/data/memory_byte_array.h"
25 #include "sfntly/port/endian.h"
26 #include "sfntly/port/file_input_stream.h"
27 #include "sfntly/port/memory_output_stream.h"
28 #include "test/test_data.h"
29 #include "test/test_font_utils.h"
30 #include "sfntly/table/core/cmap_table.h"
31 #include "sfntly/port/refcount.h"
32 #include "gtest/gtest.h"
33 
34 namespace sfntly {
TEST(CMapEditingTest,RemoveAllButOneCMap)35 TEST(CMapEditingTest, RemoveAllButOneCMap) {
36   FontBuilderArray builders;
37   FontFactoryPtr font_factory;
38   font_factory.Attach(FontFactory::GetInstance());
39   BuilderForFontFile(SAMPLE_TTF_FILE, font_factory, &builders);
40   ASSERT_FALSE(builders.empty());
41   FontBuilderPtr font_builder = builders[0];
42   Ptr<CMapTable::Builder> cmap_table_builder =
43       (CMapTable::Builder*)font_builder->GetTableBuilder(Tag::cmap);
44   ASSERT_NE(cmap_table_builder, reinterpret_cast<CMapTable::Builder*>(NULL));
45   CMapTable::CMapBuilderMap*
46       cmap_builders = cmap_table_builder->GetCMapBuilders();
47   ASSERT_FALSE(cmap_builders->empty());
48 
49   for (CMapTable::CMapBuilderMap::iterator
50            it = cmap_builders->begin(); it != cmap_builders->end();) {
51     if (it->second->cmap_id() == CMapTable::WINDOWS_BMP) {
52       ++it;
53     } else {
54       cmap_builders->erase(it++);
55     }
56   }
57   ASSERT_EQ(cmap_builders->size(), (uint32_t)1);
58   Font* font = font_builder->Build();
59   CMapTablePtr cmap_table = down_cast<CMapTable*>(font->GetTable(Tag::cmap));
60   ASSERT_EQ(1, cmap_table->NumCMaps());
61   CMapTable::CMapPtr cmap;
62   cmap.Attach(cmap_table->GetCMap(CMapTable::WINDOWS_BMP));
63   ASSERT_EQ(CMapTable::WINDOWS_BMP, cmap->cmap_id());
64   delete font;
65 }
66 
TEST(CMapEditingTest,CopyAllCMapsToNewFont)67 TEST(CMapEditingTest, CopyAllCMapsToNewFont) {
68   FontArray fonts;
69   FontFactoryPtr font_factory;
70   font_factory.Attach(FontFactory::GetInstance());
71   LoadFont(SAMPLE_TTF_FILE, font_factory, &fonts);
72 
73   ASSERT_FALSE(fonts.empty());
74   ASSERT_FALSE(fonts[0] == NULL);
75   FontPtr font = fonts[0];
76   CMapTablePtr cmap_table = down_cast<CMapTable*>(font->GetTable(Tag::cmap));
77   FontBuilderPtr font_builder;
78   font_builder.Attach(font_factory->NewFontBuilder());
79   Ptr<CMapTable::Builder> cmap_table_builder =
80       (CMapTable::Builder*)font_builder->NewTableBuilder(Tag::cmap);
81 
82   CMapTable::CMapIterator cmap_iter(cmap_table, NULL);
83   while (cmap_iter.HasNext()) {
84     CMapTable::CMapPtr cmap;
85     cmap.Attach(cmap_iter.Next());
86     if (!cmap)
87       continue;
88     cmap_table_builder->NewCMapBuilder(cmap->cmap_id(), cmap->ReadFontData());
89   }
90 
91   FontPtr new_font;
92   new_font.Attach(font_builder->Build());
93   CMapTablePtr new_cmap_table =
94       down_cast<CMapTable*>(font->GetTable(Tag::cmap));
95   ASSERT_EQ(cmap_table->NumCMaps(), new_cmap_table->NumCMaps());
96   CMapTable::CMapPtr cmap;
97   cmap.Attach(cmap_table->GetCMap(CMapTable::WINDOWS_BMP));
98   ASSERT_NE(cmap, reinterpret_cast<CMapTable::CMap*>(NULL));
99   ASSERT_EQ(CMapTable::WINDOWS_BMP, cmap->cmap_id());
100 }
101 }
102