Coverage Report

Created: 2023-12-14 14:04

/src/harfbuzz/src/hb-ot-name.cc
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2018  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.hh"
28
29
#ifndef HB_NO_NAME
30
31
#include "hb-ot-name-table.hh"
32
33
#include "hb-utf.hh"
34
35
36
/**
37
 * SECTION:hb-ot-name
38
 * @title: hb-ot-name
39
 * @short_description: OpenType font name information
40
 * @include: hb-ot.h
41
 *
42
 * Functions for fetching name strings from OpenType fonts.
43
 **/
44
45
46
/**
47
 * hb_ot_name_list_names:
48
 * @face: font face.
49
 * @num_entries: (out) (optional): number of returned entries.
50
 *
51
 * Enumerates all available name IDs and language combinations. Returned
52
 * array is owned by the @face and should not be modified.  It can be
53
 * used as long as @face is alive.
54
 *
55
 * Returns: (transfer none) (array length=num_entries): Array of available name entries.
56
 * Since: 2.1.0
57
 **/
58
const hb_ot_name_entry_t *
59
hb_ot_name_list_names (hb_face_t    *face,
60
           unsigned int *num_entries /* OUT */)
61
412k
{
62
412k
  const OT::name_accelerator_t &name = *face->table.name;
63
412k
  if (num_entries) *num_entries = name.names.length;
64
412k
  return (const hb_ot_name_entry_t *) name.names;
65
412k
}
66
67
template <typename utf_t>
68
static inline unsigned int
69
hb_ot_name_get_utf (hb_face_t       *face,
70
        hb_ot_name_id_t  name_id,
71
        hb_language_t    language,
72
        unsigned int    *text_size /* IN/OUT */,
73
        typename utf_t::codepoint_t *text /* OUT */)
74
1.23M
{
75
1.23M
  const OT::name_accelerator_t &name = *face->table.name;
76
77
1.23M
  if (!language)
78
1.23M
    language = hb_language_from_string ("en", 2);
79
80
1.23M
  unsigned int width;
81
1.23M
  int idx = name.get_index (name_id, language, &width);
82
1.23M
  if (idx != -1)
83
33.4k
  {
84
33.4k
    hb_bytes_t bytes = name.get_name (idx);
85
86
33.4k
    if (width == 2) /* UTF16-BE */
87
30.8k
      return OT::hb_ot_name_convert_utf<hb_utf16_be_t, utf_t> (bytes, text_size, text);
88
89
2.65k
    if (width == 1) /* ASCII */
90
2.65k
      return OT::hb_ot_name_convert_utf<hb_ascii_t, utf_t> (bytes, text_size, text);
91
2.65k
  }
92
93
1.20M
  if (text_size)
94
401k
  {
95
401k
    if (*text_size)
96
401k
      *text = 0;
97
401k
    *text_size = 0;
98
401k
  }
99
1.20M
  return 0;
100
1.23M
}
hb-ot-name.cc:unsigned int hb_ot_name_get_utf<hb_utf8_t>(hb_face_t*, unsigned int, hb_language_impl_t const*, unsigned int*, hb_utf8_t::codepoint_t*)
Line
Count
Source
74
412k
{
75
412k
  const OT::name_accelerator_t &name = *face->table.name;
76
77
412k
  if (!language)
78
412k
    language = hb_language_from_string ("en", 2);
79
80
412k
  unsigned int width;
81
412k
  int idx = name.get_index (name_id, language, &width);
82
412k
  if (idx != -1)
83
11.1k
  {
84
11.1k
    hb_bytes_t bytes = name.get_name (idx);
85
86
11.1k
    if (width == 2) /* UTF16-BE */
87
10.2k
      return OT::hb_ot_name_convert_utf<hb_utf16_be_t, utf_t> (bytes, text_size, text);
88
89
885
    if (width == 1) /* ASCII */
90
885
      return OT::hb_ot_name_convert_utf<hb_ascii_t, utf_t> (bytes, text_size, text);
91
885
  }
92
93
401k
  if (text_size)
94
401k
  {
95
401k
    if (*text_size)
96
401k
      *text = 0;
97
401k
    *text_size = 0;
98
401k
  }
99
401k
  return 0;
100
412k
}
hb-ot-name.cc:unsigned int hb_ot_name_get_utf<hb_utf16_xe_t<unsigned short> >(hb_face_t*, unsigned int, hb_language_impl_t const*, unsigned int*, hb_utf16_xe_t<unsigned short>::codepoint_t*)
Line
Count
Source
74
412k
{
75
412k
  const OT::name_accelerator_t &name = *face->table.name;
76
77
412k
  if (!language)
78
412k
    language = hb_language_from_string ("en", 2);
79
80
412k
  unsigned int width;
81
412k
  int idx = name.get_index (name_id, language, &width);
82
412k
  if (idx != -1)
83
11.1k
  {
84
11.1k
    hb_bytes_t bytes = name.get_name (idx);
85
86
11.1k
    if (width == 2) /* UTF16-BE */
87
10.2k
      return OT::hb_ot_name_convert_utf<hb_utf16_be_t, utf_t> (bytes, text_size, text);
88
89
885
    if (width == 1) /* ASCII */
90
885
      return OT::hb_ot_name_convert_utf<hb_ascii_t, utf_t> (bytes, text_size, text);
91
885
  }
92
93
401k
  if (text_size)
94
0
  {
95
0
    if (*text_size)
96
0
      *text = 0;
97
0
    *text_size = 0;
98
0
  }
99
401k
  return 0;
100
412k
}
hb-ot-name.cc:unsigned int hb_ot_name_get_utf<hb_utf32_xe_t<unsigned int, true> >(hb_face_t*, unsigned int, hb_language_impl_t const*, unsigned int*, hb_utf32_xe_t<unsigned int, true>::codepoint_t*)
Line
Count
Source
74
412k
{
75
412k
  const OT::name_accelerator_t &name = *face->table.name;
76
77
412k
  if (!language)
78
412k
    language = hb_language_from_string ("en", 2);
79
80
412k
  unsigned int width;
81
412k
  int idx = name.get_index (name_id, language, &width);
82
412k
  if (idx != -1)
83
11.1k
  {
84
11.1k
    hb_bytes_t bytes = name.get_name (idx);
85
86
11.1k
    if (width == 2) /* UTF16-BE */
87
10.2k
      return OT::hb_ot_name_convert_utf<hb_utf16_be_t, utf_t> (bytes, text_size, text);
88
89
885
    if (width == 1) /* ASCII */
90
885
      return OT::hb_ot_name_convert_utf<hb_ascii_t, utf_t> (bytes, text_size, text);
91
885
  }
92
93
401k
  if (text_size)
94
0
  {
95
0
    if (*text_size)
96
0
      *text = 0;
97
0
    *text_size = 0;
98
0
  }
99
401k
  return 0;
100
412k
}
101
102
/**
103
 * hb_ot_name_get_utf8:
104
 * @face: font face.
105
 * @name_id: OpenType name identifier to fetch.
106
 * @language: language to fetch the name for.
107
 * @text_size: (inout) (optional): input size of @text buffer, and output size of
108
 *                                   text written to buffer.
109
 * @text: (out caller-allocates) (array length=text_size): buffer to write fetched name into.
110
 *
111
 * Fetches a font name from the OpenType 'name' table.
112
 * If @language is #HB_LANGUAGE_INVALID, English ("en") is assumed.
113
 * Returns string in UTF-8 encoding. A NUL terminator is always written
114
 * for convenience, and isn't included in the output @text_size.
115
 *
116
 * Returns: full length of the requested string, or 0 if not found.
117
 * Since: 2.1.0
118
 **/
