Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / src / hb-shape.cc
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  * Copyright © 2012  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #include "hb-private.hh"
30
31 #include "hb-shaper-private.hh"
32 #include "hb-shape-plan-private.hh"
33 #include "hb-buffer-private.hh"
34 #include "hb-font-private.hh"
35
36 /**
37  * SECTION:hb-shape
38  * @title: Shaping
39  * @short_description: Conversion of text strings into positioned glyphs
40  * @include: hb.h
41  *
42  * Shaping is the central operation of HarfBuzz. Shaping operates on buffers,
43  * which are sequences of Unicode characters that use the same font and have
44  * the same text direction, script and language. After shaping the buffer
45  * contains the output glyphs and their positions.
46  **/
47
48 static const char **static_shaper_list;
49
50 #ifdef HB_USE_ATEXIT
51 static
52 void free_static_shaper_list (void)
53 {
54   free (static_shaper_list);
55 }
56 #endif
57
58 /**
59  * hb_shape_list_shapers:
60  *
61  * Retrieves the list of shapers supported by HarfBuzz.
62  *
63  * Return value: (transfer none) (array zero-terminated=1): an array of
64  *    constant strings
65  *
66  * Since: 0.9.2
67  **/
68 const char **
69 hb_shape_list_shapers (void)
70 {
71 retry:
72   const char **shaper_list = (const char **) hb_atomic_ptr_get (&static_shaper_list);
73
74   if (unlikely (!shaper_list))
75   {
76     /* Not found; allocate one. */
77     shaper_list = (const char **) calloc (1 + HB_SHAPERS_COUNT, sizeof (const char *));
78     if (unlikely (!shaper_list)) {
79       static const char *nil_shaper_list[] = {nullptr};
80       return nil_shaper_list;
81     }
82
83     const hb_shaper_pair_t *shapers = _hb_shapers_get ();
84     unsigned int i;
85     for (i = 0; i < HB_SHAPERS_COUNT; i++)
86       shaper_list[i] = shapers[i].name;
87     shaper_list[i] = nullptr;
88
89     if (!hb_atomic_ptr_cmpexch (&static_shaper_list, nullptr, shaper_list)) {
90       free (shaper_list);
91       goto retry;
92     }
93
94 #ifdef HB_USE_ATEXIT
95     atexit (free_static_shaper_list); /* First person registers atexit() callback. */
96 #endif
97   }
98
99   return shaper_list;
100 }
101
102
103 /**
104  * hb_shape_full:
105  * @font: an #hb_font_t to use for shaping
106  * @buffer: an #hb_buffer_t to shape
107  * @features: (array length=num_features) (allow-none): an array of user
108  *    specified #hb_feature_t or %NULL
109  * @num_features: the length of @features array
110  * @shaper_list: (array zero-terminated=1) (allow-none): a %NULL-terminated
111  *    array of shapers to use or %NULL
112  *
113  * See hb_shape() for details. If @shaper_list is not %NULL, the specified
114  * shapers will be used in the given order, otherwise the default shapers list
115  * will be used.
116  *
117  * Return value: false if all shapers failed, true otherwise
118  *
119  * Since: 0.9.2
120  **/
121 hb_bool_t
122 hb_shape_full (hb_font_t          *font,
123                hb_buffer_t        *buffer,
124                const hb_feature_t *features,
125                unsigned int        num_features,
126                const char * const *shaper_list)
127 {
128   hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached2 (font->face, &buffer->props,
129                                                               features, num_features,
130                                                               font->coords, font->num_coords,
131                                                               shaper_list);
132   hb_bool_t res = hb_shape_plan_execute (shape_plan, font, buffer, features, num_features);
133   hb_shape_plan_destroy (shape_plan);
134
135   if (res)
136     buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
137   return res;
138 }
139
140 /**
141  * hb_shape:
142  * @font: an #hb_font_t to use for shaping
143  * @buffer: an #hb_buffer_t to shape
144  * @features: (array length=num_features) (allow-none): an array of user
145  *    specified #hb_feature_t or %NULL
146  * @num_features: the length of @features array
147  *
148  * Shapes @buffer using @font turning its Unicode characters content to
149  * positioned glyphs. If @features is not %NULL, it will be used to control the
150  * features applied during shaping.
151  *
152  * Since: 0.9.2
153  **/
154 void
155 hb_shape (hb_font_t           *font,
156           hb_buffer_t         *buffer,
157           const hb_feature_t  *features,
158           unsigned int         num_features)
159 {
160   hb_shape_full (font, buffer, features, num_features, nullptr);
161 }