Skip to content

Commit a910cb8

Browse files
committed
Use enum for font styles instead of macros
There are four supported font styles, originally defined using macros. Switching to enum for these identifiers improves code clarity, self- documentation, and ensures type safety when working with related constants.
1 parent 32f7a84 commit a910cb8

File tree

10 files changed

+26
-24
lines changed

10 files changed

+26
-24
lines changed

apps/calc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ static const char *apps_calc_labels[] = {
7373
};
7474

7575
#define APPS_CALC_VALUE_SIZE twin_int_to_fixed(29)
76-
#define APPS_CALC_VALUE_STYLE TWIN_TEXT_ROMAN
76+
#define APPS_CALC_VALUE_STYLE TwinStyleRoman
7777
#define APPS_CALC_VALUE_FG 0xff000000
7878
#define APPS_CALC_VALUE_BG 0x80808080
7979
#define APPS_CALC_BUTTON_SIZE twin_int_to_fixed(15)
80-
#define APPS_CALC_BUTTON_STYLE TWIN_TEXT_BOLD
80+
#define APPS_CALC_BUTTON_STYLE TwinStyleBold
8181
#define APPS_CALC_BUTTON_FG 0xff000000
8282
#define APPS_CALC_BUTTON_BG 0xc0808080
8383

apps/clock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void _apps_clock_date(apps_clock_t *clock, struct tm *t)
103103
twin_path_rotate(path, TWIN_ANGLE_90);
104104
twin_path_translate(path, D(0.8), 0);
105105
twin_path_set_font_size(path, D(0.25));
106-
twin_path_set_font_style(path, TWIN_TEXT_UNHINTED);
106+
twin_path_set_font_style(path, TwinStyleUnhinted);
107107
twin_text_metrics_utf8(path, text, &metrics);
108108
height = metrics.ascent + metrics.descent;
109109
width = metrics.right_side_bearing - metrics.left_side_bearing;
@@ -133,7 +133,7 @@ static void _apps_clock_face(apps_clock_t *clock)
133133
APPS_CLOCK_BORDER_WIDTH);
134134

135135
twin_path_set_font_size(path, D(0.2));
136-
twin_path_set_font_style(path, TWIN_TEXT_UNHINTED);
136+
twin_path_set_font_style(path, TwinStyleUnhinted);
137137

138138
for (m = 1; m <= 60; m++) {
139139
twin_state_t state = twin_path_save(path);

apps/hello.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static twin_time_t _apps_hello_timeout(twin_time_t maybe_unused now,
2020

2121
*strchr(t, '\n') = '\0';
2222
twin_label_set(labelb, t, 0xff008000, twin_int_to_fixed(12),
23-
TWIN_TEXT_OBLIQUE);
23+
TwinStyleOblique);
2424
return 1000;
2525
}
2626

@@ -34,14 +34,14 @@ void apps_hello_start(twin_screen_t *screen,
3434
twin_toplevel_t *top = twin_toplevel_create(
3535
screen, TWIN_ARGB32, TwinWindowApplication, x, y, w, h, name);
3636
twin_label_t *labela = twin_label_create(
37-
&top->box, name, 0xff000080, twin_int_to_fixed(12), TWIN_TEXT_ROMAN);
37+
&top->box, name, 0xff000080, twin_int_to_fixed(12), TwinStyleRoman);
3838
twin_widget_t *widget =
3939
twin_widget_create(&top->box, 0xff800000, 1, 2, 0, 0);
4040
twin_label_t *labelb = twin_label_create(
41-
&top->box, name, 0xff008000, twin_int_to_fixed(12), TWIN_TEXT_OBLIQUE);
41+
&top->box, name, 0xff008000, twin_int_to_fixed(12), TwinStyleOblique);
4242
twin_button_t *button = twin_button_create(
4343
&top->box, "Button", 0xff800000, twin_int_to_fixed(18),
44-
TWIN_TEXT_BOLD | TWIN_TEXT_OBLIQUE);
44+
TwinStyleBold | TwinStyleOblique);
4545
twin_widget_set(&labela->widget, 0xc0c0c0c0);
4646
(void) widget;
4747
twin_widget_set(&labelb->widget, 0xc0c0c0c0);

