File: xmltext-device.c

package info (click to toggle)
mupdf 1.25.1%2Bds1-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 21,620 kB
  • sloc: ansic: 270,929; python: 20,709; java: 6,916; javascript: 1,865; makefile: 1,130; xml: 550; sh: 430; cpp: 325; cs: 313; awk: 10; sed: 7; lisp: 3
file content (394 lines) | stat: -rw-r--r-- 13,759 bytes parent folder | download | duplicates (3)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright (C) 2004-2021 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF 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 Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.

#include "mupdf/fitz.h"


static int s_xml_starttag_begin(fz_context *ctx, fz_output *out, const char *id)
{
	fz_write_printf(ctx, out, "<%s", id);
	return 0;
}

static int s_xml_starttag_end(fz_context *ctx, fz_output *out)
{
	fz_write_printf(ctx, out, ">\n");
	return 0;
}

static int s_xml_starttag_empty_end(fz_context *ctx, fz_output *out)
{
	fz_write_printf(ctx, out, "/>\n");
	return 0;
}

static int s_xml_endtag(fz_context *ctx, fz_output *out, const char *id)
{
	fz_write_printf(ctx, out, "</%s>\n", id);
	return 0;
}

static int s_write_attribute_int(fz_context *ctx, fz_output *out, const char *id, int value)
{
	fz_write_printf(ctx, out, " %s=\"%i\"", id, value);
	return 0;
}

static int s_write_attribute_size(fz_context *ctx, fz_output *out, const char *id, size_t value)
{
	fz_write_printf(ctx, out, " %s=\"%zi\"", id, value);
	return 0;
}

static int s_write_attribute_float(fz_context *ctx, fz_output *out, const char *id, float value)
{
	fz_write_printf(ctx, out, " %s=\"%g\"", id, value);
	return 0;
}

static int s_write_attribute_string(fz_context *ctx, fz_output *out, const char *id, const char *value)
{
	fz_write_printf(ctx, out, " %s=\"%s\"", id, value);
	return 0;
}

static int s_write_attribute_char(fz_context *ctx, fz_output *out, const char *id, char value)
{
	if (value == '"') fz_write_printf(ctx, out, " %s=\"\\%c\"", id, value);
	else fz_write_printf(ctx, out, " %s=\"%c\"", id, value);
	return 0;
}

static int s_write_attribute_matrix(fz_context *ctx, fz_output *out, const char *id, const fz_matrix *matrix)
{
	fz_write_printf(ctx, out,
		" %s=\"%g %g %g %g %g %g\"",
		id,
		matrix->a,
		matrix->b,
		matrix->c,
		matrix->d,
		matrix->e,
		matrix->f
		);
	return 0;
}




typedef struct
{
	fz_device super;
	fz_output *out;
} fz_xmltext_device;

