Imported Upstream version 8.2.2
[platform/upstream/harfbuzz.git] / src / test-gsub-get-alternates.cc
1 /*
2  * Copyright © 2010,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
27 #include <hb.h>
28 #include <hb-ot.h>
29
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 int
34 main (int argc, char **argv)
35 {
36   if (argc != 3) {
37     fprintf (stderr, "usage: %s font-file text\n", argv[0]);
38     exit (1);
39   }
40
41   /* Create the face */
42   hb_blob_t *blob = hb_blob_create_from_file_or_fail (argv[1]);
43   hb_face_t *face = hb_face_create (blob, 0 /* first face */);
44   hb_blob_destroy (blob);
45   blob = nullptr;
46
47   hb_font_t *font = hb_font_create (face);
48   hb_buffer_t *buffer = hb_buffer_create ();
49
50   hb_buffer_add_utf8 (buffer, argv[2], -1, 0, -1);
51   hb_buffer_guess_segment_properties (buffer);
52   hb_shape (font, buffer, NULL, 0);
53
54   hb_tag_t features[] = {HB_TAG('a','a','l','t'), HB_TAG_NONE};
55   hb_set_t *lookup_indexes = hb_set_create ();
56   hb_ot_layout_collect_lookups (face,
57                                 HB_OT_TAG_GSUB,
58                                 NULL, NULL,
59                                 features,
60                                 lookup_indexes);
61   printf ("lookups %u\n", hb_set_get_population (lookup_indexes));
62
63   unsigned count;
64   hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &count);
65   for (unsigned i = 0; i < count; i++)
66   {
67     unsigned alt_count = 0;
68     for (hb_codepoint_t lookup_index = HB_SET_VALUE_INVALID;
69          hb_set_next (lookup_indexes, &lookup_index);)
70       if ((alt_count = hb_ot_layout_lookup_get_glyph_alternates (face,
71                                                                  lookup_index,
72                                                                  info[i].codepoint,
73                                                                  0,
74                                                                  NULL,
75                                                                  NULL)))
76         break;
77     printf ("glyph %u alt count %u\n", info[i].codepoint, alt_count);
78   }
79
80   hb_set_destroy (lookup_indexes);
81   hb_buffer_destroy (buffer);
82   hb_font_destroy (font);
83   hb_face_destroy (face);
84
85   return 0;
86 }