summaryrefslogtreecommitdiff
path: root/gtk/font_pango.c
blob: a376967a5a5faaf5eb674cbb04eae6761de336fd (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
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2004 James Bursa <bursa@users.sourceforge.net>
 */

#include <assert.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include "netsurf/css/css.h"
#include "netsurf/render/font.h"
#include "netsurf/utils/utils.h"
#include "netsurf/utils/log.h"


struct font_set {
	int x;
};


struct font_set *nsfont_new_set()
{
	return 0;
}


struct font_data *nsfont_open(struct font_set *set, struct css_style *style)
{
	struct font_data *data;
	unsigned int size = PANGO_SCALE * 11;
	PangoFontDescription *fontdesc;
	PangoWeight weight = PANGO_WEIGHT_NORMAL;
	PangoStyle styl = PANGO_STYLE_NORMAL;

	if (style->font_size.size == CSS_FONT_SIZE_LENGTH)
		size = style->font_size.value.length.value * PANGO_SCALE;

	switch (style->font_weight) {
		case CSS_FONT_WEIGHT_NORMAL:
			weight = PANGO_WEIGHT_NORMAL; break;
		case CSS_FONT_WEIGHT_BOLD:
			weight = PANGO_WEIGHT_BOLD; break;
		case CSS_FONT_WEIGHT_100: weight = 100; break;
		case CSS_FONT_WEIGHT_200: weight = 200; break;
		case CSS_FONT_WEIGHT_300: weight = 300; break;
		case CSS_FONT_WEIGHT_400: weight = 400; break;
		case CSS_FONT_WEIGHT_500: weight = 500; break;
		case CSS_FONT_WEIGHT_600: weight = 600; break;
		case CSS_FONT_WEIGHT_700: weight = 700; break;
		case CSS_FONT_WEIGHT_800: weight = 800; break;
		case CSS_FONT_WEIGHT_900: weight = 900; break;
		default: break;
	}

	switch (style->font_style) {
		case CSS_FONT_STYLE_ITALIC: styl = PANGO_STYLE_ITALIC; break;
		case CSS_FONT_STYLE_OBLIQUE: styl = PANGO_STYLE_OBLIQUE; break;
		default: break;
	}

	fontdesc = pango_font_description_new();
	pango_font_description_set_size(fontdesc, size);
	pango_font_description_set_family_static(fontdesc, "Sans");
	pango_font_description_set_weight(fontdesc, weight);
	pango_font_description_set_style(fontdesc, styl);

	data = malloc(sizeof *data);
	assert(data);

	data->id = fontdesc;
	data->size = size;
	data->space_width = font_width(data, " ", 1);

	return data;
}


void nsfont_free_set(struct font_set *set)
{
}


unsigned long nsfont_width(struct font_data *font, const char *text,
		unsigned int length)
{
	int width;
	PangoContext *context;
	PangoLayout *layout;

	assert(font && text);

	if (length == 0)
		return 0;

	context = gdk_pango_context_get();
	layout = pango_layout_new(context);
	pango_layout_set_font_description(layout, font->id);
	pango_layout_set_text(layout, text, length);

	pango_layout_get_pixel_size(layout, &width, 0);

	g_object_unref(layout);
	g_object_unref(context);

	return width;
}


void nsfont_position_in_string(struct font_data *font, const char *text,
		unsigned int length, unsigned long x, int *char_offset,
		int *pixel_offset)
{
	int index;
	PangoContext *context;
	PangoLayout *layout;
	PangoRectangle pos;

	assert(font && text);

	context = gdk_pango_context_get();
	layout = pango_layout_new(context);
	pango_layout_set_font_description(layout, font->id);
	pango_layout_set_text(layout, text, length);

	pango_layout_xy_to_index(layout, x * PANGO_SCALE, 0, &index, 0);
	pango_layout_index_to_pos(layout, index, &pos);

	g_object_unref(layout);
	g_object_unref(context);

	*char_offset = index;
	*pixel_offset = PANGO_PIXELS(pos.x);
}


char *nsfont_split(struct font_data *font, const char *text, unsigned int length,
		unsigned int width, unsigned int *used_width)
{
	int index = length;
	int x_pos;
	PangoContext *context;
	PangoLayout *layout;
	PangoLayoutLine *line;

	assert(font && text);

	context = gdk_pango_context_get();
	layout = pango_layout_new(context);
	pango_layout_set_font_description(layout, font->id);
	pango_layout_set_text(layout, text, length);

	pango_layout_set_width(layout, width * PANGO_SCALE);
	pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
	line = pango_layout_get_line(layout, 1);
	if (line)
		index = line->start_index - 1;
	pango_layout_line_index_to_x(pango_layout_get_line(layout, 0),
			index, 0, &x_pos);

	g_object_unref(layout);
	g_object_unref(context);

	*used_width = PANGO_PIXELS(x_pos);
	return text + index;
}