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