(root)/
harfbuzz-8.3.0/
src/
hb-wasm-api.h
       1  /*
       2   * Copyright © 2023  Behdad Esfahbod
       3   *
       4   *  This is part of HarfBuzz, a text shaping library.
       5   *
       6   * Permission is hereby granted, without written agreement and without
       7   * license or royalty fees, to use, copy, modify, and distribute this
       8   * software and its documentation for any purpose, provided that the
       9   * above copyright notice and the following two paragraphs appear in
      10   * all copies of this software.
      11   *
      12   * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
      13   * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
      14   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
      15   * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
      16   * DAMAGE.
      17   *
      18   * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
      19   * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
      20   * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
      21   * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
      22   * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
      23   */
      24  
      25  #ifndef HB_WASM_API_H
      26  #define HB_WASM_API_H
      27  
      28  /*
      29  #include "hb.h"
      30  */
      31  
      32  #include <stdint.h>
      33  
      34  
      35  #ifndef HB_WASM_BEGIN_DECLS
      36  # ifdef __cplusplus
      37  #  define HB_WASM_BEGIN_DECLS	extern "C" {
      38  #  define HB_WASM_END_DECLS	}
      39  # else /* !__cplusplus */
      40  #  define HB_WASM_BEGIN_DECLS
      41  #  define HB_WASM_END_DECLS
      42  # endif /* !__cplusplus */
      43  #endif
      44  
      45  
      46  HB_WASM_BEGIN_DECLS
      47  
      48  #ifndef HB_WASM_API
      49  #define HB_WASM_API(ret_t, name) ret_t name
      50  #endif
      51  #ifndef HB_WASM_API_COMPOUND /* compound return type */
      52  #define HB_WASM_API_COMPOUND(ret_t, name) HB_WASM_API(ret_t, name)
      53  #endif
      54  #ifndef HB_WASM_INTERFACE
      55  #define HB_WASM_INTERFACE(ret_t, name) ret_t name
      56  #endif
      57  #ifndef HB_WASM_EXEC_ENV
      58  #define HB_WASM_EXEC_ENV
      59  #endif
      60  #ifndef HB_WASM_EXEC_ENV_COMPOUND
      61  #define HB_WASM_EXEC_ENV_COMPOUND HB_WASM_EXEC_ENV
      62  #endif
      63  
      64  
      65  #ifndef bool_t
      66  #define bool_t uint32_t
      67  #endif
      68  #ifndef ptr_t
      69  #define ptr_t(type_t) type_t *
      70  #endif
      71  #ifndef ptr_d
      72  #define ptr_d(type_t, name) type_t *name
      73  #endif
      74  
      75  typedef uint32_t codepoint_t;
      76  typedef int32_t position_t;
      77  typedef uint32_t mask_t;
      78  typedef uint32_t tag_t;
      79  #define TAG(c1,c2,c3,c4) ((tag_t)((((uint32_t)(c1)&0xFF)<<24)|(((uint32_t)(c2)&0xFF)<<16)|(((uint32_t)(c3)&0xFF)<<8)|((uint32_t)(c4)&0xFF)))
      80  
      81  typedef enum {
      82    DIRECTION_INVALID = 0,
      83    DIRECTION_LTR = 4,
      84    DIRECTION_RTL,
      85    DIRECTION_TTB,
      86    DIRECTION_BTT
      87  } direction_t;
      88  #define DIRECTION_IS_VALID(dir)		((((unsigned int) (dir)) & ~3U) == 4)
      89  #define DIRECTION_IS_HORIZONTAL(dir)	((((unsigned int) (dir)) & ~1U) == 4)
      90  #define DIRECTION_IS_VERTICAL(dir)	((((unsigned int) (dir)) & ~1U) == 6)
      91  #define DIRECTION_IS_FORWARD(dir)	((((unsigned int) (dir)) & ~2U) == 4)
      92  #define DIRECTION_IS_BACKWARD(dir)	((((unsigned int) (dir)) & ~2U) == 5)
      93  #define DIRECTION_REVERSE(dir)		((direction_t) (((unsigned int) (dir)) ^ 1))
      94  
      95  typedef tag_t script_t; /* ISO 15924 representation of Unicode scripts. */
      96  
      97  
      98  /* common */
      99  
     100  HB_WASM_API (direction_t, script_get_horizontal_direction) (HB_WASM_EXEC_ENV
     101  							    script_t script);
     102  
     103  
     104  /* blob */
     105  
     106  typedef struct
     107  {
     108    uint32_t length;
     109    ptr_t(char) data;
     110  } blob_t;
     111  #define BLOB_INIT {0, 0}
     112  
     113  HB_WASM_API (void, blob_free) (HB_WASM_EXEC_ENV
     114  			       ptr_d(blob_t, blob));
     115  
     116  /* buffer */
     117  
     118  typedef struct
     119  {
     120    uint32_t codepoint;
     121    uint32_t mask;
     122    uint32_t cluster;
     123    uint32_t var1;
     124    uint32_t var2;
     125  } glyph_info_t;
     126  
     127  typedef struct
     128  {
     129    position_t x_advance;
     130    position_t y_advance;
     131    position_t x_offset;
     132    position_t y_offset;
     133    uint32_t var;
     134  } glyph_position_t;
     135  
     136  typedef struct
     137  {
     138    uint32_t length;
     139    ptr_t(glyph_info_t) info;
     140    ptr_t(glyph_position_t) pos;
     141  } buffer_contents_t;
     142  #define BUFFER_CONTENTS_INIT {0, 0, 0}
     143  
     144  HB_WASM_API (bool_t, buffer_contents_realloc) (HB_WASM_EXEC_ENV
     145  					       ptr_d(buffer_contents_t, contents),
     146  					       uint32_t size);
     147  
     148  HB_WASM_API (void, buffer_contents_free) (HB_WASM_EXEC_ENV
     149  					  ptr_d(buffer_contents_t, contents));
     150  
     151  typedef struct buffer_t buffer_t;
     152  
     153  HB_WASM_API (bool_t, buffer_copy_contents) (HB_WASM_EXEC_ENV
     154  					    ptr_d(buffer_t, buffer),
     155  					    ptr_d(buffer_contents_t, contents));
     156  
     157  HB_WASM_API (bool_t, buffer_set_contents) (HB_WASM_EXEC_ENV
     158  					   ptr_d(buffer_t, buffer),
     159  					   ptr_d(const buffer_contents_t, contents));
     160  
     161  HB_WASM_API (direction_t, buffer_get_direction) (HB_WASM_EXEC_ENV
     162  						 ptr_d(buffer_t, buffer));
     163  
     164  HB_WASM_API (script_t, buffer_get_script) (HB_WASM_EXEC_ENV
     165  					   ptr_d(buffer_t, buffer));
     166  
     167  HB_WASM_API (void, buffer_reverse) (HB_WASM_EXEC_ENV
     168  				    ptr_d(buffer_t, buffer));
     169  
     170  HB_WASM_API (void, buffer_reverse_clusters) (HB_WASM_EXEC_ENV
     171  					     ptr_d(buffer_t, buffer));
     172  
     173  /* face */
     174  
     175  typedef struct face_t face_t;
     176  
     177  HB_WASM_API (ptr_t(face_t), face_create) (HB_WASM_EXEC_ENV
     178  					  ptr_d(blob_t, blob),
     179  					  unsigned int);
     180  
     181  HB_WASM_API (bool_t, face_copy_table) (HB_WASM_EXEC_ENV
     182  				       ptr_d(face_t, face),
     183  				       tag_t table_tag,
     184  				       ptr_d(blob_t, blob));
     185  
     186  HB_WASM_API (unsigned, face_get_upem) (HB_WASM_EXEC_ENV
     187  				       ptr_d(face_t, face));
     188  
     189  /* font */
     190  
     191  typedef struct font_t font_t;
     192  
     193  HB_WASM_API (ptr_t(font_t), font_create) (HB_WASM_EXEC_ENV
     194  					  ptr_d(face_t, face));
     195  
     196  HB_WASM_API (ptr_t(face_t), font_get_face) (HB_WASM_EXEC_ENV
     197  					    ptr_d(font_t, font));
     198  
     199  HB_WASM_API (void, font_get_scale) (HB_WASM_EXEC_ENV
     200  				    ptr_d(font_t, font),
     201  				    ptr_d(int32_t, x_scale),
     202  				    ptr_d(int32_t, y_scale));
     203  
     204  HB_WASM_API (codepoint_t, font_get_glyph) (HB_WASM_EXEC_ENV
     205  					      ptr_d(font_t, font),
     206  					      codepoint_t unicode,
     207  					      codepoint_t variation_selector);
     208  
     209  HB_WASM_API (position_t, font_get_glyph_h_advance) (HB_WASM_EXEC_ENV
     210  						    ptr_d(font_t, font),
     211  						    codepoint_t glyph);
     212  
     213  HB_WASM_API (position_t, font_get_glyph_v_advance) (HB_WASM_EXEC_ENV
     214  						    ptr_d(font_t, font),
     215  						    codepoint_t glyph);
     216  
     217  typedef struct
     218  {
     219    position_t x_bearing;
     220    position_t y_bearing;
     221    position_t width;
     222    position_t height;
     223  } glyph_extents_t;
     224  
     225  HB_WASM_API (bool_t, font_get_glyph_extents) (HB_WASM_EXEC_ENV
     226  					      ptr_d(font_t, font),
     227  					      codepoint_t glyph,
     228  					      ptr_d(glyph_extents_t, extents));
     229  
     230  HB_WASM_API (void, font_glyph_to_string) (HB_WASM_EXEC_ENV
     231  					  ptr_d(font_t, font),
     232  					  codepoint_t glyph,
     233  					  char *s, uint32_t size);
     234  
     235  
     236  typedef struct
     237  {
     238    unsigned int length;
     239    ptr_t(int) coords;
     240  } coords_t;
     241  
     242  HB_WASM_API (bool_t, font_copy_coords) (HB_WASM_EXEC_ENV
     243  					  ptr_d(font_t, font),
     244  					  ptr_d(coords_t, coords));
     245  
     246  HB_WASM_API (bool_t, font_set_coords) (HB_WASM_EXEC_ENV
     247  					  ptr_d(font_t, font),
     248  					  ptr_d(coords_t, coords));
     249  
     250  /* outline */
     251  
     252  enum glyph_outline_point_type_t
     253  {
     254    MOVE_TO,
     255    LINE_TO,
     256    QUADRATIC_TO,
     257    CUBIC_TO,
     258  };
     259  
     260  typedef struct
     261  {
     262    float x;
     263    float y;
     264    uint32_t type;
     265  } glyph_outline_point_t;
     266  
     267  typedef struct
     268  {
     269    uint32_t n_points;
     270    ptr_t(glyph_outline_point_t) points;
     271    uint32_t n_contours;
     272    ptr_t(uint32_t) contours;
     273  } glyph_outline_t;
     274  #define GLYPH_OUTLINE_INIT {0, 0, 0, 0}
     275  
     276  HB_WASM_API (void, glyph_outline_free) (HB_WASM_EXEC_ENV
     277  					ptr_d(glyph_outline_t, outline));
     278  
     279  HB_WASM_API (bool_t, font_copy_glyph_outline) (HB_WASM_EXEC_ENV
     280  					       ptr_d(font_t, font),
     281  					       codepoint_t glyph,
     282  					       ptr_d(glyph_outline_t, outline));
     283  
     284  
     285  /* shape */
     286  
     287  typedef struct
     288  {
     289    tag_t    tag;
     290    uint32_t value;
     291    uint32_t start;
     292    uint32_t end;
     293  } feature_t;
     294  #define FEATURE_GLOBAL_START	0
     295  #define FEATURE_GLOBAL_END	((uint32_t) -1)
     296  
     297  HB_WASM_API (bool_t, shape_with) (HB_WASM_EXEC_ENV
     298  				  ptr_d(font_t, font),
     299  				  ptr_d(buffer_t, buffer),
     300  				  ptr_d(const feature_t, features),
     301  				  uint32_t num_features,
     302  				  const char *shaper);
     303  
     304  /* Implement these in your shaper. */
     305  
     306  HB_WASM_INTERFACE (ptr_t(void), shape_plan_create) (ptr_d(face_t, face));
     307  
     308  HB_WASM_INTERFACE (bool_t, shape) (ptr_d(void, shape_plan),
     309  				   ptr_d(font_t, font),
     310  				   ptr_d(buffer_t, buffer),
     311  				   ptr_d(const feature_t, features),
     312  				   uint32_t num_features);
     313  
     314  HB_WASM_INTERFACE (void, shape_plan_destroy) (ptr_d(void, shape_plan));
     315  
     316  
     317  HB_WASM_END_DECLS
     318  
     319  #endif /* HB_WASM_API_H */