From c81ae185d8740b498943345596441efdffa04e40 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 12 Jan 2023 20:58:59 +0100 Subject: Add support for FILE_NAMES_ALIASES MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libfonts.h | 56 +++++++++++++++++++++++++++++++++++------ libfonts_parse_alias_line.c | 61 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 97 insertions(+), 20 deletions(-) diff --git a/libfonts.h b/libfonts.h index 779ea9e..242b187 100644 --- a/libfonts.h +++ b/libfonts.h @@ -47,6 +47,48 @@ enum libfonts_default_font { }; +/** + * Alias font list file type + */ +enum libfonts_alias_line_type { + /** + * Improperly formatted line + */ + LIBFONTS_ALIAS_LINE_MALFORMATTED, + + /** + * Empty or white-space only line + */ + LIBFONTS_ALIAS_LINE_BLANK, + + /** + * Comment line + */ + LIBFONTS_ALIAS_LINE_COMMENT, + + /** + * Alias definition line + */ + LIBFONTS_ALIAS_LINE_ALIAS_DEFINITION, + + /** + * Line containing just the string ”FILE_NAMES_ALIASES” + * indicating that the file name, minus its suffix, of + * each font file, in the same directory as the font alias + * file, should be set as an alias for the font contained + * in that file + * + * For example, if the director contains the files + * "My Font.ttf" and "My Font Bold.ttf", the font names + * "My Font" and "My Font Bold" should be aliases for the + * two fonts, whose proper names are determined by their + * content and ought to be listed in the file "fonts.dir" + * in the same directory + */ + LIBFONTS_ALIAS_LINE_FONT_NAMES_ALIASES +}; + + /** * Antialiasing algorithm */ @@ -1689,6 +1731,7 @@ int libfonts_get_font_root_dirs(char ***, size_t *, struct libfonts_context *); * and are located in any font directory, i.e. directories returned by * `libfonts_get_font_root_dirs` subdirectors (and further decedents) * + * @param typep Output parameter fot the line's type * @param aliasp Output parameter for the new alias * @param namep Output parameter for the alias target, * which can be ether a proper XFDL (font name), @@ -1697,17 +1740,16 @@ int libfonts_get_font_root_dirs(char ***, size_t *, struct libfonts_context *); * newline or NUL byte * @param endp Output parameter for the parsing end, i.e. the * first newline or NUL byte in `line` - * @return 1 if the line included a font alias, - * 0 if the line was blank or a comment line, - * -1 on failure + * @return 0 on success, -1 on failure * * @throws ENOMEM Failed to allocate memory for `*aliasp` or `*namep` - * @throws EBADMSG The line is malformatted * - * `*aliasp` and `*namep` are set to `NULL` unless the function - * returns 1; `*endp` is updated even on failure + * Unless `*typep` is set to (or would be set to) `LIBFONTS_ALIAS_LINE_ALIAS_DEFINITION`, + * `*aliasp` and `*namep` are set to `NULL`. On failure, `*aliasp` and `*namep` are set + * to `NULL` and `*typep` is seto to `LIBFONTS_ALIAS_LINE_BLANK`. `*endp` is updated even + * on failure. */ -int libfonts_parse_alias_line(char **, char **, const char *, char **); +int libfonts_parse_alias_line(enum libfonts_alias_line_type *, char **, char **, const char *, char **); /* TODO add font listing */ diff --git a/libfonts_parse_alias_line.c b/libfonts_parse_alias_line.c index 453ea51..0496449 100644 --- a/libfonts_parse_alias_line.c +++ b/libfonts_parse_alias_line.c @@ -4,7 +4,7 @@ static int -read_field(char **valuep, const char *s, const char **endp) +read_field(enum libfonts_alias_line_type *typep, char **valuep, const char *s, const char **endp) { char end1 = ' '; char end2 = '\t'; @@ -27,15 +27,15 @@ read_field(char **valuep, const char *s, const char **endp) esc += 1; s++; if (!*s || *s == '\n') - goto ebadmsg; + goto badline; } s++; } end = s; len = (size_t)(end - start) - esc; if (end1 == '"' && *s != '"') { - ebadmsg: - errno = EBADMSG; + badline: + *typep = LIBFONTS_ALIAS_LINE_MALFORMATTED; *endp = s; return -1; } @@ -60,9 +60,25 @@ read_field(char **valuep, const char *s, const char **endp) return 0; } +static int +is_file_names_aliases(const char *line, const char **endp) +{ + if (strncmp(line, "FILE_NAMES_ALIASES", sizeof("FILE_NAMES_ALIASES") - 1)) + return 0; + line = &line[sizeof("FILE_NAMES_ALIASES") - 1]; + while (isblank(*line)) + line++; + if (!*line || *line == '\n') { + *endp = line; + return 1; + } + return 0; +} + int -libfonts_parse_alias_line(char **aliasp, char **namep, const char *line, char **endp) +libfonts_parse_alias_line(enum libfonts_alias_line_type *typep, char **aliasp, char **namep, const char *line, char **endp) { + enum libfonts_alias_line_type type = LIBFONTS_ALIAS_LINE_BLANK; int ret = 0; if (aliasp) @@ -72,38 +88,57 @@ libfonts_parse_alias_line(char **aliasp, char **namep, const char *line, char ** while (isblank(*line)) line++; - if (!*line || *line == '!') + if (!*line) + goto out; + if (*line == '!') { + type = LIBFONTS_ALIAS_LINE_COMMENT; goto out; + } + + if (is_file_names_aliases(line, &line)) { + type = LIBFONTS_ALIAS_LINE_FONT_NAMES_ALIASES; + goto out; + } - if (read_field(aliasp, line, &line)) + if (read_field(&type, aliasp, line, &line)) goto fail; if (!isblank(*line)) - goto ebadmsg; + goto badline; do { line++; } while (isblank(*line)); - if (read_field(namep, line, &line)) + if (read_field(&type, namep, line, &line)) goto fail; + type = LIBFONTS_ALIAS_LINE_ALIAS_DEFINITION; while (isblank(*line)) line++; if (*line && *line != '\n') - goto ebadmsg; + goto badline; - ret = 1; goto out; -ebadmsg: - errno = EBADMSG; +badline: + type = LIBFONTS_ALIAS_LINE_MALFORMATTED; fail: + if (aliasp) { + free(*aliasp); + *aliasp = NULL; + } + if (namep) { + free(*namep); + *namep = NULL; + } ret = -1; while (*line && *line != '\n') line++; out: if (endp) *endp = *(char **)(void *)&line; + if (typep) + *typep = type; return ret; } -- cgit v1.2.3-70-g09d2