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
73 if (length == 0) {
74 *width = 0;
75 return NSERROR_OK;
76 }
77
79
80 desc = nsfont_style_to_description(fstyle);
81 pango_layout_set_font_description(nsfont_pango_layout, desc);
82 pango_font_description_free(desc);
83
84 pango_layout_set_text(nsfont_pango_layout, string, length);
85
86 pango_layout_get_pixel_size(nsfont_pango_layout, width, 0);
87
88 NSLOG(netsurf, DEEPDEBUG,
89 "fstyle: %p string:\"%.*s\", length: %" PRIsizet ", width: %dpx",
90 fstyle, (int)length, string, length, *width);
91
92
93 return NSERROR_OK;
94}
95
96
97/**
98 * Find the position in a string where an x coordinate falls.
99 *
100 * \param[in] fstyle style for this text
101 * \param[in] string UTF-8 string to measure
102 * \param[in] length length of string, in bytes
103 * \param[in] x coordinate to search for
104 * \param[out] char_offset updated to offset in string of actual_x, [0..length]
105 * \param[out] actual_x updated to x coordinate of character closest to x
106 * \return NSERROR_OK and char_offset and actual_x updated or appropriate
107 * error code on faliure
108 */
109static nserror
111 const char *string,
112 size_t length,
113 int x,
114 size_t *char_offset,
115 int *actual_x)
116{
117 int index;
118 PangoFontDescription *desc;
119 PangoRectangle pos;
120
122
123 desc = nsfont_style_to_description(fstyle);
124 pango_layout_set_font_description(nsfont_pango_layout, desc);
125 pango_font_description_free(desc);
126
127 pango_layout_set_text(nsfont_pango_layout, string, length);
128
129 if (pango_layout_xy_to_index(nsfont_pango_layout,
130 x * PANGO_SCALE,
131 0, &index, 0) == FALSE) {
132 index = length;
133 }
134
135 pango_layout_index_to_pos(nsfont_pango_layout, index, &pos);
136
137 *char_offset = index;
138 *actual_x = PANGO_PIXELS(pos.x);
139
140 NSLOG(netsurf, DEEPDEBUG,
141 "fstyle: %p string:\"%.*s\", length: %" PRIsizet ", "
142 "search_x: %dpx, offset: %" PRIsizet ", actual_x: %dpx",
143 fstyle, (int)length, string, length, x, *char_offset, *actual_x);
144 return NSERROR_OK;
145}
146
147
148/**
149 * Find where to split a string to make it fit a width.
150 *
151 * \param[in] fstyle style for this text
152 * \param[in] string UTF-8 string to measure
153 * \param[in] length length of string, in bytes
154 * \param[in] x width available
155 * \param[out] char_offset updated to offset in string of actual_x, [1..length]
156 * \param[out] actual_x updated to x coordinate of character closest to x
157 * \return NSERROR_OK or appropriate error code on faliure
158 *
159 * On exit, char_offset indicates first character after split point.
160 *
161 * \note char_offset of 0 must never be returned.
162 *
163 * Returns:
164 * char_offset giving split point closest to x, where actual_x <= x
165 * else
166 * char_offset giving split point closest to x, where actual_x > x
167 *
168 * Returning char_offset == length means no split possible
169 */
170static nserror
172 const char *string,
173 size_t length,
174 int x,
175 size_t *char_offset,
176 int *actual_x)
177{
178 int index = length;
179 PangoFontDescription *desc;
180 PangoContext *context;
181 PangoLayout *layout;
182 PangoLayoutLine *line;
183
184 context = gdk_pango_context_get();
185 layout = pango_layout_new(context);
186
187 desc = nsfont_style_to_description(fstyle);
188 pango_layout_set_font_description(layout, desc);
189 pango_font_description_free(desc);
190
191 pango_layout_set_text(layout, string, length);
192
193 /* Limit width of layout to the available width */
194 pango_layout_set_width(layout, x * PANGO_SCALE);
195
196 /* Request word wrapping */
197 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
198
199 /* Prevent pango treating linebreak characters as line breaks */
200 pango_layout_set_single_paragraph_mode(layout, TRUE);
201
202 /* Obtain the second line of the layout (if there is one) */
203 line = pango_layout_get_line_readonly(layout, 1);
204 if (line != NULL) {
205 /* Pango split the text. The line's start_index indicates the
206 * start of the character after the line break. */
207 index = line->start_index;
208 }
209
210 g_object_unref(layout);
211 g_object_unref(context);
212
213 *char_offset = index;
214 /* Obtain the pixel offset of the split character */
215 nsfont_width(fstyle, string, index, actual_x);
216
217 NSLOG(netsurf, DEEPDEBUG,
218 "fstyle: %p string:\"%.*s\", length: %" PRIsizet ", "
219 "split_x: %dpx, offset: %" PRIsizet ", actual_x: %dpx",
220 fstyle, (int)length, string, length, x, *char_offset, *actual_x);
221 return NSERROR_OK;
222}
223
224
225/**
226 * Render a string.
227 *
228 * \param x x coordinate
229 * \param y y coordinate
230 * \param string UTF-8 string to measure
231 * \param length length of string
232 * \param fstyle plot style for this text
233 * \return true on success, false on error and error reported
234 */
235nserror nsfont_paint(int x, int y, const char *string, size_t length,
236 const plot_font_style_t *fstyle)
237{
238 PangoFontDescription *desc;
239 PangoLayoutLine *line;
240
241 if (length == 0)
242 return NSERROR_OK;
243
245
246 desc = nsfont_style_to_description(fstyle);
247 pango_layout_set_font_description(nsfont_pango_layout, desc);
248 pango_font_description_free(desc);
249
250 pango_layout_set_text(nsfont_pango_layout, string, length);
251
252 line = pango_layout_get_line_readonly(nsfont_pango_layout, 0);
253 cairo_move_to(current_cr, x, y);
255 pango_cairo_show_layout_line(current_cr, line);
256
257 return NSERROR_OK;
258}
259
260
261/* exported interface documented in gtk/layout_pango.h */
262PangoFontDescription *
264{
265 unsigned int size;
266 PangoFontDescription *desc;
267 PangoStyle style = PANGO_STYLE_NORMAL;
268
269 switch (fstyle->family) {
271 desc = pango_font_description_from_string(nsoption_charp(font_serif));
272 break;
274 desc = pango_font_description_from_string(nsoption_charp(font_mono));
275 break;
277 desc = pango_font_description_from_string(nsoption_charp(font_cursive));
278 break;
280 desc = pango_font_description_from_string(nsoption_charp(font_fantasy));
281 break;
283 default:
284 desc = pango_font_description_from_string(nsoption_charp(font_sans));
285 break;
286 }
287
288 size = (fstyle->size * PANGO_SCALE) / PLOT_STYLE_SCALE;
289
290 if (fstyle->flags & FONTF_ITALIC)
291 style = PANGO_STYLE_ITALIC;
292 else if (fstyle->flags & FONTF_OBLIQUE)
293 style = PANGO_STYLE_OBLIQUE;
294
295 pango_font_description_set_style(desc, style);
296
297 pango_font_description_set_weight(desc, (PangoWeight) fstyle->weight);
298
299 pango_font_description_set_size(desc, size);
300
301 if (fstyle->flags & FONTF_SMALLCAPS) {
302 pango_font_description_set_variant(desc,
303 PANGO_VARIANT_SMALL_CAPS);
304 } else {
305 pango_font_description_set_variant(desc, PANGO_VARIANT_NORMAL);
306 }
307
308 return desc;
309}
310
313 .position = nsfont_position_in_string,
314 .split = nsfont_split,
315};
316
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_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:110
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:263
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:235
static PangoContext * nsfont_pango_context
Definition: layout_pango.c:40
static struct gui_layout_table layout_table
Definition: layout_pango.c:311
struct gui_layout_table * nsgtk_layout_table
Definition: layout_pango.c:317
static nserror nsfont_split(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x)
Find where to split a string to make it fit a width.
Definition: layout_pango.c:171
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:160
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