Coverage Report

Created: 2024-05-15 07:05

/src/harfbuzz/src/hb-shape.cc
Line
Count
Source (jump to first uncovered line)
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.hh"
30
31
#include "hb-shaper.hh"
32
#include "hb-shape-plan.hh"
33
#include "hb-buffer.hh"
34
#include "hb-font.hh"
35
#include "hb-machinery.hh"
36
37
38
#ifndef HB_NO_SHAPER
39
40
/**
41
 * SECTION:hb-shape
42
 * @title: hb-shape
43
 * @short_description: Conversion of text strings into positioned glyphs
44
 * @include: hb.h
45
 *
46
 * Shaping is the central operation of HarfBuzz. Shaping operates on buffers,
47
 * which are sequences of Unicode characters that use the same font and have
48
 * the same text direction, script, and language. After shaping the buffer
49
 * contains the output glyphs and their positions.
50
 **/
51
52
53
static inline void free_static_shaper_list ();
54
55
static const char * const nil_shaper_list[] = {nullptr};
56
57
static struct hb_shaper_list_lazy_loader_t : hb_lazy_loader_t<const char *,
58
                    hb_shaper_list_lazy_loader_t>
59
{
60
  static const char ** create ()
61
0
  {
62
0
    const char **shaper_list = (const char **) hb_calloc (1 + HB_SHAPERS_COUNT, sizeof (const char *));
63
0
    if (unlikely (!shaper_list))
64
0
      return nullptr;
65
66
0
    const hb_shaper_entry_t *shapers = _hb_shapers_get ();
67
0
    unsigned int i;
68
0
    for (i = 0; i < HB_SHAPERS_COUNT; i++)
69
0
      shaper_list[i] = shapers[i].name;
70
0
    shaper_list[i] = nullptr;
71
72
0
    hb_atexit (free_static_shaper_list);
73
74
0
    return shaper_list;
75
0
  }
76
  static void destroy (const char **l)
77
0
  { hb_free (l); }
78
  static const char * const * get_null ()
79
0
  { return nil_shaper_list; }
80
} static_shaper_list;
81
82
static inline
83
void free_static_shaper_list ()
84
0
{
85
0
  static_shaper_list.free_instance ();
86
0
}
87
88
89
/**
90
 * hb_shape_list_shapers:
91
 *
92
 * Retrieves the list of shapers supported by HarfBuzz.
93
 *
94
 * Return value: (transfer none) (array zero-terminated=1): an array of
95
 *    constant strings
96
 *
97
 * Since: 0.9.2
98
 **/
99
const char **
100
hb_shape_list_shapers ()
101
0
{
102
0
  return static_shaper_list.get_unconst ();
103
0
}
104
105
106
/**
107
 * hb_shape_full:
108
 * @font: an #hb_font_t to use for shaping
109
 * @buffer: an #hb_buffer_t to shape
110
 * @features: (array length=num_features) (nullable): an array of user
111
 *    specified #hb_feature_t or `NULL`
112
 * @num_features: the length of @features array
113
 * @shaper_list: (array zero-terminated=1) (nullable): a `NULL`-terminated
114
 *    array of shapers to use or `NULL`
115
 *
116
 * See hb_shape() for details. If @shaper_list is not `NULL`, the specified
117
 * shapers will be used in the given order, otherwise the default shapers list
118
 * will be used.
119
 *
120
 * Return value: false if all shapers failed, true otherwise
121
 *
122
 * Since: 0.9.2
123
 **/
124
hb_bool_t
125
hb_shape_full (hb_font_t          *font,
126
         hb_buffer_t        *buffer,
127
         const hb_feature_t *features,
128
         unsigned int        num_features,
129
         const char * const *shaper_list)
130
7.57M
{
131
7.57M
  if (unlikely (!buffer->len))
132
30.0k
    return true;
133
134
7.54M
  buffer->enter ();
135
136
7.54M
  hb_buffer_t *text_buffer = nullptr;
137
7.54M
  if (buffer->flags & HB_BUFFER_FLAG_VERIFY)
138
418k
  {
139
418k
    text_buffer = hb_buffer_create ();
140
418k
    hb_buffer_append (text_buffer, buffer, 0, -1);
141
418k
  }
142
143
7.54M
  hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached2 (font->face, &buffer->props,
144
7.54M
                    features, num_features,
145
7.54M
                    font->coords, font->num_coords,
146
7.54M
                    shaper_list);
147
148
7.54M
  hb_bool_t res = hb_shape_plan_execute (shape_plan, font, buffer, features, num_features);
149
150
7.54M
  if (buffer->max_ops <= 0)
151
0
    buffer->shaping_failed = true;
152
153
7.54M
  hb_shape_plan_destroy (shape_plan);
154
155
7.54M
  if (text_buffer)
156
418k
  {
157
418k
    if (res && buffer->successful && !buffer->shaping_failed
158
418k
      && text_buffer->successful
159
418k
      && !buffer->verify (text_buffer,
160
389k
        font,
161
389k
        features,
162
389k
        num_features,
163
389k
        shaper_list))
164
10.7k
      res = false;
165
418k
    hb_buffer_destroy (text_buffer);
166
418k
  }
167
168
7.54M
  buffer->leave ();
169
170
7.54M
  return res;
171
7.57M
}
172
173
/**
174
 * hb_shape:
175
 * @font: an #hb_font_t to use for shaping
176
 * @buffer: an #hb_buffer_t to shape
177
 * @features: (array length=num_features) (nullable): an array of user
178
 *    specified #hb_feature_t or `NULL`
179
 * @num_features: the length of @features array
180
 *
181
 * Shapes @buffer using @font turning its Unicode characters content to
182
 * positioned glyphs. If @features is not `NULL`, it will be used to control the
183
 * features applied during shaping. If two @features have the same tag but
184
 * overlapping ranges the value of the feature with the higher index takes
185
 * precedence.
186
 *
187
 * Since: 0.9.2
188
 **/
189
void
190
hb_shape (hb_font_t           *font,
191
    hb_buffer_t         *buffer,
192
    const hb_feature_t  *features,
193
    unsigned int         num_features)
194
862k
{
195
862k
  hb_shape_full (font, buffer, features, num_features, nullptr);
196
862k
}
197
198
199
#endif