rsvg-text.c 25.2 KB
Newer Older
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
/* vim: set sw=4: -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
   rsvg-text.c: Text handling routines for RSVG

   Copyright (C) 2000 Eazel, Inc.
   Copyright (C) 2002 Dom Lachowicz <cinamod@hotmail.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Raph Levien <raph@artofcode.com>
*/

#include <string.h>

#include "rsvg-private.h"
29
#include "rsvg-styles.h"
30 31 32 33 34
#include "rsvg-text.h"
#include "rsvg-css.h"

#include "rsvg-shapes.h"

35 36 37 38
#include <ft2build.h>
#include FT_GLYPH_H
#include FT_OUTLINE_H

Dom Lachowicz's avatar
Dom Lachowicz committed
39
#include <pango/pangoft2.h>
40

Caleb Michael Moore's avatar
Caleb Michael Moore committed
41 42 43 44 45 46 47 48 49 50 51 52 53
typedef struct _RsvgNodeText RsvgNodeText;

struct _RsvgNodeText {
	RsvgNode super;
	RsvgLength x, y, dx, dy;
};

typedef struct _RsvgNodeTref RsvgNodeTref;

struct _RsvgNodeTref {
	RsvgNode super;
	RsvgNode * link;
};
54
char *
55
rsvg_make_valid_utf8 (const char *str, int len)
56 57 58 59 60 61 62
{
	GString *string;
	const char *remainder, *invalid;
	int remaining_bytes, valid_bytes;
	
	string = NULL;
	remainder = str;
63

Dom Lachowicz's avatar
Dom Lachowicz committed
64 65 66 67
	if (len < 0)
		remaining_bytes = strlen (str);
	else
		remaining_bytes = len;
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	while (remaining_bytes != 0)
		{
			if (g_utf8_validate (remainder, remaining_bytes, &invalid))
				break;
			valid_bytes = invalid - remainder;
      
			if (string == NULL) 
				string = g_string_sized_new (remaining_bytes);
			
			g_string_append_len (string, remainder, valid_bytes);
			g_string_append_c (string, '?');
			
			remaining_bytes -= valid_bytes + 1;
			remainder = invalid + 1;
		}
	
	if (string == NULL) 
86 87
		return len < 0 ? g_strndup (str, len) : g_strdup (str);

88 89 90 91 92
	g_string_append (string, remainder);
	
	return g_string_free (string, FALSE);
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
93 94
static GString *
_rsvg_text_chomp (GString * in, gboolean *lastwasspace)
95
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
96 97 98
	GString * out;
	guint i;
	out = g_string_new(in->str);
99

Caleb Michael Moore's avatar
Caleb Michael Moore committed
100
	for (i = 0; i < out->len;)
101
		{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
102 103
			if (out->str[i] == '\n')
				g_string_erase(out, i, 1);
104
			else
Caleb Michael Moore's avatar
Caleb Michael Moore committed
105
				i++;
106
		}
107

Caleb Michael Moore's avatar
Caleb Michael Moore committed
108 109 110 111 112
	for (i = 0; i < out->len; i++)
		if (out->str[i] == '\t')
			out->str[i] = ' ';

	for (i = 0; i < out->len;)
113
		{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
114 115 116
			if (out->str[i] == ' ' && *lastwasspace)
					g_string_erase(out, i, 1);
			else 
117
				{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
118 119
					if (out->str[i] == ' ')
						*lastwasspace = TRUE;
120
					else
Caleb Michael Moore's avatar
Caleb Michael Moore committed
121 122
						*lastwasspace = FALSE;
					i++;
123
				}
124 125
		}
	
Caleb Michael Moore's avatar
Caleb Michael Moore committed
126
	return out;
127 128
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
129

Caleb Michael Moore's avatar
Caleb Michael Moore committed
130
static void
Caleb Michael Moore's avatar
Caleb Michael Moore committed
131
_rsvg_node_text_set_atts (RsvgNode * self, RsvgHandle *ctx, RsvgPropertyBag *atts)
132
{
133
	const char * klazz = NULL, * id = NULL, *value;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
134
	RsvgNodeText * text = (RsvgNodeText *)self;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
135

136
	if (rsvg_property_bag_size (atts))
137
		{
138
			if ((value = rsvg_property_bag_lookup (atts, "x")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
139
				text->x = _rsvg_css_parse_length (value);
140
			if ((value = rsvg_property_bag_lookup (atts, "y")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
141
				text->y = _rsvg_css_parse_length (value);
142
			if ((value = rsvg_property_bag_lookup (atts, "dx")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
143
				text->dx = _rsvg_css_parse_length (value);
144
			if ((value = rsvg_property_bag_lookup (atts, "dy")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
145
				text->dy = _rsvg_css_parse_length (value);
146 147 148
			if ((value = rsvg_property_bag_lookup (atts, "class")))
				klazz = value;
			if ((value = rsvg_property_bag_lookup (atts, "id")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
149 150
				{
					id = value;
151
					rsvg_defs_register_name (ctx->priv->defs, value, self);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
152
				}
153

Caleb Michael Moore's avatar
Caleb Michael Moore committed
154 155 156
			rsvg_parse_style_attrs (ctx, self->state, "text", klazz, id, atts);
		}	
}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
157

Caleb Michael Moore's avatar
Caleb Michael Moore committed
158 159 160 161 162
void
rsvg_text_render_text (RsvgDrawingCtx *ctx,
					   const char *text,
					   gdouble *x,
					   gdouble *y);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
163

164 165

static void
Caleb Michael Moore's avatar
Caleb Michael Moore committed
166 167
_rsvg_node_text_type_tspan(RsvgNodeText * self, RsvgDrawingCtx *ctx, 
						   gdouble * x, gdouble * y, gboolean * lastwasspace);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
168

Caleb Michael Moore's avatar
Caleb Michael Moore committed
169 170 171
static void
_rsvg_node_text_type_tref(RsvgNodeTref * self, RsvgDrawingCtx *ctx, 
						  gdouble * x, gdouble * y, gboolean * lastwasspace);
172 173

static void
Caleb Michael Moore's avatar
Caleb Michael Moore committed
174 175
_rsvg_node_text_type_children(RsvgNode * self, RsvgDrawingCtx *ctx, 
							  gdouble * x, gdouble * y, gboolean * lastwasspace)
176
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
177
	guint i;
178 179

	rsvg_push_discrete_layer(ctx);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
180
	for (i = 0; i < self->children->len; i++)
181
		{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
182
			RsvgNode * node = g_ptr_array_index(self->children, i);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
183
			if (!strcmp(node->type->str, "RSVG_NODE_CHARS"))
184
				{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
185 186 187 188
					RsvgNodeChars * chars = (RsvgNodeChars *)node;
					GString * str = _rsvg_text_chomp(chars->contents, lastwasspace);
					rsvg_text_render_text (ctx, str->str, x, y);				
					g_string_free(str, TRUE);
189
				}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
190
			else if (!strcmp(node->type->str, "tspan"))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
191 192 193 194 195 196
				{
					RsvgNodeText * tspan = (RsvgNodeText *)node;
					rsvg_state_push(ctx);
					_rsvg_node_text_type_tspan(tspan, ctx, x, y, lastwasspace);
					rsvg_state_pop(ctx);
				}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
197
			else if (!strcmp(node->type->str, "tref"))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
198 199 200 201 202
				{
					RsvgNodeTref * tref = (RsvgNodeTref *)node;
					_rsvg_node_text_type_tref(tref, ctx, x, y, lastwasspace);
				}
		}		
203
	rsvg_pop_discrete_layer(ctx);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
204 205
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
206 207 208
static int
_rsvg_node_text_length_tref(RsvgNodeTref * self, RsvgDrawingCtx *ctx, 
							gdouble * x, gboolean * lastwasspace);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
209

Caleb Michael Moore's avatar
Caleb Michael Moore committed
210 211 212
static int
_rsvg_node_text_length_tspan(RsvgNodeText * self, RsvgDrawingCtx *ctx, 
							 gdouble * x, gboolean * lastwasspace);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
213

Caleb Michael Moore's avatar
Caleb Michael Moore committed
214 215 216
static gdouble 
rsvg_text_length_text_as_string (RsvgDrawingCtx *ctx,
								 const char *text);
217

Caleb Michael Moore's avatar
Caleb Michael Moore committed
218 219 220
static int
_rsvg_node_text_length_children(RsvgNode * self, RsvgDrawingCtx *ctx, 
								gdouble * x, gboolean * lastwasspace)
221
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
222 223 224
	guint i;
	int out = FALSE;
	for (i = 0; i < self->children->len; i++)
225
		{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
226
			RsvgNode * node = g_ptr_array_index(self->children, i);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
227
			if (!strcmp(node->type->str, "RSVG_NODE_CHARS"))
228
				{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
229 230 231 232
					RsvgNodeChars * chars = (RsvgNodeChars *)node;
					GString * str = _rsvg_text_chomp(chars->contents, lastwasspace);
					*x += rsvg_text_length_text_as_string(ctx, str->str);
					g_string_free(str, TRUE);
233
				}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
234
			else if (!strcmp(node->type->str, "tspan"))
235
				{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
236 237
					RsvgNodeText * tspan = (RsvgNodeText *)node;
					out = _rsvg_node_text_length_tspan(tspan, ctx, x, lastwasspace);
238
				}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
239
			else if (!strcmp(node->type->str, "tref"))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
240 241 242 243 244 245
				{
					RsvgNodeTref * tref = (RsvgNodeTref *)node;
					out = _rsvg_node_text_length_tref(tref, ctx, x, lastwasspace);
				}
			if (out)
				break;
246
		}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
247
	return out;
248 249
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
250 251

static void
Caleb Michael Moore's avatar
Caleb Michael Moore committed
252 253
_rsvg_node_text_draw(RsvgNode * self, RsvgDrawingCtx *ctx, 
					 int dominate)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
254
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
255 256 257 258 259 260 261 262 263 264 265
	double x, y;
	gboolean lastwasspace = TRUE;
	RsvgNodeText * text = (RsvgNodeText *)self;
	rsvg_state_reinherit_top(ctx, self->state, dominate);

	x = _rsvg_css_normalize_length(&text->x, ctx, 'h');
	y = _rsvg_css_normalize_length(&text->y, ctx, 'v');
	x += _rsvg_css_normalize_length(&text->dx, ctx, 'h');
	y += _rsvg_css_normalize_length(&text->dy, ctx, 'v');

	if (rsvg_state_current(ctx)->text_anchor != TEXT_ANCHOR_START)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
266
		{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
267 268 269 270 271 272
			double length = 0;
			_rsvg_node_text_length_children(self, ctx, &length, &lastwasspace);
			if (rsvg_state_current(ctx)->text_anchor == TEXT_ANCHOR_END)
				x -= length;
			if (rsvg_state_current(ctx)->text_anchor == TEXT_ANCHOR_MIDDLE)
				x -= length / 2;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
273
		}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

	lastwasspace = TRUE;
	_rsvg_node_text_type_children(self, ctx, &x, &y, &lastwasspace);
}

RsvgNode *
rsvg_new_text (void)
{
	RsvgNodeText *text;
	text = g_new (RsvgNodeText, 1);
	_rsvg_node_init(&text->super);
	text->super.draw = _rsvg_node_text_draw;
	text->super.set_atts = _rsvg_node_text_set_atts;
	text->x = text->y = text->dx = text->dy = _rsvg_css_parse_length("0");
	return &text->super;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
289
}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
290

Caleb Michael Moore's avatar
Caleb Michael Moore committed
291
static void
Caleb Michael Moore's avatar
Caleb Michael Moore committed
292 293 294
_rsvg_node_text_type_tspan(RsvgNodeText * self, RsvgDrawingCtx *ctx, 
						   gdouble * x, gdouble * y, 
						   gboolean * lastwasspace)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
295
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
296 297 298
	rsvg_state_reinherit_top(ctx, self->super.state, 0);

	if (self->x.factor != 'n')
Caleb Michael Moore's avatar
Caleb Michael Moore committed
299
		{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
300 301
			*x = _rsvg_css_normalize_length(&self->x, ctx, 'h');
			if (rsvg_state_current(ctx)->text_anchor != TEXT_ANCHOR_START)
302
				{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
303 304 305 306 307 308 309
					double length = 0;
					gboolean lws = *lastwasspace;
					_rsvg_node_text_length_children(&self->super, ctx, &length, &lws);
					if (rsvg_state_current(ctx)->text_anchor == TEXT_ANCHOR_END)
						*x -= length;
					if (rsvg_state_current(ctx)->text_anchor == TEXT_ANCHOR_MIDDLE)
						*x -= length / 2;
310
				}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
311
		}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
312 313 314 315 316
	if (self->y.factor != 'n')
		*y = _rsvg_css_normalize_length(&self->y, ctx, 'v');
	*x += _rsvg_css_normalize_length(&self->dx, ctx, 'h');
	*y += _rsvg_css_normalize_length(&self->dy, ctx, 'v');
	_rsvg_node_text_type_children(&self->super, ctx, x, y, lastwasspace);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
317 318
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
319 320
static int
_rsvg_node_text_length_tspan(RsvgNodeText * self, RsvgDrawingCtx *ctx, gdouble * x, gboolean * lastwasspace)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
321
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
322 323 324
	if (self->x.factor != 'n' || self->y.factor != 'n')
		return TRUE;
	return _rsvg_node_text_length_children(&self->super, ctx, x, lastwasspace);
325 326
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
327 328
static void
_rsvg_node_tspan_set_atts (RsvgNode * self, RsvgHandle *ctx, RsvgPropertyBag *atts)
329
{
330
	const char * klazz = NULL, * id = NULL, *value;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
331 332
	RsvgNodeText * text = (RsvgNodeText *)self;

333
	if (rsvg_property_bag_size (atts))
334
		{
335
			if ((value = rsvg_property_bag_lookup (atts, "x")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
336
				text->x = _rsvg_css_parse_length (value);
337
			if ((value = rsvg_property_bag_lookup (atts, "y")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
338
				text->y = _rsvg_css_parse_length (value);
339
			if ((value = rsvg_property_bag_lookup (atts, "dx")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
340
				text->dx = _rsvg_css_parse_length (value);
341
			if ((value = rsvg_property_bag_lookup (atts, "dy")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
342
				text->dy = _rsvg_css_parse_length (value);
343 344 345
			if ((value = rsvg_property_bag_lookup (atts, "class")))
				klazz = value;
			if ((value = rsvg_property_bag_lookup (atts, "id")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
346 347
				{
					id = value;
348
					rsvg_defs_register_name (ctx->priv->defs, value, self);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
349
				}
350

Caleb Michael Moore's avatar
Caleb Michael Moore committed
351 352 353
			rsvg_parse_style_attrs (ctx, self->state, "tspan", klazz, id, atts);
		}	
}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
354

Caleb Michael Moore's avatar
Caleb Michael Moore committed
355 356 357 358 359 360 361 362 363 364 365
RsvgNode *
rsvg_new_tspan (void)
{
	RsvgNodeText *text;
	text = g_new (RsvgNodeText, 1);
	_rsvg_node_init(&text->super);
	text->super.set_atts = _rsvg_node_tspan_set_atts;
	text->x.factor = text->y.factor = 'n';
	text->dx = text->dy = _rsvg_css_parse_length("0");
	return &text->super;
}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
366

Caleb Michael Moore's avatar
Caleb Michael Moore committed
367 368 369 370 371 372 373
static void
_rsvg_node_text_type_tref(RsvgNodeTref * self, RsvgDrawingCtx *ctx, 
						  gdouble * x, gdouble * y, gboolean * lastwasspace)
{
	if (self->link)
		_rsvg_node_text_type_children(self->link, ctx, x, y, lastwasspace);
}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
374

Caleb Michael Moore's avatar
Caleb Michael Moore committed
375 376 377 378 379 380 381 382
static int
_rsvg_node_text_length_tref(RsvgNodeTref * self, RsvgDrawingCtx *ctx, gdouble * x, 
							gboolean * lastwasspace)
{
	if (self->link)
		return _rsvg_node_text_length_children(self->link, ctx, x, lastwasspace);
	return FALSE;
}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
383

Caleb Michael Moore's avatar
Caleb Michael Moore committed
384 385 386 387 388
static void
_rsvg_node_tref_set_atts (RsvgNode * self, RsvgHandle *ctx, RsvgPropertyBag *atts)
{
	const char *value;
	RsvgNodeTref * text = (RsvgNodeTref *)self;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
389

Caleb Michael Moore's avatar
Caleb Michael Moore committed
390 391 392
	if (rsvg_property_bag_size (atts))
		{
			if ((value = rsvg_property_bag_lookup (atts, "xlink:href")))
393
				rsvg_defs_add_resolver (ctx->priv->defs, &text->link, value);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
394
			if ((value = rsvg_property_bag_lookup (atts, "id")))
395
				rsvg_defs_register_name (ctx->priv->defs, value, self);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
396 397 398 399 400 401 402 403 404 405 406 407
		}	
}

RsvgNode *
rsvg_new_tref (void)
{
	RsvgNodeTref *text;
	text = g_new (RsvgNodeTref, 1);
	_rsvg_node_init(&text->super);
	text->super.set_atts = _rsvg_node_tref_set_atts;
	text->link = NULL;
	return &text->super;
408
}
409 410 411 412 413 414

typedef struct _RsvgTextLayout RsvgTextLayout;

struct _RsvgTextLayout
{
	PangoLayout * layout;
415
	RsvgDrawingCtx  * ctx;
416
	TextAnchor anchor;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
417
	gdouble x, y;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
418
	gboolean orientation;
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
};

typedef struct _RenderCtx RenderCtx;

struct _RenderCtx
{
	GString      *path;
	gboolean      wrote;
	gdouble       offset_x;
	gdouble       offset_y;
};

typedef void (* RsvgTextRenderFunc) (PangoFont  *font,
									 PangoGlyph  glyph,
									 FT_Int32    load_flags,
									 gint        x,
									 gint        y,
									 gpointer    render_data);

#ifndef FT_GLYPH_FORMAT_OUTLINE
#define FT_GLYPH_FORMAT_OUTLINE ft_glyph_format_outline
#endif

#ifndef FT_LOAD_TARGET_MONO
#define FT_LOAD_TARGET_MONO FT_LOAD_MONOCHROME
#endif

static RenderCtx *
rsvg_render_ctx_new (void)
{
	RenderCtx *ctx;
	
	ctx = g_new0 (RenderCtx, 1);
	ctx->path = g_string_new (NULL);
	
	return ctx;
}

static void
rsvg_render_ctx_free(RenderCtx *ctx)
{
	g_string_free(ctx->path, TRUE);
	g_free(ctx);
}

static void
rsvg_text_ft2_subst_func (FcPattern *pattern,
                          gpointer   data)
{
	RsvgHandle *ctx = (RsvgHandle *)data;

	(void)ctx;

Dom Lachowicz's avatar
Dom Lachowicz committed
472 473 474 475
	FcPatternAddBool (pattern, FC_HINTING, 0);
	FcPatternAddBool (pattern, FC_ANTIALIAS, 0);
	FcPatternAddBool (pattern, FC_AUTOHINT, 0);	
	FcPatternAddBool (pattern, FC_SCALABLE, 1);
476 477 478
}

static PangoContext *
479
rsvg_text_get_pango_context (RsvgDrawingCtx *ctx)
480 481 482 483 484 485
{
	PangoContext    *context;
	PangoFT2FontMap *fontmap;
	
	fontmap = PANGO_FT2_FONT_MAP (pango_ft2_font_map_new ());
	
486
	pango_ft2_font_map_set_resolution (fontmap, ctx->dpi_x, ctx->dpi_y);
487 488 489 490 491 492 493 494
	
	pango_ft2_font_map_set_default_substitute (fontmap,
											   rsvg_text_ft2_subst_func,
											   ctx,
											   (GDestroyNotify) NULL);
	
	context = pango_ft2_font_map_create_context (fontmap);
	g_object_unref (fontmap);
495 496 497 498 499 500 501 502 503 504 505 506

  /*  Workaround for bug #143542 (PangoFT2Fontmap leak),
   *  see also bug #344235 (Text layer rendering leaks font file descriptor):
   *
   *  Calling pango_ft2_font_map_substitute_changed() causes the
   *  font_map cache to be flushed, thereby removing the circular
   *  reference that causes the leak.
   */
  g_object_weak_ref (G_OBJECT(context),
                     (GWeakNotify) pango_ft2_font_map_substitute_changed,
                     fontmap);

507 508 509 510 511 512 513 514 515 516 517
	
	return context;
}

static void
rsvg_text_layout_free(RsvgTextLayout * layout)
{
	g_object_unref(G_OBJECT(layout->layout));
	g_free(layout);
}

518 519 520 521 522
static PangoLayout *
rsvg_text_create_layout (RsvgDrawingCtx *ctx,
						 RsvgState *state,
						 const char *text,
						 PangoContext *context)
523 524
{
	PangoFontDescription *font_desc;
525
	PangoLayout *layout;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
526

527
	if (state->lang)
528
		pango_context_set_language (context,
529 530
									pango_language_from_string (state->lang));
	
531 532
	if (state->unicode_bidi == UNICODE_BIDI_OVERRIDE ||
		state->unicode_bidi == UNICODE_BIDI_EMBED)
533
		pango_context_set_base_dir (context, state->text_dir);
534
	
535
	font_desc = pango_font_description_copy (pango_context_get_font_description (context));
536 537 538 539 540 541 542 543
	
	if (state->font_family)
		pango_font_description_set_family_static (font_desc, state->font_family);
	
	pango_font_description_set_style (font_desc, state->font_style);
	pango_font_description_set_variant (font_desc, state->font_variant);
	pango_font_description_set_weight (font_desc, state->font_weight);
	pango_font_description_set_stretch (font_desc, state->font_stretch); 
Caleb Michael Moore's avatar
Caleb Michael Moore committed
544
	pango_font_description_set_size (font_desc, _rsvg_css_normalize_length(&state->font_size, ctx, 'v') * PANGO_SCALE / ctx->dpi_y * 72); 
545 546 547

	layout = pango_layout_new (context);
	pango_layout_set_font_description (layout, font_desc);
548 549 550
	pango_font_description_free (font_desc);
	
	if (text)
551
		pango_layout_set_text (layout, text, -1);
552
	else
553 554 555 556
		pango_layout_set_text (layout, NULL, 0);

	pango_layout_set_alignment (layout, (state->text_dir == PANGO_DIRECTION_LTR || 
										 state->text_dir == PANGO_DIRECTION_TTB_LTR) ? 
557
								PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT);
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

	return layout;
}


static RsvgTextLayout *
rsvg_text_layout_new (RsvgDrawingCtx *ctx,
					  RsvgState *state,
					  const char *text)
{
	RsvgTextLayout * layout;

	if (ctx->pango_context == NULL)
		ctx->pango_context = rsvg_text_get_pango_context (ctx);
	
	layout = g_new0 (RsvgTextLayout, 1);
	
	layout->layout = rsvg_text_create_layout (ctx, state, text,
											  ctx->pango_context);
	layout->ctx = ctx;
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
	
	layout->anchor = state->text_anchor;

	return layout;
}

static void
rsvg_text_layout_get_offsets (RsvgTextLayout *layout,
                              gint           *x,
                              gint           *y)
{
	PangoRectangle  ink;
	PangoRectangle  logical;
	
	pango_layout_get_pixel_extents (layout->layout, &ink, &logical);
	
	if (ink.width < 1 || ink.height < 1) {
		*x = *y = 0;
		return;
	}
	
	*x = MIN (ink.x, logical.x);
600
	*y = MIN (ink.y, logical.y);
601 602 603 604 605 606
}

static FT_Int32
rsvg_text_layout_render_flags (RsvgTextLayout *layout)
{
	gint flags = 0;
Dom Lachowicz's avatar
Dom Lachowicz committed
607 608 609 610

	flags |= FT_LOAD_NO_BITMAP;
	flags |= FT_LOAD_TARGET_MONO;
	flags |= FT_LOAD_NO_HINTING;
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

	return flags;
}

static void
rsvg_text_vector_coords (RenderCtx       *ctx,
						 const FT_Vector *vector,
						 gdouble         *x,
						 gdouble         *y)
{
	*x = ctx->offset_x + (double)vector->x / 64;
	*y = ctx->offset_y - (double)vector->y / 64;
}

static gint
626
moveto (const FT_Vector *to,
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
		gpointer   data)
{
	RenderCtx * ctx;
	gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
	gdouble x, y;
	
	ctx = (RenderCtx *)data;
	
	if (ctx->wrote)
		g_string_append(ctx->path, "Z ");
	else
		ctx->wrote = TRUE;

	g_string_append_c(ctx->path, 'M');
	
	rsvg_text_vector_coords(ctx, to, &x, &y);
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), x));
	g_string_append_c(ctx->path, ',');
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), y));
	g_string_append_c(ctx->path, ' ');

	return 0;
}

static gint
652
lineto (const FT_Vector *to,
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
		gpointer   data)
{
	RenderCtx * ctx;
	gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
	gdouble x, y;
	
	ctx = (RenderCtx *)data;
	
	if (!ctx->wrote)
		return 0;

	g_string_append_c(ctx->path, 'L');
	
	rsvg_text_vector_coords(ctx, to, &x, &y);
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), x));
	g_string_append_c(ctx->path, ',');
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), y));
	g_string_append_c(ctx->path, ' ');

	return 0;
}

static gint
676 677
conicto (const FT_Vector *ftcontrol,
		 const FT_Vector *to,
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
		 gpointer   data)
{
	RenderCtx * ctx;
	gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
	gdouble x, y;
	
	ctx = (RenderCtx *)data;

	if (!ctx->wrote)
		return 0;

	g_string_append_c(ctx->path, 'Q');
	
	rsvg_text_vector_coords(ctx, ftcontrol, &x, &y);
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), x));
	g_string_append_c(ctx->path, ',');
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), y));
	
	rsvg_text_vector_coords(ctx, to, &x, &y);
	g_string_append_c(ctx->path, ' ');
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), x));
	g_string_append_c(ctx->path, ',');
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), y));
	g_string_append_c(ctx->path, ' ');

	return 0;
}

