hb-ot-shape-closure.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright © 2012 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. #include "batch.hh"
  27. #include "font-options.hh"
  28. #include "main-font-text.hh"
  29. #include "shape-options.hh"
  30. #include "text-options.hh"
  31. const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_NONE;
  32. const unsigned SUBPIXEL_BITS = 0;
  33. struct shape_closure_consumer_t
  34. {
  35. void add_options (struct option_parser_t *parser)
  36. {
  37. parser->set_summary ("Find glyph set from input text under shaping closure.");
  38. shaper.add_options (parser);
  39. GOptionEntry entries[] =
  40. {
  41. {"no-glyph-names", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &this->show_glyph_names, "Use glyph indices instead of names", nullptr},
  42. {nullptr}
  43. };
  44. parser->add_group (entries,
  45. "format",
  46. "Format options:",
  47. "Options controlling output formatting",
  48. this);
  49. }
  50. void init (const font_options_t *font_opts)
  51. {
  52. glyphs = hb_set_create ();
  53. font = hb_font_reference (font_opts->font);
  54. failed = false;
  55. buffer = hb_buffer_create ();
  56. }
  57. template <typename text_options_type>
  58. bool consume_line (text_options_type &text_opts)
  59. {
  60. unsigned int text_len;
  61. const char *text;
  62. if (!(text = text_opts.get_line (&text_len)))
  63. return false;
  64. hb_set_clear (glyphs);
  65. shaper.shape_closure (text, text_len, font, buffer, glyphs);
  66. if (hb_set_is_empty (glyphs))
  67. return true;
  68. /* Print it out! */
  69. bool first = true;
  70. for (hb_codepoint_t i = -1; hb_set_next (glyphs, &i);)
  71. {
  72. if (first)
  73. first = false;
  74. else
  75. printf (" ");
  76. if (show_glyph_names)
  77. {
  78. char glyph_name[64];
  79. hb_font_glyph_to_string (font, i, glyph_name, sizeof (glyph_name));
  80. printf ("%s", glyph_name);
  81. } else
  82. printf ("%u", i);
  83. }
  84. return true;
  85. }
  86. void finish (const font_options_t *font_opts)
  87. {
  88. printf ("\n");
  89. hb_font_destroy (font);
  90. font = nullptr;
  91. hb_set_destroy (glyphs);
  92. glyphs = nullptr;
  93. hb_buffer_destroy (buffer);
  94. buffer = nullptr;
  95. }
  96. bool failed;
  97. protected:
  98. shape_options_t shaper;
  99. hb_bool_t show_glyph_names = true;
  100. hb_set_t *glyphs = nullptr;
  101. hb_font_t *font = nullptr;
  102. hb_buffer_t *buffer = nullptr;
  103. };
  104. int
  105. main (int argc, char **argv)
  106. {
  107. using main_t = main_font_text_t<shape_closure_consumer_t, font_options_t, text_options_t>;
  108. return batch_main<main_t> (argc, argv);
  109. }