From d5b33f2fe1603e894e21f45afb4c00c8d670fb5c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 13 Feb 2018 13:50:50 -0800 Subject: [PATCH] [subset] hb_subset_input_t changes --- src/Makefile.sources | 1 + src/hb-face.h | 1 - src/hb-set.cc | 14 ++--- src/hb-subset-input.cc | 109 ++++++++++++++++++++++++++++++++++++ src/hb-subset-plan.cc | 2 +- src/hb-subset-private.hh | 11 +++- src/hb-subset.cc | 40 +------------ src/hb-subset.h | 12 +++- test/api/test-subset-glyf.c | 4 +- test/api/test-subset.c | 2 +- util/hb-subset.cc | 20 +++---- 11 files changed, 153 insertions(+), 63 deletions(-) create mode 100644 src/hb-subset-input.cc diff --git a/src/Makefile.sources b/src/Makefile.sources index f2821e3de..f412a65d2 100644 --- a/src/Makefile.sources +++ b/src/Makefile.sources @@ -185,6 +185,7 @@ HB_ICU_headers = hb-icu.h HB_SUBSET_sources = \ hb-subset.cc \ hb-subset-glyf.cc \ + hb-subset-input.cc \ hb-subset-plan.cc \ $(NULL) diff --git a/src/hb-face.h b/src/hb-face.h index 9842d52b6..0ce8d0462 100644 --- a/src/hb-face.h +++ b/src/hb-face.h @@ -71,7 +71,6 @@ hb_face_set_user_data (hb_face_t *face, hb_destroy_func_t destroy, hb_bool_t replace); - HB_EXTERN void * hb_face_get_user_data (hb_face_t *face, hb_user_data_key_t *key); diff --git a/src/hb-set.cc b/src/hb-set.cc index 0b4f871e8..f3b864c12 100644 --- a/src/hb-set.cc +++ b/src/hb-set.cc @@ -50,6 +50,13 @@ hb_set_create (void) return set; } +static const hb_set_t _hb_set_nil = { + HB_OBJECT_HEADER_STATIC, + true, /* in_error */ + + {0} /* elts */ +}; + /** * hb_set_get_empty: * @@ -60,13 +67,6 @@ hb_set_create (void) hb_set_t * hb_set_get_empty (void) { - static const hb_set_t _hb_set_nil = { - HB_OBJECT_HEADER_STATIC, - true, /* in_error */ - - {0} /* elts */ - }; - return const_cast (&_hb_set_nil); } diff --git a/src/hb-subset-input.cc b/src/hb-subset-input.cc new file mode 100644 index 000000000..75c016843 --- /dev/null +++ b/src/hb-subset-input.cc @@ -0,0 +1,109 @@ +/* + * Copyright © 2018 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Garret Rieger, Rod Sheeter, Behdad Esfahbod + */ + +#include "hb-object-private.hh" +#include "hb-subset-private.hh" +#include "hb-set-private.hh" + +/** + * hb_subset_input_create: + * + * Return value: New subset input. + * + * Since: 1.8.0 + **/ +hb_subset_input_t * +hb_subset_input_create (void) +{ + hb_subset_input_t *input = hb_object_create(); + + /* Unlike libharfbuzz, in this lib we return nullptr + * in case of allocation failure. */ + if (unlikely (!input)) + return nullptr; + + input->unicodes = hb_set_create (); + input->glyphs = hb_set_create (); + + return input; +} + +/** + * hb_subset_input_reference: (skip) + * @subset_input: a subset_input. + * + * + * + * Return value: + * + * Since: 1.8.0 + **/ +hb_subset_input_t * +hb_subset_input_reference (hb_subset_input_t *subset_input) +{ + return hb_object_reference (subset_input); +} + +/** + * hb_subset_input_destroy: + * @subset_input: a subset_input. + * + * Since: 1.8.0 + **/ +void +hb_subset_input_destroy(hb_subset_input_t *subset_input) +{ + if (!hb_object_destroy (subset_input)) return; + + hb_set_destroy (subset_input->unicodes); + hb_set_destroy (subset_input->glyphs); + + free (subset_input); +} + +/** + * hb_subset_input_unicode_set: + * @subset_input: a subset_input. + * + * Since: 1.8.0 + **/ +HB_EXTERN hb_set_t * +hb_subset_input_unicode_set (hb_subset_input_t *subset_input) +{ + return subset_input->unicodes; +} + +/** + * hb_subset_input_glyph_set: + * @subset_input: a subset_input. + * + * Since: 1.8.0 + **/ +HB_EXTERN hb_set_t * +hb_subset_input_glyph_set (hb_subset_input_t *subset_input) +{ + return subset_input->glyphs; +} diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index a06d38ae5..c9d39c5b0 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -140,7 +140,7 @@ hb_subset_plan_create (hb_face_t *face, plan->gids_to_retain.init(); plan->gids_to_retain_sorted.init(); - _populate_codepoints (input->codepoints, plan->codepoints); + _populate_codepoints (input->unicodes, plan->codepoints); _populate_gids_to_retain (face, plan->codepoints, plan->gids_to_retain, diff --git a/src/hb-subset-private.hh b/src/hb-subset-private.hh index 9689da808..4ef1b9540 100644 --- a/src/hb-subset-private.hh +++ b/src/hb-subset-private.hh @@ -38,7 +38,16 @@ struct hb_subset_input_t { hb_object_header_t header; ASSERT_POD (); - hb_set_t *codepoints; + hb_set_t *unicodes; + hb_set_t *glyphs; + + /* TODO + * + * features + * lookups + * nameIDs + * ... + */ }; #endif /* HB_SUBSET_PRIVATE_HH */ diff --git a/src/hb-subset.cc b/src/hb-subset.cc index 3b022dd44..09ae19fbf 100644 --- a/src/hb-subset.cc +++ b/src/hb-subset.cc @@ -21,14 +21,12 @@ * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. * - * Google Author(s): Garret Rieger, Rod Sheeter + * Google Author(s): Garret Rieger, Rod Sheeter, Behdad Esfahbod */ #include "hb-object-private.hh" #include "hb-open-type-private.hh" -#include "hb-private.hh" - #include "hb-subset-glyf.hh" #include "hb-subset-private.hh" #include "hb-subset-plan.hh" @@ -53,7 +51,7 @@ struct hb_subset_profile_t { * * Return value: New profile with default settings. * - * Since: 1.7.5 + * Since: 1.8.0 **/ hb_subset_profile_t * hb_subset_profile_create () @@ -64,7 +62,7 @@ hb_subset_profile_create () /** * hb_subset_profile_destroy: * - * Since: 1.7.5 + * Since: 1.8.0 **/ void hb_subset_profile_destroy (hb_subset_profile_t *profile) @@ -74,38 +72,6 @@ hb_subset_profile_destroy (hb_subset_profile_t *profile) free (profile); } -/** - * hb_subset_input_create: - * - * Return value: New subset input. - * - * Since: 1.7.5 - **/ -hb_subset_input_t * -hb_subset_input_create (hb_set_t *codepoints) -{ - if (unlikely (!codepoints)) - codepoints = hb_set_get_empty(); - - hb_subset_input_t *input = hb_object_create(); - input->codepoints = hb_set_reference(codepoints); - return input; -} - -/** - * hb_subset_input_destroy: - * - * Since: 1.7.5 - **/ -void -hb_subset_input_destroy(hb_subset_input_t *subset_input) -{ - if (!hb_object_destroy (subset_input)) return; - - hb_set_destroy (subset_input->codepoints); - free (subset_input); -} - template static hb_blob_t * _subset (hb_subset_plan_t *plan, hb_face_t *source) diff --git a/src/hb-subset.h b/src/hb-subset.h index 3eb41de84..81e11fe65 100644 --- a/src/hb-subset.h +++ b/src/hb-subset.h @@ -47,17 +47,27 @@ hb_subset_profile_destroy (hb_subset_profile_t *profile); /* * hb_subset_input_t + * * Things that change based on the input. Characters to keep, etc. */ typedef struct hb_subset_input_t hb_subset_input_t; HB_EXTERN hb_subset_input_t * -hb_subset_input_create (hb_set_t *codepoints); +hb_subset_input_create (void); + +HB_EXTERN hb_subset_input_t * +hb_subset_input_reference (hb_subset_input_t *subset_input); HB_EXTERN void hb_subset_input_destroy (hb_subset_input_t *subset_input); +HB_EXTERN hb_set_t * +hb_subset_input_unicode_set (hb_subset_input_t *subset_input); + +HB_EXTERN hb_set_t * +hb_subset_input_glyph_set (hb_subset_input_t *subset_input); + /* hb_subset() */ diff --git a/test/api/test-subset-glyf.c b/test/api/test-subset-glyf.c index fa41800b0..014f24338 100644 --- a/test/api/test-subset-glyf.c +++ b/test/api/test-subset-glyf.c @@ -94,9 +94,9 @@ test_subset_glyf (void) hb_face_t *face_abc_subset; hb_blob_t *glyf_expected_blob; hb_blob_t *glyf_actual_blob; - hb_set_t *codepoints = hb_set_create(); hb_subset_profile_t *profile = hb_subset_profile_create(); - hb_subset_input_t *input = hb_subset_input_create (codepoints); + hb_subset_input_t *input = hb_subset_input_create (); + hb_set_t *codepoints = hb_set_reference (hb_subset_input_unicode_set (input)); g_assert (face_abc); g_assert (face_ac); diff --git a/test/api/test-subset.c b/test/api/test-subset.c index 4184a563f..44eba1581 100644 --- a/test/api/test-subset.c +++ b/test/api/test-subset.c @@ -43,7 +43,7 @@ test_subset (void) hb_face_t *face = hb_face_create(font_blob, 0); hb_subset_profile_t *profile = hb_subset_profile_create(); - hb_subset_input_t *input = hb_subset_input_create (hb_set_get_empty ()); + hb_subset_input_t *input = hb_subset_input_create (); hb_face_t *out_face = hb_subset(face, profile, input); g_assert(out_face); diff --git a/util/hb-subset.cc b/util/hb-subset.cc index a3505a814..16abd14d5 100644 --- a/util/hb-subset.cc +++ b/util/hb-subset.cc @@ -37,13 +37,13 @@ struct subset_consumer_t { subset_consumer_t (option_parser_t *parser) - : failed (false), options (parser), font (nullptr), codepoints (nullptr) {} + : failed (false), options (parser), font (nullptr), input (nullptr) {} void init (hb_buffer_t *buffer_, const font_options_t *font_opts) { font = hb_font_reference (font_opts->get_font ()); - codepoints = hb_set_create(); + input = hb_subset_input_create (); } void consume_line (const char *text, @@ -51,14 +51,13 @@ struct subset_consumer_t const char *text_before, const char *text_after) { - // text appears to be a g_string when set by --unicodes - // TODO(Q1) are gunichar and hbcodepoint_t interchangeable? // TODO(Q1) does this only get called with at least 1 codepoint? + hb_set_t *codepoints = hb_subset_input_unicode_set (input); gchar *c = (gchar *)text; do { gunichar cp = g_utf8_get_char(c); - hb_codepoint_t hb_cp = cp; // TODO(Q1) is this safe? - hb_set_add(codepoints, hb_cp); + hb_codepoint_t hb_cp = cp; + hb_set_add (codepoints, hb_cp); } while ((c = g_utf8_find_next_char(c, text + text_len)) != nullptr); } @@ -90,12 +89,10 @@ struct subset_consumer_t void finish (const font_options_t *font_opts) { - // TODO(Q1) check for errors from creates and such hb_subset_profile_t *subset_profile = hb_subset_profile_create(); - hb_subset_input_t *subset_input = hb_subset_input_create (codepoints); hb_face_t *face = hb_font_get_face (font); - hb_face_t *new_face = hb_subset(face, subset_profile, subset_input); + hb_face_t *new_face = hb_subset(face, subset_profile, input); hb_blob_t *result = hb_face_reference_blob (new_face); failed = !hb_blob_get_length (result); @@ -103,8 +100,7 @@ struct subset_consumer_t write_file (options.output_file, result); hb_subset_profile_destroy (subset_profile); - hb_subset_input_destroy (subset_input); - hb_set_destroy (codepoints); + hb_subset_input_destroy (input); hb_blob_destroy (result); hb_face_destroy (new_face); hb_font_destroy (font); @@ -116,7 +112,7 @@ struct subset_consumer_t private: output_options_t options; hb_font_t *font; - hb_set_t *codepoints; + hb_subset_input_t *input; }; int