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// Remove VC++ nag on fopen.
18#define _CRT_SECURE_NO_WARNINGS
19
20#include "sample/subsetter/subset_util.h"
21
22#include <stdio.h>
23
24#include <vector>
25#include <memory>
26
27#include "sfntly/font.h"
28#include "sfntly/data/memory_byte_array.h"
29#include "sfntly/port/memory_output_stream.h"
30#include "sfntly/port/type.h"
31#include "sfntly/tag.h"
32#include "sfntly/tools/subsetter/subsetter.h"
33
34namespace sfntly {
35
36SubsetUtil::SubsetUtil() {
37}
38
39SubsetUtil::~SubsetUtil() {
40}
41
42void SubsetUtil::Subset(const char *input_file_path,
43                        const char *output_file_path) {
44  UNREFERENCED_PARAMETER(output_file_path);
45  ByteVector input_buffer;
46  FILE* input_file = fopen(input_file_path, "rb");
47  if (input_file == NULL) {
48    fprintf(stderr, "file not found\n");
49    return;
50  }
51  fseek(input_file, 0, SEEK_END);
52  size_t file_size = ftell(input_file);
53  fseek(input_file, 0, SEEK_SET);
54  input_buffer.resize(file_size);
55  size_t bytes_read = fread(&(input_buffer[0]), 1, file_size, input_file);
56  UNREFERENCED_PARAMETER(bytes_read);
57  fclose(input_file);
58
59  FontFactoryPtr factory;
60  factory.Attach(FontFactory::GetInstance());
61
62  FontArray font_array;
63  factory->LoadFonts(&input_buffer, &font_array);
64  if (font_array.empty() || font_array[0] == NULL)
65    return;
66
67  IntegerList glyphs;
68  for (int32_t i = 0; i < 10; i++) {
69    glyphs.push_back(i);
70  }
71  glyphs.push_back(11);
72  glyphs.push_back(10);
73
74  Ptr<Subsetter> subsetter = new Subsetter(font_array[0], factory);
75  subsetter->SetGlyphs(&glyphs);
76  IntegerSet remove_tables;
77  remove_tables.insert(Tag::DSIG);
78  subsetter->SetRemoveTables(&remove_tables);
79
80  FontBuilderPtr font_builder;
81  font_builder.Attach(subsetter->Subset());
82
83  FontPtr new_font;
84  new_font.Attach(font_builder->Build());
85
86  // TODO(arthurhsu): glyph renumbering/Loca table
87  // TODO(arthurhsu): alter CMaps
88
89  MemoryOutputStream output_stream;
90  factory->SerializeFont(new_font, &output_stream);
91
92  FILE* output_file = fopen(output_file_path, "wb");
93  fwrite(output_stream.Get(), 1, output_stream.Size(), output_file);
94  fflush(output_file);
95  fclose(output_file);
96}
97
98}  // namespace sfntly
99