font: font_texture_create() support font size argument

This commit is contained in:
Johan Malm 2021-08-20 20:20:49 +01:00
parent 16620698fb
commit a668f6f73d
6 changed files with 58 additions and 35 deletions

View file

@ -5,11 +5,16 @@ struct server;
struct wlr_texture; struct wlr_texture;
struct wlr_box; struct wlr_box;
struct font {
char *name;
int size;
};
/** /**
* font_height - get font vertical extents * font_height - get font vertical extents
* @font_description: string describing font, for example 'sans 10' * @font: description of font including family name and size
*/ */
int font_height(const char *font_description); int font_height(struct font *font);
/** /**
* texture_create - Create ARGB8888 texture using pango * texture_create - Create ARGB8888 texture using pango
@ -21,7 +26,7 @@ int font_height(const char *font_description);
* @color: foreground color in rgba format * @color: foreground color in rgba format
*/ */
void font_texture_create(struct server *server, struct wlr_texture **texture, void font_texture_create(struct server *server, struct wlr_texture **texture,
int max_width, const char *text, const char *font, float *color); int max_width, const char *text, struct font *font, float *color);
/** /**
* font_finish - free some font related resources * font_finish - free some font related resources

View file

@ -8,20 +8,21 @@
#include "labwc.h" #include "labwc.h"
static PangoRectangle static PangoRectangle
font_extents(const char *font_description, const char *string) font_extents(struct font *font, const char *string)
{ {
PangoRectangle rect; PangoRectangle rect;
cairo_surface_t *surface; cairo_surface_t *surface;
cairo_t *c; cairo_t *c;
PangoLayout *layout; PangoLayout *layout;
PangoFontDescription *font;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1); surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
c = cairo_create(surface); c = cairo_create(surface);
layout = pango_cairo_create_layout(c); layout = pango_cairo_create_layout(c);
font = pango_font_description_from_string(font_description); PangoFontDescription *desc = pango_font_description_new();
pango_font_description_set_family(desc, font->name);
pango_font_description_set_size(desc, font->size * PANGO_SCALE);
pango_layout_set_font_description(layout, font); pango_layout_set_font_description(layout, desc);
pango_layout_set_text(layout, string, -1); pango_layout_set_text(layout, string, -1);
pango_layout_set_single_paragraph_mode(layout, TRUE); pango_layout_set_single_paragraph_mode(layout, TRUE);
pango_layout_set_width(layout, -1); pango_layout_set_width(layout, -1);
@ -34,22 +35,21 @@ font_extents(const char *font_description, const char *string)
cairo_destroy(c); cairo_destroy(c);
cairo_surface_destroy(surface); cairo_surface_destroy(surface);
pango_font_description_free(font); pango_font_description_free(desc);
g_object_unref(layout); g_object_unref(layout);
return rect; return rect;
} }
int int
font_height(const char *font_description) font_height(struct font *font)
{ {
PangoRectangle rectangle; PangoRectangle rectangle = font_extents(font, "abcdefg");
rectangle = font_extents(font_description, "abcdefg");
return rectangle.height; return rectangle.height;
} }
void void
font_texture_create(struct server *server, struct wlr_texture **texture, font_texture_create(struct server *server, struct wlr_texture **texture,
int max_width, const char *text, const char *font, float *color) int max_width, const char *text, struct font *font, float *color)
{ {
if (!text || !*text) { if (!text || !*text) {
return; return;
@ -76,7 +76,9 @@ font_texture_create(struct server *server, struct wlr_texture **texture,
pango_layout_set_text(layout, text, -1); pango_layout_set_text(layout, text, -1);
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
PangoFontDescription *desc = pango_font_description_from_string(font); PangoFontDescription *desc = pango_font_description_new();
pango_font_description_set_family(desc, font->name);
pango_font_description_set_size(desc, font->size * PANGO_SCALE);
pango_layout_set_font_description(layout, desc); pango_layout_set_font_description(layout, desc);
pango_font_description_free(desc); pango_font_description_free(desc);
pango_cairo_update_layout(cairo, layout); pango_cairo_update_layout(cairo, layout);

View file

@ -23,8 +23,8 @@ static bool in_item = false;
static struct menuitem *current_item; static struct menuitem *current_item;
#define MENUWIDTH (110) #define MENUWIDTH (110)
#define MENUHEIGHT (25) #define MENU_ITEM_PADDING_Y (4)
#define MENU_PADDING_WIDTH (7) #define MENU_ITEM_PADDING_X (7)
static struct menuitem * static struct menuitem *
menuitem_create(struct server *server, struct menu *menu, const char *text) menuitem_create(struct server *server, struct menu *menu, const char *text)
@ -34,19 +34,24 @@ menuitem_create(struct server *server, struct menu *menu, const char *text)
return NULL; return NULL;
} }
struct theme *theme = server->theme; struct theme *theme = server->theme;
menuitem->box.width = MENUWIDTH; struct font font = {
menuitem->box.height = MENUHEIGHT; .name = rc.font_name_menuitem,
.size = rc.font_size_menuitem,
};
/* TODO: use rc.font_menu_item */ menuitem->box.width = MENUWIDTH;
font_texture_create(server, &menuitem->texture.active, MENUWIDTH, menuitem->box.height = font_height(&font) + 2 * MENU_ITEM_PADDING_Y;
text, rc.font_name_activewindow, theme->menu_items_active_text_color);
font_texture_create(server, &menuitem->texture.inactive, MENUWIDTH, int item_max_width = MENUWIDTH - 2 * MENU_ITEM_PADDING_X;
text, rc.font_name_activewindow, theme->menu_items_text_color); font_texture_create(server, &menuitem->texture.active, item_max_width,
text, &font, theme->menu_items_active_text_color);
font_texture_create(server, &menuitem->texture.inactive, item_max_width,
text, &font, theme->menu_items_text_color);
/* center align vertically */ /* center align vertically */
menuitem->texture.offset_y = menuitem->texture.offset_y =
(menuitem->box.height - menuitem->texture.active->height) / 2; (menuitem->box.height - menuitem->texture.active->height) / 2;
menuitem->texture.offset_x = MENU_PADDING_WIDTH; menuitem->texture.offset_x = MENU_ITEM_PADDING_X;
wl_list_insert(&menu->menuitems, &menuitem->link); wl_list_insert(&menu->menuitems, &menuitem->link);
return menuitem; return menuitem;

