1/* cmapdump.c -- parse a CMap file and dump it as a c-struct */
2
3#include "mupdf/fitz.h"
4#include "mupdf/pdf.h"
5
6#include <stdio.h>
7#include <string.h>
8
9static void
10clean(char *p)
11{
12 while (*p)
13 {
14 if ((*p == '/') || (*p == '.') || (*p == '\\') || (*p == '-'))
15 *p = '_';
16 p ++;
17 }
18}
19
20int
21cmapdump_main(int argc, char **argv)
22{
23 pdf_cmap *cmap;
24 fz_stream *fi;
25 char name[256];
26 int i, k;
27 fz_context *ctx;
28
29 if (argc < 2)
30 {
31 fprintf(stderr, "usage: cmapdump > out.c [lots of cmap files]\n");
32 return 1;
33 }
34
35 ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
36 if (!ctx)
37 {
38 fprintf(stderr, "cannot initialise context\n");
39 return 1;
40 }
41
42 printf("/* This is an automatically generated file. Do not edit. */\n");
43
44 for (i = 1; i < argc; i++)
45 {
46 fi = fz_open_file(ctx, argv[i]);
47 cmap = pdf_load_cmap(ctx, fi);
48 fz_drop_stream(ctx, fi);
49
50 strcpy(name, cmap->cmap_name);
51 clean(name);
52
53 printf("\n/* %s */\n\n", cmap->cmap_name);
54
55 if (cmap->rlen)
56 {
57 printf("static const pdf_range cmap_%s_ranges[] = {", name);
58 for (k = 0; k < cmap->rlen; k++)
59 {
60 printf("\n{%u,%u,%u},", cmap->ranges[k].low, cmap->ranges[k].high, cmap->ranges[k].out);
61 }
62 printf("\n};\n\n");
63 }
64
65 if (cmap->xlen)
66 {
67 printf("static const pdf_xrange cmap_%s_xranges[] = {", name);
68 for (k = 0; k < cmap->xlen; k++)
69 {
70 printf("\n{%u,%u,%u},", cmap->xranges[k].low, cmap->xranges[k].high, cmap->xranges[k].out);
71 }
72 printf("\n};\n\n");
73 }
74
75 if (cmap->mlen > 0)
76 {
77 printf("static const pdf_mrange cmap_%s_mranges[] = {", name);
78 for (k = 0; k < cmap->mlen; k++)
79 {
80 printf("\n{%u,%u},", cmap->mranges[k].low, cmap->mranges[k].out);
81 }
82 printf("\n};\n\n");
83 }
84
85 if (cmap->dlen > 0)
86 {
87 int ti = 0, tn = cmap->dict[0];
88 printf("static const int cmap_%s_table[] = {\n", name);
89 for (k = 0; k < cmap->dlen; k++)
90 {
91 if (ti > tn)
92 {
93 printf("\n");
94 ti = 1;
95 tn = cmap->dict[k];
96 }
97 else
98 ++ti;
99 printf("%u,", cmap->dict[k]);
100 }
101 printf("\n};\n\n");
102 }
103
104 printf("static pdf_cmap cmap_%s = {\n", name);
105 printf("\t{ -1, pdf_drop_cmap_imp },\n");
106 printf("\t/* cmapname */ \"%s\",\n", cmap->cmap_name);
107 printf("\t/* usecmap */ \"%s\", NULL,\n", cmap->usecmap_name);
108 printf("\t/* wmode */ %u,\n", cmap->wmode);
109 printf("\t/* codespaces */ %u, {\n", cmap->codespace_len);
110 if (cmap->codespace_len == 0)
111 {
112 printf("\t\t{ 0, 0, 0 },\n");
113 }
114 for (k = 0; k < cmap->codespace_len; k++)
115 {
116 int n = cmap->codespace[k].n;
117 printf("\t\t{ %u, 0x%0*x, 0x%0*x },\n", n,
118 n*2, cmap->codespace[k].low,
119 n*2, cmap->codespace[k].high);
120 }
121 printf("\t},\n");
122
123 if (cmap->rlen)
124 printf("\t%u, %u, (pdf_range*)cmap_%s_ranges,\n", cmap->rlen, cmap->rlen, name);
125 else
126 printf("\t0, 0, NULL, /* ranges */\n");
127 if (cmap->xlen)
128 printf("\t%u, %u, (pdf_xrange*)cmap_%s_xranges,\n", cmap->xlen, cmap->xlen, name);
129 else
130 printf("\t0, 0, NULL, /* xranges */\n");
131 if (cmap->mlen)
132 printf("\t%u, %u, (pdf_mrange*)cmap_%s_mranges,\n", cmap->mlen, cmap->mlen, name);
133 else
134 printf("\t0, 0, NULL, /* mranges */\n");
135 if (cmap->dict)
136 printf("\t%u, %u, (int*)cmap_%s_table,\n", cmap->dlen, cmap->dlen, name);
137 else
138 printf("\t0, 0, NULL, /* table */\n");
139 printf("\t0, 0, 0, NULL /* splay tree */\n");
140 printf("};\n");
141
142 pdf_drop_cmap(ctx, cmap);
143 }
144
145 fz_drop_context(ctx);
146 return 0;
147}
148