static gint
707 708 709
cubicto (const FT_Vector *ftcontrol1,
		 const FT_Vector *ftcontrol2,
		 const FT_Vector *to,
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
		 gpointer   data)
{
	RenderCtx * ctx;
	gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
	gdouble x, y;
	
	ctx = (RenderCtx *)data;
	
	if (!ctx->wrote)
		return 0;

	g_string_append_c(ctx->path, 'C');
	
	rsvg_text_vector_coords(ctx, ftcontrol1, &x, &y);
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), x));
	g_string_append_c(ctx->path, ',');
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), y));
	
	rsvg_text_vector_coords(ctx, ftcontrol2, &x, &y);
	g_string_append_c(ctx->path, ' ');
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), x));
	g_string_append_c(ctx->path, ',');
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), y));
	
	rsvg_text_vector_coords(ctx, to, &x, &y);
	g_string_append_c(ctx->path, ' ');
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), x));
	g_string_append_c(ctx->path, ',');
	g_string_append(ctx->path, g_ascii_dtostr(buf, sizeof(buf), y));
	g_string_append_c(ctx->path, ' ');	

	return 0;
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
744
static gint
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
rsvg_text_layout_render_glyphs (RsvgTextLayout     *layout,
								PangoFont          *font,
								PangoGlyphString   *glyphs,
								RsvgTextRenderFunc  render_func,
								gint                x,
								gint                y,
								gpointer            render_data)
{
	PangoGlyphInfo *gi;
	FT_Int32        flags;
	FT_Vector       pos;
	gint            i;
	gint            x_position = 0;
	
	flags = rsvg_text_layout_render_flags (layout);

	for (i = 0, gi = glyphs->glyphs; i < glyphs->num_glyphs; i++, gi++)
		{
			if (gi->glyph)
				{
					pos.x = x + x_position + gi->geometry.x_offset;
					pos.y = y + gi->geometry.y_offset;
					
Dom Lachowicz's avatar
Dom Lachowicz committed
768
					render_func (font, gi->glyph, flags,
769 770 771
								 pos.x, pos.y,
								 render_data);
				}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
772
		  
773 774
			x_position += glyphs->glyphs[i].geometry.width;
		}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
775
	return x_position;
776 777 778 779 780 781 782 783 784 785 786 787
}

