about summary refs log tree commit diff stats
path: root/font.c
blob: a6b822519ed6e957fe32dd27edbc2a4b82e491e8 (plain) (blame)
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
/*
 * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
 * See LICENSE file for license details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>

unsigned int
textwidth_l(BlitzFont *font, char *text, unsigned int len)
{
	if(font->set) {
		XRectangle r;
		XmbTextExtents(font->set, text, len, nil, &r);
		return r.width;
	}
	return XTextWidth(font->xfont, text, len);
}

unsigned int
textwidth(BlitzFont *font, char *text)
{
	return blitz_textwidth_l(font, text, strlen(text));
}

void
loadfont(Blitz *blitz, BlitzFont *font)
{
	char *fontname = font->fontstr;
	char **missing = nil, *def = "?";
	int n;

	setlocale(LC_ALL, "");
	if(font->set)
		XFreeFontSet(blitz->dpy, font->set);
	font->set = XCreateFontSet(blitz->dpy, fontname, &missing, &n, &def);
	if(missing) {
		while(n--)
			fprintf(stderr, "missing fontset: %s\n", missing[n]);
		XFreeStringList(missing);
		if(font->set) {
			XFreeFontSet(blitz->dpy, font->set);
			font->set = nil;
		}
	}
	if(font->set) {
		XFontSetExtents *font_extents;
		XFontStruct **xfonts;
		char **font_names;
		unsigned int i;

		font->ascent = font->descent = 0;
		font_extents = XExtentsOfFontSet(font->set);
		n = XFontsOfFontSet(font->set, &xfonts, &font_names);
		for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
			if(font->ascent < (*xfonts)->ascent)
				font->ascent = (*xfonts)->ascent;
			if(font->descent < (*xfonts)->descent)
				font->descent = (*xfonts)->descent;
			xfonts++;
		}
	}
	else {
		if(font->xfont)
			XFreeFont(blitz->dpy, font->xfont);
		font->xfont = nil;
		font->xfont = XLoadQueryFont(blitz->dpy, fontname);
		if (!font->xfont) {
			fontname = "fixed";
			font->xfont = XLoadQueryFont(blitz->dpy, fontname);
		}
		if (!font->xfont) {
			fprintf(stderr, "%s", "error, cannot load 'fixed' font\n");
			exit(1);
		}
		font->ascent = font->xfont->ascent;
		font->descent = font->xfont->descent;
	}
}
/span> == 'source' then source.initialize(arg) else assert(false, 'unknown app "'..Current_app..'"') end end function App.resize(w,h) if Current_app == 'run' then if run.resize then run.resize(w,h) end elseif Current_app == 'source' then if source.resize then source.resize(w,h) end else assert(false, 'unknown app "'..Current_app..'"') end Last_resize_time = Current_time end function App.filedropped(file) if Current_app == 'run' then if run.filedropped then run.filedropped(file) end elseif Current_app == 'source' then if source.filedropped then source.filedropped(file) end else assert(false, 'unknown app "'..Current_app..'"') end end function App.focus(in_focus) if in_focus then Last_focus_time = Current_time end if Current_app == 'run' then if run.focus then run.focus(in_focus) end elseif Current_app == 'source' then if source.focus then source.focus(in_focus) end else assert(false, 'unknown app "'..Current_app..'"') end end function App.draw() if Current_app == 'run' then run.draw() elseif Current_app == 'source' then source.draw() else assert(false, 'unknown app "'..Current_app..'"') end end function App.update(dt) Current_time = Current_time + dt -- some hysteresis while resizing if Current_time < Last_resize_time + 0.1 then return end -- if Current_app == 'run' then run.update(dt) elseif Current_app == 'source' then source.update(dt) else assert(false, 'unknown app "'..Current_app..'"') end end function App.keychord_pressed(chord, key) -- ignore events for some time after window in focus (mostly alt-tab) if Current_time < Last_focus_time + 0.01 then return end -- if chord == 'C-e' then -- carefully save settings if Current_app == 'run' then local source_settings = Settings.source Settings = run.settings() Settings.source = source_settings if run.quit then run.quit() end Current_app = 'source' elseif Current_app == 'source' then Settings.source = source.settings() if source.quit then source.quit() end Current_app = 'run' else assert(false, 'unknown app "'..Current_app..'"') end Settings.current_app = Current_app love.filesystem.write('config', json.encode(Settings)) -- reboot load_file_from_source_or_save_directory('main.lua') App.undo_initialize() App.run_tests_and_initialize() return end if Current_app == 'run' then if run.keychord_pressed then run.keychord_pressed(chord, key) end elseif Current_app == 'source' then if source.keychord_pressed then source.keychord_pressed(chord, key) end else assert(false, 'unknown app "'..Current_app..'"') end end function App.textinput(t) -- ignore events for some time after window in focus (mostly alt-tab) if Current_time < Last_focus_time + 0.01 then return end -- if Current_app == 'run' then if run.textinput then run.textinput(t) end elseif Current_app == 'source' then if source.textinput then source.textinput(t) end else assert(false, 'unknown app "'..Current_app..'"') end end function App.keyreleased(chord, key) -- ignore events for some time after window in focus (mostly alt-tab) if Current_time < Last_focus_time + 0.01 then return end -- if Current_app == 'run' then if run.key_released then run.key_released(chord, key) end elseif Current_app == 'source' then if source.key_released then source.key_released(chord, key) end else assert(false, 'unknown app "'..Current_app..'"') end end function App.mousepressed(x,y, mouse_button) --? print('mouse press', x,y) if Current_app == 'run' then if run.mouse_pressed then run.mouse_pressed(x,y, mouse_button) end elseif Current_app == 'source' then if source.mouse_pressed then source.mouse_pressed(x,y, mouse_button) end else assert(false, 'unknown app "'..Current_app..'"') end end function App.mousereleased(x,y, mouse_button) if Current_app == 'run' then if run.mouse_released then run.mouse_released(x,y, mouse_button) end elseif Current_app == 'source' then if source.mouse_released then source.mouse_released(x,y, mouse_button) end else assert(false, 'unknown app "'..Current_app..'"') end end function love.quit() if Current_app == 'run' then local source_settings = Settings.source Settings = run.settings() Settings.source = source_settings else Settings.source = source.settings() end Settings.current_app = Current_app love.filesystem.write('config', json.encode(Settings)) if Current_app == 'run' then if run.quit then run.quit() end elseif Current_app == 'source' then if source.quit then source.quit() end else assert(false, 'unknown app "'..Current_app..'"') end end