View file

@ -125,7 +125,11 @@ osd_update(struct server *server)
y = OSD_BORDER_WIDTH; y = OSD_BORDER_WIDTH;
/* vertically center align */ /* vertically center align */
y += (OSD_ITEM_HEIGHT - font_height("sans 10")) / 2; struct font font = {
.name = "sans",
.size = 10,
};
y += (OSD_ITEM_HEIGHT - font_height(&font)) / 2;
wl_list_for_each(view, &server->views, link) { wl_list_for_each(view, &server->views, link) {
if (!isfocusable(view)) { if (!isfocusable(view)) {

View file

@ -246,20 +246,26 @@ ssd_update_title(struct view *view)
{ {
struct theme *theme = view->server->theme; struct theme *theme = view->server->theme;
/* TODO: use window.active.label.text.color here */ struct font font = {
/* TODO: set max_width propertly */ .name = rc.font_name_activewindow,
font_texture_create(view->server, &view->title, 200, .size = rc.font_size_activewindow,
view->impl->get_string_prop(view, "title"), };
rc.font_name_activewindow,
theme->menu_items_active_text_color);
/* get the size we can play within */
struct ssd_part *part; struct ssd_part *part;
wl_list_for_each(part, &view->ssd.parts, link) { wl_list_for_each(part, &view->ssd.parts, link) {
if (part->type == LAB_SSD_PART_TITLE) { if (part->type == LAB_SSD_PART_TITLE) {
part->box = ssd_box(view, part->type); part->box = ssd_box(view, LAB_SSD_PART_TITLEBAR);
break; break;
} }
} }
/* TODO: use window.active.label.text.color here */
font_texture_create(view->server, &view->title, part->box.width,
view->impl->get_string_prop(view, "title"),
&font, theme->menu_items_active_text_color);
part->box = ssd_box(view, LAB_SSD_PART_TITLE);
} }
void void

View file

@ -374,10 +374,11 @@ create_corners(struct theme *theme, struct wlr_renderer *renderer)
static void static void
post_processing(struct theme *theme) post_processing(struct theme *theme)
{ {
char buf[256]; struct font font = {
snprintf(buf, sizeof(buf), "%s %d", rc.font_name_activewindow, .name = rc.font_name_activewindow,
rc.font_size_activewindow); .size = rc.font_size_activewindow,
theme->title_height = font_height(buf) + 2 * theme->padding_height; };
theme->title_height = font_height(&font) + 2 * theme->padding_height;
if (rc.corner_radius >= theme->title_height) { if (rc.corner_radius >= theme->title_height) {
theme->title_height = rc.corner_radius + 1; theme->title_height = rc.corner_radius + 1;