summaryrefslogtreecommitdiff
path: root/desktop/save_pdf/font_haru.c
blob: c907531216ae17bc7e1522759dd9517592076f85 (plain)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * Copyright 2008 Adam Blokus <adamblokus@gmail.com>
 * Copyright 2009 John Tytgat <joty@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 /** \file
  * Font handling in Haru pdf documents (implementation).
  *
  * The functions were written to implement the same interface as the Pango ones
  * so that the usage of the latter wouldn't have to be modified.
  */

#include "utils/config.h"
#ifdef WITH_PDF_EXPORT

/*#define FONT_HARU_DEBUG */
 
#include <assert.h> 
#include <float.h> 
#include <math.h>
#include <string.h> 

#include <hpdf.h>

#include "css/css.h"
#include "css/utils.h"

#include "desktop/options.h"
#include "desktop/save_pdf/font_haru.h"
#include "render/font.h"
#include "utils/log.h"


static bool haru_nsfont_init(HPDF_Doc *pdf, HPDF_Page *page,
		const char *string, char **string_nt, int length);

static bool haru_nsfont_width(const plot_font_style_t *fstyle,
		const char *string, size_t length,
		int *width);

static bool haru_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);

static bool haru_nsfont_split(const plot_font_style_t *fstyle,
		const char *string, size_t length,
	 	int x, size_t *char_offset, int *actual_x);
	 	
static float pdf_text_scale = DEFAULT_EXPORT_SCALE;

const struct font_functions haru_nsfont = {
	haru_nsfont_width,
 	haru_nsfont_position_in_string,
 	haru_nsfont_split
};

/**
 * Haru error handler
 * for debugging purposes - it immediately exits the program on the first error,
 * as it would otherwise flood the user with all resulting complications,
 * covering the most important error source.
 */
static void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no,
		void *user_data)
{
	LOG(("ERROR: in font_haru \n\terror_no=%x\n\tdetail_no=%d\n", 
			(HPDF_UINT)error_no, (HPDF_UINT)detail_no));
#ifdef FONT_HARU_DEBUG	
	exit(1);
#endif	
}

static bool haru_nsfont_init(HPDF_Doc *pdf, HPDF_Page *page,
		const char *string, char **string_nt, int length)
{
	
	*pdf = HPDF_New(error_handler, NULL);
	
	if (*pdf == NULL)
		return false;

	*page = HPDF_AddPage(*pdf);
	
	if (*page == NULL) {
		HPDF_Free(*pdf);
		return false;	
	}
	
	*string_nt = malloc((length + 1) * sizeof(char));
	if (*string_nt == NULL) {
		HPDF_Free(*pdf);
		return false;
	}
	
	memcpy(*string_nt, string, length);
	(*string_nt)[length] = '\0';	
	return true;
}

/**
 * Measure the width of a string.
 *
 * \param  fstyle  style for this text
 * \param  string  string to measure (no UTF-8 currently)
 * \param  length  length of string
 * \param  width   updated to width of string[0..length]
 * \return  true on success, false on error and error reported
 */
bool haru_nsfont_width(const plot_font_style_t *fstyle,
		const char *string, size_t length,
	 	int *width)
{
	HPDF_Doc pdf;
	HPDF_Page page;
	char *string_nt;
	HPDF_REAL width_real;

	*width = 0;

	if (length == 0)
		return true;

	if (!haru_nsfont_init(&pdf, &page, string, &string_nt, length))
		return false;

	if (!haru_nsfont_apply_style(fstyle, pdf, page, NULL, NULL)) {
		free(string_nt);
		HPDF_Free(pdf);
		return false;
	}

	width_real = HPDF_Page_TextWidth(page, string_nt);
	*width = width_real;

#ifdef FONT_HARU_DEBUG		
	LOG(("Measuring string: %s ; Calculated width: %f %i",string_nt, width_real, *width));
#endif
	free(string_nt);
	HPDF_Free(pdf);

	return true;
}


/**
 * Find the position in a string where an x coordinate falls.
 *
 * \param  fstyle	style for this text
 * \param  string	string to measure (no UTF-8 currently)
 * \param  length	length of string
 * \param  x		x coordinate to search for
 * \param  char_offset	updated to offset in string of actual_x, [0..length]
 * \param  actual_x	updated to x coordinate of character closest to x
 * \return  true on success, false on error and error reported
 */

bool haru_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)
{
	HPDF_Doc pdf;
	HPDF_Page page;
	char *string_nt;
	HPDF_UINT offset;
	HPDF_REAL real_width;
	
	if (!haru_nsfont_init(&pdf, &page, string, &string_nt, length))
		return false;
	
	if (HPDF_Page_SetWidth(page, x) != HPDF_OK
			|| !haru_nsfont_apply_style(fstyle, pdf, page, NULL, NULL)) {
		free(string_nt);
		HPDF_Free(pdf);
		return false;
	}

	
	offset = HPDF_Page_MeasureText(page, string_nt, x,
			HPDF_FALSE, &real_width);
	

	if (real_width < x)
		*char_offset = offset;
	else {
		assert(fabs(real_width - x) < FLT_EPSILON);
		assert(offset > 0);
		*char_offset = offset - 1;
	}
	
	/*TODO: this is only the right edge of the character*/
	*actual_x = real_width;
	
#ifdef FONT_HARU_DEBUG	
	LOG(("Position in string: %s at x: %i; Calculated position: %i",
			string_nt, x, *char_offset));	
#endif	
	free(string_nt);
	HPDF_Free(pdf);
	
	return true;
}