static void
rsvg_text_render_vectors (PangoFont     *font,
						  PangoGlyph     pango_glyph,
						  FT_Int32       flags,
						  gint           x,
						  gint           y,
						  gpointer       ud)
{
	static const FT_Outline_Funcs outline_funcs =
		{
788 789 790 791
			(FT_Outline_MoveToFunc)moveto,
			(FT_Outline_LineToFunc)lineto,
			(FT_Outline_ConicToFunc)conicto,
			(FT_Outline_CubicToFunc)cubicto,
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
			0,
			0
		};
	
	FT_Face   face;
	FT_Glyph  glyph;
	
	RenderCtx *context = (RenderCtx *)ud;
	
	face = pango_ft2_font_get_face (font);

	FT_Load_Glyph (face, (FT_UInt) pango_glyph, flags);

	FT_Get_Glyph (face->glyph, &glyph);
	
	if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
		{
			FT_OutlineGlyph outline_glyph = (FT_OutlineGlyph) glyph;
			
			context->offset_x = (gdouble) x / PANGO_SCALE;
			context->offset_y = (gdouble) y / PANGO_SCALE - (int)face->size->metrics.ascender / 64;

			FT_Outline_Decompose (&outline_glyph->outline, &outline_funcs, context);			
		}
	
	FT_Done_Glyph (glyph);
}

