]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Base rc.title_height on font vertical extents
authorJohan Malm <jgm323@gmail.com>
Wed, 5 Aug 2020 19:14:17 +0000 (20:14 +0100)
committerJohan Malm <jgm323@gmail.com>
Wed, 5 Aug 2020 19:14:17 +0000 (20:14 +0100)
include/common/font.h [new file with mode: 0644]
include/config/rcxml.h
meson.build
src/common/font.c [new file with mode: 0644]
src/common/meson.build
src/config/rcxml.c
src/deco.c
tests/meson.build
tools/pango/.gitignore [new file with mode: 0644]
tools/pango/Makefile [new file with mode: 0644]
tools/pango/font-height.c [new file with mode: 0644]

diff --git a/include/common/font.h b/include/common/font.h
new file mode 100644 (file)
index 0000000..04f99f0
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef __LABWC_FONT_H
+#define __LABWC_FONT_H
+
+/*
+ * font_height - get font vertical extents
+ * @font_description: string describing font, for example 'sans 10'
+ */
+int font_height(const char *font_description);
+
+#endif /* __LABWC_FONT_H */
index 0b5484a55db214d3dce7db97e707ebf9702cae98..45a7caf3b236303990eaeafccba9d930160f6d13 100644 (file)
@@ -13,6 +13,7 @@ struct rcxml {
        char *font_name_activewindow;
        int font_size_activewindow;
        struct wl_list keybinds;
+       int title_height; /* not set in rc.xml, but derived from font, etc */
 };
 
 extern struct rcxml rc;
index 49ee4ef23060e649d0e76a81b75fd4f410b4827a..a4db297a4b214459910b03efe34e224d50437a91 100644 (file)
@@ -43,19 +43,21 @@ xkbcommon       = dependency('xkbcommon')
 xml2            = dependency('libxml-2.0')
 glib            = dependency('glib-2.0')
 cairo           = dependency('cairo')
-pango           = dependency('pango')
+pangocairo      = dependency('pangocairo')
 
 labwc_inc       = include_directories('include')
 
 subdir('protocols')
-subdir('src')
-subdir('tests')
-subdir('docs')
 
 labwc_deps      = [
-  server_protos, wayland_server, wlroots, xkbcommon, xml2, glib, cairo, pango
+  server_protos, wayland_server, wlroots, xkbcommon, xml2, glib,
+  cairo, pangocairo
 ]
 
+subdir('src')
+subdir('tests')
+subdir('docs')
+
 executable(
   meson.project_name(),
   labwc_sources,
diff --git a/src/common/font.c b/src/common/font.c
new file mode 100644 (file)
index 0000000..acf4975
--- /dev/null
@@ -0,0 +1,41 @@
+#include <cairo.h>
+#include <pango/pangocairo.h>
+
+#include "common/font.h"
+
+static PangoRectangle font_extents(const char *font_description,
+                                  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);
+
+       pango_layout_set_font_description(layout, font);
+       pango_layout_set_text(layout, string, -1);
+       pango_layout_set_single_paragraph_mode(layout, TRUE);
+       pango_layout_set_width(layout, -1);
+       pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_MIDDLE);
+       pango_layout_get_extents(layout, NULL, &rect);
+       pango_extents_to_pixels(&rect, NULL);
+
+       /* we put a 2 px edge on each side - because Openbox does it :) */
+       rect.width += 4;
+
+       g_object_unref(layout);
+       pango_font_description_free(font);
+       return rect;
+}
+
+int font_height(const char *font_description)
+{
+       PangoRectangle rectangle;
+       rectangle = font_extents(font_description, "abcdefg");
+       return rectangle.height;
+}
index 36446b5950db501fb27c0017051420f366e8402e..fe8e710dbc924866122921c1cf9242a1b032e835 100644 (file)
@@ -1,4 +1,5 @@
 labwc_sources += files(
   'buf.c',
+  'font.c',
   'spawn.c',
 )
index 8f25bf3c3fa37618245985e13b43ee30ec942e20..b5c86842890864182d6823ce772749eb7b793dc4 100644 (file)
@@ -14,6 +14,7 @@
 #include "config/keybind.h"
 #include "config/config-dir.h"
 #include "common/bug-on.h"
+#include "common/font.h"
 
 static bool in_keybind = false;
 static bool is_attribute = false;
@@ -232,6 +233,14 @@ static void bind(const char *binding, const char *action)
        fprintf(stderr, "binding: %s: %s\n", binding, action);
 }
 
+static void set_title_height(void)
+{
+       char buf[256];
+       snprintf(buf, sizeof(buf), "%s %d", rc.font_name_activewindow,
+                rc.font_size_activewindow);
+       rc.title_height = font_height(buf);
+}
+
 static void post_processing(void)
 {
        if (!wl_list_length(&rc.keybinds)) {
@@ -241,6 +250,8 @@ static void post_processing(void)
                bind("A-F3", "Execute");
        }
        /* TODO: Set all char* variables if NULL */
+
+       set_title_height();
 }
 
 static void rcxml_path(char *buf, size_t len, const char *filename)
index d5a64ad61ff55efa6e57f5253611344e87f9fcf3..50a4c332a5883ca28df350053a2a0b24fbdffea6 100644 (file)
@@ -6,18 +6,18 @@
 
 #include "labwc.h"
 #include "theme/theme.h"
+#include "config/rcxml.h"
 
 /* Based on expected font height of Sans 8 */