apps/multi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static void apps_circletext_start(twin_screen_t *screen,
5151
twin_fill(pixmap, 0xffffffff, TWIN_SOURCE, 0, 0, wid, hei);
5252
twin_window_set_name(window, "circletext");
5353

54-
twin_path_set_font_style(path, TWIN_TEXT_UNHINTED);
54+
twin_path_set_font_style(path, TwinStyleUnhinted);
5555
twin_path_circle(pen, 0, 0, D(1));
5656

5757
twin_path_translate(path, D(200), D(200));
@@ -193,7 +193,7 @@ static void apps_jelly_start(twin_screen_t *screen, int x, int y, int w, int h)
193193
fy += D(s + 2);
194194
twin_path_move(path, fx, fy);
195195
#define TEXT "jelly text"
196-
/* twin_path_set_font_style (path, TWIN_TEXT_UNHINTED); */
196+
/* twin_path_set_font_style (path, TwinStyleUnhinted); */
197197
twin_path_utf8(path, TEXT);
198198
twin_paint_path(pixmap, 0xff000000, path);
199199
twin_path_empty(path);

include/twin.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ typedef uint16_t twin_rgb16_t;
1616
typedef uint32_t twin_argb32_t;
1717
typedef uint32_t twin_ucs4_t;
1818
typedef int16_t twin_coord_t;
19-
typedef int16_t twin_style_t;
2019
typedef int16_t twin_count_t;
2120
typedef int16_t twin_keysym_t;
2221
typedef uint8_t twin_js_number_t;
@@ -321,6 +320,14 @@ typedef struct _twin_point {
321320

322321
typedef struct _twin_path twin_path_t;
323322

323+
typedef enum _twin_style {
324+
TwinStyleRoman = 0,
325+
TwinStyleBold = 1,
326+
TwinStyleOblique = 2,
327+
TwinStyleBoldOblique = 3,
328+
TwinStyleUnhinted = 4,
329+
} twin_style_t;
330+
324331
typedef enum _twin_cap {
325332
TwinCapRound,
326333
TwinCapButt,
@@ -673,11 +680,6 @@ twin_fixed_t twin_fixed_sqrt(twin_fixed_t a);
673680

674681
bool twin_has_ucs4(twin_font_t *font, twin_ucs4_t ucs4);
675682

676-
#define TWIN_TEXT_ROMAN 0
677-
#define TWIN_TEXT_BOLD 1
678-
#define TWIN_TEXT_OBLIQUE 2
679-
#define TWIN_TEXT_UNHINTED 4
680-
681683
void twin_path_ucs4_stroke(twin_path_t *path, twin_ucs4_t ucs4);
682684

683685
void twin_path_ucs4(twin_path_t *path, twin_ucs4_t ucs4);

src/draw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ static twin_argb32_t _twin_apply_alpha(twin_argb32_t v)
696696
if (!alpha)
697697
return 0;
698698

699-
/* twin needs ARGB format */
699+
/* twin needs ARGB format */
700700
#if __BYTE_ORDER == __BIG_ENDIAN
701701
return alpha << 24 | twin_int_mult(twin_get_8(v, 24), alpha, t1) << 16 |
702702
twin_int_mult(twin_get_8(v, 16), alpha, t2) << 8 |

src/font.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void _twin_text_compute_info(twin_path_t *path,
3434
/*
3535
* Only hint axis aligned text
3636
*/
37-
if ((path->state.font_style & TWIN_TEXT_UNHINTED) == 0 &&
37+
if ((path->state.font_style & TwinStyleUnhinted) == 0 &&
3838
((path->state.matrix.m[0][1] == 0 && path->state.matrix.m[1][0] == 0) ||
3939
(path->state.matrix.m[0][0] == 0 &&
4040
path->state.matrix.m[1][1] == 0))) {
@@ -94,7 +94,7 @@ static void _twin_text_compute_info(twin_path_t *path,
9494
info->margin.x = info->pen.x;
9595
info->margin.y = info->pen.y;
9696
if (font->type == TWIN_FONT_TYPE_STROKE &&
97-
(path->state.font_style & TWIN_TEXT_BOLD)) {
97+
(path->state.font_style & TwinStyleBold)) {
9898
twin_fixed_t pen_x_add;
9999
twin_fixed_t pen_y_add;
100100

@@ -126,7 +126,7 @@ static void _twin_text_compute_info(twin_path_t *path,
126126
info->pen.x = info->pen.y = 0;
127127
info->margin.x = info->margin.y = 0;
128128
} else {
129-
if (path->state.font_style & TWIN_TEXT_BOLD)
129+
if (path->state.font_style & TwinStyleBold)
130130
info->pen.x = path->state.font_size / 16;
131131
else
132132
info->pen.x = path->state.font_size / 24;
@@ -143,7 +143,7 @@ static void _twin_text_compute_info(twin_path_t *path,
143143
info->pen_matrix.m[2][1] = 0;
144144
twin_matrix_scale(&info->pen_matrix, info->pen.x, info->pen.y);
145145

146-
if (path->state.font_style & TWIN_TEXT_OBLIQUE) {
146+
if (path->state.font_style & TwinStyleOblique) {
147147
twin_matrix_t m;
148148

149149
m.m[0][0] = TWIN_FIXED_ONE;

src/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int log_add_callback(log_func_t fn, void *udata, int level)
115115
{
116116
for (int i = 0; i < MAX_CALLBACKS; i++) {
117117
if (!L.callbacks[i].fn) {
118-
L.callbacks[i] = (callback_t){fn, udata, level};
118+
L.callbacks[i] = (callback_t) {fn, udata, level};
119119
return 0;
120120
}
121121
}

src/path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ twin_path_t *twin_path_create(void)
440440
path->sublen = 0;
441441
twin_matrix_identity(&path->state.matrix);
442442
path->state.font_size = TWIN_FIXED_ONE * 15;
443-
path->state.font_style = TWIN_TEXT_ROMAN;
443+
path->state.font_style = TwinStyleRoman;
444444
path->state.cap_style = TwinCapRound;
445445
return path;
446446
}

src/window.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static void twin_window_frame(twin_window_t *window)
199199
if (!name)
200200
name = "twin";
201201
twin_path_set_font_size(path, name_height);
202-
twin_path_set_font_style(path, TWIN_TEXT_OBLIQUE | TWIN_TEXT_UNHINTED);
202+
twin_path_set_font_style(path, TwinStyleOblique | TwinStyleUnhinted);
203203
text_width = twin_width_utf8(path, name);
204204

205205
title_right = (text_x + text_width + bw + icon_size + bw + icon_size + bw +

0 commit comments

Comments
 (0)