static void
rsvg_text_layout_render_line (RsvgTextLayout     *layout,
							  PangoLayoutLine    *line,
							  RsvgTextRenderFunc  render_func,
							  gint                x,
							  gint                y,
							  gpointer            render_data)
{
	PangoRectangle  rect;
	GSList         *list;
	gint            x_off = 0;
	
	for (list = line->runs; list; list = list->next)
		{
			PangoLayoutRun *run = list->data;
			
			pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
										NULL, &rect);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
838 839 840 841 842
			x_off += rsvg_text_layout_render_glyphs (layout,
													 run->item->analysis.font, run->glyphs,
													 render_func,
													 x + x_off, y,
													 render_data);
843 844 845 846 847 848 849 850 851 852
			
		}
}

static void
rsvg_text_layout_render (RsvgTextLayout     *layout,
						 RsvgTextRenderFunc  render_func,
						 gpointer            render_data)
{
	PangoLayoutIter *iter;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
853
	gint             offx, offy;
854
	gint             x, y;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
855 856

	rsvg_text_layout_get_offsets (layout, &offx, &offy);
857

Caleb Michael Moore's avatar
Caleb Michael Moore committed
858 859
	x = offx + layout->x;
	y = offy + layout->y;
860 861 862 863 864 865

	x *= PANGO_SCALE;
	y *= PANGO_SCALE;
	
	iter = pango_layout_get_iter (layout->layout);
	
866
	if (iter)
867 868 869 870 871 872 873 874 875 876 877 878
		{
			PangoRectangle   rect;
			PangoLayoutLine *line;
			gint             baseline;
			
			line = pango_layout_iter_get_line (iter);
			
			pango_layout_iter_get_line_extents (iter, NULL, &rect);
			baseline = pango_layout_iter_get_baseline (iter);
			
			rsvg_text_layout_render_line (layout, line,
										  render_func,
Caleb Michael Moore's avatar
Caleb Michael Moore committed
879
										  x + rect.x,
880 881
										  y + baseline,
										  render_data);
882 883

			layout->x += rect.width / PANGO_SCALE + offx;
884
		}
885

886
	pango_layout_iter_free (iter);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
887 888
}

