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_box;
struct font {
char *name;
int size;
};
/**
* 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
@ -21,7 +26,7 @@ int font_height(const char *font_description);
* @color: foreground color in rgba format
*/
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

View file

@ -8,20 +8,21 @@
#include "labwc.h"
static PangoRectangle
font_extents(const char *font_description, const char *string)
font_extents(struct font *font, const char *string)
{
PangoRectangle rect;
cairo_surface_t *surface;
cairo_t *c;
PangoLayout *layout;
PangoFontDescription *font;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
c = cairo_create(surface);
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_single_paragraph_mode(layout, TRUE);
pango_layout_set_width(layout, -1);
@ -34,22 +35,21 @@ font_extents(const char *font_description, const char *string)
cairo_destroy(c);
cairo_surface_destroy(surface);
pango_font_description_free(font);
pango_font_description_free(desc);
g_object_unref(layout);
return rect;
}
int
font_height(const char *font_description)
font_height(struct font *font)
{
PangoRectangle rectangle;
rectangle = font_extents(font_description, "abcdefg");
PangoRectangle rectangle = font_extents(font, "abcdefg");
return rectangle.height;
}
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)
{
if (!text || !*text) {
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_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_font_description_free(desc);
pango_cairo_update_layout(cairo, layout);

View file

@ -23,8 +23,8 @@ static bool in_item = false;
static struct menuitem *current_item;
#define MENUWIDTH (110)
#define MENUHEIGHT (25)
#define MENU_PADDING_WIDTH (7)
#define MENU_ITEM_PADDING_Y (4)
#define MENU_ITEM_PADDING_X (7)
static struct menuitem *
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;
}
struct theme *theme = server->theme;
menuitem->box.width = MENUWIDTH;
menuitem->box.height = MENUHEIGHT;
struct font font = {
.name = rc.font_name_menuitem,
.size = rc.font_size_menuitem,
};
/* TODO: use rc.font_menu_item */
font_texture_create(server, &menuitem->texture.active, MENUWIDTH,
text, rc.font_name_activewindow, theme->menu_items_active_text_color);
font_texture_create(server, &menuitem->texture.inactive, MENUWIDTH,
text, rc.font_name_activewindow, theme->menu_items_text_color);
menuitem->box.width = MENUWIDTH;
menuitem->box.height = font_height(&font) + 2 * MENU_ITEM_PADDING_Y;
int item_max_width = MENUWIDTH - 2 * MENU_ITEM_PADDING_X;
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 */
menuitem->texture.offset_y =
(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);
return menuitem;

View file

@ -125,7 +125,11 @@ osd_update(struct server *server)
y = OSD_BORDER_WIDTH;
/* 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) {
if (!isfocusable(view)) {

View file

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

View file

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