[theme] lots of progress on fonts

This commit is contained in:
Milo Turner 2020-02-12 12:05:43 -05:00
parent 2e0ce61829
commit 04e282eeba
7 changed files with 108 additions and 10 deletions

View File

@ -29,7 +29,7 @@ int ax_end_theme(struct ax_ctxt* ax, struct ax_theme** out_thm);
int ax_set_theme_color( int ax_set_theme_color(
struct ax_ctxt* ax, const char* cat, int64_t rgb); struct ax_ctxt* ax, const char* cat, int64_t rgb);
int ax_set_theme_font( int ax_set_theme_font(
struct ax_ctxt* ax, const char* cat, const char* fnt_path); struct ax_ctxt* ax, const char* cat, const char* fnt_path, size_t fnt_size);
int ax_set_theme_iconset( int ax_set_theme_iconset(
struct ax_ctxt* ax, const char* iconset_dir); struct ax_ctxt* ax, const char* iconset_dir);

View File

@ -84,7 +84,6 @@ int ax_set_theme_color(
{ {
ASSERT(ax->thmb != NULL, "`ax_set_theme_color' called while not building a theme"); ASSERT(ax->thmb != NULL, "`ax_set_theme_color' called while not building a theme");
int rv; int rv;
enum ax_color_cat cat; enum ax_color_cat cat;
if ((rv = ax__string_to_color_cat(cat_name, &cat)) != 0) { if ((rv = ax__string_to_color_cat(cat_name, &cat)) != 0) {
#define FMT "invalid color category `%s'" #define FMT "invalid color category `%s'"
@ -93,16 +92,25 @@ int ax_set_theme_color(
return rv; return rv;
#undef FMT #undef FMT
} }
ax__theme_set_color(ax->thmb, cat, rgb); ax__theme_set_color(ax->thmb, cat, rgb);
return 0; return 0;
} }
int ax_set_theme_font( int ax_set_theme_font(
struct ax_ctxt* ax, const char* cat, const char* fnt_path) struct ax_ctxt* ax, const char* cat_name, const char* fnt_path, size_t fnt_size)
{ {
ASSERT(ax->thmb != NULL, "`ax_set_theme_font' called while not building a theme"); ASSERT(ax->thmb != NULL, "`ax_set_theme_font' called while not building a theme");
UNIMPLEMENTED(); int rv;
enum ax_font_cat cat;
if ((rv = ax__string_to_font_cat(cat_name, &cat)) != 0) {
#define FMT "invalid font category `%s'"
sprintf(ax__make_error_buf(ax, strlen(FMT) + strlen(cat_name)),
FMT, cat_name);
return rv;
#undef FMT
}
ax__theme_set_font(ax->thmb, cat, fnt_path, fnt_size);
return 0;
} }
int ax_set_theme_iconset( int ax_set_theme_iconset(

View File

@ -15,11 +15,23 @@ static void theme_init(struct ax_theme* thm)
thm->colors[AX_COLOR_ON_SURFACE] = thm->colors[AX_COLOR_ON_SURFACE] =
thm->colors[AX_COLOR_ON_PRIMARY] = thm->colors[AX_COLOR_ON_PRIMARY] =
thm->colors[AX_COLOR_ON_SECONDARY] = 0x000000; thm->colors[AX_COLOR_ON_SECONDARY] = 0x000000;
for (size_t i = 0; i < AX_FONT__COUNT; i++) {
thm->font_path[i] = NULL;
}
thm->font_size[AX_FONT_H1] = 96;
thm->font_size[AX_FONT_H2] = 60;
thm->font_size[AX_FONT_H3] = 34;
thm->font_size[AX_FONT_H4] = 24;
thm->font_size[AX_FONT_BODY] = 16;
thm->font_size[AX_FONT_BUTTON] = 14;
} }
void ax__theme_builder_init(struct ax_theme_builder* thmb) void ax__theme_builder_init(struct ax_theme_builder* thmb)
{ {
theme_init(&thmb->thm); theme_init(&thmb->thm);
thmb->dflt_font_path = NULL;
} }
void ax__theme_builder_cleanup(struct ax_theme_builder* thmb) void ax__theme_builder_cleanup(struct ax_theme_builder* thmb)
@ -28,9 +40,30 @@ void ax__theme_builder_cleanup(struct ax_theme_builder* thmb)
void ax__theme_set_color( void ax__theme_set_color(
struct ax_theme_builder* thmb, struct ax_theme_builder* thmb,
enum ax_color_cat cat, int64_t val) enum ax_color_cat cat,
int64_t val)
{ {
thmb->thm.colors[cat] = val;
}
void ax__theme_set_font(
struct ax_theme_builder* thmb,
enum ax_font_cat cat,
const char* path, size_t size)
{
const char** dst_path;
if (cat == AX_FONT__DEFAULT) {
dst_path = &thmb->dflt_font_path;
} else {
ASSERT(cat < AX_FONT__COUNT, "invalid font category");
dst_path = &thmb->thm.font_path[cat];
thmb->thm.font_size[cat] = size;
}
char* new_path;
UNIMPLEMENTED(); UNIMPLEMENTED();
*dst_path = new_path;
} }
void ax__theme_builder_finish( void ax__theme_builder_finish(
@ -55,7 +88,12 @@ int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat)
return 0; return 0;
} }
void ax__color_cat_to_string(enum ax_color_cat cat, const char** out_str) int ax__string_to_font_cat(const char* str, enum ax_font_cat* out_cat)
{ {
UNIMPLEMENTED(); enum ax_font_cat val;
#include "../../_build/font.parser.inc"
if (out_cat != NULL) {
*out_cat = val;
}
return 0;
} }

View File

@ -8,3 +8,11 @@
(AX_COLOR_ON_SURFACE "on surface") (AX_COLOR_ON_SURFACE "on surface")
(AX_COLOR_ON_PRIMARY "on primary") (AX_COLOR_ON_PRIMARY "on primary")
(AX_COLOR_ON_SECONDARY "on secondary")] (AX_COLOR_ON_SECONDARY "on secondary")]
[font (AX_FONT_H1 "h1")
(AX_FONT_H2 "h2")
(AX_FONT_H3 "h3")
(AX_FONT_H4 "h4")
(AX_FONT_BODY "body")
(AX_FONT_BUTTON "button")
(AX_FONT__DEFAULT "default")]

