20617554c3eeba111853a6315d162459256ef0f1
[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 #include "hb-subset-private.hh"
33
34 /*
35  * Command line interface to the harfbuzz font subsetter.
36  */
37
38 struct subset_consumer_t
39 {
40   subset_consumer_t (option_parser_t *parser)
41       : failed (false), options (parser), subset_options (parser), font (nullptr), input (nullptr) {}
42
43   void init (hb_buffer_t  *buffer_,
44              const font_options_t *font_opts)
45   {
46     font = hb_font_reference (font_opts->get_font ());
47     input = hb_subset_input_create_or_fail ();
48   }
49
50   void consume_line (const char   *text,
51                      unsigned int  text_len,
52                      const char   *text_before,
53                      const char   *text_after)
54   {
55     // TODO(Q1) does this only get called with at least 1 codepoint?
56     hb_set_t *codepoints = hb_subset_input_unicode_set (input);
57     gchar *c = (gchar *)text;
58     do {
59       gunichar cp = g_utf8_get_char(c);
60       hb_codepoint_t hb_cp = cp;
61       hb_set_add (codepoints, hb_cp);
62     } while ((c = g_utf8_find_next_char(c, text + text_len)) != nullptr);
63   }
64
65   hb_bool_t
66   write_file (const char *output_file, hb_blob_t *blob) {
67     unsigned int data_length;
68     const char* data = hb_blob_get_data (blob, &data_length);
69
70     FILE *fp_out = fopen(output_file, "wb");
71     if (fp_out == nullptr) {
72       fprintf(stderr, "Unable to open output file\n");
73       return false;
74     }
75     int bytes_written = fwrite(data, 1, data_length, fp_out);
76
77     fclose (fp_out);
78
79     if (bytes_written == -1) {
80       fprintf(stderr, "Unable to write output file\n");
81       return false;
82     }
83     if ((unsigned int) bytes_written != data_length) {
84       fprintf(stderr, "Expected %u bytes written, got %d\n", data_length,
85               bytes_written);
86       return false;
87     }
88     return true;
89   }
90
91   void finish (const font_options_t *font_opts)
92   {
93     input->drop_hints = subset_options.drop_hints;
94
95     hb_subset_profile_t *subset_profile = hb_subset_profile_create();
96     hb_face_t *face = hb_font_get_face (font);
97
98     hb_face_t *new_face = hb_subset(face, subset_profile, input);
99     hb_blob_t *result = hb_face_reference_blob (new_face);
100
101     failed = !hb_blob_get_length (result);
102     if (!failed)
103       write_file (options.output_file, result);
104
105     hb_subset_profile_destroy (subset_profile);
106     hb_subset_input_destroy (input);
107     hb_blob_destroy (result);
108     hb_face_destroy (new_face);
109     hb_font_destroy (font);
110   }
111
112   public:
113   bool failed;
114
115   private:
116   output_options_t options;
117   subset_options_t subset_options;
118   hb_font_t *font;
119   hb_subset_input_t *input;
120 };
121
122 int
123 main (int argc, char **argv)
124 {
125   main_font_text_t<subset_consumer_t, 10, 0> driver;
126   return driver.main (argc, argv);
127 }