]> git.mdlowis.com Git - proto/labwc.git/commitdiff
font: font_texture_create() support font size argument
authorJohan Malm <jgm323@gmail.com>
Fri, 20 Aug 2021 19:20:49 +0000 (20:20 +0100)
committerJohan Malm <jgm323@gmail.com>
Fri, 20 Aug 2021 19:20:49 +0000 (20:20 +0100)
include/common/font.h
src/common/font.c
src/menu/menu.c
src/osd.c
src/ssd.c
src/theme.c

index efcf48b4dd634786b9774219a13fcf659eb7714b..f282a9686827c028c475f4366855dff7678edefd 100644 (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
index 2bdfa6eaa8cd326bb44a55bb5067459390a008af..0e0b3243c3baa679bb0e15ed0b8eaa63ec5acd53 100644 (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);
index 01b26de5a8960ac4fea9a9b2632ac66bd6cb53b7..54a2b83a74a078e853b21d1b1baa25e45ca120d9 100644 (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;
+       struct font font = {
+               .name = rc.font_name_menuitem,
+               .size = rc.font_size_menuitem,
+       };
+
        menuitem->box.width = MENUWIDTH;
-       menuitem->box.height = MENUHEIGHT;
+       menuitem->box.height = font_height(&font) + 2 * MENU_ITEM_PADDING_Y;
 
-       /* 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);
+       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;
index 1cd7607dd33306a82e3125e933dff4ffc19b9ee9..375dad9369aa62378a651b41f18b67e33f5512c5 100644 (file)
--- a/src/osd.c
+++ b/src/osd.c
@@ -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)) {
index f034b09e28636196797ca29c35d33b1ff87f8574..e8d5a5c254b2ba2a328afab3fa4a638025b0a97c 100644 (file)
--- a/src/ssd.c
+++ b/src/ssd.c
@@ -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
index 989b3121a97cf6f66deb18fcb1a4accfa797e29a..e5419c7f660931081deda871ac7ef112602841d4 100644 (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;