mirror of
				https://github.com/swaywm/sway.git
				synced 2025-11-03 09:01:43 -05:00 
			
		
		
		
	Fix constant scale factor in font code
This commit is contained in:
		
							parent
							
								
									b2226ac655
								
							
						
					
					
						commit
						af44154119
					
				
					 4 changed files with 36 additions and 22 deletions
				
			
		| 
						 | 
				
			
			@ -5,10 +5,12 @@
 | 
			
		|||
#include <pango/pangocairo.h>
 | 
			
		||||
#include <stdarg.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text, bool markup);
 | 
			
		||||
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text,
 | 
			
		||||
		int32_t scale, bool markup);
 | 
			
		||||
void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
 | 
			
		||||
		bool markup, const char *fmt, ...);
 | 
			
		||||
void pango_printf(cairo_t *cairo, const char *font, bool markup, const char *fmt, ...);
 | 
			
		||||
		int32_t scale, bool markup, const char *fmt, ...);
 | 
			
		||||
void pango_printf(cairo_t *cairo, const char *font, int32_t scale, bool markup, const char *fmt, ...);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -92,7 +92,7 @@ int get_font_text_height(const char *font) {
 | 
			
		|||
	cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 200);
 | 
			
		||||
	cairo_t *cr = cairo_create(surface);
 | 
			
		||||
	int width, height;
 | 
			
		||||
	get_text_size(cr, font, &width, &height, false, "Gg");
 | 
			
		||||
	get_text_size(cr, font, &width, &height, 1, false, "Gg");
 | 
			
		||||
	cairo_surface_destroy(surface);
 | 
			
		||||
	cairo_destroy(cr);
 | 
			
		||||
	return height;
 | 
			
		||||
