summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEbrahim Byagowi <ebrahim@gnu.org>2018-10-12 03:00:59 +0330
committerBehdad Esfahbod <behdad@behdad.org>2018-10-12 16:06:39 -0400
commitdc49bd8d813571fe16d9e5342e4a3926ff947bd6 (patch)
treeec4607af7b8a59a460498af7c42c5cc7bafe7614
parente9f9c0d81c73d8b6d87700aadb5b886bd289769a (diff)
Add two APIs for getting stylistic set labels
* hb_ot_layout_feature_get_characters * hb_ot_layout_feature_get_name_ids However HarfBuzz currently doesn't expose an API for retrieving the actual information associated with NameId from the `name` table and that should be done separately.
-rw-r--r--docs/harfbuzz-sections.txt2
-rw-r--r--src/hb-ot-layout-common.hh14
-rw-r--r--src/hb-ot-layout.cc133
-rw-r--r--src/hb-ot-layout.h29
-rw-r--r--test/api/CMakeLists.txt2
-rw-r--r--test/api/Makefile.am1
-rw-r--r--test/api/fonts/cv01.otfbin0 -> 1956 bytes
-rw-r--r--test/api/test-ot-nameid.c96
8 files changed, 277 insertions, 0 deletions
diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt
index 9472af4f..fccfcb0e 100644
--- a/docs/harfbuzz-sections.txt
+++ b/docs/harfbuzz-sections.txt
@@ -479,7 +479,9 @@ HB_OT_TAG_GSUB
HB_OT_TAG_JSTF
hb_ot_layout_collect_lookups
hb_ot_layout_collect_features
+hb_ot_layout_feature_get_characters
hb_ot_layout_feature_get_lookups
+hb_ot_layout_feature_get_name_ids
hb_ot_layout_feature_with_variations_get_lookups
hb_ot_layout_get_attach_points
hb_ot_layout_get_glyph_class
diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh
index e5e996d4..bf0a9c1e 100644
--- a/src/hb-ot-layout-common.hh
+++ b/src/hb-ot-layout-common.hh
@@ -521,6 +521,20 @@ struct FeatureParams
return Null(FeatureParamsSize);
}
+ inline const FeatureParamsStylisticSet& get_stylistic_set_params (hb_tag_t tag) const
+ {
+ if ((tag & 0xFFFF0000u) == HB_TAG ('s','s','\0','\0')) /* ssXX */
+ return u.stylisticSet;
+ return Null(FeatureParamsStylisticSet);
+ }
+
+ inline const FeatureParamsCharacterVariants& get_character_variants_params (hb_tag_t tag) const
+ {
+ if ((tag & 0xFFFF0000u) == HB_TAG ('c','v','\0','\0')) /* cvXX */
+ return u.characterVariants;
+ return Null(FeatureParamsCharacterVariants);
+ }
+
private:
union {
FeatureParamsSize size;
diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc
index aa36954d..4c751e64 100644
--- a/src/hb-ot-layout.cc
+++ b/src/hb-ot-layout.cc
@@ -1116,6 +1116,139 @@ hb_ot_layout_get_size_params (hb_face_t *face,
return false;
}
+/**
+ * hb_ot_layout_feature_get_name_ids:
+ * @face: #hb_face_t to work upon
+ * @table_tag:
+ * @feature_index:
+ * @feature_tag: ssXX and cvXX tag
+ * @label_id: (out) (allow-none): The ‘name’ table name ID that specifies a string
+ * for a user-interface label for this feature. (May be NULL.)
+ * @tooltip_id: (out) (allow-none): The ‘name’ table name ID that specifies a string
+ * that an application can use for tooltip text for this
+ * feature. (May be NULL.)
+ * @sample_id: (out) (allow-none): The ‘name’ table name ID that specifies sample text
+ * that illustrates the effect of this feature. (May be NULL.)
+ * @num_named_parameters: (out) (allow-none): Number of named parameters. (May be zero.)
+ * @first_param_id: (out) (allow-none): The first ‘name’ table name ID used to specify
+ * strings for user-interface labels for the feature
+ * parameters. (Must be zero if numParameters is zero.)
+ *
+ * Return value: true if could find any feature with the tag, false otherwise
+ *
+ * Since: REPLACEME
+ **/
+hb_bool_t
+hb_ot_layout_feature_get_name_ids (hb_face_t *face,
+ hb_tag_t table_tag,
+ unsigned int feature_index,
+ hb_tag_t feature_tag,
+ hb_name_id_t *label_id, /* OUT. May be NULL */
+ hb_name_id_t *tooltip_id, /* OUT. May be NULL */
+ hb_name_id_t *sample_id, /* OUT. May be NULL */
+ unsigned int *num_named_parameters, /* OUT. May be NULL */
+ hb_name_id_t *first_param_id /* OUT. May be NULL */)
+{
+ static_assert ((OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX), "");
+ const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
+
+ const OT::Feature &f = g.get_feature (feature_index);
+
+ const OT::FeatureParams &feature_params = f.get_feature_params ();
+ if (&feature_params != &Null (OT::FeatureParams))
+ {
+ const OT::FeatureParamsStylisticSet& ss_params =
+ feature_params.get_stylistic_set_params (feature_tag);
+ if (&ss_params != &Null (OT::FeatureParamsStylisticSet)) /* ssXX */
+ {
+#define PARAM(a, A) if (a) *a = A
+ PARAM(label_id, ss_params.uiNameID);
+ // ssXX features don't have the rest
+ PARAM(tooltip_id, HB_NAME_ID_INVALID);
+ PARAM(sample_id, HB_NAME_ID_INVALID);
+ PARAM(num_named_parameters, 0);
+ PARAM(first_param_id, HB_NAME_ID_INVALID);
+ return true;
+ }
+ const OT::FeatureParamsCharacterVariants& cv_params =
+ feature_params.get_character_variants_params (feature_tag);
+ if (&cv_params != &Null (OT::FeatureParamsCharacterVariants)) /* cvXX */
+ {
+ PARAM(label_id, cv_params.featUILableNameID);
+ PARAM(tooltip_id, cv_params.featUITooltipTextNameID);
+ PARAM(sample_id, cv_params.sampleTextNameID);
+ PARAM(num_named_parameters, cv_params.numNamedParameters);
+ PARAM(first_param_id, cv_params.firstParamUILabelNameID);
+ return true;
+ }
+ }
+
+ PARAM(label_id, HB_NAME_ID_INVALID);
+ PARAM(tooltip_id, HB_NAME_ID_INVALID);
+ PARAM(sample_id, HB_NAME_ID_INVALID);
+ PARAM(num_named_parameters, 0);
+ PARAM(first_param_id, HB_NAME_ID_INVALID);
+#undef PARAM
+ return false;
+}
+
+/**
+ * hb_ot_layout_feature_get_characters::
+ * @face: #hb_face_t to work upon
+ * @feature_tag: cvXX tag
+ * @table_tag:
+ * @feature_index:
+ * @start_offset: In case the resulting char_count was equal to its input value, there
+ * is a chance there were more characters on the tag so this API can be
+ * called with an offset till resulting char_count gets to a number
+ * lower than input buffer (or consider using just a bigger buffer for
+ * one shot copying).
+ * @char_count: (in/out) (allow-none): The count of characters for which this feature
+ * provides glyph variants. (May be zero.)
+ * @characters: (out) (allow-none): A buffer pointer. The Unicode Scalar Value
+ * of the characters for which this feature provides glyph variants.
+ *
+ * Return value: Number of total sample characters in the cvXX feature.
+ *
+ * Since: REPLACEME
+ **/
+unsigned int
+hb_ot_layout_feature_get_characters (hb_face_t *face,
+ hb_tag_t table_tag,
+ unsigned int feature_index,
+ hb_tag_t feature_tag,
+ unsigned int start_offset,
+ unsigned int *char_count, /* IN/OUT. May be NULL */
+ hb_codepoint_t *characters /* OUT. May be NULL */)
+{
+ static_assert ((OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX), "");
+ const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
+
+ const OT::Feature &f = g.get_feature (feature_index);
+
+ const OT::FeatureParams &feature_params = f.get_feature_params ();
+
+ const OT::FeatureParamsCharacterVariants& cv_params =
+ feature_params.get_character_variants_params(feature_tag);
+ if (&cv_params != &Null (OT::FeatureParamsCharacterVariants))
+ {
+ unsigned int len = 0;
+ if (char_count && characters && start_offset < cv_params.characters.len)
+ {
+ len = MIN (cv_params.characters.len - start_offset, *char_count);
+ for (unsigned int i = 0; i < len; ++i)
+ characters[i] = cv_params.characters[start_offset + i];
+ }
+#define PARAM(a, A) if (a) *a = A
+ PARAM(char_count, len);
+ return cv_params.characters.len;
+ }
+ PARAM(char_count, 0);
+ PARAM(characters, 0);
+#undef PARAM
+ return 0;
+}
+
/*
* Parts of different types are implemented here such that they have direct
diff --git a/src/hb-ot-layout.h b/src/hb-ot-layout.h
index eb0df893..0be85cb2 100644
--- a/src/hb-ot-layout.h
+++ b/src/hb-ot-layout.h
@@ -330,6 +330,35 @@ hb_ot_layout_get_size_params (hb_face_t *face,
unsigned int *range_start, /* OUT. May be NULL */
unsigned int *range_end /* OUT. May be NULL */);
+/**
+ * hb_name_id_t:
+ *
+ * Since: REPLACEME
+ */
+typedef unsigned int hb_name_id_t;
+
+#define HB_NAME_ID_INVALID 0xFFFF
+
+HB_EXTERN hb_bool_t
+hb_ot_layout_feature_get_name_ids (hb_face_t *face,
+ hb_tag_t table_tag,
+ unsigned int feature_index,
+ hb_tag_t feature_tag,
+ hb_name_id_t *label_id /* OUT. May be NULL */,
+ hb_name_id_t *tooltip_id /* OUT. May be NULL */,
+ hb_name_id_t *sample_id /* OUT. May be NULL */,
+ unsigned int *num_named_parameters /* OUT. May be NULL */,
+ hb_name_id_t *first_param_id /* OUT. May be NULL */);
+
+
+HB_EXTERN unsigned int
+hb_ot_layout_feature_get_characters (hb_face_t *face,
+ hb_tag_t table_tag,
+ unsigned int feature_index,
+ hb_tag_t feature_tag,
+ unsigned int start_offset,
+ unsigned int *char_count /* IN/OUT. May be NULL */,
+ hb_codepoint_t *characters /* OUT. May be NULL */);
/*
* BASE
diff --git a/test/api/CMakeLists.txt b/test/api/CMakeLists.txt
index b540eb50..77cb3577 100644
--- a/test/api/CMakeLists.txt
+++ b/test/api/CMakeLists.txt
@@ -3,6 +3,8 @@ if (HB_HAVE_GLIB)
extract_make_variable (TEST_PROGS ${MAKEFILEAM})
list (APPEND TEST_PROGS
+ test-ot-color
+ test-ot-nameid
test-ot-tag
test-c
test-cplusplus
diff --git a/test/api/Makefile.am b/test/api/Makefile.am
index 3ff7f5a8..0e3f9a40 100644
--- a/test/api/Makefile.am
+++ b/test/api/Makefile.am
@@ -71,6 +71,7 @@ endif
TEST_PROGS += \
test-ot-color \
+ test-ot-nameid \
test-ot-tag \
$(NULL)
diff --git a/test/api/fonts/cv01.otf b/test/api/fonts/cv01.otf
new file mode 100644
index 00000000..01dbf01f
--- /dev/null
+++ b/test/api/fonts/cv01.otf
Binary files differ
diff --git a/test/api/test-ot-nameid.c b/test/api/test-ot-nameid.c
new file mode 100644
index 00000000..1389129f
--- /dev/null
+++ b/test/api/test-ot-nameid.c
@@ -0,0 +1,96 @@
+/*
+ * Copyright © 2018 Ebrahim Byagowi
+ *
+ * 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.
+ *
+ */
+
+#include <hb.h>
+#include <hb-ot.h>
+#include <glib.h>
+
+static const char *font_path = "fonts/cv01.otf";
+
+int
+main (int argc, char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+
+#if GLIB_CHECK_VERSION(2,37,2)
+ gchar *default_path = g_test_build_filename (G_TEST_DIST, font_path, NULL);
+#else
+ gchar *default_path = g_strdup (font_path);
+#endif
+
+ hb_blob_t *blob;
+ hb_face_t *face;
+ hb_font_t *font;
+
+ char *path = argc > 1 && *argv[1] ? argv[1] : (char *) default_path;
+ blob = hb_blob_create_from_file (path);
+ if (hb_blob_get_length (blob) == 0)
+ g_error ("Font not found.");
+
+ face = hb_face_create (blob, 0);
+ font = hb_font_create (face);
+
+ hb_tag_t cv01 = HB_TAG ('c','v','0','1');
+ unsigned int feature_index = 0;
+ // FIXME: See why below doesn't work
+ // if (!hb_ot_layout_language_find_feature (face, HB_OT_TAG_GSUB, 0, 0, cv01, &feature_index))
+ // g_error ("Failed to find feature index");
+
+ hb_name_id_t label_id;
+ hb_name_id_t tooltip_id;
+ hb_name_id_t sample_id;
+ unsigned int num_named_parameters;
+ hb_name_id_t first_param_id;
+ if (!hb_ot_layout_feature_get_name_ids (face, HB_OT_TAG_GSUB, feature_index,
+ cv01, &label_id, &tooltip_id, &sample_id,
+ &num_named_parameters, &first_param_id))
+ g_error ("Failed to get name ids");
+
+ g_assert (label_id == 256);
+ g_assert (tooltip_id == 257);
+ g_assert (sample_id == 258);
+ g_assert (num_named_parameters == 2);
+ g_assert (first_param_id == 259);
+
+ hb_codepoint_t characters[100];
+ unsigned int char_count = 100;
+
+ unsigned int all_chars;
+ all_chars = hb_ot_layout_feature_get_characters (face, HB_OT_TAG_GSUB, feature_index,
+ cv01, 0, &char_count, characters);
+
+ g_assert (all_chars == 2);
+ g_assert (char_count == 2);
+ g_assert (characters[0] == 10);
+ g_assert (characters[1] == 24030);
+
+ hb_font_destroy (font);
+ hb_face_destroy (face);
+ hb_blob_destroy (blob);
+
+ g_free (default_path);
+
+ return 0;
+}