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 <stdio.h>
18#include <stdlib.h>
19
20#include <map>
21#include <utility>
22
23#include "sfntly/font.h"
24#include "subtly/character_predicate.h"
25#include "subtly/stats.h"
26#include "subtly/subsetter.h"
27#include "subtly/utils.h"
28
29using namespace subtly;
30
31void PrintUsage(const char* program_name) {
32  fprintf(stdout, "Usage: %s <input_font_file> <output_font_file>"
33          "<start_char> <end_char>\n", program_name);
34}
35
36int main(int argc, const char** argv) {
37  const char* program_name = argv[0];
38  if (argc < 5) {
39    PrintUsage(program_name);
40    exit(1);
41  }
42
43  const char* input_font_path = argv[1];
44  const char* output_font_path = argv[2];
45  FontPtr font;
46  font.Attach(subtly::LoadFont(input_font_path));
47  if (font->num_tables() == 0) {
48    fprintf(stderr, "Could not load font %s.\n", input_font_path);
49    exit(1);
50  }
51
52  const char* start_char = argv[3];
53  const char* end_char = argv[4];
54  if (start_char[1] != 0) {
55    fprintf(stderr, "Start character %c invalid.\n", start_char[0]);
56    exit(1);
57  }
58  if (end_char[1] != 0) {
59    fprintf(stderr, "Start character %c invalid.\n", end_char[0]);
60    exit(1);
61  }
62  int32_t original_size = TotalFontSize(font);
63
64
65  Ptr<CharacterPredicate> range_predicate =
66      new AcceptRange(start_char[0], end_char[0]);
67  Ptr<Subsetter> subsetter = new Subsetter(font, range_predicate);
68  Ptr<Font> new_font;
69  new_font.Attach(subsetter->Subset());
70  if (!new_font) {
71    fprintf(stdout, "Cannot create subset.\n");
72    return 0;
73  }
74
75  subtly::SerializeFont(output_font_path, new_font);
76  subtly::PrintComparison(stdout, font, new_font);
77  int32_t new_size = TotalFontSize(new_font);
78  fprintf(stdout, "Went from %d to %d: %lf%% of original\n",
79          original_size, new_size,
80          static_cast<double>(new_size) / original_size * 100);
81  return 0;
82}
83