/**
 * Find where to split a string to make it fit a width.
 *
 * \param  fstyle	style for this text
 * \param  string	string to measure (no UTF-8 currently)
 * \param  length	length of string
 * \param  x		width available
 * \param  char_offset	updated to offset in string of actual_x, [0..length]
 * \param  actual_x	updated to x coordinate of character closest to x
 * \return  true on success, false on error and error reported
 */

bool haru_nsfont_split(const plot_font_style_t *fstyle,
		const char *string, size_t length,
		int x, size_t *char_offset, int *actual_x)
{
	HPDF_Doc pdf;
	HPDF_Page page;
	char *string_nt;
	HPDF_REAL real_width;
	HPDF_UINT offset;
	
	
	if (!haru_nsfont_init(&pdf, &page, string, &string_nt, length))
		return false;
	
	if (HPDF_Page_SetWidth(page, x) != HPDF_OK
		    || !haru_nsfont_apply_style(fstyle, pdf, page, NULL, NULL)) {
		free(string_nt);
		HPDF_Free(pdf);
		return false;
	}
	
	offset = HPDF_Page_MeasureText(page, string_nt, x,
			HPDF_TRUE, &real_width);
	
#ifdef FONT_HARU_DEBUG	
	LOG(("Splitting string: %s for width: %i ; Calculated position: %i Calculated real_width: %f", 
	string_nt, x, *char_offset, real_width));	
#endif	
	*char_offset = offset - 1;
	
	/*TODO: this is only the right edge of the character*/
	*actual_x = real_width;
	
	free(string_nt);
	HPDF_Free(pdf);
	
	return true;	
}

/**
 * Apply font style to a Haru HPDF_Page
 *
 * \param  fstyle	plot style for this page
 * \param  doc		document owning the page
 * \param  page		the page to apply the style to
 * \param  font		if this is non NULL it is updated to the font based
 *			on given style
 * \param  font_size	if this is non NULL it is updated to the font size
 *			based on given style
 * \return true on success, false on error and error reported
 *
 * When both font and font_size are NULL, the HPDF_Page is updated for given
 * style, otherwise it is left to the called to do this.
 */
bool haru_nsfont_apply_style(const plot_font_style_t *fstyle,
		HPDF_Doc doc, HPDF_Page page,
		HPDF_Font *font, HPDF_REAL *font_size)
{
	HPDF_Font pdf_font;
	HPDF_REAL size;
	char font_name[50];
	bool roman = false;
	bool bold = false;
	bool styled = false;

	/*TODO: style handling, we are mapping the
		styles on the basic 14 fonts only
	*/
	switch (fstyle->family) {
	case PLOT_FONT_FAMILY_SERIF:
		strcpy(font_name, "Times");
		roman = true;
		break;
	case PLOT_FONT_FAMILY_MONOSPACE:
		strcpy(font_name, "Courier");
		break;
	case PLOT_FONT_FAMILY_SANS_SERIF:
		strcpy(font_name, "Helvetica");
		break;
	case PLOT_FONT_FAMILY_CURSIVE:			
	case PLOT_FONT_FAMILY_FANTASY:		
	default:
		strcpy(font_name, "Times");
		roman=true;
		break;
	}
	
	if (fstyle->weight == 700) {
		strcat(font_name, "-Bold");
		bold = true;
	}

	if ((fstyle->flags & FONTF_ITALIC) || (fstyle->flags & FONTF_OBLIQUE)) {
		if (!bold)
			strcat(font_name,"-");
		if (roman)
			strcat(font_name,"Italic");
		else
			strcat(font_name,"Oblique");
			
		styled = true;
	}
	
	if (roman && !styled && !bold)
		strcat(font_name, "-Roman");

#ifdef FONT_HARU_DEBUG		
	LOG(("Setting font: %s", font_name));
#endif		

	size = fstyle->size;

	if (font != NULL)
		size *= pdf_text_scale;

	if (size <= 0)
		return true;

	size /= FONT_SIZE_SCALE;

	if (size > HPDF_MAX_FONTSIZE)
		size = HPDF_MAX_FONTSIZE;

	if (font_size)
		*font_size = size;

	pdf_font = HPDF_GetFont(doc, font_name, "StandardEncoding");
	if (pdf_font == NULL)
		return false;
	if (font != NULL)
		*font = pdf_font;

	if (font == NULL || font_size == NULL)
		HPDF_Page_SetFontAndSize(page, pdf_font, size);
	
	return true;
}

/**
 * Sync the text scale with the scale for the whole content
 */
void haru_nsfont_set_scale(float s)
{
	pdf_text_scale = s;
}

#endif /* WITH_PDF_EXPORT */