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 return NSERROR_OK;
141}
142
143
144/**
145 * Find where to split a string to make it fit a width.
146 *
147 * \param[in] fstyle style for this text
148 * \param[in] string UTF-8 string to measure
149 * \param[in] length length of string, in bytes
150 * \param[in] x width available
151 * \param[out] char_offset updated to offset in string of actual_x, [1..length]
152 * \param[out] actual_x updated to x coordinate of character closest to x
153 * \return NSERROR_OK or appropriate error code on faliure
154 *
155 * On exit, char_offset indicates first character after split point.
156 *
157 * \note char_offset of 0 must never be returned.
158 *
159 * Returns:
160 * char_offset giving split point closest to x, where actual_x <= x
161 * else
162 * char_offset giving split point closest to x, where actual_x > x
163 *
164 * Returning char_offset == length means no split possible
165 */
166static nserror
168 const char *string,
169 size_t length,
170 int x,
171 size_t *char_offset,
172 int *actual_x)
173{
174 int index = length;
175 PangoFontDescription *desc;
176 PangoContext *context;
177 PangoLayout *layout;
178 PangoLayoutLine *line;
179
180 context = gdk_pango_context_get();
181 layout = pango_layout_new(context);
182
183 desc = nsfont_style_to_description(fstyle);
184 pango_layout_set_font_description(layout, desc);
185 pango_font_description_free(desc);
186
187 pango_layout_set_text(layout, string, length);
188
189 /* Limit width of layout to the available width */
190 pango_layout_set_width(layout, x * PANGO_SCALE);
191
192 /* Request word wrapping */
193 pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
194
195 /* Prevent pango treating linebreak characters as line breaks */
196 pango_layout_set_single_paragraph_mode(layout, TRUE);
197
198 /* Obtain the second line of the layout (if there is one) */
199 line = pango_layout_get_line_readonly(layout, 1);
200 if (line != NULL) {
201 /* Pango split the text. The line's start_index indicates the
202 * start of the character after the line break. */
203 index = line->start_index;
204 }
205
206 g_object_unref(layout);
207 g_object_unref(context);
208
209 *char_offset = index;
210 /* Obtain the pixel offset of the split character */
211 nsfont_width(fstyle, string, index, actual_x);
212
213 return NSERROR_OK;
214}
215
216
217/**
218 * Render a string.
219 *
220 * \param x x coordinate
221 * \param y y coordinate
222 * \param string UTF-8 string to measure
223 * \param length length of string
224 * \param fstyle plot style for this text
225 * \return true on success, false on error and error reported
226 */
227nserror nsfont_paint(int x, int y, const char *string, size_t length,
228 const plot_font_style_t *fstyle)
229{
230 PangoFontDescription *desc;
231 PangoLayoutLine *line;
232
233 if (length == 0)
234 return NSERROR_OK;
235
237
238 desc = nsfont_style_to_description(fstyle);
239 pango_layout_set_font_description(nsfont_pango_layout, desc);
240 pango_font_description_free(desc);
241
242 pango_layout_set_text(nsfont_pango_layout, string, length);
243
244 line = pango_layout_get_line_readonly(nsfont_pango_layout, 0);
245 cairo_move_to(current_cr, x, y);
247 pango_cairo_show_layout_line(current_cr, line);
248
249 return NSERROR_OK;
250}
251
252
253/* exported interface documented in gtk/layout_pango.h */
254PangoFontDescription *
256{
257 unsigned int size;
258 PangoFontDescription *desc;
259 PangoStyle style = PANGO_STYLE_NORMAL;
260
261 switch (fstyle->family) {
263 desc = pango_font_description_from_string(nsoption_charp(font_serif));
264 break;
266 desc = pango_font_description_from_string(nsoption_charp(font_mono));
267 break;
269 desc = pango_font_description_from_string(nsoption_charp(font_cursive));
270 break;
272 desc = pango_font_description_from_string(nsoption_charp(font_fantasy));
273 break;
275 default:
276 desc = pango_font_description_from_string(nsoption_charp(font_sans));
277 break;
278 }
279
280 size = (fstyle->size * PANGO_SCALE) / PLOT_STYLE_SCALE;
281
282 if (fstyle->flags & FONTF_ITALIC)
283 style = PANGO_STYLE_ITALIC;
284 else if (fstyle->flags & FONTF_OBLIQUE)
285 style = PANGO_STYLE_OBLIQUE;
286
287 pango_font_description_set_style(desc, style);
288
289 pango_font_description_set_weight(desc, (PangoWeight) fstyle->weight);
290
291 pango_font_description_set_size(desc, size);
292
293 if (fstyle->flags & FONTF_SMALLCAPS) {
294 pango_font_description_set_variant(desc,
295 PANGO_VARIANT_SMALL_CAPS);
296 } else {
297 pango_font_description_set_variant(desc, PANGO_VARIANT_NORMAL);
298 }
299
300 return desc;
301}
302
305 .position = nsfont_position_in_string,
306 .split = nsfont_split,
307};
308
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:255
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:227
static PangoContext * nsfont_pango_context
Definition: layout_pango.c:40
static struct gui_layout_table layout_table
Definition: layout_pango.c:303
struct gui_layout_table * nsgtk_layout_table
Definition: layout_pango.c:309
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:167
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:159
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:297
static nserror line(const struct redraw_context *ctx, const plot_style_t *style, const struct rect *line)
Plots a line.
Definition: plot.c:579