| 
						 | 
				
			
			@ -180,7 +180,7 @@ static void render_title_bar(swayc_t *view, cairo_t *cr, struct wlc_geometry *b,
 | 
			
		|||
		get_text_size(cr, config->font, &width, &height, false, "%s", view->name);
 | 
			
		||||
		cairo_move_to(cr, x + 2, y + 2);
 | 
			
		||||
		cairo_set_source_u32(cr, colors->text);
 | 
			
		||||
		pango_printf(cr, config->font, false, "%s", view->name);
 | 
			
		||||
		pango_printf(cr, config->font, 1, false, "%s", view->name);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// titlebars has a border all around for tabbed layouts
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -51,7 +51,8 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color, double x, double y
 | 
			
		|||
 | 
			
		||||
static void render_block(struct window *window, struct config *config, struct status_block *block, double *x, bool edge) {
 | 
			
		||||
	int width, height, sep_width;
 | 
			
		||||
	get_text_size(window->cairo, window->font, &width, &height, block->markup, "%s", block->full_text);
 | 
			
		||||
	get_text_size(window->cairo, window->font, &width, &height,
 | 
			
		||||
			window->scale, block->markup, "%s", block->full_text);
 | 
			
		||||
 | 
			
		||||
	int textwidth = width;
 | 
			
		||||
	double block_width = width;
 | 
			
		||||
| 
						 | 
				
			
			@ -75,7 +76,8 @@ static void render_block(struct window *window, struct config *config, struct st
 | 
			
		|||
	// Add separator
 | 
			
		||||
	if (!edge) {
 | 
			
		||||
		if (config->sep_symbol) {
 | 
			
		||||
			get_text_size(window->cairo, window->font, &sep_width, &height, false, "%s", config->sep_symbol);
 | 
			
		||||
			get_text_size(window->cairo, window->font, &sep_width, &height,
 | 
			
		||||
					window->scale, false, "%s", config->sep_symbol);
 | 
			
		||||
			if (sep_width > block->separator_block_width) {
 | 
			
		||||
				block->separator_block_width = sep_width + margin * 2;
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			@ -137,7 +139,8 @@ static void render_block(struct window *window, struct config *config, struct st
 | 
			
		|||
 | 
			
		||||
	cairo_move_to(window->cairo, offset, margin);
 | 
			
		||||
	cairo_set_source_u32(window->cairo, block->color);
 | 
			
		||||
	pango_printf(window->cairo, window->font, block->markup, "%s", block->full_text);
 | 
			
		||||
	pango_printf(window->cairo, window->font, window->scale,
 | 
			
		||||
			block->markup, "%s", block->full_text);
 | 
			
		||||
 | 
			
		||||
	pos += width;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -160,7 +163,8 @@ static void render_block(struct window *window, struct config *config, struct st
 | 
			
		|||
		if (config->sep_symbol) {
 | 
			
		||||
			offset = pos + (block->separator_block_width - sep_width) / 2;
 | 
			
		||||
			cairo_move_to(window->cairo, offset, margin);
 | 
			
		||||
			pango_printf(window->cairo, window->font, false, "%s", config->sep_symbol);
 | 
			
		||||
			pango_printf(window->cairo, window->font, window->scale,
 | 
			
		||||
					false, "%s", config->sep_symbol);
 | 
			
		||||
		} else {
 | 
			
		||||
			cairo_set_line_width(window->cairo, 1);
 | 
			
		||||
			cairo_move_to(window->cairo, pos + block->separator_block_width/2,
 | 
			
		||||
| 
						 | 
				
			
			@ -200,7 +204,8 @@ static const char *strip_workspace_name(bool strip_num, const char *ws_name) {
 | 
			
		|||
void workspace_button_size(struct window *window, const char *workspace_name, int *width, int *height) {
 | 
			
		||||
	const char *stripped_name = strip_workspace_name(swaybar.config->strip_workspace_numbers, workspace_name);
 | 
			
		||||
 | 
			
		||||
	get_text_size(window->cairo, window->font, width, height, false, "%s", stripped_name);
 | 
			
		||||
	get_text_size(window->cairo, window->font, width, height,
 | 
			
		||||
			window->scale, false, "%s", stripped_name);
 | 
			
		||||
	*width += 2 * ws_horizontal_padding;
 | 
			
		||||
	*height += 2 * ws_vertical_padding;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -235,14 +240,16 @@ static void render_workspace_button(struct window *window, struct config *config
 | 
			
		|||
	// text
 | 
			
		||||
	cairo_set_source_u32(window->cairo, box_colors.text);
 | 
			
		||||
	cairo_move_to(window->cairo, (int)*x + ws_horizontal_padding, margin);
 | 
			
		||||
	pango_printf(window->cairo, window->font, false, "%s", stripped_name);
 | 
			
		||||
	pango_printf(window->cairo, window->font, window->scale,
 | 
			
		||||
			false, "%s", stripped_name);
 | 
			
		||||
 | 
			
		||||
	*x += width + ws_spacing;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) {
 | 
			
		||||
	int width, height;
 | 
			
		||||
	get_text_size(window->cairo, window->font, &width, &height, false, "%s", config->mode);
 | 
			
		||||
	get_text_size(window->cairo, window->font, &width, &height,
 | 
			
		||||
			window->scale, false, "%s", config->mode);
 | 
			
		||||
 | 
			
		||||
	// background
 | 
			
		||||
	cairo_set_source_u32(window->cairo, config->colors.binding_mode.background);
 | 
			
		||||
| 
						 | 
				
			
			@ -259,7 +266,8 @@ static void render_binding_mode_indicator(struct window *window, struct config *
 | 
			
		|||
	// text
 | 
			
		||||
	cairo_set_source_u32(window->cairo, config->colors.binding_mode.text);
 | 
			
		||||
	cairo_move_to(window->cairo, (int)pos + ws_horizontal_padding, margin);
 | 
			
		||||
	pango_printf(window->cairo, window->font, false, "%s", config->mode);
 | 
			
		||||
	pango_printf(window->cairo, window->font, window->scale,
 | 
			
		||||
			false, "%s", config->mode);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void render(struct output *output, struct config *config, struct status_line *line) {
 | 
			
		||||
| 
						 | 
				
			
			@ -283,10 +291,12 @@ void render(struct output *output, struct config *config, struct status_line *li
 | 
			
		|||
	int width, height;
 | 
			
		||||
 | 
			
		||||
	if (line->protocol == TEXT) {
 | 
			
		||||
		get_text_size(window->cairo, window->font, &width, &height, config->pango_markup, "%s", line->text_line);
 | 
			
		||||
		get_text_size(window->cairo, window->font, &width, &height,
 | 
			
		||||
				window->scale, config->pango_markup, "%s", line->text_line);
 | 
			
		||||
		cairo_move_to(cairo, (window->width * window->scale)
 | 
			
		||||
				- margin - width, margin);
 | 
			
		||||
		pango_printf(window->cairo, window->font, config->pango_markup, "%s", line->text_line);
 | 
			
		||||
		pango_printf(window->cairo, window->font, window->scale,
 | 
			
		||||
				config->pango_markup, "%s", line->text_line);
 | 
			
		||||
	} else if (line->protocol == I3BAR && line->block_line) {
 | 
			
		||||
		double pos = window->width - 0.5;
 | 
			
		||||
		bool edge = true;
 | 
			
		||||
| 
						 | 
				
			
			@ -319,7 +329,7 @@ void render(struct output *output, struct config *config, struct status_line *li
 | 
			
		|||
void set_window_height(struct window *window, int height) {
 | 
			
		||||
	int text_width, text_height;
 | 
			
		||||
	get_text_size(window->cairo, window->font,
 | 
			
		||||
			&text_width, &text_height, false,
 | 
			
		||||
			&text_width, &text_height, window->scale, false,
 | 
			
		||||
			"Test string for measuring purposes");
 | 
			
		||||
	if (height > 0) {
 | 
			
		||||
		margin = (height - text_height) / 2;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,10 +7,12 @@
 | 
			
		|||
#include <stdbool.h>
 | 
			
		||||
#include "log.h"
 | 
			
		||||
 | 
			
		||||
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text, bool markup) {
 | 
			
		||||
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text,
 | 
			
		||||
		int32_t scale, bool markup) {
 | 
			
		||||
	PangoLayout *layout = pango_cairo_create_layout(cairo);
 | 
			
		||||
	PangoAttrList *attrs = pango_attr_list_new();
 | 
			
		||||
	pango_attr_list_insert(attrs, pango_attr_scale_new(2));
 | 
			
		||||
	sway_log(L_DEBUG, "Font scale: %d", scale);
 | 
			
		||||
	pango_attr_list_insert(attrs, pango_attr_scale_new(scale));
 | 
			
		||||
	if (markup) {
 | 
			
		||||
		pango_layout_set_markup(layout, text, -1);
 | 
			
		||||
	} else {
 | 
			
		||||
| 
						 | 
				
			
			@ -26,7 +28,7 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
 | 
			
		||||
		bool markup, const char *fmt, ...) {
 | 
			
		||||
		int32_t scale, bool markup, const char *fmt, ...) {
 | 
			
		||||
	char *buf = malloc(2048);
 | 
			
		||||
 | 
			
		||||
	va_list args;
 | 
			
		||||
| 
						 | 
				
			
			@ -36,7 +38,7 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
 | 
			
		|||
	}
 | 
			
		||||
	va_end(args);
 | 
			
		||||
 | 
			
		||||
	PangoLayout *layout = get_pango_layout(cairo, font, buf, markup);
 | 
			
		||||
	PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup);
 | 
			
		||||
	pango_cairo_update_layout(cairo, layout);
 | 
			
		||||
 | 
			
		||||
	pango_layout_get_pixel_size(layout, width, height);
 | 
			
		||||
| 
						 | 
				
			
			@ -46,7 +48,7 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
 | 
			
		|||
	free(buf);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void pango_printf(cairo_t *cairo, const char *font, bool markup, const char *fmt, ...) {
 | 
			
		||||
void pango_printf(cairo_t *cairo, const char *font, int32_t scale, bool markup, const char *fmt, ...) {
 | 
			
		||||
	char *buf = malloc(2048);
 | 
			
		||||
 | 
			
		||||
	va_list args;
 | 
			
		||||
| 
						 | 
				
			
			@ -56,7 +58,7 @@ void pango_printf(cairo_t *cairo, const char *font, bool markup, const char *fmt
 | 
			
		|||
	}
 | 
			
		||||
	va_end(args);
 | 
			
		||||
 | 
			
		||||
	PangoLayout *layout = get_pango_layout(cairo, font, buf, markup);
 | 
			
		||||
	PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup);
 | 
			
		||||
	pango_cairo_update_layout(cairo, layout);
 | 
			
		||||
 | 
			
		||||
	pango_cairo_show_layout(cairo, layout);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue