From ec3270c7bb162a8e8996192922628502dab9d5a2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 24 Feb 2023 13:31:10 -0700 Subject: [PATCH] [wasm-graphite] Ensure native direction here too Binds buffer_get_script and script_get_horizontal_direction. --- src/hb-wasm-api-buffer.hh | 8 +++++++ src/hb-wasm-api-common.hh | 44 ++++++++++++++++++++++++++++++++++++++ src/hb-wasm-api-list.hh | 3 +++ src/hb-wasm-api.cc | 1 + src/hb-wasm-api.h | 11 ++++++++++ src/meson.build | 1 + src/wasm-graphite/shape.cc | 21 ++++++++++++------ 7 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 src/hb-wasm-api-common.hh diff --git a/src/hb-wasm-api-buffer.hh b/src/hb-wasm-api-buffer.hh index 40f5700a6..1d9612e88 100644 --- a/src/hb-wasm-api-buffer.hh +++ b/src/hb-wasm-api-buffer.hh @@ -132,6 +132,14 @@ HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV return (direction_t) hb_buffer_get_direction (buffer); } +HB_WASM_API (script_t, buffer_get_script) (HB_WASM_EXEC_ENV + ptr_d(buffer_t, buffer)) +{ + HB_REF2OBJ (buffer); + + return hb_script_to_iso15924_tag (hb_buffer_get_script (buffer)); +} + HB_WASM_API (void, buffer_reverse) (HB_WASM_EXEC_ENV ptr_d(buffer_t, buffer)) { diff --git a/src/hb-wasm-api-common.hh b/src/hb-wasm-api-common.hh new file mode 100644 index 000000000..c38ca9ced --- /dev/null +++ b/src/hb-wasm-api-common.hh @@ -0,0 +1,44 @@ +/* + * Copyright © 2023 Behdad Esfahbod + * + * 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. + */ + +#ifndef HB_WASM_API_COMMON_HH +#define HB_WASM_API_COMMON_HH + +#include "hb-wasm-api.hh" + +namespace hb { +namespace wasm { + + +HB_WASM_API (direction_t, script_get_horizontal_direction) (HB_WASM_EXEC_ENV + script_t script) +{ + return (direction_t) + hb_script_get_horizontal_direction (hb_script_from_iso15924_tag (script)); +} + + +}} + +#endif /* HB_WASM_API_COMMON_HH */ diff --git a/src/hb-wasm-api-list.hh b/src/hb-wasm-api-list.hh index 475bf06e2..0ded35980 100644 --- a/src/hb-wasm-api-list.hh +++ b/src/hb-wasm-api-list.hh @@ -50,6 +50,8 @@ static void debugprint4 (HB_WASM_EXEC_ENV char *str, int32_t i1, int32_t i2, int * https://github.com/bytecodealliance/wasm-micro-runtime/blob/main/doc/export_native_api.md */ static NativeSymbol _hb_wasm_native_symbols[] = { + /* common */ + NATIVE_SYMBOL ("(i)i", script_get_horizontal_direction), /* blob */ NATIVE_SYMBOL ("(i)", blob_free), @@ -60,6 +62,7 @@ static NativeSymbol _hb_wasm_native_symbols[] = NATIVE_SYMBOL ("(ii)", buffer_copy_contents), NATIVE_SYMBOL ("(ii)i", buffer_set_contents), NATIVE_SYMBOL ("(i)i", buffer_get_direction), + NATIVE_SYMBOL ("(i)i", buffer_get_script), NATIVE_SYMBOL ("(i)", buffer_reverse), NATIVE_SYMBOL ("(i)", buffer_reverse_clusters), diff --git a/src/hb-wasm-api.cc b/src/hb-wasm-api.cc index bc3babd56..ae5c1835e 100644 --- a/src/hb-wasm-api.cc +++ b/src/hb-wasm-api.cc @@ -29,6 +29,7 @@ #include "hb-wasm-api-blob.hh" #include "hb-wasm-api-buffer.hh" +#include "hb-wasm-api-common.hh" #include "hb-wasm-api-face.hh" #include "hb-wasm-api-font.hh" #include "hb-wasm-api-shape.hh" diff --git a/src/hb-wasm-api.h b/src/hb-wasm-api.h index d72f4d2ac..a59b405a4 100644 --- a/src/hb-wasm-api.h +++ b/src/hb-wasm-api.h @@ -92,6 +92,14 @@ typedef enum { #define DIRECTION_IS_BACKWARD(dir) ((((unsigned int) (dir)) & ~2U) == 5) #define DIRECTION_REVERSE(dir) ((direction_t) (((unsigned int) (dir)) ^ 1)) +typedef tag_t script_t; /* ISO 15924 representation of Unicode scripts. */ + + +/* common */ + +HB_WASM_API (direction_t, script_get_horizontal_direction) (HB_WASM_EXEC_ENV + script_t script); + /* blob */ @@ -151,6 +159,9 @@ HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV ptr_d(buffer_t, buffer)); +HB_WASM_API (script_t, buffer_get_script) (HB_WASM_EXEC_ENV + ptr_d(buffer_t, buffer)); + HB_WASM_API (void, buffer_reverse) (HB_WASM_EXEC_ENV ptr_d(buffer_t, buffer)); diff --git a/src/meson.build b/src/meson.build index 74998ce42..f6b33159e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -329,6 +329,7 @@ hb_wasm_sources = files( 'hb-wasm-api.hh', 'hb-wasm-api-blob.hh', 'hb-wasm-api-buffer.hh', + 'hb-wasm-api-common.hh', 'hb-wasm-api-face.hh', 'hb-wasm-api-font.hh', 'hb-wasm-api-shape.hh', diff --git a/src/wasm-graphite/shape.cc b/src/wasm-graphite/shape.cc index fcf87c00e..e1a863b10 100644 --- a/src/wasm-graphite/shape.cc +++ b/src/wasm-graphite/shape.cc @@ -38,15 +38,24 @@ static void free_table (const void *data, const void *table_data) bool_t shape (font_t *font, buffer_t *buffer) { - face_t *face = font_get_face (font); - - blob_t blob = face_reference_table (face, TAG ('c','m','a','p')); - - blob_free (&blob); + direction_t direction = buffer_get_direction (buffer); + direction_t horiz_dir = script_get_horizontal_direction (buffer_get_script (buffer)); + /* TODO vertical: + * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType + * Ogham fonts are supposed to be implemented BTT or not. Need to research that + * first. */ + if ((DIRECTION_IS_HORIZONTAL (direction) && + direction != horiz_dir && horiz_dir != DIRECTION_INVALID) || + (DIRECTION_IS_VERTICAL (direction) && + direction != DIRECTION_TTB)) + { + buffer_reverse_clusters (buffer); + direction = DIRECTION_REVERSE (direction); + } buffer_contents_t contents = buffer_copy_contents (buffer); - direction_t direction = buffer_get_direction (buffer); + face_t *face = font_get_face (font); const gr_face_ops ops = {sizeof (gr_face_ops), ©_table, &free_table}; gr_face *grface = gr_make_face_with_ops (face, &ops, gr_face_preloadAll);