Mercurial > molko
diff libmlk-core/core/font.h @ 243:71b3b7036de7
misc: lot of cleanups,
- prefix libraries with libmlk,
- move assets from source directories closes #2520,
- prefix header guards closes #2519
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 28 Nov 2020 22:37:30 +0100 |
parents | libcore/core/font.h@dd7c8d4321a3 |
children | c4da052c0def |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libmlk-core/core/font.h Sat Nov 28 22:37:30 2020 +0100 @@ -0,0 +1,153 @@ +/* + * font.h -- basic font management + * + * Copyright (c) 2020 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MOLKO_CORE_FONT_H +#define MOLKO_CORE_FONT_H + +/** + * \file font.h + * \brief Basic font management. + * \ingroup drawing + */ + +#include <stdbool.h> +#include <stddef.h> + +struct texture; + +/** + * \brief Font style for rendering. + */ +enum font_style { + FONT_STYLE_ANTIALIASED, /*!< Pretty antialiasing looking. */ + FONT_STYLE_NONE /*!< No antialiasing. */ +}; + +/** + * \brief Font object. + */ +struct font { + enum font_style style; /*!< (+) Style for rendering. */ + void *handle; /*!< (*) 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 False on errors. + */ +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 False on errors. + */ +bool +font_openmem(struct font *font, const void *buffer, size_t buflen, unsigned int size); + +/** + * Convenient shortcut to shallow copy src into dst. + * + * Use this function when you want your own local font to update its attributes + * without changing the calling site. + * + * This is a shortcut to `memcpy(dst, src, sizeof (*src))`. + * + * \pre dst != NULL + * \param dst the destination font + * \param src the source font (may be NULL) + * \note Internal handle points to the same pointer, do not use \ref font_finish + * on the destination. + */ +void +font_shallow(struct font *dst, const struct font *src); + +/** + * Tells if the font was properly opened. + * + * \param font the font to check (may be NULL) + * \return True if the native handle was opened. + */ +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_ok(font) + * \pre tex != NULL + * \param font the font to use + * \param tex the texture to generate + * \param text the UTF-8 text + * \param color the foreground color + * \return False on errors. + */ +bool +font_render(struct font *font, struct texture *tex, const char *text, unsigned int color); + +/** + * Get the maximum height for all glyphs. + * + * \pre font_ok(font) + * \param font the font handle + * \return the height in pixels + */ +unsigned int +font_height(const struct font *font); + +/** + * Compute the text size required with this font. + * + * \pre font_ok(font) + * \pre text != NULL + * \param font the font object + * \param text the UTF-8 text + * \param w pointer to width (may be NULL) + * \param h pointer to height (may be NULL) + * \return False in case of error and pointers to w and h are left unmodified. + */ +bool +font_query(const struct font *font, const char *text, unsigned int *w, unsigned int *h); + +/** + * Close the font. + * + * \pre font != NULL + * \param font the font handle + */ +void +font_finish(struct font *font); + +#endif /* !MOLKO_CORE_FONT_H */