static void
fz_xmltext_text(fz_context *ctx, fz_device *dev_, const fz_text *text, fz_matrix ctm,
	fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
{
	fz_xmltext_device *dev = (fz_xmltext_device*) dev_;

	fz_text_span *span;
	for (span = text->head; span; span = span->next)
	{
		int i;

		s_xml_starttag_begin(ctx, dev->out, "span");
		s_write_attribute_matrix(ctx, dev->out, "ctm", &ctm);
		s_write_attribute_string(ctx, dev->out, "font_name", span->font->name);
		if (span->font->flags.is_mono)          s_write_attribute_int(ctx, dev->out, "is_mono", 1);
		if (span->font->flags.is_serif)         s_write_attribute_int(ctx, dev->out, "is_serif", 1);
		if (span->font->flags.is_italic)        s_write_attribute_int(ctx, dev->out, "is_italic", 1);
		if (span->font->flags.ft_substitute)    s_write_attribute_int(ctx, dev->out, "ft_substitute", 1);
		if (span->font->flags.ft_stretch)       s_write_attribute_int(ctx, dev->out, "ft_stretch", 1);
		if (span->font->flags.fake_bold)        s_write_attribute_int(ctx, dev->out, "fake_bold", 1);
		if (span->font->flags.fake_italic)      s_write_attribute_int(ctx, dev->out, "fake_italic", 1);
		if (span->font->flags.has_opentype)     s_write_attribute_int(ctx, dev->out, "has_opentype", 1);
		if (span->font->flags.invalid_bbox)     s_write_attribute_int(ctx, dev->out, "invalid_bbox", 1);
		s_write_attribute_matrix(ctx, dev->out, "trm", &span->trm);
		s_write_attribute_int(ctx, dev->out, "len", span->len);
		s_write_attribute_int(ctx, dev->out, "wmode", span->wmode);
		s_write_attribute_int(ctx, dev->out, "bidi_level", span->bidi_level);
		s_write_attribute_int(ctx, dev->out, "markup_dir", span->markup_dir);
		s_write_attribute_int(ctx, dev->out, "language", span->language);
		s_write_attribute_int(ctx, dev->out, "cap", span->cap);
		s_xml_starttag_end(ctx, dev->out);

		for (i=0; i<span->len; ++i)
		{
			fz_text_item *item = &span->items[i];

			s_xml_starttag_begin(ctx, dev->out, "char");
			s_write_attribute_float(ctx, dev->out, "x", item->x);
			s_write_attribute_float(ctx, dev->out, "y", item->y);
			s_write_attribute_int(ctx, dev->out, "gid", item->gid);
			s_write_attribute_int(ctx, dev->out, "ucs", item->ucs);

			/*
			 * Firefox complains if we put special characters here; it's only for debugging
			 * so this isn't really a problem.
			 */
			s_write_attribute_char(ctx, dev->out, "debug_char",
				(item->ucs >= 32 && item->ucs < 128 && item->ucs != '"')
					? item->ucs : ' '
				);
			s_write_attribute_float(ctx, dev->out, "adv", span->items[i].adv);
			s_xml_starttag_empty_end(ctx, dev->out);
		}

		s_xml_endtag(ctx, dev->out, "span");
	}
}

static void
fz_xmltext_fill_text(fz_context *ctx, fz_device *dev_, const fz_text *text, fz_matrix ctm,
	fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
{
	fz_xmltext_text(ctx, dev_, text, ctm, colorspace, color, alpha, color_params);
}

static void
fz_xmltext_stroke_text(fz_context *ctx, fz_device *dev_, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm,
	fz_colorspace *colorspace, const float *color, float alpha, fz_color_params color_params)
{
	fz_xmltext_text(ctx, dev_, text, ctm, colorspace, color, alpha, color_params);
}

static void
fz_xmltext_clip_text(fz_context *ctx, fz_device *dev_, const fz_text *text, fz_matrix ctm, fz_rect scissor)
{
	fz_xmltext_text(ctx, dev_, text, ctm, NULL, NULL, 0 /*alpha*/, fz_default_color_params);
}

static void
fz_xmltext_clip_stroke_text(fz_context *ctx, fz_device *dev_, const fz_text *text, const fz_stroke_state *stroke, fz_matrix ctm, fz_rect scissor)
{
	fz_xmltext_text(ctx, dev_, text, ctm, NULL, 0, 0, fz_default_color_params);
}

static void
fz_xmltext_ignore_text(fz_context *ctx, fz_device *dev_, const fz_text *text, fz_matrix ctm)
{
}

static void
fz_stext_close_device(fz_context *ctx, fz_device *dev_)
{
}



static void fz_xmltext_fill_image(fz_context *ctx, fz_device *dev_, fz_image *img, fz_matrix ctm, float alpha, fz_color_params color_params)
{
	fz_xmltext_device *dev = (fz_xmltext_device*) dev_;
	fz_pixmap *pixmap = NULL;
	fz_try(ctx)
	{
		const char *type = NULL;
		fz_compressed_buffer *compressed;
		s_xml_starttag_begin(ctx, dev->out, "image");
		/* First try to write compressed data. */
		compressed = fz_compressed_image_buffer(ctx, img);
		if (compressed)
		{
			if (compressed->params.type == FZ_IMAGE_UNKNOWN)
			{
				/* unknown image type. */
			}
			else if (compressed->params.type == FZ_IMAGE_RAW)
			{
				type = "raw";
				s_write_attribute_string(ctx, dev->out, "type", type);
			}
			else if (compressed->params.type == FZ_IMAGE_FAX)
			{
				type = "fax";
				s_write_attribute_string(ctx, dev->out, "type", type);
				s_write_attribute_int(ctx, dev->out, "columns", compressed->params.u.fax.columns);
				s_write_attribute_int(ctx, dev->out, "rows", compressed->params.u.fax.rows);
				s_write_attribute_int(ctx, dev->out, "k", compressed->params.u.fax.k);
				s_write_attribute_int(ctx, dev->out, "end_of_line", compressed->params.u.fax.end_of_line);
				s_write_attribute_int(ctx, dev->out, "encoded_byte_align", compressed->params.u.fax.encoded_byte_align);
				s_write_attribute_int(ctx, dev->out, "end_of_block", compressed->params.u.fax.end_of_block);
				s_write_attribute_int(ctx, dev->out, "black_is_1", compressed->params.u.fax.black_is_1);
				s_write_attribute_int(ctx, dev->out, "damaged_rows_before_error", compressed->params.u.fax.damaged_rows_before_error);
			}
			else if (compressed->params.type == FZ_IMAGE_FLATE)
			{
				type = "flate";
				s_write_attribute_string(ctx, dev->out, "type", type);
				s_write_attribute_int(ctx, dev->out, "columns", compressed->params.u.flate.columns);
				s_write_attribute_int(ctx, dev->out, "colors", compressed->params.u.flate.colors);
				s_write_attribute_int(ctx, dev->out, "predictor", compressed->params.u.flate.predictor);
				s_write_attribute_int(ctx, dev->out, "bpc", compressed->params.u.flate.bpc);
			}
			else if (compressed->params.type == FZ_IMAGE_LZW)
			{
				type = "lzw";
				s_write_attribute_string(ctx, dev->out, "type", type);
				s_write_attribute_int(ctx, dev->out, "columns", compressed->params.u.lzw.columns);
				s_write_attribute_int(ctx, dev->out, "colors", compressed->params.u.lzw.colors);
				s_write_attribute_int(ctx, dev->out, "predictor", compressed->params.u.lzw.predictor);
				s_write_attribute_int(ctx, dev->out, "bpc", compressed->params.u.lzw.bpc);
				s_write_attribute_int(ctx, dev->out, "early_change", compressed->params.u.lzw.early_change);
			}
			else if (compressed->params.type == FZ_IMAGE_BMP)
			{
				type = "bmp";
				s_write_attribute_string(ctx, dev->out, "type", type);
			}
			else if (compressed->params.type == FZ_IMAGE_GIF)
			{
				type = "gif";
				s_write_attribute_string(ctx, dev->out, "type", type);
			}
			else if (compressed->params.type == FZ_IMAGE_JBIG2)
			{
				type = "jbig2";
				s_write_attribute_string(ctx, dev->out, "type", type);
				/* do we need to write out *compressed->params.globals somehow? */
			}
			else if (compressed->params.type == FZ_IMAGE_JPEG)
			{
				type = "jpeg";
				s_write_attribute_string(ctx, dev->out, "type", type);
				s_write_attribute_int(ctx, dev->out, "color_transform", compressed->params.u.jpeg.color_transform);
				if (compressed->params.u.jpeg.invert_cmyk)
					s_write_attribute_int(ctx, dev->out, "invert_cmyk", 1);
			}
			else if (compressed->params.type == FZ_IMAGE_JPX)
			{
				type = "jpx";
				s_write_attribute_string(ctx, dev->out, "type", type);
				s_write_attribute_int(ctx, dev->out, "smask_in_data", compressed->params.u.jpx.smask_in_data);
			}
			else if (compressed->params.type == FZ_IMAGE_JXR)
			{
				type = "jxr";
				s_write_attribute_string(ctx, dev->out, "type", type);
			}
			else if (compressed->params.type == FZ_IMAGE_PNG)
			{
				type = "png";
				s_write_attribute_string(ctx, dev->out, "type", type);
			}
			else if (compressed->params.type == FZ_IMAGE_PNM)
			{
				type = "pnm";
				s_write_attribute_string(ctx, dev->out, "type", type);
			}
			else if (compressed->params.type == FZ_IMAGE_TIFF)
			{
				type = "tiff";
				s_write_attribute_string(ctx, dev->out, "type", type);
			}
			else
			{
				/* Unrecognised. */
			}

			if (type)
			{
				/* Write out raw data. */
				unsigned char *data;
				size_t	datasize = fz_buffer_storage(ctx, compressed->buffer, &data);
				size_t i;
				s_write_attribute_size(ctx, dev->out, "datasize", datasize);
				s_xml_starttag_end(ctx, dev->out);
				for (i=0; i<datasize; ++i)
				{
					if (i % 32 == 0) fz_write_printf(ctx, dev->out, "\n   ");
					if (i % 4 == 0) fz_write_printf(ctx, dev->out, " ");
					fz_write_printf(ctx, dev->out, "%02x", data[i]);
				}
				fz_write_printf(ctx, dev->out, "\n");
			}
		}

		if (!type)
		{
			/* Compressed data not available, so write out raw pixel values. */
			int l2factor = 0;
			int y;
			s_write_attribute_string(ctx, dev->out, "type", "pixmap");
			s_xml_starttag_end(ctx, dev->out);
			pixmap = img->get_pixmap(ctx, img, NULL /*subarea*/, img->w, img->h, &l2factor);
			s_write_attribute_int(ctx, dev->out, "x", pixmap->x);
			s_write_attribute_int(ctx, dev->out, "y", pixmap->y);
			s_write_attribute_int(ctx, dev->out, "w", pixmap->w);
			s_write_attribute_int(ctx, dev->out, "h", pixmap->h);
			s_write_attribute_int(ctx, dev->out, "n", pixmap->n);
			s_write_attribute_int(ctx, dev->out, "s", pixmap->s);
			s_write_attribute_int(ctx, dev->out, "alpha", pixmap->alpha);
			s_write_attribute_int(ctx, dev->out, "flags", pixmap->flags);
			s_write_attribute_int(ctx, dev->out, "xres", pixmap->xres);
			s_write_attribute_int(ctx, dev->out, "yres", pixmap->yres);
			s_write_attribute_matrix(ctx, dev->out, "ctm", &ctm);
			s_xml_starttag_end(ctx, dev->out);
			for (y=0; y<pixmap->h; ++y)
			{
				int x;
				s_xml_starttag_begin(ctx, dev->out, "line");
				s_write_attribute_int(ctx, dev->out, "y", y);
				s_xml_starttag_end(ctx, dev->out);
				for (x=0; x<pixmap->w; ++x)
				{
					int b;
					fz_write_printf(ctx, dev->out, " ");
					for (b=0; b<pixmap->n; ++b)
					{
						fz_write_printf(ctx, dev->out, "%02x", pixmap->samples[y*(size_t)pixmap->stride + x*(size_t)pixmap->n + b]);
					}
				}
				s_xml_endtag(ctx, dev->out, "line");
			}
		}
		s_xml_endtag(ctx, dev->out, "image");
	}
	fz_always(ctx)
	{
		fz_drop_pixmap(ctx, pixmap);
	}
	fz_catch(ctx)
	{
		fz_rethrow(ctx);
	}
}

fz_device *fz_new_xmltext_device(fz_context *ctx, fz_output *out)
{
	fz_xmltext_device *dev = fz_new_derived_device(ctx, fz_xmltext_device);

	dev->super.close_device = fz_stext_close_device;

	dev->super.fill_text = fz_xmltext_fill_text;
	dev->super.stroke_text = fz_xmltext_stroke_text;
	dev->super.clip_text = fz_xmltext_clip_text;
	dev->super.clip_stroke_text = fz_xmltext_clip_stroke_text;
	dev->super.ignore_text = fz_xmltext_ignore_text;
	dev->super.fill_image = fz_xmltext_fill_image;

	dev->out = out;

	return (fz_device*)dev;
}