File: text-decoder.c

package info (click to toggle)
mupdf 1.27.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,224 kB
  • sloc: ansic: 335,320; python: 20,906; java: 7,520; javascript: 2,213; makefile: 1,152; xml: 675; cpp: 639; sh: 513; cs: 307; awk: 10; sed: 7; lisp: 3
file content (236 lines) | stat: -rw-r--r-- 7,316 bytes parent folder | download | duplicates (2)
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
// Copyright (C) 2024 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"
#include "mupdf/pdf.h"

static int simple_text_decode_bound(fz_text_decoder *dec, unsigned char *s, int n)
{
	return n * 4 + 1;
}

static int simple_text_decode_size(fz_text_decoder *dec, unsigned char *s, int n)
{
	const unsigned short *table = dec->table1;
	unsigned char *e = s + n;
	int len = 1;
	while (s < e)
		len += fz_runelen(table[*s++]);
	return len;
}

static void simple_text_decode(fz_text_decoder *dec, char *p, unsigned char *s, int n)
{
	const unsigned short *table = dec->table1;
	unsigned char *e = s + n;
	while (s < e)
		p += fz_runetochar(p, table[*s++]);
	*p = 0;
}

static int utf16be_text_decode_bound(fz_text_decoder *dec, unsigned char *s, int n)
{
	return n * 2 + 1;
}

static int utf16le_text_decode_bound(fz_text_decoder *dec, unsigned char *s, int n)
{
	return n * 2 + 1;
}

static int utf16be_text_decode_size(fz_text_decoder *dec, unsigned char *s, int n)
{
	unsigned char *e = s + n;
	int len = 1;
	while (s + 1 < e) {
		len += fz_runelen(s[0] << 8 | s[1]);
		s += 2;
	}
	return len;
}

static int utf16le_text_decode_size(fz_text_decoder *dec, unsigned char *s, int n)
{
	unsigned char *e = s + n;
	int len = 1;
	while (s + 1 < e) {
		len += fz_runelen(s[0] | s[1] << 8);
		s += 2;
	}
	return len;
}

static void utf16be_text_decode(fz_text_decoder *dec, char *p, unsigned char *s, int n)
{
	unsigned char *e = s + n;
	while (s + 1 < e) {
		p += fz_runetochar(p, s[0] << 8 | s[1]);
		s += 2;
	}
	*p = 0;
}

static void utf16le_text_decode(fz_text_decoder *dec, char *p, unsigned char *s, int n)
{
	unsigned char *e = s + n;
	while (s + 1 < e) {
		p += fz_runetochar(p, s[0] | s[1] << 8);
		s += 2;
	}
	*p = 0;
}

static int cjk_text_decode_bound(fz_text_decoder *dec, unsigned char *s, int n)
{
	return n * 4 + 1;
}

static int cjk_text_decode_size(fz_text_decoder *dec, unsigned char *s, int n)
{
	unsigned char *e = s + n;
	pdf_cmap *to_cid = dec->table1;
	pdf_cmap *to_uni = dec->table2;
	unsigned int raw;
	int cid, uni;
	int len = 1;
	while (s < e) {
		s += pdf_decode_cmap(to_cid, s, e, &raw);
		cid = pdf_lookup_cmap(to_cid, raw);
		uni = pdf_lookup_cmap(to_uni, cid);
		if (uni < 0) {
			// ASCII control characters are missing in the CMaps
			if (raw < 32)
				uni = raw;
			else
				uni = FZ_REPLACEMENT_CHARACTER;
		}
		len += fz_runelen(uni);
	}
	return len;
}

static void cjk_text_decode(fz_text_decoder *dec, char *p, unsigned char *s, int n)
{
	unsigned char *e = s + n;
	pdf_cmap *to_cid = dec->table1;
	pdf_cmap *to_uni = dec->table2;
	unsigned int raw;
	int cid, uni;
	while (s < e) {
		s += pdf_decode_cmap(to_cid, s, e, &raw);
		cid = pdf_lookup_cmap(to_cid, raw);
		uni = pdf_lookup_cmap(to_uni, cid);
		if (uni < 0) {
			// ASCII control characters are missing in the CMaps
			if (raw < 32)
				uni = raw;
			else
				uni = FZ_REPLACEMENT_CHARACTER;
		}
		p += fz_runetochar(p, uni);
	}
	*p = 0;
}

static void fz_init_simple_text_decoder(fz_context *ctx, fz_text_decoder *dec, const unsigned short *table)
{
	dec->decode_bound = simple_text_decode_bound;
	dec->decode_size = simple_text_decode_size;
	dec->decode = simple_text_decode;
	dec->table1 = (void*)table;
}