889
static GString * 
890
rsvg_text_render_text_as_string (RsvgDrawingCtx *ctx,
891 892 893
								 const char *text,
								 gdouble *x,
								 gdouble *y)
894 895 896
{
	RsvgTextLayout *layout;
	RenderCtx      *render;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
897
	RsvgState      *state;
898
	GString        *output;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
899
	state = rsvg_state_current(ctx);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
900

901 902 903 904
	state->fill_rule = FILL_RULE_EVENODD;	
	state->has_fill_rule = TRUE;

	layout = rsvg_text_layout_new (ctx, state, text);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
905 906
	layout->x = *x;
	layout->y = *y;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
907 908
	layout->orientation = rsvg_state_current(ctx)->text_dir == PANGO_DIRECTION_TTB_LTR || 
		rsvg_state_current(ctx)->text_dir == PANGO_DIRECTION_TTB_RTL;
909 910 911 912 913 914 915 916
	render = rsvg_render_ctx_new ();

	rsvg_text_layout_render (layout, rsvg_text_render_vectors, 
							 (gpointer)render);

	if (render->wrote)
		g_string_append_c(render->path, 'Z');

Caleb Michael Moore's avatar
Caleb Michael Moore committed
917 918
	*x = layout->x;
	*y = layout->y;
919

920
	output = g_string_new(render->path->str);
921 922
	rsvg_render_ctx_free (render);
	rsvg_text_layout_free (layout);
923 924 925 926
	return output;
}

