summaryrefslogtreecommitdiff
path: root/src/entry.c
blob: e6338de3b9329bc963b85720b6f55bad9ab4f786 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <arpa/inet.h>
#include <cairo/cairo.h>
#include <glib.h>
#include <pango/pangocairo.h>
#include <pango/pango.h>
#include <wchar.h>
#include "client.h"
#include "entry.h"
#include "log.h"
#include "nelem.h"

static void calculate_font_extents(struct entry *entry, uint32_t scale);

void entry_init(struct entry *entry, uint32_t scale)
{
	calculate_font_extents(entry, scale);
	struct color color;

	/*
	 * Cairo uses native 32 bit integers for pixels, so if this processor
	 * is little endian we have to tell OpenGL to swizzle the texture.
	 */
	if (htonl(0xFFu) != 0xFFu) {
		entry->image.swizzle = true;
	}

	/*
	 * Create the cairo surface and context we'll be using.
	 */
	cairo_surface_t *surface = cairo_image_surface_create(
			CAIRO_FORMAT_ARGB32,
			entry->surface.width,
			entry->surface.height
			);
	cairo_surface_set_device_scale(surface, scale, scale);
	cairo_t *cr = cairo_create(surface);

	/* Running size of current drawing area. */
	int32_t width = entry->surface.width / scale;
	int32_t height = entry->surface.height / scale;

	/* Draw the outer outline */
	color = entry->border.outline_color;
	cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
	cairo_paint(cr);

	/* Move and clip following draws to be within this outline */
	cairo_translate(
			cr,
			entry->border.outline_width,
			entry->border.outline_width);
	width -= 2 * entry->border.outline_width;
	height -= 2 * entry->border.outline_width;
	cairo_rectangle(cr, 0, 0, width, height);
	cairo_clip(cr);

	/* Draw the border */
	color = entry->border.color;
	cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
	cairo_paint(cr);

	/* Move and clip following draws to be within the border */
	cairo_translate(cr, entry->border.width, entry->border.width);
	width -= 2 * entry->border.width;
	height -= 2 * entry->border.width;
	cairo_rectangle(cr, 0, 0, width, height);
	cairo_clip(cr);

	/* Draw the inner outline */
	color = entry->border.outline_color;
	cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
	cairo_paint(cr);

	/* Move and clip following draws to be within this outline */
	cairo_translate(
			cr,
			entry->border.outline_width,
			entry->border.outline_width);
	width -= 2 * entry->border.outline_width;
	height -= 2 * entry->border.outline_width;
	cairo_rectangle(cr, 0, 0, width, height);
	cairo_clip(cr);

	/* Draw the entry background */
	color = entry->background_color;
	cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
	cairo_paint(cr);

	/* Move and clip following draws to be within the specified padding */
	cairo_translate(cr, entry->padding, entry->padding);
	width -= 2 * entry->padding;
	height -= 2 * entry->padding;
	cairo_rectangle(cr, 0, 0, width, height);
	cairo_clip(cr);

	/*
	 * Move the cursor back up, so that Pango draws in the correct place if
	 * we're doing a tight layout.
	 */
	cairo_translate(cr, -entry->text_bounds.x, -entry->text_bounds.y);

	/* Setup Pango. */
	PangoContext *context = pango_cairo_create_context(cr);
	PangoLayout *layout = pango_layout_new(context);
	pango_layout_set_text(layout, "", -1);

	PangoFontDescription *font_description =
		pango_font_description_from_string(entry->font_name);
	pango_font_description_set_size(
			font_description,
			entry->font_size * PANGO_SCALE);
	pango_layout_set_font_description(layout, font_description);
	pango_font_description_free(font_description);

	entry->pangocairo.surface = surface;
	entry->pangocairo.cr = cr;
	entry->pangocairo.layout = layout;
	entry->image.width = entry->surface.width;
	entry->image.height = entry->surface.height;
	entry->image.buffer = cairo_image_surface_get_data(surface);
}

void entry_update(struct entry *entry)
{
	cairo_t *cr = entry->pangocairo.cr;
	PangoLayout *layout = entry->pangocairo.layout;

	/* Redraw the background. */
	struct color color = entry->background_color;
	cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
	cairo_paint(cr);

	/* Draw our text with Pango. */
	color = entry->foreground_color;
	cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
	//const wchar_t *src = entry->password;
	//wcsrtombs(entry->password_mb, &src, N_ELEM(entry->password_mb), NULL);
	size_t len = 0;
	for (unsigned int i = 0; i < entry->password_length; i++) {
		len += wcrtomb(entry->password_mb + len, entry->password_character, NULL);
	}
	entry->password_mb[len] = '\0';
	fprintf(stderr, "%s\n", entry->password_mb);
	pango_layout_set_text(layout, entry->password_mb, -1);
	pango_cairo_update_layout(cr, layout);
	pango_cairo_show_layout(cr, layout);

	entry->image.redraw = true;
}

void entry_set_scale(struct entry *entry, uint32_t scale)
{
	cairo_surface_set_device_scale(entry->pangocairo.surface, scale, scale);
}

void calculate_font_extents(struct entry *entry, uint32_t scale)
{
	/*
	 * To calculate the size of the password box, we do the following:
	 * 	1. Load the font we're going to use.
	 * 	2. Create a string of the desired length using the specified
	 * 	   password character, e.g. "·······".
	 * 	3. Render the string with Pango in some abstract layout.
	 * 	4. Measure the bounding box of the layout.
	 * 	5. Add on the size of the border / outline.
	 */
	PangoFontMap *font_map = pango_cairo_font_map_get_default();
	PangoContext *context = pango_font_map_create_context(font_map);
	PangoLayout *layout = pango_layout_new(context);

	PangoFontDescription *font_description =
		pango_font_description_from_string(entry->font_name);
	pango_font_description_set_size(
			font_description,
			entry->font_size * PANGO_SCALE);
	pango_layout_set_font_description(layout, font_description);
	PangoFont *font =
		pango_font_map_load_font(font_map, context, font_description);
	pango_font_description_free(font_description);

	font_description = pango_font_describe(font);
	log_info("Using font: %s\n",
			pango_font_description_to_string(font_description));
	g_object_unref(font);

	char *buf = calloc(MAX_PASSWORD_LENGTH, 4);
	size_t len = 0;
	for (unsigned int i = 0; i < entry->num_characters; i++) {
		len += wcrtomb(buf + len, entry->password_character, NULL);
	}
	buf[len] = '\0';
	pango_layout_set_text(layout,buf, -1);
	free(buf);

	PangoRectangle rect;
	if (entry->tight_layout) {
		pango_layout_get_pixel_extents(layout, &rect, NULL);
	} else {
		pango_layout_get_pixel_extents(layout, NULL, &rect);
	}
	int width = rect.width;
	int height = rect.height;
	width += 2 * (
			entry->border.width
			+ 2 * entry->border.outline_width
			+ entry->padding
		     );
	height += 2 * (
			entry->border.width
			+ 2 * entry->border.outline_width
			+ entry->padding
		     );
	entry->surface.width = width * scale;
	entry->surface.height = height * scale;
	entry->text_bounds = rect;

	g_object_unref(layout);
	g_object_unref(context);
}