aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2022-07-22 19:52:07 +0200
committerMattias Andrée <maandree@kth.se>2022-07-22 19:52:07 +0200
commit8f015537e7c016722dbd7fffee520461fe394f28 (patch)
tree9289ac5cfc9e95550169d346a23eaf64ce81c8f0
parentAdd LIBFONTS_ANTIALIASING_GREYSCALE_FORCED (diff)
downloadlibfonts-8f015537e7c016722dbd7fffee520461fe394f28.tar.gz
libfonts-8f015537e7c016722dbd7fffee520461fe394f28.tar.bz2
libfonts-8f015537e7c016722dbd7fffee520461fe394f28.tar.xz
Add default fonts
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--Makefile1
-rw-r--r--libfonts.h88
-rw-r--r--libfonts_get_default_font_name.c49
3 files changed, 138 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index 97a3499..74bb9e1 100644
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,7 @@ OBJ =\
libfonts_calculate_subpixel_order.o\
libfonts_decode_font_description.o\
libfonts_encode_font_description.o\
+ libfonts_get_default_font_name.o\
libfonts_get_output_dpi.o
HDR =\
diff --git a/libfonts.h b/libfonts.h
index 2d8e94d..5cf2c83 100644
--- a/libfonts.h
+++ b/libfonts.h
@@ -3,6 +3,29 @@
#define LIBFONTS_H
#include <stdint.h>
+#include <unistd.h>
+
+
+/**
+ * Style-based default fonts
+ */
+enum libfonts_default_font {
+ /**
+ * A proportional font without serifs
+ */
+ LIBFONTS_DEFAULT_SANS_SERIF,
+
+ /**
+ * A proportional font with serifs
+ */
+ LIBFONTS_DEFAULT_SERIF,
+
+ /**
+ * A monospace font, which may escape
+ * the character cell
+ */
+ LIBFONTS_DEFAULT_MONOSPACE
+};
/**
@@ -506,6 +529,71 @@ struct libfonts_font_description {
/**
+ * Get the font a default font name aliases to
+ *
+ * @param font The default font
+ * @param name Output buffer for the font the default font is an alias
+ * for; will always be NUL-terminated (unless `size` is 0)
+ * @param size Buffer size of `name`
+ * @return The minimum value required on `size` for a
+ * complete output to `name`, or -1 on failure
+ *
+ * @throws EINVAL Unrecognised value on `font`
+ */
+ssize_t libfonts_get_default_font(enum libfonts_default_font, char *, size_t);
+/* TODO implement libfonts_get_default_font
+ *
+ * /etc/libfonts/default-fonts.conf
+ *
+ * sans-serif = $FONTNAME
+ * serif = $FONTNAME
+ * monospace = $FONTNAME
+ *
+ * fallback, look in /etc/libfonts/default-fonts/sans-serif/
+ * /etc/libfonts/default-fonts/serif/
+ * /etc/libfonts/default-fonts/monospace/
+ *
+ * as a last resort look around for some
+ * font that matches the specification as
+ * well as possible
+ */
+
+/**
+ * Get the alias for a default font
+ *
+ * @param font The default font
+ * @param name Output buffer for the font name; will always
+ * be NUL-terminated (unless `size` is 0)
+ * @param size Buffer size of `name`
+ * @param index The index of the alias (some default fonts
+ * have multiple aliases)
+ * @return The minimum value required on `size` for a
+ * complete output to `name`, 0 if `index` is
+ * equal to or greater than the number of
+ * aliases, or -1 on failure
+ *
+ * @throws EINVAL Unrecognised value on `font`
+ */
+ssize_t libfonts_get_default_font_name(enum libfonts_default_font, char *, size_t, size_t);
+
+/**
+ * Get a default font a font name represents
+ *
+ * @param font Output parameter for the default font
+ * @param name The alias for the default font, optionally
+ * with style and size information
+ * @param style Output parameter for the offset in `name`
+ * where the style and size information begins;
+ * will be set to 0 if this information is
+ * excluded from `name`
+ * @return 1 on successful completion, 0 if `name` is not
+ * an alias for a default font
+ */
+int libfonts_get_default_font_by_name(enum libfonts_default_font *, const char *, size_t *);
+/* TODO implement libfonts_get_default_font_by_name */
+
+
+/**
* Encode an X Logical Font Description (XLFD) string
*
* @param desc The font description to encode
diff --git a/libfonts_get_default_font_name.c b/libfonts_get_default_font_name.c
new file mode 100644
index 0000000..5fe7704
--- /dev/null
+++ b/libfonts_get_default_font_name.c
@@ -0,0 +1,49 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+ssize_t
+libfonts_get_default_font_name(enum libfonts_default_font font, char *name, size_t size, size_t index)
+{
+ const char *alias;
+ size_t len;
+
+ if (font == LIBFONTS_DEFAULT_SANS_SERIF) {
+ if (index == 0)
+ alias = "sans-serif";
+ else if (index == 1)
+ alias = "sans serif";
+ else if (index == 2)
+ alias = "sans";
+ else
+ return 0;
+
+ } else if (font == LIBFONTS_DEFAULT_SERIF) {
+ if (index == 0)
+ alias = "serif";
+ else
+ return 0;
+
+ } else if (font == LIBFONTS_DEFAULT_MONOSPACE) {
+ if (index == 0)
+ alias = "monospace";
+ else if (index == 1)
+ alias = "monospaced";
+ else
+ return 0;
+
+ } else {
+ errno = EINVAL;
+ return -1;
+ }
+
+ len = strlen(alias);
+ if (size) {
+ size -= 1;
+ if (size > len)
+ size = len;
+ memcpy(name, alias, size);
+ name[size] = '\0';
+ }
+ return (ssize_t)len;
+}