shape-format.hh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright © 2011 Google, Inc.
  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. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef SHAPE_FORMAT_OPTIONS_HH
  27. #define SHAPE_FORMAT_OPTIONS_HH
  28. #include "options.hh"
  29. struct shape_format_options_t
  30. {
  31. void add_options (option_parser_t *parser);
  32. void serialize (hb_buffer_t *buffer,
  33. hb_font_t *font,
  34. hb_buffer_serialize_format_t format,
  35. hb_buffer_serialize_flags_t flags,
  36. GString *gs);
  37. void serialize_line_no (unsigned int line_no,
  38. GString *gs);
  39. void serialize_buffer_of_text (hb_buffer_t *buffer,
  40. unsigned int line_no,
  41. const char *text,
  42. unsigned int text_len,
  43. hb_font_t *font,
  44. GString *gs);
  45. void serialize_message (unsigned int line_no,
  46. const char *type,
  47. const char *msg,
  48. GString *gs);
  49. void serialize_buffer_of_glyphs (hb_buffer_t *buffer,
  50. unsigned int line_no,
  51. const char *text,
  52. unsigned int text_len,
  53. hb_font_t *font,
  54. hb_buffer_serialize_format_t output_format,
  55. hb_buffer_serialize_flags_t format_flags,
  56. GString *gs);
  57. hb_bool_t show_glyph_names = true;
  58. hb_bool_t show_positions = true;
  59. hb_bool_t show_advances = true;
  60. hb_bool_t show_clusters = true;
  61. hb_bool_t show_text = false;
  62. hb_bool_t show_unicode = false;
  63. hb_bool_t show_line_num = false;
  64. hb_bool_t show_extents = false;
  65. hb_bool_t show_flags = false;
  66. hb_bool_t trace = false;
  67. };
  68. static gboolean
  69. parse_verbose (const char *name G_GNUC_UNUSED,
  70. const char *arg G_GNUC_UNUSED,
  71. gpointer data G_GNUC_UNUSED,
  72. GError **error G_GNUC_UNUSED)
  73. {
  74. shape_format_options_t *format_opts = (shape_format_options_t *) data;
  75. format_opts->show_text = format_opts->show_unicode = format_opts->show_line_num = true;
  76. return true;
  77. }
  78. static gboolean
  79. parse_ned (const char *name G_GNUC_UNUSED,
  80. const char *arg G_GNUC_UNUSED,
  81. gpointer data G_GNUC_UNUSED,
  82. GError **error G_GNUC_UNUSED)
  83. {
  84. shape_format_options_t *format_opts = (shape_format_options_t *) data;
  85. format_opts->show_clusters = format_opts->show_advances = false;
  86. return true;
  87. }
  88. inline void
  89. shape_format_options_t::serialize (hb_buffer_t *buffer,
  90. hb_font_t *font,
  91. hb_buffer_serialize_format_t output_format,
  92. hb_buffer_serialize_flags_t flags,
  93. GString *gs)
  94. {
  95. unsigned int num_glyphs = hb_buffer_get_length (buffer);
  96. unsigned int start = 0;
  97. while (start < num_glyphs)
  98. {
  99. char buf[32768];
  100. unsigned int consumed;
  101. start += hb_buffer_serialize (buffer, start, num_glyphs,
  102. buf, sizeof (buf), &consumed,
  103. font, output_format, flags);
  104. if (!consumed)
  105. break;
  106. g_string_append (gs, buf);
  107. }
  108. }
  109. inline void
  110. shape_format_options_t::serialize_line_no (unsigned int line_no,
  111. GString *gs)
  112. {
  113. if (show_line_num)
  114. g_string_append_printf (gs, "%d: ", line_no);
  115. }
  116. inline void
  117. shape_format_options_t::serialize_buffer_of_text (hb_buffer_t *buffer,
  118. unsigned int line_no,
  119. const char *text,
  120. unsigned int text_len,
  121. hb_font_t *font,
  122. GString *gs)
  123. {
  124. if (show_text)
  125. {
  126. serialize_line_no (line_no, gs);
  127. g_string_append_c (gs, '(');
  128. g_string_append_len (gs, text, text_len);
  129. g_string_append_c (gs, ')');
  130. g_string_append_c (gs, '\n');
  131. }
  132. if (show_unicode)
  133. {
  134. serialize_line_no (line_no, gs);
  135. serialize (buffer, font, HB_BUFFER_SERIALIZE_FORMAT_TEXT, HB_BUFFER_SERIALIZE_FLAG_DEFAULT, gs);
  136. g_string_append_c (gs, '\n');
  137. }
  138. }
  139. inline void
  140. shape_format_options_t::serialize_message (unsigned int line_no,
  141. const char *type,
  142. const char *msg,
  143. GString *gs)
  144. {
  145. serialize_line_no (line_no, gs);
  146. g_string_append_printf (gs, "%s: %s", type, msg);
  147. g_string_append_c (gs, '\n');
  148. }
  149. inline void
  150. shape_format_options_t::serialize_buffer_of_glyphs (hb_buffer_t *buffer,
  151. unsigned int line_no,
  152. const char *text,
  153. unsigned int text_len,
  154. hb_font_t *font,
  155. hb_buffer_serialize_format_t output_format,
  156. hb_buffer_serialize_flags_t format_flags,
  157. GString *gs)
  158. {
  159. serialize_line_no (line_no, gs);
  160. serialize (buffer, font, output_format, format_flags, gs);
  161. g_string_append_c (gs, '\n');
  162. }
  163. void
  164. shape_format_options_t::add_options (option_parser_t *parser)
  165. {
  166. GOptionEntry entries[] =
  167. {
  168. {"show-text", 0, 0, G_OPTION_ARG_NONE, &this->show_text, "Prefix each line of output with its corresponding input text", nullptr},
  169. {"show-unicode", 0, 0, G_OPTION_ARG_NONE, &this->show_unicode, "Prefix each line of output with its corresponding input codepoint(s)", nullptr},
  170. {"show-line-num", 0, 0, G_OPTION_ARG_NONE, &this->show_line_num, "Prefix each line of output with its corresponding input line number", nullptr},
  171. {"verbose", 'v', G_OPTION_FLAG_NO_ARG,
  172. G_OPTION_ARG_CALLBACK, (gpointer) &parse_verbose, "Prefix each line of output with all of the above", nullptr},
  173. {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE,
  174. G_OPTION_ARG_NONE, &this->show_glyph_names, "Output glyph indices instead of names", nullptr},
  175. {"no-positions", 0, G_OPTION_FLAG_REVERSE,
  176. G_OPTION_ARG_NONE, &this->show_positions, "Do not output glyph positions", nullptr},
  177. {"no-advances", 0, G_OPTION_FLAG_REVERSE,
  178. G_OPTION_ARG_NONE, &this->show_advances, "Do not output glyph advances", nullptr},
  179. {"no-clusters", 0, G_OPTION_FLAG_REVERSE,
  180. G_OPTION_ARG_NONE, &this->show_clusters, "Do not output cluster indices", nullptr},
  181. {"show-extents", 0, 0, G_OPTION_ARG_NONE, &this->show_extents, "Output glyph extents", nullptr},
  182. {"show-flags", 0, 0, G_OPTION_ARG_NONE, &this->show_flags, "Output glyph flags", nullptr},
  183. {"ned", 'v', G_OPTION_FLAG_NO_ARG,
  184. G_OPTION_ARG_CALLBACK, (gpointer) &parse_ned, "No Extra Data; Do not output clusters or advances", nullptr},
  185. {"trace", 'V', 0, G_OPTION_ARG_NONE, &this->trace, "Output interim shaping results", nullptr},
  186. {nullptr}
  187. };
  188. parser->add_group (entries,
  189. "output-syntax",
  190. "Output syntax:\n"
  191. " text: [<glyph name or index>=<glyph cluster index within input>@<horizontal displacement>,<vertical displacement>+<horizontal advance>,<vertical advance>|...]\n"
  192. " json: [{\"g\": <glyph name or index>, \"ax\": <horizontal advance>, \"ay\": <vertical advance>, \"dx\": <horizontal displacement>, \"dy\": <vertical displacement>, \"cl\": <glyph cluster index within input>}, ...]\n"
  193. "\nOutput syntax options:",
  194. "Options for the syntax of the output",
  195. this);
  196. }
  197. #endif