Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / util / hb-subset.cc
1 /*
2  * Copyright © 2010  Behdad Esfahbod
3  * Copyright © 2011,2012  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Google Author(s): Garret Rieger, Rod Sheeter
26  */
27
28 #include <stdio.h>
29
30 #include "main-font-text.hh"
31 #include "hb-subset.h"
32
33 /*
34  * Command line interface to the harfbuzz font subsetter.
35  */
36
37 struct subset_consumer_t
38 {
39   subset_consumer_t (option_parser_t *parser)
40       : failed (false), options (parser), subset_options (parser), font (nullptr), input (nullptr) {}
41
42   void init (hb_buffer_t  *buffer_,
43              const font_options_t *font_opts)
44   {
45     font = hb_font_reference (font_opts->get_font ());
46     input = hb_subset_input_reference (subset_options.input);
47   }
48
49   void consume_line (const char   *text,
50                      unsigned int  text_len,
51                      const char   *text_before,
52                      const char   *text_after)
53   {
54     // TODO(Q1) does this only get called with at least 1 codepoint?
55     hb_set_t *codepoints = hb_subset_input_unicode_set (input);
56     if (0 == strcmp (text, "*"))
57     {
58       hb_face_t *face = hb_font_get_face (font);
59       hb_face_collect_unicodes (face, codepoints);
60       return;
61     }
62
63     gchar *c = (gchar *)text;
64     do {
65       gunichar cp = g_utf8_get_char(c);
66       hb_codepoint_t hb_cp = cp;
67       hb_set_add (codepoints, hb_cp);
68     } while ((c = g_utf8_find_next_char(c, text + text_len)));
69   }
70
71   hb_bool_t
72   write_file (const char *output_file, hb_blob_t *blob) {
73     unsigned int data_length;
74     const char* data = hb_blob_get_data (blob, &data_length);
75
76     FILE *fp_out = fopen(output_file, "wb");
77     if (!fp_out) {
78       fprintf(stderr, "Unable to open output file\n");
79       return false;
80     }
81     int bytes_written = fwrite(data, 1, data_length, fp_out);
82
83     fclose (fp_out);
84
85     if (bytes_written == -1) {
86       fprintf(stderr, "Unable to write output file\n");
87       return false;
88     }
89     if ((unsigned int) bytes_written != data_length) {
90       fprintf(stderr, "Expected %u bytes written, got %d\n", data_length,
91               bytes_written);
92       return false;
93     }
94     return true;
95   }
96
97   void finish (const font_options_t *font_opts)
98   {
99     hb_face_t *face = hb_font_get_face (font);
100
101     hb_face_t *new_face = hb_subset (face, input);
102     hb_blob_t *result = hb_face_reference_blob (new_face);
103
104     failed = !hb_blob_get_length (result);
105     if (!failed)
106       write_file (options.output_file, result);
107
108     hb_subset_input_destroy (input);
109     hb_blob_destroy (result);
110     hb_face_destroy (new_face);
111     hb_font_destroy (font);
112   }
113
114   public:
115   bool failed;
116
117   private:
118   output_options_t options;
119   subset_options_t subset_options;
120   hb_font_t *font;
121   hb_subset_input_t *input;
122 };
123
124 int
125 main (int argc, char **argv)
126 {
127   main_font_text_t<subset_consumer_t, 10, 0> driver;
128   return driver.main (argc, argv);
129 }