]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/jni/scripts/cmapdump.c
Add MuPDF native source codes
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / jni / scripts / cmapdump.c
1 /* cmapdump.c -- parse a CMap file and dump it as a c-struct */
2
3 #include <stdio.h>
4 #include <string.h>
5
6 /* We never want to build memento versions of the cmapdump util */
7 #undef MEMENTO
8
9 #include "mupdf/pdf.h"
10
11 #include "../source/fitz/context.c"
12 #include "../source/fitz/error.c"
13 #include "../source/fitz/memory.c"
14 #include "../source/fitz/string.c"
15 #include "../source/fitz/buffer.c"
16 #include "../source/fitz/stream-open.c"
17 #include "../source/fitz/stream-read.c"
18 #include "../source/fitz/strtod.c"
19 #include "../source/fitz/ftoa.c"
20 #include "../source/fitz/printf.c"
21
22 #include "../source/pdf/pdf-lex.c"
23 #include "../source/pdf/pdf-cmap.c"
24 #include "../source/pdf/pdf-cmap-parse.c"
25
26 static void
27 clean(char *p)
28 {
29         while (*p)
30         {
31                 if ((*p == '/') || (*p == '.') || (*p == '\\') || (*p == '-'))
32                         *p = '_';
33                 p ++;
34         }
35 }
36
37 int
38 main(int argc, char **argv)
39 {
40         pdf_cmap *cmap;
41         fz_stream *fi;
42         FILE *fo;
43         char name[256];
44         char *realname;
45         int i, k, m;
46         fz_context *ctx;
47
48         if (argc < 3)
49         {
50                 fprintf(stderr, "usage: cmapdump output.c lots of cmap files\n");
51                 return 1;
52         }
53
54         ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
55         if (!ctx)
56         {
57                 fprintf(stderr, "cannot initialise context\n");
58                 return 1;
59         }
60
61 #undef fopen
62         fo = fopen(argv[1], "wb");
63         if (!fo)
64         {
65                 fprintf(stderr, "cmapdump: could not open output file '%s'\n", argv[1]);
66                 return 1;
67         }
68
69         fprintf(fo, "/* This is an automatically generated file. Do not edit. */\n");
70
71         for (i = 2; i < argc; i++)
72         {
73                 realname = strrchr(argv[i], '/');
74                 if (!realname)
75                         realname = strrchr(argv[i], '\\');
76                 if (realname)
77                         realname ++;
78                 else
79                         realname = argv[i];
80
81                 /* ignore VCS folders (such as .svn) */
82                 if (*realname == '.')
83                         continue;
84
85                 if (strlen(realname) > (sizeof name - 1))
86                 {
87                         fprintf(stderr, "cmapdump: file name too long\n");
88                         if (fclose(fo))
89                         {
90                                 fprintf(stderr, "cmapdump: could not close output file '%s'\n", argv[1]);
91                                 return 1;
92                         }
93                         return 1;
94                 }
95
96                 strcpy(name, realname);
97                 clean(name);
98
99                 fi = fz_open_file(ctx, argv[i]);
100                 cmap = pdf_load_cmap(ctx, fi);
101                 fz_close(fi);
102
103                 fprintf(fo, "\n/* %s */\n\n", cmap->cmap_name);
104
105                 if (cmap->rlen)
106                 {
107                         fprintf(fo, "static const pdf_range cmap_%s_ranges[] = {", name);
108                         for (k = 0; k < cmap->rlen; k++)
109                         {
110                                 if (k % 4 == 0)
111                                         fprintf(fo, "\n");
112                                 fprintf(fo, "{%uu,%uu,%uu},", cmap->ranges[k].low, cmap->ranges[k].high, cmap->ranges[k].out);
113                         }
114                         fprintf(fo, "\n};\n\n");
115                 }
116
117                 if (cmap->xlen)
118                 {
119                         fprintf(fo, "static const pdf_xrange cmap_%s_xranges[] = {", name);
120                         for (k = 0; k < cmap->xlen; k++)
121                         {
122                                 if (k % 4 == 0)
123                                         fprintf(fo, "\n");
124                                 fprintf(fo, "{%uu,%uu,%uu},", cmap->xranges[k].low, cmap->xranges[k].high, cmap->xranges[k].out);
125                         }
126                         fprintf(fo, "\n};\n\n");
127                 }
128
129                 if (cmap->mlen > 0)
130                 {
131                         fprintf(fo, "static const pdf_mrange cmap_%s_mranges[] = {", name);
132                         for (k = 0; k < cmap->mlen; k++)
133                         {
134                                 fprintf(fo, "\n{%uu,%uu,{", cmap->mranges[k].low, cmap->mranges[k].len);
135                                 for (m = 0; m < PDF_MRANGE_CAP; ++m)
136                                         fprintf(fo, "%uu,", cmap->mranges[k].out[m]);
137                                 fprintf(fo, "}},");
138                         }
139                         fprintf(fo, "\n};\n\n");
140                 }
141
142                 fprintf(fo, "static pdf_cmap cmap_%s = {\n", name);
143                 fprintf(fo, "\t{-1, pdf_free_cmap_imp}, ");
144                 fprintf(fo, "\"%s\", ", cmap->cmap_name);
145                 fprintf(fo, "\"%s\", 0, ", cmap->usecmap_name);
146                 fprintf(fo, "%u, ", cmap->wmode);
147                 fprintf(fo, "%u,\n\t{ ", cmap->codespace_len);
148                 if (cmap->codespace_len == 0)
149                 {
150                         fprintf(fo, "{0,0,0},");
151                 }
152                 for (k = 0; k < cmap->codespace_len; k++)
153                 {
154                         fprintf(fo, "{%u,%uu,%uu},", cmap->codespace[k].n, cmap->codespace[k].low, cmap->codespace[k].high);
155                 }
156                 fprintf(fo, " },\n");
157
158                 if (cmap->rlen)
159                         fprintf(fo, "\t%u, %u, (pdf_range*) cmap_%s_ranges,\n", cmap->rlen, cmap->rlen, name);
160                 else
161                         fprintf(fo, "\t0, 0, NULL,\n");
162                 if (cmap->xlen)
163                         fprintf(fo, "\t%u, %u, (pdf_xrange*) cmap_%s_xranges,\n", cmap->xlen, cmap->xlen, name);
164                 else
165                         fprintf(fo, "\t0, 0, NULL,\n");
166                 if (cmap->mlen)
167                         fprintf(fo, "\t%u, %u, (pdf_mrange*) cmap_%s_mranges,\n", cmap->mlen, cmap->mlen, name);
168                 else
169                         fprintf(fo, "\t0, 0, NULL,\n");
170
171                 fprintf(fo, "};\n");
172
173                 if (getenv("verbose"))
174                         printf("\t{\"%s\",&cmap_%s},\n", cmap->cmap_name, name);
175         }
176
177         if (fclose(fo))
178         {
179                 fprintf(stderr, "cmapdump: could not close output file '%s'\n", argv[1]);
180                 return 1;
181         }
182
183         fz_free_context(ctx);
184         return 0;
185 }
186
187 void fz_new_font_context(fz_context *ctx)
188 {
189 }
190
191 void fz_drop_font_context(fz_context *ctx)
192 {
193 }
194
195 fz_font_context *fz_keep_font_context(fz_context *ctx)
196 {
197         return NULL;
198 }
199
200 void fz_new_colorspace_context(fz_context *ctx)
201 {
202 }
203
204 void fz_drop_colorspace_context(fz_context *ctx)
205 {
206 }
207
208 fz_colorspace_context *fz_keep_colorspace_context(fz_context *ctx)
209 {
210         return NULL;
211 }
212
213 void fz_new_aa_context(fz_context *ctx)
214 {
215 }
216
217 void fz_free_aa_context(fz_context *ctx)
218 {
219 }
220
221 void fz_copy_aa_context(fz_context *dst, fz_context *src)
222 {
223 }
224
225 void *fz_keep_storable(fz_context *ctx, fz_storable *s)
226 {
227         return s;
228 }
229
230 void fz_drop_storable(fz_context *ctx, fz_storable *s)
231 {
232 }
233
234 void fz_new_store_context(fz_context *ctx, unsigned int max)
235 {
236 }
237
238 void fz_drop_store_context(fz_context *ctx)
239 {
240 }
241
242 fz_store *fz_keep_store_context(fz_context *ctx)
243 {
244         return NULL;
245 }
246
247 int fz_store_scavenge(fz_context *ctx, unsigned int size, int *phase)
248 {
249         return 0;
250 }
251
252 void fz_new_glyph_cache_context(fz_context *ctx)
253 {
254 }
255
256 void fz_drop_glyph_cache_context(fz_context *ctx)
257 {
258 }
259
260 fz_glyph_cache *fz_keep_glyph_cache(fz_context *ctx)
261 {
262         return NULL;
263 }
264
265 void fz_new_document_handler_context(fz_context *ctx)
266 {
267 }
268
269 void fz_drop_document_handler_context(fz_context *ctx)
270 {
271 }
272
273 fz_document_handler_context *fz_keep_document_handler_context(fz_context *ctx)
274 {
275         return NULL;
276 }