1 #include "mupdf/fitz.h"
2 #include "mupdf/pdf.h"
3 
4 #include <string.h>
5 #include <stdlib.h>
6 
7 #include "encodings.h"
8 #include "glyphlist.h"
9 #include "smallcaps.h"
10 
11 #define FROM_UNICODE(ENC) \
12 	int l = 0; \
13 	int r = nelem(ENC##_from_unicode) - 1; \
14 	if (u < 128) \
15 		return u; \
16 	while (l <= r) \
17 	{ \
18 		int m = (l + r) >> 1; \
19 		if (u < ENC##_from_unicode[m].u) \
20 			r = m - 1; \
21 		else if (u > ENC##_from_unicode[m].u) \
22 			l = m + 1; \
23 		else \
24 			return ENC##_from_unicode[m].c; \
25 	} \
26 	return -1; \
27 
fz_iso8859_1_from_unicode(int u)28 int fz_iso8859_1_from_unicode(int u) { FROM_UNICODE(iso8859_1) }
fz_iso8859_7_from_unicode(int u)29 int fz_iso8859_7_from_unicode(int u) { FROM_UNICODE(iso8859_7) }
fz_koi8u_from_unicode(int u)30 int fz_koi8u_from_unicode(int u) { FROM_UNICODE(koi8u) }
fz_windows_1250_from_unicode(int u)31 int fz_windows_1250_from_unicode(int u) { FROM_UNICODE(windows_1250) }
fz_windows_1251_from_unicode(int u)32 int fz_windows_1251_from_unicode(int u) { FROM_UNICODE(windows_1251) }
fz_windows_1252_from_unicode(int u)33 int fz_windows_1252_from_unicode(int u) { FROM_UNICODE(windows_1252) }
34 
35 int
fz_unicode_from_glyph_name_strict(const char * name)36 fz_unicode_from_glyph_name_strict(const char *name)
37 {
38 	int l = 0;
39 	int r = nelem(single_name_list) - 1;
40 
41 	while (l <= r)
42 	{
43 		int m = (l + r) >> 1;
44 		int c = strcmp(name, single_name_list[m]);
45 		if (c < 0)
46 			r = m - 1;
47 		else if (c > 0)
48 			l = m + 1;
49 		else
50 			return single_code_list[m];
51 	}
52 	return 0;
53 }
54 
55 int
fz_unicode_from_glyph_name(const char * name)56 fz_unicode_from_glyph_name(const char *name)
57 {
58 	char buf[64];
59 	char *p;
60 	int l = 0;
61 	int r = nelem(single_name_list) - 1;
62 	int code = 0;
63 
64 	fz_strlcpy(buf, name, sizeof buf);
65 
66 	/* kill anything after first period and underscore */
67 	p = strchr(buf, '.');
68 	if (p) p[0] = 0;
69 	p = strchr(buf, '_');
70 	if (p) p[0] = 0;
71 
72 	while (l <= r)
73 	{
74 		int m = (l + r) >> 1;
75 		int c = strcmp(buf, single_name_list[m]);
76 		if (c < 0)
77 			r = m - 1;
78 		else if (c > 0)
79 			l = m + 1;
80 		else
81 			return single_code_list[m];
82 	}
83 
84 	if (buf[0] == 'u' && buf[1] == 'n' && buf[2] == 'i' && strlen(buf) == 7)
85 		code = strtol(buf + 3, NULL, 16);
86 	else if (buf[0] == 'u')
87 		code = strtol(buf + 1, NULL, 16);
88 	else if (buf[0] == 'a' && buf[1] != 0 && buf[2] != 0)
89 		code = strtol(buf + 1, NULL, 10);
90 
91 	return (code > 0 && code <= 0x10ffff) ? code : FZ_REPLACEMENT_CHARACTER;
92 }
93 
94 static const char *empty_dup_list[] = { 0 };
95 
96 const char **
fz_duplicate_glyph_names_from_unicode(int ucs)97 fz_duplicate_glyph_names_from_unicode(int ucs)
98 {
99 	int l = 0;
100 	int r = nelem(agl_dup_offsets) / 2 - 1;
101 	while (l <= r)
102 	{
103 		int m = (l + r) >> 1;
104 		if (ucs < agl_dup_offsets[m << 1])
105 			r = m - 1;
106 		else if (ucs > agl_dup_offsets[m << 1])
107 			l = m + 1;
108 		else
109 			return agl_dup_names + agl_dup_offsets[(m << 1) + 1];
110 	}
111 	return empty_dup_list;
112 }
113 
114 const char *
fz_glyph_name_from_unicode_sc(int u)115 fz_glyph_name_from_unicode_sc(int u)
116 {
117 	int l = 0;
118 	int r = nelem(glyph_name_from_unicode_sc) / 2 - 1;
119 	while (l <= r)
120 	{
121 		int m = (l + r) >> 1;
122 		if (u < glyph_name_from_unicode_sc[m].u)
123 			r = m - 1;
124 		else if (u > glyph_name_from_unicode_sc[m].u)
125 			l = m + 1;
126 		else
127 			return glyph_name_from_unicode_sc[m].n;
128 	}
129 	return NULL;
130 }
131