aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2021-11-11 21:04:20 +0100
committerMattias Andrée <maandree@kth.se>2021-11-11 21:04:20 +0100
commitc5c011296aef1a194d090f494c6a873756529123 (patch)
treed0206c56b2213de583a5420664a189e6b219fbae
parentStore DPI in floating point instead of deci-integer + implement libfonts_get_output_dpi (diff)
downloadlibfonts-c5c011296aef1a194d090f494c6a873756529123.tar.gz
libfonts-c5c011296aef1a194d090f494c6a873756529123.tar.bz2
libfonts-c5c011296aef1a194d090f494c6a873756529123.tar.xz
Implement libfonts_encode_font_description
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--Makefile1
-rw-r--r--libfonts.h2
-rw-r--r--libfonts_encode_font_description.c105
3 files changed, 107 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index dae5d3a..53e9215 100644
--- a/Makefile
+++ b/Makefile
@@ -18,6 +18,7 @@ LIB_NAME = fonts
OBJ =\
libfonts_calculate_subpixel_order.o\
+ libfonts_encode_font_description.o\
libfonts_get_output_dpi.o
HDR =\
diff --git a/libfonts.h b/libfonts.h
index a31a5e9..b7a1e43 100644
--- a/libfonts.h
+++ b/libfonts.h
@@ -309,7 +309,7 @@ struct libfonts_font_description {
int libfonts_encode_font_description(const struct libfonts_font_description *, char[static 256]);
int libfonts_decode_font_description(struct libfonts_font_description *, const char *);
-int libfonts_do_font_descriptions_match(const char *, const char *);
+int libfonts_do_font_descriptions_match(const char * /* no wildcards */, const char * /* wildcards allowed */);
int libfonts_get_default_rendering_settings(struct libfonts_rendering_settings *);
int libfonts_get_output_rendering_settings(struct libfonts_rendering_settings *, const char *);
diff --git a/libfonts_encode_font_description.c b/libfonts_encode_font_description.c
new file mode 100644
index 0000000..8e0d87a
--- /dev/null
+++ b/libfonts_encode_font_description.c
@@ -0,0 +1,105 @@
+/* See LICENSE file for copyright and license details. */
+#include "libfonts.h"
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+
+#define LIST_FIELDS(X)\
+ X(foundry)\
+ X(family_name)\
+ X(weight_name)\
+ X(slant)\
+ X(setwidth_name)\
+ X(add_style_name)\
+ X(pixel_size)\
+ X(point_size)\
+ X(resolution_x)\
+ X(resolution_y)\
+ X(spacing)\
+ X(average_width)\
+ X(charset_registry)\
+ X(charset_encoding)
+
+
+static char *
+encode(char *out, const char *s)
+{
+ for (; *s; s++)
+ *out++ = (*s == '-' ? '~' : *s);
+ return out;
+}
+
+static char *
+encode_range(char *out, const char *s)
+{
+ if (*s == '-') {
+ *out++ = '~';
+ s++;
+ }
+ for (; *s; s++)
+ *out++ = (*s == '-' ? isdigit(s[-1]) ? '_' : '~' : *s);
+ return out;
+}
+
+int
+libfonts_encode_font_description(const struct libfonts_font_description *desc, char out[static 256])
+{
+ size_t n = 1;
+ char *p;
+
+ if (desc->private_font_name) {
+ if (strlen(desc->private_font_name) > 255)
+ goto einval;
+ stpcpy(out, desc->private_font_name);
+ return 0;
+ }
+
+ if (desc->xlfd_version)
+ n += strlen(desc->xlfd_version) + 1;
+
+#define X(F)\
+ if (!desc->F)\
+ goto einval;\
+ n += strlen(desc->F) + 1;
+ LIST_FIELDS(X)
+#undef X
+
+ if (desc->charset_subset)
+ n += strlen(desc->charset_subset) + 2;
+
+ if (desc->unrecognised_fields)
+ n += strlen(desc->unrecognised_fields) + 1;
+
+ if (n > 255)
+ goto einval;
+
+ p = out;
+ if (desc->xlfd_version) {
+ *p++ = '+';
+ p = encode(p, desc->xlfd_version);
+ }
+
+#define X(F)\
+ *p++ = '-';\
+ p = encode(p, desc->F);
+ LIST_FIELDS(X)
+#undef X
+
+ if (desc->charset_subset) {
+ *p++ = '[';
+ p = encode_range(p, desc->charset_subset);
+ *p++ = ']';
+ }
+
+ if (desc->unrecognised_fields) {
+ *p++ = '-';
+ p = stpcpy(p, desc->unrecognised_fields);
+ }
+
+ *p = '\0';
+ return 0;
+
+einval:
+ errno = EINVAL;
+ return -1;
+}