-#define TITLE_HEIGHT (14)
 #define BORDER_WIDTH (1)
 
 struct wlr_box deco_max_extents(struct view *view)
 {
        struct wlr_box box = {
                .x = view->x - BORDER_WIDTH,
-               .y = view->y - TITLE_HEIGHT - BORDER_WIDTH,
+               .y = view->y - rc.title_height - BORDER_WIDTH,
                .width = view->surface->current.width + 2 * BORDER_WIDTH,
-               .height = view->surface->current.height + TITLE_HEIGHT +
+               .height = view->surface->current.height + rc.title_height +
                          2 * BORDER_WIDTH,
        };
        return box;
@@ -33,44 +33,44 @@ struct wlr_box deco_box(struct view *view, enum deco_part deco_part)
        switch (deco_part) {
        case LAB_DECO_BUTTON_CLOSE:
                wlr_texture_get_size(theme.xbm_close, &box.width, &box.height);
-               margin = (TITLE_HEIGHT - box.height) / 2;
+               margin = (rc.title_height - box.height) / 2;
                box.x = view->x + view->surface->current.width + margin -
-                       TITLE_HEIGHT;
-               box.y = view->y - TITLE_HEIGHT + margin;
+                       rc.title_height;
+               box.y = view->y - rc.title_height + margin;
                break;
        case LAB_DECO_BUTTON_MAXIMIZE:
                wlr_texture_get_size(theme.xbm_maximize, &box.width,
                                     &box.height);
-               margin = (TITLE_HEIGHT - box.height) / 2;
+               margin = (rc.title_height - box.height) / 2;
                box.x = view->x + view->surface->current.width + margin -
-                       TITLE_HEIGHT * 2;
-               box.y = view->y - TITLE_HEIGHT + margin;
+                       rc.title_height * 2;
+               box.y = view->y - rc.title_height + margin;
                break;
        case LAB_DECO_BUTTON_ICONIFY:
                wlr_texture_get_size(theme.xbm_iconify, &box.width,
                                     &box.height);
-               margin = (TITLE_HEIGHT - box.height) / 2;
+               margin = (rc.title_height - box.height) / 2;
                box.x = view->x + view->surface->current.width + margin -
-                       TITLE_HEIGHT * 3;
-               box.y = view->y - TITLE_HEIGHT + margin;
+                       rc.title_height * 3;
+               box.y = view->y - rc.title_height + margin;
                break;
        case LAB_DECO_PART_TITLE:
                box.x = view->x;
-               box.y = view->y - TITLE_HEIGHT;
+               box.y = view->y - rc.title_height;
                box.width = view->surface->current.width;
-               box.height = TITLE_HEIGHT;
+               box.height = rc.title_height;
                break;
        case LAB_DECO_PART_TOP:
                box.x = view->x - BORDER_WIDTH;
-               box.y = view->y - TITLE_HEIGHT - BORDER_WIDTH;
+               box.y = view->y - rc.title_height - BORDER_WIDTH;
                box.width = view->surface->current.width + 2 * BORDER_WIDTH;
                box.height = BORDER_WIDTH;
                break;
        case LAB_DECO_PART_RIGHT:
                box.x = view->x + view->surface->current.width;
-               box.y = view->y - TITLE_HEIGHT;
+               box.y = view->y - rc.title_height;
                box.width = BORDER_WIDTH;
-               box.height = view->surface->current.height + TITLE_HEIGHT;
+               box.height = view->surface->current.height + rc.title_height;
                break;
        case LAB_DECO_PART_BOTTOM:
                box.x = view->x - BORDER_WIDTH;
@@ -80,9 +80,9 @@ struct wlr_box deco_box(struct view *view, enum deco_part deco_part)
                break;
        case LAB_DECO_PART_LEFT:
                box.x = view->x - BORDER_WIDTH;
-               box.y = view->y - TITLE_HEIGHT;
+               box.y = view->y - rc.title_height;
                box.width = BORDER_WIDTH;
-               box.height = view->surface->current.height + TITLE_HEIGHT;
+               box.height = view->surface->current.height + rc.title_height;
                break;
        default:
                break;
index de7d3087cfca6096fa5c91146a3353a1a8cb97a1..eedb43a9ff70d480356c262b1b82e1c5a4fe94b1 100644 (file)
@@ -4,9 +4,10 @@ rcxml_lib = static_library(
     '../src/config/rcxml.c',
     '../src/config/keybind.c',
     '../src/config/config-dir.c',
-    '../src/common/buf.c'
+    '../src/common/buf.c',
+    '../src/common/font.c',
   ),
-  dependencies: [xml2, wayland_server, xkbcommon, glib, wlroots],
+  dependencies: labwc_deps,
   include_directories: [labwc_inc],
 )
 
diff --git a/tools/pango/.gitignore b/tools/pango/.gitignore
new file mode 100644 (file)
index 0000000..583e383
--- /dev/null
@@ -0,0 +1,2 @@
+*.o
+font-height
diff --git a/tools/pango/Makefile b/tools/pango/Makefile
new file mode 100644 (file)
index 0000000..88ab215
--- /dev/null
@@ -0,0 +1,15 @@
+CFLAGS += -g -Wall -O0 -std=c11
+CFLAGS += `pkg-config cairo pango pangocairo --cflags`
+CFLAGS += -I../../include
+LIBS   += `pkg-config cairo pango pangocairo --libs`
+LDFLAGS += $(LIBS)
+
+PROGS = font-height
+
+all: $(PROGS)
+
+font-height: font-height.c ../../src/common/font.c
+       $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
+
+clean:
+       rm -f $(PROGS) *.o
diff --git a/tools/pango/font-height.c b/tools/pango/font-height.c
new file mode 100644 (file)
index 0000000..24690f5
--- /dev/null
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common/font.h"
+
+int main(int argc, char **argv)
+{
+       if (argc < 2) {
+               printf("Usage: %s <font-description> (e.g. 'sans 10')\n", argv[0]);
+               exit(1);
+       }
+       printf("height=%d\n", font_height(argv[1]));
+}