File: im_text.c

package info (click to toggle)
vips 7.28.5-1%2Bdeb7u1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 15,372 kB
  • sloc: ansic: 95,587; cpp: 50,751; sh: 11,548; python: 1,290; makefile: 916; perl: 40
file content (257 lines) | stat: -rw-r--r-- 6,666 bytes parent folder | download
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* im_text
 *
 * Written on: 20/5/04
 * 29/7/04
 *	- !HAVE_PANGOFT2 was broken, thanks Kenneth
 * 15/11/04
 *	- gah, still broken, thanks Stefan
 * 5/4/06
 * 	- return an error for im_text( "" ) rather than trying to make an
 * 	  empty image
 * 2/2/10
 * 	- gtkdoc
 */

/*

    This file is part of VIPS.
    
    VIPS is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser 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

 */

/*

    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>

#include <stdio.h>
#include <string.h>

#include <vips/vips.h>

#ifdef HAVE_PANGOFT2
#include <pango/pango.h>
#include <pango/pangoft2.h>
#endif /*HAVE_PANGOFT2*/

#ifdef HAVE_PANGOFT2

static PangoLayout *
text_layout_new( PangoContext *context, 
	const char *text, const char *font, int width, int alignment, int dpi )
{
	PangoLayout *layout;
	PangoFontDescription *font_description;

	layout = pango_layout_new( context );
	pango_layout_set_markup( layout, text, -1 );

	font_description = pango_font_description_from_string( font );
	pango_layout_set_font_description( layout, font_description );
	pango_font_description_free( font_description );

	if( width > 0 )
		pango_layout_set_width( layout, width * PANGO_SCALE );

	if( alignment < 0 || alignment > 2 )
		alignment = PANGO_ALIGN_RIGHT;
	pango_layout_set_alignment( layout, (PangoAlignment) alignment );

	return( layout );
}

static int
text_ft_to_vips( FT_Bitmap *bitmap, IMAGE *out )
{
	int y;
	
	if( im_outcheck( out ) )
                return( -1 );
        im_initdesc( out, bitmap->width, bitmap->rows, 1, 
		IM_BBITS_BYTE, IM_BANDFMT_UCHAR,
		IM_CODING_NONE, IM_TYPE_B_W, 1.0, 1.0, 0, 0 );
        if( im_setupout( out ) )
                return( -1 );

	for( y = 0; y < bitmap->rows; y++ ) 
		if( im_writeline( y, out, 
			(VipsPel *) bitmap->buffer + y * bitmap->pitch ) )
			return( -1 );

	return( 0 );
}

static int
text_layout_render_to_image( PangoLayout *layout, IMAGE *out )
{
	PangoRectangle logical_rect;
	FT_Bitmap bitmap;
	int left;
	int top;
	int width;
	int height;

	pango_layout_get_extents( layout, NULL, &logical_rect );

#ifdef DEBUG
	printf( "logical left = %d, top = %d, width = %d, height = %d\n",
		PANGO_PIXELS( logical_rect.x ),
		PANGO_PIXELS( logical_rect.y ),
		PANGO_PIXELS( logical_rect.width ),
		PANGO_PIXELS( logical_rect.height ) );
#endif /*DEBUG*/

	left = PANGO_PIXELS( logical_rect.x );
	top = PANGO_PIXELS( logical_rect.y );
	width = PANGO_PIXELS( logical_rect.width );
	height = PANGO_PIXELS( logical_rect.height );

	/* Can happen for "", for example.
	 */
	if( width == 0 || height == 0 ) {
		im_error( "im_text", 
			"%s", _( "no text to render" ) );
		return( -1 );
	}

	bitmap.width = width;
	bitmap.pitch = (bitmap.width + 3) & ~3;
	bitmap.rows = height;
	if( !(bitmap.buffer = im_malloc( NULL, bitmap.pitch * bitmap.rows )) )
		return( -1 );
	bitmap.num_grays = 256;
	bitmap.pixel_mode = ft_pixel_mode_grays;
	memset( bitmap.buffer, 0x00, bitmap.pitch * bitmap.rows );

	if( pango_layout_get_width( layout ) != -1 )
		pango_ft2_render_layout( &bitmap, layout, -left, -top );
	else
		pango_ft2_render_layout( &bitmap, layout, 0, 0 );
	if( text_ft_to_vips( &bitmap, out ) ) {
		im_free( bitmap.buffer );
		return( -1 );
	}

	im_free( bitmap.buffer );

	return( 0 );
}

static int
text_render_to_image( PangoContext *context, IMAGE *out, 
	const char *text, const char *font, int width, int alignment, int dpi )
{
	PangoLayout *layout;

	if( !(layout = text_layout_new( context, text, font, 
		width, alignment, dpi )) )
		return( -1 );

	if( text_layout_render_to_image( layout, out ) ) {
		g_object_unref( layout );
		return( -1 );
	}

	g_object_unref( layout );

	return( 0 );
}

/**
 * im_text:
 * @out: output image
 * @text: utf-8 text string to render
 * @font: font to render with
 * @width: render within this many pixels across
 * @alignment: left/centre/right alignment
 * @dpi: render at this resolution
 *
 * Draw the string @text to an image. @out is a one-band 8-bit
 * unsigned char image, with 0 for no text and 255 for text. Values inbetween
 * are used for anti-aliasing.
 *
 * @text is the text to render as a UTF-8 string. It can contain Pango markup,
 * for example "&lt;i&gt;The&lt;/i&gt;Guardian".
 *
 * @font is the font to render with, selected by fontconfig. Examples might be
 * "sans 12" or perhaps "bitstream charter bold 10".
 *
 * @width is the maximum number of pixels across to draw within. If the
 * generated text is wider than this, it will wrap to a new line. In this
 * case, @alignment can be used to set the alignment style for multi-line
 * text. 0 means left-align, 1 centre, 2 right-align.
 *
 * @dpi sets the resolution to render at. "sans 12" at 72 dpi draws characters
 * approximately 12 pixels high.
 *
 * See also: im_make_xy(), im_black(), im_gaussnoise().
 *
 * Returns: 0 on success, -1 on error
 */
int 
im_text( IMAGE *out, const char *text, const char *font, 
	int width, int alignment, int dpi )
{
	static PangoFontMap *fontmap = NULL;
	PangoContext *context;

	if( !pango_parse_markup( text, -1, 0, NULL, NULL, NULL, NULL ) ) {
		im_error( "im_text", 
			"%s", _( "invalid markup in text" ) );
		return( -1 );
	}

	/* Just have one of these, ever. It doesn't close properly when we
	 * _unref(), so keep it around for reuse.
	 */
	if( !fontmap )
		fontmap = pango_ft2_font_map_new();

	pango_ft2_font_map_set_resolution( PANGO_FT2_FONT_MAP( fontmap ), 
		dpi, dpi );
	context = pango_ft2_font_map_create_context( 
		PANGO_FT2_FONT_MAP( fontmap ) );

	if( text_render_to_image( context, out, text, font, 
		width, alignment, dpi ) ) {
		g_object_unref( context );
		return( -1 );
	}

	g_object_unref( context );

	return( 0 );
}

#else /*!HAVE_PANGOFT2*/

int 
im_text( IMAGE *out, const char *text, const char *font, 
	int width, int alignment, int dpi )
{
	im_error( "im_text", 
		"%s", _( "pangoft2 support disabled" ) );

	return( -1 );
}

#endif /*HAVE_PANGOFT2*/