void
927
rsvg_text_render_text (RsvgDrawingCtx *ctx,
928 929 930 931
					   const char *text,
					   gdouble *x,
					   gdouble *y)
{
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
	if (0 && ctx->render->create_pango_context && ctx->render->render_pango_layout)
		{
			PangoContext *context;
			PangoLayout *layout;
			PangoLayoutIter *iter;
			RsvgState *state;
			gint w, h, baseline;

			state = rsvg_state_current(ctx);
			context = ctx->render->create_pango_context (ctx);
			layout = rsvg_text_create_layout (ctx, state, text, context);
			pango_layout_get_pixel_size (layout, &w, &h);
			iter = pango_layout_get_iter (layout);
			baseline = pango_layout_iter_get_baseline (iter) / PANGO_SCALE;
			pango_layout_iter_free (iter);
			ctx->render->render_pango_layout (ctx, layout, *x, *y - baseline);
			*x += w;
		}
	else
		{
			GString * render;
			render = rsvg_text_render_text_as_string (ctx,text,x,y);
			rsvg_render_path (ctx, render->str);
			g_string_free(render, TRUE);
		}
957 958
}

959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
static gdouble
rsvg_text_layout_width  (RsvgTextLayout *layout)
{
	PangoLayoutIter *iter;
	gint             offx, offy;

	rsvg_text_layout_get_offsets (layout, &offx, &offy);
	
	iter = pango_layout_get_iter (layout->layout);
	
	if (iter)
		{
			PangoRectangle   rect;
			PangoLayoutLine *line;
			
			line = pango_layout_iter_get_line (iter);
			
			pango_layout_iter_get_line_extents (iter, NULL, &rect);

			pango_layout_iter_free (iter);
			return rect.width / PANGO_SCALE + offx;
		}

982
	return 0;
983 984
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
985 986 987
static gdouble 
rsvg_text_length_text_as_string (RsvgDrawingCtx *ctx,
								 const char *text)
988 989
{
	RsvgTextLayout *layout;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
990
	gdouble x;
991

Caleb Michael Moore's avatar
Caleb Michael Moore committed
992 993 994 995
	layout = rsvg_text_layout_new (ctx, rsvg_state_current(ctx), text);
	layout->x = layout->y = 0;
	layout->orientation = rsvg_state_current(ctx)->text_dir == PANGO_DIRECTION_TTB_LTR || 
		rsvg_state_current(ctx)->text_dir == PANGO_DIRECTION_TTB_RTL;
996

Caleb Michael Moore's avatar
Caleb Michael Moore committed
997
	x = rsvg_text_layout_width(layout);
998 999

	rsvg_text_layout_free (layout);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1000
	return x;
1001
}