NetSurf
layout_pango.c
Go to the documentation of this file.
1/*
2 * Copyright 2005 James Bursa <bursa@users.sourceforge.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * \file
21 * GTK implementation of layout handling using pango.
22 *
23 * Pango is used handle and render fonts.
24 */
25
26
27#include <assert.h>
28#include <stdio.h>
29#include <gtk/gtk.h>
30
31#include "utils/log.h"
32#include "utils/nsoption.h"
33#include "netsurf/inttypes.h"
34#include "netsurf/layout.h"
35#include "netsurf/plot_style.h"
36
37#include "gtk/layout_pango.h"
38#include "gtk/plotters.h"
39
40static PangoContext *nsfont_pango_context = NULL;
41static PangoLayout *nsfont_pango_layout = NULL;
42
43static inline void nsfont_pango_check(void)
44{
45 if (nsfont_pango_context == NULL) {
46 NSLOG(netsurf, INFO, "Creating nsfont_pango_context.");
47 nsfont_pango_context = gdk_pango_context_get();
48 }
49
50 if (nsfont_pango_layout == NULL) {
51 NSLOG(netsurf, INFO, "Creating nsfont_pango_layout.");
52 nsfont_pango_layout = pango_layout_new(nsfont_pango_context);
53 }
54}
55
56/**
57 * Measure the width of a string.
58 *
59 * \param[in] fstyle plot style for this text
60 * \param[in] string UTF-8 string to measure
61 * \param[in] length length of string, in bytes
62 * \param[out] width updated to width of string[0..length)
63 * \return NSERROR_OK and width updated or appropriate error code on faliure
64 */
65static nserror
67 const char *string,
68 size_t length,
69 int *width)
70{
71 PangoFontDescription *desc;
72 PangoRectangle pos;
73
74 if (length == 0) {
75 *width = 0;
76 return NSERROR_OK;
77 }
78
80
81 desc = nsfont_style_to_description(fstyle);
82 pango_layout_set_font_description(nsfont_pango_layout, desc);
83 pango_font_description_free(desc);
84
85 pango_layout_set_text(nsfont_pango_layout, string, length);
86
87 pango_layout_index_to_pos(nsfont_pango_layout, length, &pos);
88 *width = PANGO_PIXELS(pos.x);
89
90 NSLOG(netsurf, DEEPDEBUG,
91 "fstyle: %p string:\"%.*s\", length: %" PRIsizet ", width: %dpx",
92 fstyle, (int)length, string, length, *width);
93
94
95 return NSERROR_OK;
96}
97
98/**
99 * Find the position in a string where an x coordinate falls.
100 *
101 * \param[in] layout A pango layout with font set
102 * \param[in] string UTF-8 string to measure
103 * \param[in] length length of string, in bytes
104 * \param[in] x coordinate to search for
105 * \param[out] string_idx updated to offset in string of actual_x, [0..length]
106 * \param[out] actual_x updated to x coordinate of character closest to x or full length if string_idx is 0
107 * \return NSERROR_OK and string_idx and actual_x updated or appropriate error code on faliure
108 */
109static nserror
110layout_position(PangoLayout *layout,
111 const char *string,
112 size_t length,
113 int x,
114 size_t *string_idx,
115 int *actual_x)
116{
117 int index;
118 PangoRectangle pos;
119
120 /* deal with empty string */
121 if (length == 0) {
122 *string_idx = 0;
123 *actual_x = 0;
124 return NSERROR_OK;
125 }
126
127 x--; /* split x coordinate is exclusive */
128
129 pango_layout_set_text(layout, string, length);
130
131 if (x <= 0) {
132 /* deal with negative or zero available width */
133 index = 0;
134 } else {
135 /* compute index into string */
136 if (pango_layout_xy_to_index(layout,
137 x * PANGO_SCALE,
138 0, &index, 0) == FALSE) {
139 /* whole string fits */
140 index = length;
141 }
142 }
143
144 *string_idx = index;
145 /* if the split is at index 0 return the whole string length */
146 if (index == 0) {
147 index = length;
148 }
149 pango_layout_index_to_pos(layout, index, &pos);
150 *actual_x = PANGO_PIXELS(pos.x);
151
152 return NSERROR_OK;
153}
154
155/**
156 * Find the position in a string where an x coordinate falls.
157 *
158 * \param[in] fstyle style for this text
159 * \param[in] string UTF-8 string to measure
160 * \param[in] length length of string, in bytes
161 * \param[in] x coordinate to search for
162 * \param[out] char_offset updated to offset in string of actual_x, [0..length]
163 * \param[out] actual_x updated to x coordinate of character closest to x
164 * \return NSERROR_OK and char_offset and actual_x updated or appropriate
165 * error code on faliure
166 */
167static nserror
169 const char *string,
170 size_t length,
171 int x,
172 size_t *char_offset,
173 int *actual_x)
174{
175 PangoFontDescription *desc;
176 nserror res;
177
179
180 desc = nsfont_style_to_description(fstyle);
181 pango_layout_set_font_description(nsfont_pango_layout, desc);
182 pango_font_description_free(desc);
183
185 string,
186 length,
187 x,
188 char_offset,
189 actual_x);
190
191 NSLOG(netsurf, DEEPDEBUG,
192 "fstyle: %p string:\"%.*s\", length: %" PRIsizet ", "
193 "search_x: %dpx, offset: %" PRIsizet ", actual_x: %dpx",
194 fstyle, (int)length, string, length, x, *char_offset, *actual_x);
195 return res;
196}
197
198
199/**
200 * Find where to split a string to make it fit a width.
201 *
202 * \param[in] fstyle style for this text
203 * \param[in] string UTF-8 string to measure
204 * \param[in] length length of string, in bytes
205 * \param[in] x width available
206 * \param[out] char_offset updated to offset in string of actual_x, [1..length]
207 * \param[out] actual_x updated to x coordinate of character closest to x
208 * \return NSERROR_OK or appropriate error code on faliure
209 *
210 * On exit, char_offset indicates first character after split point.
211 *
212 * \note char_offset of 0 must never be returned.
213 *
214 * Returns:
215 * char_offset giving split point closest to x, where actual_x < x
216 * else
217 * char_offset giving split point closest to x, where actual_x >= x
218 *
219 * Returning char_offset == length means no split possible
220 */
221static nserror
223 const char *string,
224 size_t length,
225 int x,
226 size_t *string_idx,
227 int *actual_x)
228{
229 nserror res;
230 PangoContext *context;
231 PangoLayout *layout;
232 PangoFontDescription *desc;
233 size_t split_len;
234 int split_x;
235 size_t str_len;
236 PangoRectangle pos;
237
238 context = gdk_pango_context_get();
239 layout = pango_layout_new(context);
240
241 desc = nsfont_style_to_description(fstyle);
242 pango_layout_set_font_description(layout, desc);
243 pango_font_description_free(desc);
244
245 res = layout_position(layout,
246 string,
247 length,
248 x,
249 &split_len,
250 &split_x);
251 if (res != NSERROR_OK) {
252 goto split_done;
253 }
254
255 /* deal with being unable to split */
256 if ((split_len < 1) || (split_len >= length)) {
257 *string_idx = length;
258 *actual_x = split_x;
259 goto split_done;
260 }
261
262 /* if string broke on boundary do not attempt to adjust */
263 if (string[split_len] == ' ') {
264 *string_idx = split_len;
265 *actual_x = split_x;
266 goto split_done;
267 }
268
269 /* attempt to break string */
270 str_len = split_len;
271
272 /* walk backwards through string looking for space to break on */
273 while ((string[str_len] != ' ') &&
274 (str_len > 0)) {
275 str_len--;
276 }
277
278 /* walk forwards through string looking for space if back failed */
279 if (str_len == 0) {
280 str_len = split_len;
281 while ((str_len < length) &&
282 (string[str_len] != ' ')) {
283 str_len++;
284 }
285 }
286 /* include breaking character in match */
287 if ((str_len < length) && (string[str_len] == ' ')) {
288 str_len++;
289 }
290
291 *string_idx = str_len;
292
293 pango_layout_index_to_pos(layout, str_len, &pos);
294 *actual_x = PANGO_PIXELS(pos.x);
295
296split_done:
297 g_object_unref(layout);
298 g_object_unref(context);
299
300 NSLOG(netsurf, DEEPDEBUG,
301 "fstyle: %p string:\"%.*s\" / \"%.*s\", length: %" PRIsizet ", "
302 "split_x: %dpx, offset: %" PRIsizet ", actual_x: %dpx",
303 fstyle, (int)(*string_idx), string, (int)(length - *string_idx),
304 string+*string_idx, length, x, *string_idx, *actual_x);
305 return res;
306}
307
308
309/**
310 * Render a string.
311 *
312 * \param x x coordinate
313 * \param y y coordinate
314 * \param string UTF-8 string to measure
315 * \param length length of string
316 * \param fstyle plot style for this text
317 * \return true on success, false on error and error reported
318 */
319nserror nsfont_paint(int x, int y, const char *string, size_t length,
320 const plot_font_style_t *fstyle)
321{
322 PangoFontDescription *desc;
323 PangoLayoutLine *line;
324
325 if (length == 0)
326 return NSERROR_OK;
327
329
330 desc = nsfont_style_to_description(fstyle);
331 pango_layout_set_font_description(nsfont_pango_layout, desc);
332 pango_font_description_free(desc);
333
334 pango_layout_set_text(nsfont_pango_layout, string, length);
335
336 line = pango_layout_get_line_readonly(nsfont_pango_layout, 0);
337 cairo_move_to(current_cr, x, y);
339 pango_cairo_show_layout_line(current_cr, line);
340
341 return NSERROR_OK;
342}
343
344
345/* exported interface documented in gtk/layout_pango.h */
346PangoFontDescription *
348{
349 unsigned int size;
350 PangoFontDescription *desc;
351 PangoStyle style = PANGO_STYLE_NORMAL;
352
353 switch (fstyle->family) {
355 desc = pango_font_description_from_string(nsoption_charp(font_serif));
356 break;
358 desc = pango_font_description_from_string(nsoption_charp(font_mono));
359 break;
361 desc = pango_font_description_from_string(nsoption_charp(font_cursive));
362 break;
364 desc = pango_font_description_from_string(nsoption_charp(font_fantasy));
365 break;
367 default:
368 desc = pango_font_description_from_string(nsoption_charp(font_sans));
369 break;
370 }
371
372 size = (fstyle->size * PANGO_SCALE) / PLOT_STYLE_SCALE;
373
374 if (fstyle->flags & FONTF_ITALIC)
375 style = PANGO_STYLE_ITALIC;
376 else if (fstyle->flags & FONTF_OBLIQUE)
377 style = PANGO_STYLE_OBLIQUE;
378
379 pango_font_description_set_style(desc, style);
380
381 pango_font_description_set_weight(desc, (PangoWeight) fstyle->weight);
382
383 pango_font_description_set_size(desc, size);
384
385 if (fstyle->flags & FONTF_SMALLCAPS) {
386 pango_font_description_set_variant(desc,
387 PANGO_VARIANT_SMALL_CAPS);
388 } else {
389 pango_font_description_set_variant(desc, PANGO_VARIANT_NORMAL);
390 }
391
392 return desc;
393}
394
397 .position = nsfont_position_in_string,
398 .split = nsfont_split,
399};
400
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
Target independent plotting GTK+ interface.
void nsgtk_set_colour(colour c)
Set cairo context colour to nsgtk colour.
Definition: plotters.c:51
cairo_t * current_cr
Definition: plotters.c:42
Interface to platform-specific layout operation table.
Netsurf additional integer type formatting macros.
#define PRIsizet
c99 standard printf formatting for size_t type
Definition: inttypes.h:53
static nserror nsfont_split(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *string_idx, int *actual_x)
Find where to split a string to make it fit a width.
Definition: layout_pango.c:222
static nserror nsfont_position_in_string(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x)
Find the position in a string where an x coordinate falls.
Definition: layout_pango.c:168
static PangoLayout * nsfont_pango_layout
Definition: layout_pango.c:41
PangoFontDescription * nsfont_style_to_description(const plot_font_style_t *fstyle)
Definition: layout_pango.c:347
static void nsfont_pango_check(void)
Definition: layout_pango.c:43
static nserror nsfont_width(const plot_font_style_t *fstyle, const char *string, size_t length, int *width)
Measure the width of a string.
Definition: layout_pango.c:66
nserror nsfont_paint(int x, int y, const char *string, size_t length, const plot_font_style_t *fstyle)
Render a string.
Definition: layout_pango.c:319
static PangoContext * nsfont_pango_context
Definition: layout_pango.c:40
static struct gui_layout_table layout_table
Definition: layout_pango.c:395
struct gui_layout_table * nsgtk_layout_table
Definition: layout_pango.c:401
static nserror layout_position(PangoLayout *layout, const char *string, size_t length, int x, size_t *string_idx, int *actual_x)
Find the position in a string where an x coordinate falls.
Definition: layout_pango.c:110
Interface to GTK layout handling using pango.
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
plotter style interfaces, generic styles and style colour helpers.
@ FONTF_ITALIC
Definition: plot_style.h:103
@ FONTF_SMALLCAPS
Definition: plot_style.h:105
@ FONTF_OBLIQUE
Definition: plot_style.h:104
@ PLOT_FONT_FAMILY_CURSIVE
Definition: plot_style.h:92
@ PLOT_FONT_FAMILY_SANS_SERIF
Definition: plot_style.h:89
@ PLOT_FONT_FAMILY_FANTASY
Definition: plot_style.h:93
@ PLOT_FONT_FAMILY_MONOSPACE
Definition: plot_style.h:91
@ PLOT_FONT_FAMILY_SERIF
Definition: plot_style.h:90
#define PLOT_STYLE_SCALE
Scaling factor for plot styles.
Definition: plot_style.h:45
int width
Definition: gui.c:161
nserror(* width)(const struct plot_font_style *fstyle, const char *string, size_t length, int *width)
Measure the width of a string.
Definition: layout.h:49
Font style for plotting.
Definition: plot_style.h:111
plot_font_generic_family_t family
Generic family to plot with.
Definition: plot_style.h:118
colour foreground
Colour of text.
Definition: plot_style.h:123
plot_font_flags_t flags
Font flags.
Definition: plot_style.h:121
plot_style_fixed size
Font size, in pt.
Definition: plot_style.h:119
int weight
Font weight: value in range [100,900] as per CSS.
Definition: plot_style.h:120
Option reading and saving interface.
#define nsoption_charp(OPTION)
Get the value of a string option.
Definition: nsoption.h:331
static nserror line(const struct redraw_context *ctx, const plot_style_t *style, const struct rect *line)
Plots a line.
Definition: plot.c:579