comparison mupdf-source/scripts/cmapcleanx.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /* cmapclean.c -- parse a CMap file and write it back out */
2
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "mupdf/pdf.h"
7
8 struct cidrange {
9 unsigned int lo, hi, v;
10 };
11
12 static int cmpcidrange(const void *va, const void *vb)
13 {
14 unsigned int a = ((const struct cidrange *)va)->lo;
15 unsigned int b = ((const struct cidrange *)vb)->lo;
16 return a < b ? -1 : a > b ? 1 : 0;
17 }
18
19 static void pc(unsigned int c)
20 {
21 if (c <= 0xff) printf("<%02x>", c);
22 else if (c <= 0xffff) printf("<%04x>", c);
23 else if (c <= 0xffffff) printf("<%06x>", c);
24 else printf("<%010x>", c);
25 }
26
27 int
28 main(int argc, char **argv)
29 {
30 fz_context *ctx;
31 fz_stream *fi;
32 pdf_cmap *cmap;
33 int k, m, n, i;
34 struct cidrange *r;
35
36 if (argc != 2)
37 {
38 fprintf(stderr, "usage: cmapclean input.cmap\n");
39 return 1;
40 }
41
42 ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
43 if (!ctx)
44 {
45 fprintf(stderr, "cannot initialise context\n");
46 return 1;
47 }
48
49 fi = fz_open_file(ctx, argv[1]);
50 cmap = pdf_load_cmap(ctx, fi);
51 fz_drop_stream(ctx, fi);
52
53 printf("begincmap\n");
54 printf("/CMapName /%s def\n", cmap->cmap_name);
55 printf("/WMode %d def\n", cmap->wmode);
56 if (cmap->usecmap_name[0])
57 printf("/%s usecmap\n", cmap->usecmap_name);
58
59 if (cmap->codespace_len)
60 {
61 printf("begincodespacerange\n");
62 for (k = 0; k < cmap->codespace_len; k++)
63 {
64 if (cmap->codespace[k].n == 1)
65 printf("<%02x> <%02x>\n", cmap->codespace[k].low, cmap->codespace[k].high);
66 else if (cmap->codespace[k].n == 2)
67 printf("<%04x> <%04x>\n", cmap->codespace[k].low, cmap->codespace[k].high);
68 else if (cmap->codespace[k].n == 3)
69 printf("<%06x> <%06x>\n", cmap->codespace[k].low, cmap->codespace[k].high);
70 else if (cmap->codespace[k].n == 4)
71 printf("<%08x> <%08x>\n", cmap->codespace[k].low, cmap->codespace[k].high);
72 else
73 printf("<%x> <%x>\n", cmap->codespace[k].low, cmap->codespace[k].high);
74 }
75 printf("endcodespacerange\n");
76 }
77
78 n = cmap->rlen + cmap->xlen;
79 r = fz_malloc(ctx, n * sizeof *r);
80 i = 0;
81
82 for (k = 0; k < cmap->rlen; k++) {
83 r[i].lo = cmap->ranges[k].low;
84 r[i].hi = cmap->ranges[k].high;
85 r[i].v = cmap->ranges[k].out;
86 ++i;
87 }
88
89 for (k = 0; k < cmap->xlen; k++) {
90 r[i].lo = cmap->xranges[k].low;
91 r[i].hi = cmap->xranges[k].high;
92 r[i].v = cmap->xranges[k].out;
93 ++i;
94 }
95
96 qsort(r, n, sizeof *r, cmpcidrange);
97
98 if (n)
99 {
100 printf("begincidchar\n");
101 for (i = 0; i < n; ++i)
102 {
103 for (k = r[i].lo, m = r[i].v; k <= r[i].hi; ++k, ++m)
104 {
105 pc(k);
106 printf("%u\n", m);
107 }
108 }
109 printf("endcidchar\n");
110 }
111
112 #if 0
113 if (cmap->mlen > 0)
114 {
115 printf("beginbfchar\n");
116 for (k = 0; k < cmap->mlen; k++)
117 {
118 pc(cmap->mranges[k].low);
119
120 printf("<");
121 for (m = 0; m < cmap->mranges[k].len; ++m)
122 printf("%04x", cmap->mranges[k].out[m]);
123 printf(">\n");
124 }
125 printf("endbfchar\n");
126 }
127 #endif
128
129 printf("endcmap\n");
130
131 fz_drop_context(ctx);
132 return 0;
133 }