blob: ea657affe32bd52b18664d40725f4685775387b8 [file] [log] [blame]
Garret Rieger058b1262018-02-01 18:22:14 -08001/*
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 */
Rod Sheeter9ccb8362018-01-17 22:09:07 -080027
Garret Riegerc25898e2018-02-05 14:35:32 -080028#include <stdio.h>
Garret Rieger5bc0cda2018-02-02 17:49:14 -080029
Garret Rieger058b1262018-02-01 18:22:14 -080030#include "main-font-text.hh"
Rod Sheeteredcd3b82018-02-04 12:31:24 -080031#include "hb-subset.h"
Garret Rieger058b1262018-02-01 18:22:14 -080032
33/*
34 * Command line interface to the harfbuzz font subsetter.
35 */
36
37struct subset_consumer_t
38{
39 subset_consumer_t (option_parser_t *parser)
Behdad Esfahbodd5b33f22018-02-13 13:50:50 -080040 : failed (false), options (parser), font (nullptr), input (nullptr) {}
Garret Rieger058b1262018-02-01 18:22:14 -080041
42 void init (hb_buffer_t *buffer_,
43 const font_options_t *font_opts)
44 {
45 font = hb_font_reference (font_opts->get_font ());
Behdad Esfahbod9046e922018-02-13 22:07:12 -080046 input = hb_subset_input_create_or_fail ();
Garret Rieger058b1262018-02-01 18:22:14 -080047 }
48
49 void consume_line (const char *text,
50 unsigned int text_len,
51 const char *text_before,
52 const char *text_after)
53 {
Rod Sheeter6c52d942018-02-06 15:31:14 -080054 // TODO(Q1) does this only get called with at least 1 codepoint?
Behdad Esfahbodd5b33f22018-02-13 13:50:50 -080055 hb_set_t *codepoints = hb_subset_input_unicode_set (input);
Rod Sheeter6c52d942018-02-06 15:31:14 -080056 gchar *c = (gchar *)text;
57 do {
58 gunichar cp = g_utf8_get_char(c);
Behdad Esfahbodd5b33f22018-02-13 13:50:50 -080059 hb_codepoint_t hb_cp = cp;
60 hb_set_add (codepoints, hb_cp);
Rod Sheeter6c52d942018-02-06 15:31:14 -080061 } while ((c = g_utf8_find_next_char(c, text + text_len)) != nullptr);
Garret Rieger058b1262018-02-01 18:22:14 -080062 }
63
Behdad Esfahbodeac20e42018-02-04 20:08:50 -050064 hb_bool_t
65 write_file (const char *output_file, hb_blob_t *blob) {
Rod Sheeteredcd3b82018-02-04 12:31:24 -080066 unsigned int data_length;
67 const char* data = hb_blob_get_data (blob, &data_length);
68
Ebrahim Byagowi70e13ad2018-02-06 13:20:38 +033069 FILE *fp_out = fopen(output_file, "wb");
Garret Riegerc25898e2018-02-05 14:35:32 -080070 if (fp_out == nullptr) {
71 fprintf(stderr, "Unable to open output file\n");
Rod Sheeteredcd3b82018-02-04 12:31:24 -080072 return false;
73 }
Behdad Esfahbod4011d882018-02-10 14:09:06 -060074 int bytes_written = fwrite(data, 1, data_length, fp_out);
Garret Riegerc25898e2018-02-05 14:35:32 -080075
Behdad Esfahbod70b06cc2018-02-10 16:13:06 -060076 fclose (fp_out);
77
Rod Sheeteredcd3b82018-02-04 12:31:24 -080078 if (bytes_written == -1) {
79 fprintf(stderr, "Unable to write output file\n");
80 return false;
81 }
Behdad Esfahbod4011d882018-02-10 14:09:06 -060082 if ((unsigned int) bytes_written != data_length) {
83 fprintf(stderr, "Expected %u bytes written, got %d\n", data_length,
Rod Sheeteredcd3b82018-02-04 12:31:24 -080084 bytes_written);
85 return false;
Behdad Esfahbodeac20e42018-02-04 20:08:50 -050086 }
Rod Sheeteredcd3b82018-02-04 12:31:24 -080087 return true;
88 }
89
Garret Rieger058b1262018-02-01 18:22:14 -080090 void finish (const font_options_t *font_opts)
91 {
Rod Sheeteredcd3b82018-02-04 12:31:24 -080092 hb_subset_profile_t *subset_profile = hb_subset_profile_create();
Behdad Esfahbodeac20e42018-02-04 20:08:50 -050093 hb_face_t *face = hb_font_get_face (font);
Garret Rieger058b1262018-02-01 18:22:14 -080094
Behdad Esfahbodd5b33f22018-02-13 13:50:50 -080095 hb_face_t *new_face = hb_subset(face, subset_profile, input);
Behdad Esfahbodaf028122018-02-07 13:07:46 -050096 hb_blob_t *result = hb_face_reference_blob (new_face);
97
98 failed = !hb_blob_get_length (result);
99 if (!failed)
100 write_file (options.output_file, result);
Garret Rieger058b1262018-02-01 18:22:14 -0800101
Rod Sheeteredcd3b82018-02-04 12:31:24 -0800102 hb_subset_profile_destroy (subset_profile);
Behdad Esfahbodd5b33f22018-02-13 13:50:50 -0800103 hb_subset_input_destroy (input);
Garret Rieger058b1262018-02-01 18:22:14 -0800104 hb_blob_destroy (result);
Behdad Esfahbodaf028122018-02-07 13:07:46 -0500105 hb_face_destroy (new_face);
Garret Rieger058b1262018-02-01 18:22:14 -0800106 hb_font_destroy (font);
107 }
108
109 public:
110 bool failed;
111
112 private:
113 output_options_t options;
114 hb_font_t *font;
Behdad Esfahbodd5b33f22018-02-13 13:50:50 -0800115 hb_subset_input_t *input;
Garret Rieger058b1262018-02-01 18:22:14 -0800116};
Rod Sheeter9ccb8362018-01-17 22:09:07 -0800117
118int
119main (int argc, char **argv)
120{
Garret Rieger058b1262018-02-01 18:22:14 -0800121 main_font_text_t<subset_consumer_t, 10, 0> driver;
122 return driver.main (argc, argv);
Rod Sheeter9ccb8362018-01-17 22:09:07 -0800123}