119
unsigned int
120
hb_ot_name_get_utf8 (hb_face_t       *face,
121
         hb_ot_name_id_t  name_id,
122
         hb_language_t    language,
123
         unsigned int    *text_size /* IN/OUT */,
124
         char            *text      /* OUT */)
125
412k
{
126
412k
  return hb_ot_name_get_utf<hb_utf8_t> (face, name_id, language, text_size,
127
412k
          (hb_utf8_t::codepoint_t *) text);
128
412k
}
129
130
/**
131
 * hb_ot_name_get_utf16:
132
 * @face: font face.
133
 * @name_id: OpenType name identifier to fetch.
134
 * @language: language to fetch the name for.
135
 * @text_size: (inout) (optional): input size of @text buffer, and output size of
136
 *                                   text written to buffer.
137
 * @text: (out caller-allocates) (array length=text_size): buffer to write fetched name into.
138
 *
139
 * Fetches a font name from the OpenType 'name' table.
140
 * If @language is #HB_LANGUAGE_INVALID, English ("en") is assumed.
141
 * Returns string in UTF-16 encoding. A NUL terminator is always written
142
 * for convenience, and isn't included in the output @text_size.
143
 *
144
 * Returns: full length of the requested string, or 0 if not found.
145
 * Since: 2.1.0
146
 **/
147
unsigned int
148
hb_ot_name_get_utf16 (hb_face_t       *face,
149
          hb_ot_name_id_t  name_id,
150
          hb_language_t    language,
151
          unsigned int    *text_size /* IN/OUT */,
152
          uint16_t        *text      /* OUT */)
153
412k
{
154
412k
  return hb_ot_name_get_utf<hb_utf16_t> (face, name_id, language, text_size, text);
155
412k
}
156
157
/**
158
 * hb_ot_name_get_utf32:
159
 * @face: font face.
160
 * @name_id: OpenType name identifier to fetch.
161
 * @language: language to fetch the name for.
162
 * @text_size: (inout) (optional): input size of @text buffer, and output size of
163
 *                                   text written to buffer.
164
 * @text: (out caller-allocates) (array length=text_size): buffer to write fetched name into.
165
 *
166
 * Fetches a font name from the OpenType 'name' table.
167
 * If @language is #HB_LANGUAGE_INVALID, English ("en") is assumed.
168
 * Returns string in UTF-32 encoding. A NUL terminator is always written
169
 * for convenience, and isn't included in the output @text_size.
170
 *
171
 * Returns: full length of the requested string, or 0 if not found.
172
 * Since: 2.1.0
173
 **/
174
unsigned int
175
hb_ot_name_get_utf32 (hb_face_t       *face,
176
          hb_ot_name_id_t  name_id,
177
          hb_language_t    language,
178
          unsigned int    *text_size /* IN/OUT */,
179
          uint32_t        *text      /* OUT */)
180
412k
{
181
412k
  return hb_ot_name_get_utf<hb_utf32_t> (face, name_id, language, text_size, text);
182
412k
}
183
184
#include "hb-ot-name-language-static.hh"
185
186
#endif