| 1 |
/* font-manager-freetype.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2025 Jerry Casiano |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "font-manager-freetype.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-freetype |
| 25 |
* @short_description: FreeType utility functions |
| 26 |
* @title: FreeType Utility Functions |
| 27 |
* @include: font-manager-freetype.h |
| 28 |
* @see_also: #FontManagerFontInfo |
| 29 |
* |
| 30 |
* Functions used to gather information from font files using the FreeType library. |
| 31 |
*/ |
| 32 |
|
| 33 |
G_DEFINE_QUARK(font-manager-freetype-error-quark, font_manager_freetype_error) |
| 34 |
|
| 35 |
const char *FT_Error_Message (FT_Error err) |
| 36 |
{ |
| 37 |
#undef __FTERRORS_H__ |
| 38 |
#define FT_ERRORDEF( e, v, s ) case e: return s; |
| 39 |
#define FT_ERROR_START_LIST switch (err) { |
| 40 |
#define FT_ERROR_END_LIST } |
| 41 |
#include FT_ERRORS_H |
| 42 |
return "(Unknown error)"; |
| 43 |
} |
| 44 |
|
| 45 |
static void get_os2_info (JsonObject *json_obj, const FT_Face face); |
| 46 |
static void get_sfnt_info (JsonObject *json_obj, const FT_Face face); |
| 47 |
static void get_ps_info (JsonObject *json_obj, const FT_Face face); |
| 48 |
static void get_license_info (JsonObject *json_obj); |
| 49 |
static void get_fs_type (JsonObject *json_obj, const FT_Face face); |
| 50 |
static void get_font_revision (JsonObject *json_obj, const FT_Face face); |
| 51 |
static void ensure_vendor (JsonObject *json_obj, const FT_Face face); |
| 52 |
static void cleanup_version_string (JsonObject *json_obj); |
| 53 |
static void correct_filetype (JsonObject *json_obj); |
| 54 |
|
| 55 |
static void |
| 56 |
set_error (FT_Error ft_error, const gchar *ctx, GError **error) |
| 57 |
{ |
| 58 |
g_return_if_fail(error == NULL || *error == NULL); |
| 59 |
const gchar *msg_format = "Freetype Error : (%s) [%i] - %s"; |
| 60 |
g_debug(msg_format, ctx, ft_error, FT_Error_Message(ft_error)); |
| 61 |
g_set_error(error, |
| 62 |
FONT_MANAGER_FREETYPE_ERROR, |
| 63 |
FONT_MANAGER_FREETYPE_ERROR_FAILED, |
| 64 |
msg_format, ctx, ft_error, FT_Error_Message(ft_error)); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* font_manager_get_face_count: |
| 70 |
* @filepath: full path to font file to examine |
| 71 |
* @error: #GError or %NULL to ignore errors |
| 72 |
* |
| 73 |
* This function never fails. In case of an error this function returns 1. |
| 74 |
* Any valid font file should contain at least one variation. |
| 75 |
* |
| 76 |
* Returns: the number of variations contained in filepath |
| 77 |
*/ |
| 78 |
glong |
| 79 |
font_manager_get_face_count (const gchar *filepath, GError **error) |
| 80 |
{ |
| 81 |
FT_Face face; |
| 82 |
FT_Library library; |
| 83 |
FT_Long num_faces; |
| 84 |
FT_Error ft_error = FT_Init_FreeType(&library); |
| 85 |
|
| 86 |
/* Index 0 is always valid */ |
| 87 |
if (G_UNLIKELY(ft_error != 0)) { |
| 88 |
set_error(ft_error, "FT_Init_FreeType", error); |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
ft_error = FT_New_Face(library, filepath, 0, &face); |
| 93 |
|
| 94 |
if (G_UNLIKELY(ft_error != 0)) { |
| 95 |
set_error(ft_error, "FT_New_Face", error); |
| 96 |
FT_Done_FreeType(library); |
| 97 |
return 1; |
| 98 |
} |
| 99 |
|
| 100 |
num_faces = face->num_faces; |
| 101 |
FT_Done_Face(face); |
| 102 |
FT_Done_FreeType(library); |
| 103 |
return num_faces; |
| 104 |
} |
| 105 |
|
| 106 |
static const gchar *ensure_member [] = { |
| 107 |
"designer", |
| 108 |
"designer-url", |
| 109 |
"description", |
| 110 |
"license-data", |
| 111 |
"license-url", |
| 112 |
NULL |
| 113 |
}; |
| 114 |
|
| 115 |
/** |
| 116 |
* font_manager_get_font_revision: |
| 117 |
* @filepath: full path to font file to examine |
| 118 |
* |
| 119 |
* Returns: The version number as a float. This function never fails and returns |
| 120 |
* 1.0 as a default value if no version is set or if an error occurs. |
| 121 |
*/ |
| 122 |
gfloat |
| 123 |
font_manager_get_font_revision (const gchar *filepath) |
| 124 |
{ |
| 125 |
g_return_val_if_fail(filepath != NULL, 1.0); |
| 126 |
|
| 127 |
FT_Face face; |
| 128 |
FT_Library library; |
| 129 |
FT_Error ft_error; |
| 130 |
|
| 131 |
float result = 1.0; |
| 132 |
gsize filesize = 0; |
| 133 |
GError *error = NULL; |
| 134 |
g_autofree gchar *font = NULL; |
| 135 |
|
| 136 |
if (G_UNLIKELY(!g_file_get_contents(filepath, &font, &filesize, &error))) { |
| 137 |
g_critical("%s : %s", error->message, filepath); |
| 138 |
g_error_free(error); |
| 139 |
return result; |
| 140 |
} |
| 141 |
|
| 142 |
ft_error = FT_Init_FreeType(&library); |
| 143 |
|
| 144 |
if (G_UNLIKELY(ft_error)) { |
| 145 |
g_critical("%s : %s", FT_Error_Message(ft_error), filepath); |
| 146 |
return result; |
| 147 |
} |
| 148 |
|
| 149 |
ft_error = FT_New_Memory_Face(library, (const FT_Byte *) font, (FT_Long) filesize, 0, &face); |
| 150 |
|
| 151 |
if (G_UNLIKELY(ft_error)) { |
| 152 |
g_critical("%s : %s", FT_Error_Message(ft_error), filepath); |
| 153 |
return result; |
| 154 |
} |
| 155 |
|
| 156 |
TT_Header *head = (TT_Header *) FT_Get_Sfnt_Table(face, FT_SFNT_HEAD); |
| 157 |
if (head) |
| 158 |
if (head->Font_Revision) |
| 159 |
result = (float) head->Font_Revision / 65536.0; |
| 160 |
|
| 161 |
FT_Done_Face(face); |
| 162 |
FT_Done_FreeType(library); |
| 163 |
return result; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* font_manager_get_metadata: |
| 168 |
* @filepath: full path to font file to examine |
| 169 |
* @index: face index to examine |
| 170 |
* @error: #GError or %NULL to ignore errors |
| 171 |
* |
| 172 |
* See #FontManagerFontInfo for a description of the #JsonObject returned by this function. |
| 173 |
* |
| 174 |
* Returns: (transfer full) (nullable): A newly created #JsonObject or %NULL if there was an error. |
| 175 |
* Free the returned object using #json_object_unref when no longer needed. |
| 176 |
*/ |
| 177 |
JsonObject * |
| 178 |
font_manager_get_metadata (const gchar *filepath, gint index, GError **error) |
| 179 |
{ |
| 180 |
g_return_val_if_fail(filepath != NULL, NULL); |
| 181 |
g_return_val_if_fail((error == NULL || *error == NULL), NULL); |
| 182 |
|
| 183 |
FT_Face face; |
| 184 |
FT_Library library; |
| 185 |
FT_Error ft_error; |
| 186 |
|
| 187 |
gsize filesize = 0; |
| 188 |
g_autofree gchar *font = NULL; |
| 189 |
|
| 190 |
g_autoptr(JsonObject) json_obj = json_object_new(); |
| 191 |
|
| 192 |
json_object_set_string_member(json_obj, "filepath", filepath); |
| 193 |
json_object_set_int_member(json_obj, "findex", index); |
| 194 |
json_object_set_int_member(json_obj, "owner", font_manager_get_file_owner(filepath)); |
| 195 |
|
| 196 |
if (G_UNLIKELY(!g_file_get_contents(filepath, &font, &filesize, error))) { |
| 197 |
g_critical("%s : %s", (*error)->message, filepath); |
| 198 |
return NULL; |
| 199 |
} |
| 200 |
|
| 201 |
ft_error = FT_Init_FreeType(&library); |
| 202 |
|
| 203 |
if (G_UNLIKELY(ft_error)) { |
| 204 |
set_error(ft_error, "FT_Init_FreeType", error); |
| 205 |
return NULL; |
| 206 |
} |
| 207 |
|
| 208 |
ft_error = FT_New_Memory_Face(library, (const FT_Byte *) font, (FT_Long) filesize, index, &face); |
| 209 |
|
| 210 |
if (G_UNLIKELY(ft_error)) { |
| 211 |
set_error(ft_error, "FT_New_Memory_Face", error); |
| 212 |
return NULL; |
| 213 |
} |
| 214 |
|
| 215 |
g_autofree gchar *_size = g_format_size(filesize); |
| 216 |
g_autofree gchar *_md5 = g_compute_checksum_for_data(G_CHECKSUM_MD5, (const guchar *) font, filesize); |
| 217 |
json_object_set_string_member(json_obj, "filesize", _size); |
| 218 |
json_object_set_string_member(json_obj, "checksum", _md5); |
| 219 |
|
| 220 |
/* Fontconfig modifies invalid PostScript names by replacing illegal characters with - */ |
| 221 |
json_object_set_string_member(json_obj, "psname", FT_Get_Postscript_Name(face)); |
| 222 |
json_object_set_string_member(json_obj, "filetype", FT_Get_Font_Format(face)); |
| 223 |
json_object_set_int_member(json_obj, "n-glyphs", face->num_glyphs); |
| 224 |
|
| 225 |
/* Order matters */ |
| 226 |
get_os2_info(json_obj, face); |
| 227 |
get_font_revision(json_obj, face); |
| 228 |
get_sfnt_info(json_obj, face); |
| 229 |
get_ps_info(json_obj, face); |
| 230 |
get_license_info(json_obj); |
| 231 |
get_fs_type(json_obj, face); |
| 232 |
|
| 233 |
ensure_vendor(json_obj, face); |
| 234 |
correct_filetype(json_obj); |
| 235 |
|
| 236 |
/* Useful during font installation */ |
| 237 |
if (!json_object_has_member(json_obj, "family")) |
| 238 |
json_object_set_string_member(json_obj, "family", (gchar *) face->family_name); |
| 239 |
if (!json_object_has_member(json_obj, "style")) |
| 240 |
json_object_set_string_member(json_obj, "style", (gchar *) face->style_name); |
| 241 |
|
| 242 |
gboolean variable = FT_HAS_MULTIPLE_MASTERS(face); |
| 243 |
json_object_set_boolean_member(json_obj, "variable", variable); |
| 244 |
g_autoptr(GString) str = g_string_new(""); |
| 245 |
if (variable) { |
| 246 |
FT_MM_Var *mmvar = NULL; |
| 247 |
ft_error = FT_Get_MM_Var(face, &mmvar); |
| 248 |
if (G_UNLIKELY(ft_error)) { |
| 249 |
if (mmvar) |
| 250 |
FT_Done_MM_Var(library, mmvar); |
| 251 |
} else { |
| 252 |
gboolean weight = FALSE; |
| 253 |
gboolean width = FALSE; |
| 254 |
gboolean opsize = FALSE; |
| 255 |
for (unsigned int i = 0; i < mmvar->num_axis; i++) { |
| 256 |
switch (mmvar->axis[i].tag) { |
| 257 |
case FT_MAKE_TAG('w', 'g', 'h', 't'): |
| 258 |
weight = TRUE; |
| 259 |
break; |
| 260 |
case FT_MAKE_TAG('w', 'd', 't', 'h'): |
| 261 |
width = TRUE; |
| 262 |
break; |
| 263 |
case FT_MAKE_TAG('o', 'p', 's', 'z'): |
| 264 |
opsize = TRUE; |
| 265 |
break; |
| 266 |
} |
| 267 |
} |
| 268 |
g_string_append(str, "["); |
| 269 |
if (weight) |
| 270 |
g_string_append(str, "wght"); |
| 271 |
if (width) { |
| 272 |
if (weight) |
| 273 |
g_string_append(str, ","); |
| 274 |
g_string_append(str, "wdth"); |
| 275 |
} |
| 276 |
if (opsize) { |
| 277 |
if (weight || width) |
| 278 |
g_string_append(str, ","); |
| 279 |
g_string_append(str, "opsz"); |
| 280 |
} |
| 281 |
g_string_append(str, "]"); |
| 282 |
} |
| 283 |
} |
| 284 |
json_object_set_string_member(json_obj, "vars", str->str); |
| 285 |
|
| 286 |
if (!json_object_has_member(json_obj, "version")) |
| 287 |
json_object_set_string_member(json_obj, "version", "1.0"); |
| 288 |
|
| 289 |
for (int i = 0; ensure_member[i] != NULL; i++) |
| 290 |
if (!json_object_has_member(json_obj, ensure_member[i])) |
| 291 |
json_object_set_string_member(json_obj, ensure_member[i], NULL); |
| 292 |
|
| 293 |
FT_Done_Face(face); |
| 294 |
FT_Done_FreeType(library); |
| 295 |
return g_steal_pointer(&json_obj); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* font_manager_get_suggested_filename: |
| 300 |
* @metadata: #JsonObject returned by #font_manager_get_metadata() |
| 301 |
* |
| 302 |
* Returns: (transfer full): A newly allocated string. Free the result using #g_free. |
| 303 |
*/ |
| 304 |
gchar * |
| 305 |
font_manager_get_suggested_filename (JsonObject *metadata) |
| 306 |
{ |
| 307 |
const gchar *family = json_object_get_string_member(metadata, "family"); |
| 308 |
const gchar *style = json_object_get_string_member(metadata, "style"); |
| 309 |
gboolean variable = json_object_get_boolean_member(metadata, "variable"); |
| 310 |
g_autofree gchar *name = NULL; |
| 311 |
if (variable) { |
| 312 |
const gchar *vars = json_object_get_string_member(metadata, "vars"); |
| 313 |
name = g_strdup_printf("%s %s VF %s", family, style, vars); |
| 314 |
} else { |
| 315 |
name = g_strdup_printf("%s %s", family, style); |
| 316 |
} |
| 317 |
return font_manager_to_filename(name); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* font_manager_get_installation_target: |
| 322 |
* @font_file: #GFile |
| 323 |
* @target_dir: #GFile |
| 324 |
* @create_directories: whether to create suggested directories or not |
| 325 |
* @error: #GError or %NULL to ignore errors |
| 326 |
* |
| 327 |
* The #GFile returned by this function is the suggested output file based |
| 328 |
* on the metadata present in @font_file. |
| 329 |
* |
| 330 |
* If @create_directories is %FALSE the parent directory for the returned #GFile |
| 331 |
* may not exist and should be created prior to use. |
| 332 |
* |
| 333 |
* Returns: (transfer full) (nullable): |
| 334 |
* A newly-created #GFile or %NULL if there was an error. |
| 335 |
* Free the returned object using #g_object_unref(). |
| 336 |
*/ |
| 337 |
GFile * |
| 338 |
font_manager_get_installation_target (GFile *font_file, GFile *target_dir, |
| 339 |
gboolean create_directories, GError **error) |
| 340 |
{ |
| 341 |
g_return_val_if_fail((error == NULL || *error == NULL), NULL); |
| 342 |
g_autofree gchar *dir = g_file_get_path(target_dir); |
| 343 |
g_autofree gchar *filepath = g_file_get_path(font_file); |
| 344 |
g_autofree gchar *ext = font_manager_get_file_extension(filepath); |
| 345 |
g_autoptr(JsonObject) metadata = font_manager_get_metadata(filepath, 0, error); |
| 346 |
g_return_val_if_fail((error == NULL || *error == NULL), NULL); |
| 347 |
const gchar *vendor = json_object_get_string_member(metadata, "vendor"); |
| 348 |
const gchar *filetype = json_object_get_string_member(metadata, "filetype"); |
| 349 |
const gchar *family = json_object_get_string_member(metadata, "family"); |
| 350 |
g_autofree gchar *suggested = font_manager_get_suggested_filename(metadata); |
| 351 |
g_autofree gchar *filename = g_strdup_printf("%s.%s", suggested, ext); |
| 352 |
GFile *target = g_file_new_build_filename(dir, vendor, filetype, family, filename, NULL); |
| 353 |
g_autoptr(GFile) parent = g_file_get_parent(target); |
| 354 |
if (create_directories) |
| 355 |
if (!g_file_query_exists(parent, NULL)) |
| 356 |
if (!g_file_make_directory_with_parents(parent, NULL, error)) |
| 357 |
g_clear_object(&target); |
| 358 |
return target; |
| 359 |
} |
| 360 |
|
| 361 |
/* |
| 362 |
* get_license_type: |
| 363 |
* |
| 364 |
* @license: license data or %NULL |
| 365 |
* @copyright: copyright data or %NULL |
| 366 |
* @url: url data or %NULL |
| 367 |
* |
| 368 |
* Searches through known licenses for a match. |
| 369 |
* Returns an integer which can be used with get_license_name and get_license_url. |
| 370 |
* |
| 371 |
* Returns: index |
| 372 |
*/ |
| 373 |
static gint |
| 374 |
get_license_type (const gchar *license, const gchar *copyright, const gchar *url) |
| 375 |
{ |
| 376 |
for (guint i = 0; i < FONT_MANAGER_LICENSE_ENTRIES; i++) { |
| 377 |
gint l = 0; |
| 378 |
while (FontManagerLicenseData[i].keywords[l]) { |
| 379 |
if ((copyright && g_strrstr(copyright, FontManagerLicenseData[i].keywords[l])) |
| 380 |
|| (license && g_strrstr(license, FontManagerLicenseData[i].keywords[l])) |
| 381 |
|| (url && g_strrstr(url, FontManagerLicenseData[i].keywords[l]))) |
| 382 |
return i; |
| 383 |
l++; |
| 384 |
} |
| 385 |
} |
| 386 |
return FONT_MANAGER_LICENSE_ENTRIES - 1; |
| 387 |
} |
| 388 |
|
| 389 |
/* |
| 390 |
* get_license_name: |
| 391 |
* |
| 392 |
* @license_type: index returned by get_license_type |
| 393 |
* |
| 394 |
* Returns: (transfer none): string suitable for display |
| 395 |
*/ |
| 396 |
static const gchar * |
| 397 |
get_license_name (gint license_type) |
| 398 |
{ |
| 399 |
return FontManagerLicenseData[license_type].license; |
| 400 |
} |
| 401 |
|
| 402 |
/* |
| 403 |
* get_license_url: |
| 404 |
* |
| 405 |
* @license_type: index returned by get_license_type |
| 406 |
* |
| 407 |
* Returns: (transfer none): license URL or %NULL |
| 408 |
*/ |
| 409 |
static const gchar * |
| 410 |
get_license_url (gint license_type) |
| 411 |
{ |
| 412 |
return FontManagerLicenseData[license_type].license_url; |
| 413 |
} |
| 414 |
|
| 415 |
/* |
| 416 |
* get_vendor_from_notice: |
| 417 |
* |
| 418 |
* @notice: notice data (PostScript Type 1) |
| 419 |
* |
| 420 |
* Attempts to find vendor from notice data in PS_FontInfoRec |
| 421 |
* |
| 422 |
* Returns: (transfer none): vendor name or %NULL |
| 423 |
*/ |
| 424 |
static const gchar * |
| 425 |
get_vendor_from_notice (const gchar *notice) |
| 426 |
{ |
| 427 |
if (notice) |
| 428 |
for(guint i = 0; i < FONT_MANAGER_NOTICE_ENTRIES; i++) |
| 429 |
if (g_strrstr(notice, FontManagerNoticeData[i].vendor_id)) |
| 430 |
return FontManagerNoticeData[i].vendor; |
| 431 |
return NULL; |
| 432 |
} |
| 433 |
|
| 434 |
static gboolean |
| 435 |
vendor_matches (const gchar vendor[FONT_MANAGER_MAX_VENDOR_ID_LENGTH], const gchar *vendor_id) |
| 436 |
{ |
| 437 |
gboolean result; |
| 438 |
g_autoptr(GString) a = NULL; |
| 439 |
g_autoptr(GString) b = NULL; |
| 440 |
/* vendor is not necessarily NUL-terminated. */ |
| 441 |
a = g_string_new_len((const gchar *) vendor, FONT_MANAGER_MAX_VENDOR_ID_LENGTH); |
| 442 |
b = g_string_new_len((const gchar *) vendor_id, FONT_MANAGER_MAX_VENDOR_ID_LENGTH); |
| 443 |
result = g_string_equal(a, b); |
| 444 |
return result; |
| 445 |
} |
| 446 |
|
| 447 |
static gboolean |
| 448 |
is_known_vendor (gchar *vendor) |
| 449 |
{ |
| 450 |
for (guint i = 0; i < FONT_MANAGER_VENDOR_ENTRIES; i++) |
| 451 |
if (g_strcmp0(FontManagerVendorData[i].vendor, vendor) == 0) |
| 452 |
return TRUE; |
| 453 |
return FALSE; |
| 454 |
} |
| 455 |
|
| 456 |
/* |
| 457 |
* get_vendor_from_vendor_id: |
| 458 |
* |
| 459 |
* @vendor: vendor ID |
| 460 |
* |
| 461 |
* Attempts to find actual name by matching ID to a list of known foundries |
| 462 |
* |
| 463 |
* Returns: (transfer none): string suitable for display or %NULL |
| 464 |
*/ |
| 465 |
static const gchar * |
| 466 |
get_vendor_from_vendor_id (const gchar vendor[FONT_MANAGER_MAX_VENDOR_ID_LENGTH]) |
| 467 |
{ |
| 468 |
if (vendor) |
| 469 |
for (guint i = 0; i < FONT_MANAGER_VENDOR_ENTRIES; i++) |
| 470 |
if (vendor_matches(vendor, FontManagerVendorData[i].vendor_id)) |
| 471 |
return FontManagerVendorData[i].vendor; |
| 472 |
return NULL; |
| 473 |
} |
| 474 |
|
| 475 |
/* |
| 476 |
* The functions in this section gather information which is either not |
| 477 |
* available through fontconfig or for which the information available |
| 478 |
* from fontconfig is not suitable for our needs. |
| 479 |
*/ |
| 480 |
|
| 481 |
#define PANOSE_ENTRIES 10 |
| 482 |
|
| 483 |
static void |
| 484 |
get_os2_info (JsonObject *json_obj, const FT_Face face) |
| 485 |
{ |
| 486 |
TT_OS2 *os2 = (TT_OS2 *) FT_Get_Sfnt_Table(face, FT_SFNT_OS2); |
| 487 |
if (G_LIKELY(os2 && os2->version >= 0x0001 && os2->version != 0xffff)) { |
| 488 |
const gchar *_vendor = get_vendor_from_vendor_id((gchar *) os2->achVendID); |
| 489 |
if (_vendor) |
| 490 |
json_object_set_string_member(json_obj, "vendor", _vendor); |
| 491 |
JsonArray *json_arr = json_array_sized_new(PANOSE_ENTRIES); |
| 492 |
for (gint i = 0; i < PANOSE_ENTRIES; i++) |
| 493 |
json_array_add_int_element(json_arr, os2->panose[i]); |
| 494 |
json_object_set_array_member(json_obj, "panose", json_arr); |
| 495 |
// XXX |
| 496 |
//g_message("%s : %i", json_object_get_string_member(json_obj, "filepath"), (int) os2->sFamilyClass >> 8); |
| 497 |
} |
| 498 |
return; |
| 499 |
} |
| 500 |
|
| 501 |
#define SNAME_2_UTF8(s, c) \ |
| 502 |
g_convert((const gchar *) s.string, s.string_len, "UTF-8", c, NULL, NULL, NULL) \ |
| 503 |
|
| 504 |
/* Mostly lifted from fontilus by James Henstridge. Thanks. :-) */ |
| 505 |
static void |
| 506 |
get_sfnt_info (JsonObject *json_obj, const FT_Face face) |
| 507 |
{ |
| 508 |
|
| 509 |
if (!FT_IS_SFNT(face)) |
| 510 |
return; |
| 511 |
|
| 512 |
gint namecount = FT_Get_Sfnt_Name_Count(face); |
| 513 |
g_autofree gchar *vendor = NULL; |
| 514 |
gboolean vendor_set = FALSE; |
| 515 |
|
| 516 |
for (gint index = 0; index < namecount; index++) { |
| 517 |
|
| 518 |
FT_SfntName sname; |
| 519 |
if (FT_Get_Sfnt_Name(face, index, &sname) != 0) |
| 520 |
continue; |
| 521 |
|
| 522 |
if (sname.platform_id != TT_PLATFORM_MICROSOFT) |
| 523 |
continue; |
| 524 |
|
| 525 |
if (sname.string == NULL) |
| 526 |
continue; |
| 527 |
|
| 528 |
g_autofree gchar *val = NULL; |
| 529 |
|
| 530 |
switch (sname.encoding_id) { |
| 531 |
case TT_MS_ID_SJIS: |
| 532 |
val = SNAME_2_UTF8(sname, "SJIS-WIN"); |
| 533 |
break; |
| 534 |
case TT_MS_ID_PRC: |
| 535 |
val = SNAME_2_UTF8(sname, "GB2312"); |
| 536 |
break; |
| 537 |
case TT_MS_ID_BIG_5: |
| 538 |
val = SNAME_2_UTF8(sname, "BIG-5"); |
| 539 |
break; |
| 540 |
case TT_MS_ID_WANSUNG: |
| 541 |
val = SNAME_2_UTF8(sname, "WANSUNG"); |
| 542 |
break; |
| 543 |
case TT_MS_ID_JOHAB: |
| 544 |
val = SNAME_2_UTF8(sname, "JOHAB"); |
| 545 |
break; |
| 546 |
case TT_MS_ID_UCS_4: |
| 547 |
val = SNAME_2_UTF8(sname, "UCS4"); |
| 548 |
break; |
| 549 |
default: |
| 550 |
val = SNAME_2_UTF8(sname, "UTF-16BE"); |
| 551 |
break; |
| 552 |
} |
| 553 |
|
| 554 |
if (!val) |
| 555 |
continue; |
| 556 |
|
| 557 |
switch (sname.name_id) { |
| 558 |
case TT_NAME_ID_FONT_FAMILY: |
| 559 |
case TT_NAME_ID_WWS_FAMILY: |
| 560 |
case TT_NAME_ID_TYPOGRAPHIC_FAMILY: |
| 561 |
if (json_object_has_member(json_obj, "family") |
| 562 |
&& sname.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) |
| 563 |
break; |
| 564 |
json_object_set_string_member(json_obj, "family", val); |
| 565 |
break; |
| 566 |
case TT_NAME_ID_FONT_SUBFAMILY: |
| 567 |
case TT_NAME_ID_WWS_SUBFAMILY: |
| 568 |
case TT_NAME_ID_TYPOGRAPHIC_SUBFAMILY: |
| 569 |
if (json_object_has_member(json_obj, "style") |
| 570 |
&& sname.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) |
| 571 |
break; |
| 572 |
json_object_set_string_member(json_obj, "style", val); |
| 573 |
break; |
| 574 |
case TT_NAME_ID_COPYRIGHT: |
| 575 |
if (json_object_has_member(json_obj, "copyright") |
| 576 |
&& sname.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) |
| 577 |
break; |
| 578 |
json_object_set_string_member(json_obj, "copyright", val); |
| 579 |
break; |
| 580 |
case TT_NAME_ID_VERSION_STRING: |
| 581 |
if (!json_object_has_member(json_obj, "version")) { |
| 582 |
json_object_set_string_member(json_obj, "version", val); |
| 583 |
cleanup_version_string(json_obj); |
| 584 |
} |
| 585 |
break; |
| 586 |
case TT_NAME_ID_DESCRIPTION: |
| 587 |
if (json_object_has_member(json_obj, "description") |
| 588 |
&& sname.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) |
| 589 |
break; |
| 590 |
json_object_set_string_member(json_obj, "description", val); |
| 591 |
break; |
| 592 |
case TT_NAME_ID_LICENSE: |
| 593 |
if (json_object_has_member(json_obj, "license-data") |
| 594 |
&& sname.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) |
| 595 |
break; |
| 596 |
json_object_set_string_member(json_obj, "license-data", val); |
| 597 |
break; |
| 598 |
case TT_NAME_ID_LICENSE_URL: |
| 599 |
if (json_object_has_member(json_obj, "license-url") |
| 600 |
&& sname.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) |
| 601 |
break; |
| 602 |
json_object_set_string_member(json_obj, "license-url", val); |
| 603 |
break; |
| 604 |
case TT_NAME_ID_DESIGNER: |
| 605 |
if (json_object_has_member(json_obj, "designer") |
| 606 |
&& sname.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) |
| 607 |
break; |
| 608 |
json_object_set_string_member(json_obj, "designer", val); |
| 609 |
break; |
| 610 |
case TT_NAME_ID_DESIGNER_URL: |
| 611 |
if (json_object_has_member(json_obj, "designer-url") |
| 612 |
&& sname.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) |
| 613 |
break; |
| 614 |
json_object_set_string_member(json_obj, "designer-url", val); |
| 615 |
break; |
| 616 |
case TT_NAME_ID_TRADEMARK: |
| 617 |
if (vendor_set) |
| 618 |
break; |
| 619 |
if (!vendor) |
| 620 |
vendor = g_strdup(val); |
| 621 |
break; |
| 622 |
case TT_NAME_ID_MANUFACTURER: |
| 623 |
if (vendor_set) |
| 624 |
break; |
| 625 |
if (vendor) |
| 626 |
g_free(vendor); |
| 627 |
vendor = g_strdup(val); |
| 628 |
vendor_set = (sname.language_id == TT_MS_LANGID_ENGLISH_UNITED_STATES); |
| 629 |
break; |
| 630 |
default: |
| 631 |
break; |
| 632 |
} |
| 633 |
|
| 634 |
} |
| 635 |
|
| 636 |
if (vendor) { |
| 637 |
if (!json_object_has_member(json_obj, "vendor")) { |
| 638 |
if (is_known_vendor(vendor)) |
| 639 |
json_object_set_string_member(json_obj, "vendor", vendor); |
| 640 |
else { |
| 641 |
const gchar *_vendor = get_vendor_from_notice(vendor); |
| 642 |
if (_vendor) |
| 643 |
json_object_set_string_member(json_obj, "vendor", _vendor); |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
return; |
| 649 |
} |
| 650 |
|
| 651 |
static void |
| 652 |
get_ps_info (JsonObject *json_obj, const FT_Face face) |
| 653 |
{ |
| 654 |
PS_FontInfoRec ps_info; |
| 655 |
|
| 656 |
/* Error here means it's probably not a PostScript font */ |
| 657 |
if (FT_Get_PS_Font_Info(face, &ps_info) != 0) |
| 658 |
return; |
| 659 |
|
| 660 |
if (!json_object_has_member(json_obj, "version")) |
| 661 |
json_object_set_string_member(json_obj, "version", ps_info.version); |
| 662 |
if (ps_info.notice && g_utf8_validate(ps_info.notice, -1, NULL)) { |
| 663 |
if (!json_object_has_member(json_obj, "copyright")) |
| 664 |
json_object_set_string_member(json_obj, "copyright", ps_info.notice); |
| 665 |
if (!json_object_has_member(json_obj, "vendor")) { |
| 666 |
const gchar *_vendor = get_vendor_from_notice(ps_info.notice); |
| 667 |
if (_vendor) |
| 668 |
json_object_set_string_member(json_obj, "vendor", _vendor); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
return; |
| 673 |
} |
| 674 |
|
| 675 |
static void |
| 676 |
get_license_info (JsonObject *json_obj) |
| 677 |
{ |
| 678 |
const gchar *license_data = NULL; |
| 679 |
const gchar *copyright = NULL; |
| 680 |
const gchar *license_url = NULL; |
| 681 |
|
| 682 |
if (json_object_has_member(json_obj, "license-data")) |
| 683 |
license_data = json_object_get_string_member(json_obj, "license-data"); |
| 684 |
if (json_object_has_member(json_obj, "copyright")) |
| 685 |
copyright = json_object_get_string_member(json_obj, "copyright"); |
| 686 |
if (json_object_has_member(json_obj, "license-url")) |
| 687 |
license_url = json_object_get_string_member(json_obj, "license-url"); |
| 688 |
|
| 689 |
gint license_type = get_license_type(license_data, copyright, license_url); |
| 690 |
const gchar *license_name = get_license_name(license_type); |
| 691 |
json_object_set_string_member(json_obj, "license-type", license_name); |
| 692 |
|
| 693 |
if (license_type < (gint) (FONT_MANAGER_LICENSE_ENTRIES - 1)) { |
| 694 |
license_url = get_license_url(license_type); |
| 695 |
if (license_url) |
| 696 |
json_object_set_string_member(json_obj, "license-url", license_url); |
| 697 |
} |
| 698 |
|
| 699 |
return; |
| 700 |
} |
| 701 |
|
| 702 |
static void |
| 703 |
get_fs_type (JsonObject *json_obj, const FT_Face face) |
| 704 |
{ |
| 705 |
FT_UShort flags = FT_Get_FSType_Flags(face); |
| 706 |
|
| 707 |
/* Default to FT_FSTYPE_INSTALLABLE_EMBEDDING */ |
| 708 |
int fsType = 0; |
| 709 |
|
| 710 |
/* Least restrictive bit set takes precedence */ |
| 711 |
if (flags & FT_FSTYPE_RESTRICTED_LICENSE_EMBEDDING) |
| 712 |
fsType = 2; |
| 713 |
if (flags & FT_FSTYPE_PREVIEW_AND_PRINT_EMBEDDING) |
| 714 |
fsType = 4; |
| 715 |
if (flags & FT_FSTYPE_EDITABLE_EMBEDDING) |
| 716 |
fsType = 8; |
| 717 |
if (flags & FT_FSTYPE_INSTALLABLE_EMBEDDING) |
| 718 |
fsType = 0; |
| 719 |
|
| 720 |
/* Additional restrictions */ |
| 721 |
if (fsType == 4 || fsType == 8) { |
| 722 |
if (flags & FT_FSTYPE_NO_SUBSETTING) |
| 723 |
fsType += 16; |
| 724 |
if (flags & FT_FSTYPE_BITMAP_EMBEDDING_ONLY){ |
| 725 |
if (FT_HAS_FIXED_SIZES(face)) { |
| 726 |
fsType += 32; |
| 727 |
} else { |
| 728 |
/* Restricts embedding to bitmaps but contains none. */ |
| 729 |
fsType = 2; |
| 730 |
} |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
json_object_set_int_member(json_obj, "fsType", fsType); |
| 735 |
return; |
| 736 |
} |
| 737 |
|
| 738 |
/* NOTE : |
| 739 |
* These functions set defaults that are suitable for display. |
| 740 |
* In some cases, they may not set correct information or possibly even |
| 741 |
* modify correct information to something expected to be more useful |
| 742 |
* to the user. |
| 743 |
*/ |
| 744 |
|
| 745 |
static void |
| 746 |
ensure_vendor (JsonObject *json_obj, const FT_Face face) |
| 747 |
{ |
| 748 |
if (!json_object_has_member(json_obj, "vendor")) |
| 749 |
/* Translators : For context see https://docs.microsoft.com/en-us/typography/opentype/spec/os2#achvendid */ |
| 750 |
json_object_set_string_member(json_obj, "vendor", N_("Unknown Vendor")); |
| 751 |
return; |
| 752 |
} |
| 753 |
|
| 754 |
static void |
| 755 |
get_font_revision (JsonObject *json_obj, const FT_Face face) |
| 756 |
{ |
| 757 |
TT_Header *head = (TT_Header *) FT_Get_Sfnt_Table(face, FT_SFNT_HEAD); |
| 758 |
if (head) { |
| 759 |
if (head->Font_Revision) { |
| 760 |
g_autofree gchar *rev = g_strdup_printf("%.2f", (float) head->Font_Revision / 65536.0); |
| 761 |
json_object_set_string_member(json_obj, "version", rev); |
| 762 |
return; |
| 763 |
} |
| 764 |
} |
| 765 |
return; |
| 766 |
} |
| 767 |
|
| 768 |
static void |
| 769 |
correct_filetype (JsonObject *json_obj) |
| 770 |
{ |
| 771 |
const gchar *filetype = json_object_get_string_member(json_obj, "filetype"); |
| 772 |
/* Compact Font Format doesn't really mean much. */ |
| 773 |
if (g_strcmp0(filetype, "CFF") == 0) { |
| 774 |
const gchar *filepath = json_object_get_string_member(json_obj, "filepath"); |
| 775 |
g_autofree gchar *ext = font_manager_get_file_extension(filepath); |
| 776 |
if (g_ascii_strcasecmp(ext, "otf") == 0) |
| 777 |
json_object_set_string_member(json_obj, "filetype", "OpenType"); |
| 778 |
else if (g_ascii_strcasecmp(ext, "ttf") == 0) |
| 779 |
json_object_set_string_member(json_obj, "filetype", "TrueType"); |
| 780 |
else if (g_ascii_strcasecmp(ext, "ttc") == 0) |
| 781 |
json_object_set_string_member(json_obj, "filetype", "TrueType Collection"); |
| 782 |
} |
| 783 |
return; |
| 784 |
} |
| 785 |
|
| 786 |
/* A lot of version strings have garbage in them. Try to pull just a number. */ |
| 787 |
|
| 788 |
static const gchar *VERSION_STRING_EXCLUDES[] = { |
| 789 |
"Version", |
| 790 |
"version", |
| 791 |
"Revision", |
| 792 |
"revision", |
| 793 |
"$Revision", |
| 794 |
"$:", |
| 795 |
"$", |
| 796 |
/* These do not even contain actual version numbers, completely pointless */ |
| 797 |
";FFEdit", |
| 798 |
"Altsys Metamorphosis:", |
| 799 |
"Altsys Fontographer", |
| 800 |
"Macromedia Fontographer", |
| 801 |
"Fontmaker" |
| 802 |
}; |
| 803 |
|
| 804 |
static void |
| 805 |
_cleanup_version_string (JsonObject *json_obj, const gchar *ch) |
| 806 |
{ |
| 807 |
const gchar *version = json_object_get_string_member(json_obj, "version"); |
| 808 |
if (g_strrstr(version, ch) != NULL) { |
| 809 |
gchar ** str_arr = g_strsplit(version, ch, 0); |
| 810 |
int i = 0; |
| 811 |
while (str_arr[i] != NULL) { |
| 812 |
/* Take the first thing that even looks like a double to be a version number */ |
| 813 |
if (g_strrstr(str_arr[i], ".") != NULL) { |
| 814 |
json_object_set_string_member(json_obj, "version", g_strstrip(str_arr[i])); |
| 815 |
break; |
| 816 |
} |
| 817 |
i++; |
| 818 |
} |
| 819 |
g_strfreev(str_arr); |
| 820 |
} |
| 821 |
return; |
| 822 |
} |
| 823 |
|
| 824 |
static void |
| 825 |
cleanup_version_string (JsonObject *json_obj) |
| 826 |
{ |
| 827 |
const gchar **excludes = VERSION_STRING_EXCLUDES; |
| 828 |
const gchar *version = json_object_get_string_member(json_obj, "version"); |
| 829 |
|
| 830 |
for (guint i = 0; i < G_N_ELEMENTS(VERSION_STRING_EXCLUDES); i++) { |
| 831 |
if (g_strrstr(version, excludes[i]) != NULL) { |
| 832 |
g_autofree gchar *res = font_manager_str_replace(version, excludes[i], ""); |
| 833 |
if (res) { |
| 834 |
json_object_set_string_member(json_obj, "version", g_strstrip(res)); |
| 835 |
version = json_object_get_string_member(json_obj, "version"); |
| 836 |
} |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
_cleanup_version_string(json_obj, ";"); |
| 841 |
_cleanup_version_string(json_obj, ":"); |
| 842 |
return; |
| 843 |
} |
| 844 |
|