diff src/core/font.h @ 94:ed72843a7194

core: simplify font/texture interfaces Expose the structure to avoid calling functions and heap allocations. While here remove some useless functions.
author David Demelier <markand@malikania.fr>
date Mon, 30 Mar 2020 13:34:42 +0200
parents 8f95462ac5f8
children
line wrap: on
line diff
--- a/src/core/font.h	Thu Mar 26 10:28:11 2020 +0100
+++ b/src/core/font.h	Mon Mar 30 13:34:42 2020 +0200
@@ -25,70 +25,81 @@
  * \ingroup drawing
  */
 
+#include <stdbool.h>
 #include <stddef.h>
 
-/**
- * \brief Font object.
- *
- * This object is not publicly defined because it contains
- * implementation-defined data.
- */
-struct font;
 struct texture;
 
 /**
  * \brief Font style for rendering.
  */
 enum font_style {
-	FONT_STYLE_NONE,                /*!< No antialiasing. */
-	FONT_STYLE_ANTIALIASED          /*!< Pretty antialiasing looking. */
+	FONT_STYLE_ANTIALIASED,         /*!< Pretty antialiasing looking. */
+	FONT_STYLE_NONE                 /*!< No antialiasing. */
+};
+
+/**
+ * \brief Font object.
+ */
+struct font {
+	enum font_style style;          /*!< (RW) Style for rendering. */
+	unsigned long color;            /*!< (RW) Color for rendering. */
+	void *handle;                   /*!< (RO) Native handle. */
 };
 
 /**
  * Open font from path file.
  *
+ * \pre font != NULL
  * \pre path != NULL
+ * \param font the font to initialize
  * \param path the path to the font
  * \param size the desired size
- * \return the font or NULL on error
+ * \return False on errors.
  */
-struct font *
-font_openf(const char *path, unsigned int size);
+bool
+font_open(struct font *font, const char *path, unsigned int size);
 
 /**
  * Open font from memory buffer.
  *
+ * \pre font != NULL
  * \pre buffer != NULL
+ * \param font the font to initialize
  * \param buffer the memory buffer
  * \param buflen the memory buffer length
  * \param size the desired size
  * \warning The buffer must remain valid until font is closed
- * \return the font or NULL on error
+ * \return False on errors.
  */
-struct font *
-font_openb(const void *buffer, size_t buflen, unsigned int size);
-
-/**
- * Similar to \ref font_render_ex with predefined convenient values.
- */
-struct texture *
-font_render(struct font *font, const char *text, unsigned long color);
+bool
+font_openmem(struct font *font, const void *buffer, size_t buflen, unsigned int size);
 
 /**
- * Render a text.
+ * Tells if the font was properly opened.
  *
  * \pre font != NULL
- * \pre text != NULL
- * \param font the font handle
- * \param text the text in UTF-8
- * \param color the color
- * \param style the font style
+ * \param font the font to check
+ * \return True if the native handle was opened.
  */
-struct texture *
-font_render_ex(struct font *font,
-               const char *text,
-               unsigned long color,
-               enum font_style style);
+bool
+font_ok(const struct font *font);
+
+/**
+ * Render some text into the texture.
+ *
+ * This function use the current color/style and other properties in the font
+ * to render the texture.
+ *
+ * \pre font != NULL
+ * \pre tex != NULL
+ * \param font the font to use
+ * \param tex the texture to generate
+ * \param text the UTF-8 text
+ * \return False on errors.
+ */
+bool
+font_render(struct font *font, struct texture *tex, const char *text);
 
 /**
  * Get the maximum height for all glyphs.
@@ -107,6 +118,6 @@
  * \param font the font handle
  */
 void
-font_close(struct font *font);
+font_finish(struct font *font);
 
 #endif /* !MOLKO_FONT_H */