Make font_texture_create() more generic

Move font_texture_create() to font.c so it can be used for purposes other
than rendering the menu, for example server side decoration.

Refactor menu.c and menu.h to use this more generic font_texture_create()
This commit is contained in:
Johan Malm 2021-08-07 08:35:46 +01:00
parent 8fc6f795db
commit 5ecf0e1e7e
5 changed files with 104 additions and 72 deletions

View file

@ -1,12 +1,28 @@
#ifndef __LABWC_FONT_H #ifndef __LABWC_FONT_H
#define __LABWC_FONT_H #define __LABWC_FONT_H
struct server;
struct wlr_texture;
struct wlr_box;
/** /**
* font_height - get font vertical extents * font_height - get font vertical extents
* @font_description: string describing font, for example 'sans 10' * @font_description: string describing font, for example 'sans 10'
*/ */
int font_height(const char *font_description); int font_height(const char *font_description);
/**
* texture_create - Create ARGB8888 texture using pango
* @server: context (for wlr_renderer)
* @texture: texture pointer; existing pointer will be freed
* @max_width: max allowable width; will be ellipsized if longer
* @text: text to be generated as texture
* @font: 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);
/** /**
* font_finish - free some font related resources * font_finish - free some font related resources
* Note: use on exit * Note: use on exit

View file

@ -7,9 +7,13 @@
struct menuitem { struct menuitem {
char *action; char *action;
char *command; char *command;
struct wlr_box geo_box; struct wlr_box box;
struct wlr_texture *active_texture; struct {
struct wlr_texture *inactive_texture; struct wlr_texture *active;
struct wlr_texture *inactive;
int offset_x;
int offset_y;
} texture;
bool selected; bool selected;
struct wl_list link; struct wl_list link;
}; };

View file

@ -1,7 +1,11 @@
#include <cairo.h> #include <cairo.h>
#include <drm_fourcc.h>
#include <pango/pangocairo.h> #include <pango/pangocairo.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/util/box.h>
#include <wlr/util/log.h>
#include "common/font.h" #include "common/font.h"
#include "labwc.h"
static PangoRectangle static PangoRectangle
font_extents(const char *font_description, const char *string) font_extents(const char *font_description, const char *string)
@ -43,6 +47,53 @@ font_height(const char *font_description)
return rectangle.height; 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)
{
if (!text || !*text) {
return;
}
if (*texture) {
wlr_texture_destroy(*texture);
*texture = NULL;
}
PangoRectangle rect = font_extents(font, text);
if (max_width && rect.width > max_width) {
rect.width = max_width;
}
cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
rect.width, rect.height);
cairo_t *cairo = cairo_create(surf);
cairo_set_source_rgba(cairo, color[0], color[1], color[2], color[3]);
cairo_move_to(cairo, 0, 0);
PangoLayout *layout = pango_cairo_create_layout(cairo);
pango_layout_set_width(layout, rect.width * PANGO_SCALE);
pango_layout_set_text(layout, text, -1);
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
PangoFontDescription *desc = pango_font_description_from_string(font);
pango_layout_set_font_description(layout, desc);
pango_font_description_free(desc);
pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout);
g_object_unref(layout);
cairo_surface_flush(surf);
unsigned char *data = cairo_image_surface_get_data(surf);
*texture = wlr_texture_from_pixels(server->renderer, DRM_FORMAT_ARGB8888,
cairo_image_surface_get_stride(surf), rect.width,
rect.height, data);
cairo_destroy(cairo);
cairo_surface_destroy(surf);
}
void void
font_finish(void) font_finish(void)
{ {

View file

@ -1,11 +1,8 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include <assert.h> #include <assert.h>
#include <cairo.h>
#include <ctype.h> #include <ctype.h>
#include <drm_fourcc.h>
#include <libxml/parser.h> #include <libxml/parser.h>
#include <libxml/tree.h> #include <libxml/tree.h>
#include <pango/pangocairo.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -21,61 +18,14 @@
#include "menu/menu.h" #include "menu/menu.h"
#include "theme.h" #include "theme.h"
static const char font[] = "Sans 8";
/* state-machine variables for processing <item></item> */ /* state-machine variables for processing <item></item> */
static bool in_item = false; static bool in_item = false;
static struct menuitem *current_item; static struct menuitem *current_item;
#define MENUWIDTH (120) #define MENUWIDTH (110)
#define MENUHEIGHT (25) #define MENUHEIGHT (25)
#define MENU_PADDING_WIDTH (7) #define MENU_PADDING_WIDTH (7)
static void
texture_create(struct server *server, struct wlr_texture **texture,
struct wlr_box *geo, const char *text, float *bg, float *fg)
{
if (*texture) {
wlr_texture_destroy(*texture);
*texture = NULL;
}
cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
geo->width, geo->height);
cairo_t *cairo = cairo_create(surf);
cairo_set_source_rgb(cairo, bg[0], bg[1], bg[2]);
cairo_paint(cairo);
cairo_set_source_rgba(cairo, fg[0], fg[1], fg[2], fg[3]);
cairo_move_to(cairo, 0, 0);
PangoLayout *layout = pango_cairo_create_layout(cairo);
pango_layout_set_width(layout, geo->width * PANGO_SCALE);
pango_layout_set_text(layout, text, -1);
PangoFontDescription *desc = pango_font_description_from_string(font);
pango_layout_set_font_description(layout, desc);
pango_font_description_free(desc);
pango_cairo_update_layout(cairo, layout);
/* center-align vertically */
int height;
pango_layout_get_pixel_size(layout, NULL, &height);
cairo_move_to(cairo, MENU_PADDING_WIDTH, (geo->height - height) / 2);
pango_cairo_show_layout(cairo, layout);
g_object_unref(layout);
cairo_surface_flush(surf);
unsigned char *data = cairo_image_surface_get_data(surf);
*texture = wlr_texture_from_pixels(server->renderer, DRM_FORMAT_ARGB8888,
cairo_image_surface_get_stride(surf), geo->width,
geo->height, data);
cairo_destroy(cairo);
cairo_surface_destroy(surf);
}
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)
{ {
@ -84,13 +34,20 @@ 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->geo_box.width = MENUWIDTH; menuitem->box.width = MENUWIDTH;
menuitem->geo_box.height = MENUHEIGHT; menuitem->box.height = MENUHEIGHT;
texture_create(server, &menuitem->active_texture, &menuitem->geo_box,
text, theme->menu_items_active_bg_color, /* TODO: use rc.font_menu_item */
theme->menu_items_active_text_color); font_texture_create(server, &menuitem->texture.active, MENUWIDTH,
texture_create(server, &menuitem->inactive_texture, &menuitem->geo_box, text, rc.font_name_activewindow, theme->menu_items_active_text_color);
text, theme->menu_items_bg_color, theme->menu_items_text_color); font_texture_create(server, &menuitem->texture.inactive, MENUWIDTH,
text, rc.font_name_activewindow, 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;
wl_list_insert(&menu->menuitems, &menuitem->link); wl_list_insert(&menu->menuitems, &menuitem->link);
return menuitem; return menuitem;
} }
@ -267,6 +224,10 @@ menu_finish(struct menu *menu)
} }
} }
/*
* TODO: call this menu_configure and return the size of the menu so that
* a background color can be rendered
*/
void void
menu_move(struct menu *menu, int x, int y) menu_move(struct menu *menu, int x, int y)
{ {
@ -276,9 +237,9 @@ menu_move(struct menu *menu, int x, int y)
int offset = 0; int offset = 0;
struct menuitem *menuitem; struct menuitem *menuitem;
wl_list_for_each_reverse (menuitem, &menu->menuitems, link) { wl_list_for_each_reverse (menuitem, &menu->menuitems, link) {
menuitem->geo_box.x = menu->x; menuitem->box.x = menu->x;
menuitem->geo_box.y = menu->y + offset; menuitem->box.y = menu->y + offset;
offset += menuitem->geo_box.height; offset += menuitem->box.height;
} }
} }
@ -288,7 +249,7 @@ menu_set_selected(struct menu *menu, int x, int y)
struct menuitem *menuitem; struct menuitem *menuitem;
wl_list_for_each (menuitem, &menu->menuitems, link) { wl_list_for_each (menuitem, &menu->menuitems, link) {
menuitem->selected = menuitem->selected =
wlr_box_contains_point(&menuitem->geo_box, x, y); wlr_box_contains_point(&menuitem->box, x, y);
} }
} }

View file

@ -544,13 +544,13 @@ render_rootmenu(struct output *output, pixman_region32_t *output_damage)
struct menuitem *menuitem; struct menuitem *menuitem;
wl_list_for_each (menuitem, &server->rootmenu->menuitems, link) { wl_list_for_each (menuitem, &server->rootmenu->menuitems, link) {
struct wlr_texture *t; struct wlr_texture *t;
t = menuitem->selected ? menuitem->active_texture : t = menuitem->selected ? menuitem->texture.active :
menuitem->inactive_texture; menuitem->texture.inactive;
struct wlr_box box = { struct wlr_box box = {
.x = menuitem->geo_box.x + ox, .x = menuitem->box.x + ox + menuitem->texture.offset_x,
.y = menuitem->geo_box.y + oy, .y = menuitem->box.y + oy + menuitem->texture.offset_y,
.width = menuitem->geo_box.width, .width = t->width,
.height = menuitem->geo_box.height, .height = t->height,
}; };
scale_box(&box, output->wlr_output->scale); scale_box(&box, output->wlr_output->scale);
wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL,