GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_widget_tattr.h
1//SPDX-License-Identifier: LGPL-2.0-or-later
2/*
3
4 Copyright (c) 2014-2021 Cyril Hrubis <metan@ucw.cz>
5
6 */
7
8#ifndef GP_WIDGET_TATTR_H
9#define GP_WIDGET_TATTR_H
10
11#include <text/gp_text.h>
12#include <widgets/gp_widget_types.h>
13#include <widgets/gp_widget_render.h>
14
15enum gp_widget_tattr {
16 /* Fonts */
17 GP_TATTR_BOLD = 0x10,
18 GP_TATTR_LARGE = 0x20,
19 /* Monospace does not support large */
20 GP_TATTR_MONO = 0x40,
21 GP_TATTR_FONT = 0x70,
22
23 /* Horizontal alignment */
24 GP_TATTR_LEFT = 0x01,
25 GP_TATTR_CENTER = 0x02,
26 GP_TATTR_RIGHT = 0x03,
27 GP_TATTR_HALIGN = 0x03,
28};
29
30/*
31 * @brief Returns text font based on text attributes and render context.
32 */
33static inline const gp_text_style *gp_widget_tattr_font(gp_widget_tattr attr, const gp_widget_render_ctx *ctx)
34{
35 if (attr & GP_TATTR_MONO) {
36 if (attr & GP_TATTR_BOLD)
37 return ctx->font_mono_bold;
38 else
39 return ctx->font_mono;
40 }
41
42 if (attr & GP_TATTR_BOLD) {
43 if (attr & GP_TATTR_LARGE)
44 return ctx->font_big_bold;
45 else
46 return ctx->font_bold;
47 }
48
49 if (attr & GP_TATTR_LARGE)
50 return ctx->font_big;
51
52 return ctx->font;
53}
54
55static inline int gp_widget_tattr_halign(gp_widget_tattr attr)
56{
57 return attr & GP_TATTR_HALIGN;
58}
59
60/*
61 * @brief Parses an attributes encoded in a string into attribute bitflags.
62 *
63 * The attributes are separated by | e.g. valid attributes string is "bold|large".
64 *
65 * @attrs Text attributes description string.
66 * @tattr A pointer to tattr to be filled in.
67 * @flags A bitwise or of GP_TATTR_FONT and GP_TATTR_HALIGN that enables
68 * parsing font and/or alignment.
69 * @return Zero on success, non-zero on parsing failure.
70 */
71int gp_widget_tattr_parse(const char *attrs, gp_widget_tattr *tattr, int flags);
72
73#endif /* GP_WIDGET_TATTR_H */