Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4680 right-hear 1
#include "fitz.h"
2
#include "mupdf.h"
3
 
4
/* Load or synthesize ToUnicode map for fonts */
5
 
6
fz_error
7
pdf_load_to_unicode(pdf_font_desc *font, pdf_xref *xref,
8
	char **strings, char *collection, fz_obj *cmapstm)
9
{
10
	fz_error error = fz_okay;
11
	pdf_cmap *cmap;
12
	int cid;
13
	int ucsbuf[8];
14
	int ucslen;
15
	int i;
16
 
17
	if (pdf_is_stream(xref, fz_to_num(cmapstm), fz_to_gen(cmapstm)))
18
	{
19
		error = pdf_load_embedded_cmap(&cmap, xref, cmapstm);
20
		if (error)
21
			return fz_rethrow(error, "cannot load embedded cmap (%d %d R)", fz_to_num(cmapstm), fz_to_gen(cmapstm));
22
 
23
		font->to_unicode = pdf_new_cmap();
24
 
25
		for (i = 0; i < (strings ? 256 : 65536); i++)
26
		{
27
			cid = pdf_lookup_cmap(font->encoding, i);
28
			if (cid >= 0)
29
			{
30
				ucslen = pdf_lookup_cmap_full(cmap, i, ucsbuf);
31
				if (ucslen == 1)
32
					pdf_map_range_to_range(font->to_unicode, cid, cid, ucsbuf[0]);
33
				if (ucslen > 1)
34
					pdf_map_one_to_many(font->to_unicode, cid, ucsbuf, ucslen);
35
			}
36
		}
37
 
38
		pdf_sort_cmap(font->to_unicode);
39
 
40
		pdf_drop_cmap(cmap);
41
	}
42
 
43
	else if (collection)
44
	{
45
		error = fz_okay;
46
 
47
		if (!strcmp(collection, "Adobe-CNS1"))
48
			error = pdf_load_system_cmap(&font->to_unicode, "Adobe-CNS1-UCS2");
49
		else if (!strcmp(collection, "Adobe-GB1"))
50
			error = pdf_load_system_cmap(&font->to_unicode, "Adobe-GB1-UCS2");
51
		else if (!strcmp(collection, "Adobe-Japan1"))
52
			error = pdf_load_system_cmap(&font->to_unicode, "Adobe-Japan1-UCS2");
53
		else if (!strcmp(collection, "Adobe-Korea1"))
54
			error = pdf_load_system_cmap(&font->to_unicode, "Adobe-Korea1-UCS2");
55
 
56
		if (error)
57
			return fz_rethrow(error, "cannot load ToUnicode system cmap %s-UCS2", collection);
58
	}
59
 
60
	if (strings)
61
	{
62
		/* TODO one-to-many mappings */
63
 
64
		font->cid_to_ucs_len = 256;
65
		font->cid_to_ucs = fz_calloc(256, sizeof(unsigned short));
66
 
67
		for (i = 0; i < 256; i++)
68
		{
69
			if (strings[i])
70
				font->cid_to_ucs[i] = pdf_lookup_agl(strings[i]);
71
			else
72
				font->cid_to_ucs[i] = '?';
73
		}
74
	}
75
 
76
	if (!font->to_unicode && !font->cid_to_ucs)
77
	{
78
		/* TODO: synthesize a ToUnicode if it's a freetype font with
79
		 * cmap and/or post tables or if it has glyph names. */
80
	}
81
 
82
	return fz_okay;
83
}