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 "sample/chromium/font_subsetter.h"
19 #include "test/test_data.h"
20 #include "test/test_font_utils.h"
21 
22 namespace {
23   // Use an additional variable to easily change name for testing.
24   const char* kInputFileName = sfntly::SAMPLE_TTF_FILE;
25   const char* kFontName = "Tuffy";
26   const char* kOutputFileName = "tuffy-s.ttf";
27   // The subset we want: Hello, world!
28   // The array is unsorted to verify that the subsetter gets the glyph id
29   // correctly.
30   const unsigned int kGlyphIds[] = { 43, 72, 79, 82, 15, 3, 90, 85, 71, 4 };
31   const unsigned int kGlyphIdsCount = sizeof(kGlyphIds) / sizeof(unsigned int);
32 }
33 
34 // This function is deliberately located at global namespace.
TestChromeSubsetter()35 bool TestChromeSubsetter() {
36   sfntly::ByteVector input_buffer;
37   sfntly::LoadFile(kInputFileName, &input_buffer);
38   EXPECT_GT(input_buffer.size(), (size_t)0);
39 
40   unsigned char* output_buffer = NULL;
41   int output_length =
42       SfntlyWrapper::SubsetFont(kFontName,
43                                 &(input_buffer[0]),
44                                 input_buffer.size(),
45                                 kGlyphIds,
46                                 kGlyphIdsCount,
47                                 &output_buffer);
48 
49   EXPECT_GT(output_length, 0);
50 
51   if (output_length > 0) {
52     FILE* output_file = NULL;
53 #if defined WIN32
54     fopen_s(&output_file, kOutputFileName, "wb");
55 #else
56     output_file = fopen(kOutputFileName, "wb");
57 #endif
58     EXPECT_TRUE((output_file != NULL));
59     if (output_file) {
60       int byte_count = fwrite(output_buffer, 1, output_length, output_file);
61       EXPECT_EQ(byte_count, output_length);
62       fflush(output_file);
63       fclose(output_file);
64     }
65 
66     delete[] output_buffer;
67     return true;
68   }
69 
70   return false;
71 }
72 
TEST(ChromeSubsetter,All)73 TEST(ChromeSubsetter, All) {
74   EXPECT_TRUE(TestChromeSubsetter());
75 }
76