View File

@ -17,16 +17,31 @@ enum ax_color_cat {
AX_COLOR__COUNT, AX_COLOR__COUNT,
}; };
enum ax_font_cat {
AX_FONT_H1 = 0,
AX_FONT_H2,
AX_FONT_H3,
AX_FONT_H4,
AX_FONT_BODY,
AX_FONT_BUTTON,
AX_FONT__COUNT,
AX_FONT__DEFAULT,
};
struct ax_theme { struct ax_theme {
int64_t colors[AX_COLOR__COUNT]; int64_t colors[AX_COLOR__COUNT];
const char* font_path[AX_FONT__COUNT];
size_t font_size[AX_FONT__COUNT];
}; };
struct ax_theme_builder { struct ax_theme_builder {
struct ax_theme thm; struct ax_theme thm;
const char* dflt_font_path;
}; };
int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat); int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat);
void ax__color_cat_to_string(enum ax_color_cat cat, const char** out_str); int ax__string_to_font_cat(const char* str, enum ax_font_cat* out_cat);
//void ax__color_cat_to_string(enum ax_color_cat cat, const char** out_str);
void ax__theme_builder_init(struct ax_theme_builder* thmb); void ax__theme_builder_init(struct ax_theme_builder* thmb);
void ax__theme_builder_cleanup(struct ax_theme_builder* thmb); void ax__theme_builder_cleanup(struct ax_theme_builder* thmb);
@ -35,7 +50,30 @@ void ax__theme_set_color(
struct ax_theme_builder* thmb, struct ax_theme_builder* thmb,
enum ax_color_cat cat, int64_t val); enum ax_color_cat cat, int64_t val);
void ax__theme_set_font(
struct ax_theme_builder* thmb,
enum ax_font_cat cat,
const char* path, size_t size);
void ax__theme_builder_finish( void ax__theme_builder_finish(
struct ax_theme_builder* thmb, struct ax_theme_builder* thmb,
struct rgn* rgn, struct rgn* rgn,
struct ax_theme** out_thm); struct ax_theme** out_thm);
static inline int64_t ax__theme_color(
struct ax_theme* thm,
enum ax_color_cat i)
{
return thm->colors[i];
}
static inline const char* ax__theme_font(
struct ax_theme* thm,
enum ax_font_cat i,
size_t* out_size)
{
if (out_size != NULL) {
*out_size = thm->font_size[i];
}
return thm->font_path[i];
}

View File

@ -17,3 +17,6 @@
#define UNIMPLEMENTED() \ #define UNIMPLEMENTED() \
ASSERT(0, "not implemented") ASSERT(0, "not implemented")
#define IMPOSSIBLE(what) \
ASSERT(0, "unreachable: " what)

View File

@ -9,7 +9,10 @@ int main(void)
int rv; int rv;
ax_begin_theme(ax); ax_begin_theme(ax);
if ((rv = ax_set_theme_color(ax, "default", 0x112233)) != 0) { if ((rv = ax_set_theme_color(ax, "primary", 0x00ff00)) != 0) {
goto cleanup;
}
if ((rv = ax_set_theme_font(ax, "default", "/usr/share/fonts/TTF/DejaVuSans.ttf", 0)) != 0) {
goto cleanup; goto cleanup;
} }