2 * Copyright © 2018 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
31 #include "hb-ot-name-table.hh"
39 * @short_description: OpenType font name information
42 * Functions for fetching name strings from OpenType fonts.
47 * hb_ot_name_list_names:
49 * @num_entries: (out) (optional): number of returned entries.
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.
55 * Returns: (transfer none) (array length=num_entries): Array of available name entries.
58 const hb_ot_name_entry_t *
59 hb_ot_name_list_names (hb_face_t *face,
60 unsigned int *num_entries /* OUT */)
62 const OT::name_accelerator_t &name = *face->table.name;
63 if (num_entries) *num_entries = name.names.length;
64 return (const hb_ot_name_entry_t *) name.names;
68 template <typename in_utf_t, typename out_utf_t>
69 static inline unsigned int
70 hb_ot_name_convert_utf (hb_bytes_t bytes,
71 unsigned int *text_size /* IN/OUT */,
72 typename out_utf_t::codepoint_t *text /* OUT */)
74 unsigned int src_len = bytes.length / sizeof (typename in_utf_t::codepoint_t);
75 const typename in_utf_t::codepoint_t *src = (const typename in_utf_t::codepoint_t *) bytes.arrayZ;
76 const typename in_utf_t::codepoint_t *src_end = src + src_len;
78 typename out_utf_t::codepoint_t *dst = text;
80 hb_codepoint_t unicode;
81 const hb_codepoint_t replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
83 if (text_size && *text_size)
85 (*text_size)--; /* Same room for NUL-termination. */
86 const typename out_utf_t::codepoint_t *dst_end = text + *text_size;
88 while (src < src_end && dst < dst_end)
90 const typename in_utf_t::codepoint_t *src_next = in_utf_t::next (src, src_end, &unicode, replacement);
91 typename out_utf_t::codepoint_t *dst_next = out_utf_t::encode (dst, dst_end, unicode);
93 break; /* Out-of-room. */
99 *text_size = dst - text;
100 *dst = 0; /* NUL-terminate. */
103 /* Accumulate length of rest. */
104 unsigned int dst_len = dst - text;
105 while (src < src_end)
107 src = in_utf_t::next (src, src_end, &unicode, replacement);
108 dst_len += out_utf_t::encode_len (unicode);
113 template <typename utf_t>
114 static inline unsigned int
115 hb_ot_name_get_utf (hb_face_t *face,
116 hb_ot_name_id_t name_id,
117 hb_language_t language,
118 unsigned int *text_size /* IN/OUT */,
119 typename utf_t::codepoint_t *text /* OUT */)
121 const OT::name_accelerator_t &name = *face->table.name;
124 language = hb_language_from_string ("en", 2);
127 int idx = name.get_index (name_id, language, &width);
130 hb_bytes_t bytes = name.get_name (idx);
132 if (width == 2) /* UTF16-BE */
133 return hb_ot_name_convert_utf<hb_utf16_be_t, utf_t> (bytes, text_size, text);
135 if (width == 1) /* ASCII */
136 return hb_ot_name_convert_utf<hb_ascii_t, utf_t> (bytes, text_size, text);
149 * hb_ot_name_get_utf8:
151 * @name_id: OpenType name identifier to fetch.
152 * @language: language to fetch the name for.
153 * @text_size: (inout) (optional): input size of @text buffer, and output size of
154 * text written to buffer.
155 * @text: (out caller-allocates) (array length=text_size): buffer to write fetched name into.
157 * Fetches a font name from the OpenType 'name' table.
158 * If @language is #HB_LANGUAGE_INVALID, English ("en") is assumed.
159 * Returns string in UTF-8 encoding. A NUL terminator is always written
160 * for convenience, and isn't included in the output @text_size.
162 * Returns: full length of the requested string, or 0 if not found.
166 hb_ot_name_get_utf8 (hb_face_t *face,
167 hb_ot_name_id_t name_id,
168 hb_language_t language,
169 unsigned int *text_size /* IN/OUT */,
170 char *text /* OUT */)
172 return hb_ot_name_get_utf<hb_utf8_t> (face, name_id, language, text_size,
173 (hb_utf8_t::codepoint_t *) text);
177 * hb_ot_name_get_utf16:
179 * @name_id: OpenType name identifier to fetch.
180 * @language: language to fetch the name for.
181 * @text_size: (inout) (optional): input size of @text buffer, and output size of
182 * text written to buffer.
183 * @text: (out caller-allocates) (array length=text_size): buffer to write fetched name into.
185 * Fetches a font name from the OpenType 'name' table.
186 * If @language is #HB_LANGUAGE_INVALID, English ("en") is assumed.
187 * Returns string in UTF-16 encoding. A NUL terminator is always written
188 * for convenience, and isn't included in the output @text_size.
190 * Returns: full length of the requested string, or 0 if not found.
194 hb_ot_name_get_utf16 (hb_face_t *face,
195 hb_ot_name_id_t name_id,
196 hb_language_t language,
197 unsigned int *text_size /* IN/OUT */,
198 uint16_t *text /* OUT */)
200 return hb_ot_name_get_utf<hb_utf16_t> (face, name_id, language, text_size, text);
204 * hb_ot_name_get_utf32:
206 * @name_id: OpenType name identifier to fetch.
207 * @language: language to fetch the name for.
208 * @text_size: (inout) (optional): input size of @text buffer, and output size of
209 * text written to buffer.
210 * @text: (out caller-allocates) (array length=text_size): buffer to write fetched name into.
212 * Fetches a font name from the OpenType 'name' table.
213 * If @language is #HB_LANGUAGE_INVALID, English ("en") is assumed.
214 * Returns string in UTF-32 encoding. A NUL terminator is always written
215 * for convenience, and isn't included in the output @text_size.
217 * Returns: full length of the requested string, or 0 if not found.
221 hb_ot_name_get_utf32 (hb_face_t *face,
222 hb_ot_name_id_t name_id,
223 hb_language_t language,
224 unsigned int *text_size /* IN/OUT */,
225 uint32_t *text /* OUT */)
227 return hb_ot_name_get_utf<hb_utf32_t> (face, name_id, language, text_size, text);