static void fz_init_utf16be_text_decoder(fz_context *ctx, fz_text_decoder *dec)
{
	dec->decode_bound = utf16be_text_decode_bound;
	dec->decode_size = utf16be_text_decode_size;
	dec->decode = utf16be_text_decode;
}

static void fz_init_utf16le_text_decoder(fz_context *ctx, fz_text_decoder *dec)
{
	dec->decode_bound = utf16le_text_decode_bound;
	dec->decode_size = utf16le_text_decode_size;
	dec->decode = utf16le_text_decode;
}

static void fz_init_cjk_text_decoder(fz_context *ctx, fz_text_decoder *dec, const char *to_cid, const char *to_uni)
{
	dec->decode_bound = cjk_text_decode_bound;
	dec->decode_size = cjk_text_decode_size;
	dec->decode = cjk_text_decode;
	dec->table1 = pdf_load_builtin_cmap(ctx, to_cid);
	if (!dec->table1)
		fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "unknown CMap: %s", to_cid);
	dec->table2 = pdf_load_builtin_cmap(ctx, to_uni);
	if (!dec->table2)
		fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "unknown CMap: %s", to_uni);
}

void fz_init_text_decoder(fz_context *ctx, fz_text_decoder *dec, const char *enc)
{
	// Recognize IANA character set identifiers (case insensitive).
	// https://www.iana.org/assignments/character-sets/character-sets.xhtml

	if (!fz_strcasecmp(enc, "utf-16"))
		fz_init_utf16le_text_decoder(ctx, dec);
	else if (!fz_strcasecmp(enc, "utf-16be"))
		fz_init_utf16be_text_decoder(ctx, dec);
	else if (!fz_strcasecmp(enc, "utf-16le"))
		fz_init_utf16le_text_decoder(ctx, dec);

	else if (!fz_strcasecmp(enc, "euc-jp"))
		fz_init_cjk_text_decoder(ctx, dec, "EUC-H", "Adobe-Japan1-UCS2");
	else if (!fz_strcasecmp(enc, "shift_jis") || !fz_strcasecmp(enc, "sjis"))
		fz_init_cjk_text_decoder(ctx, dec, "90msp-H", "Adobe-Japan1-UCS2");

	else if (!fz_strcasecmp(enc, "euc-kr"))
		fz_init_cjk_text_decoder(ctx, dec, "KSCms-UHC-H", "Adobe-Korea1-UCS2");

	else if (!fz_strcasecmp(enc, "euc-cn"))
		fz_init_cjk_text_decoder(ctx, dec, "GB-EUC-H", "Adobe-GB1-UCS2");
	else if (!fz_strcasecmp(enc, "gbk") || !fz_strcasecmp(enc, "gb2312") || !fz_strcasecmp(enc, "gb18030"))
		fz_init_cjk_text_decoder(ctx, dec, "GBK2K-H", "Adobe-GB1-UCS2");

	else if (!fz_strcasecmp(enc, "euc-tw"))
		fz_init_cjk_text_decoder(ctx, dec, "CNS-EUC-H", "Adobe-CNS1-UCS2");
	else if (!fz_strcasecmp(enc, "big5"))
		fz_init_cjk_text_decoder(ctx, dec, "ETen-B5-H", "Adobe-CNS1-UCS2");
	else if (!fz_strcasecmp(enc, "big5-hkscs"))
		fz_init_cjk_text_decoder(ctx, dec, "HKscs-B5-H", "Adobe-CNS1-UCS2");

	else if (!fz_strcasecmp(enc, "iso-8859-1"))
		fz_init_simple_text_decoder(ctx, dec, fz_unicode_from_iso8859_1);
	else if (!fz_strcasecmp(enc, "iso-8859-7"))
		fz_init_simple_text_decoder(ctx, dec, fz_unicode_from_iso8859_7);
	else if (!fz_strcasecmp(enc, "koi8-r"))
		fz_init_simple_text_decoder(ctx, dec, fz_unicode_from_koi8u);
	else if (!fz_strcasecmp(enc, "windows-1250"))
		fz_init_simple_text_decoder(ctx, dec, fz_unicode_from_windows_1250);
	else if (!fz_strcasecmp(enc, "windows-1251"))
		fz_init_simple_text_decoder(ctx, dec, fz_unicode_from_windows_1251);
	else if (!fz_strcasecmp(enc, "windows-1252"))
		fz_init_simple_text_decoder(ctx, dec, fz_unicode_from_windows_1252);

	else
		fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "unknown text encoding: %s", enc);
}