summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2018-04-04 16:36:21 +0200
committerTor Andersson <tor.andersson@artifex.com>2018-04-25 12:26:32 +0200
commita6d083bb776ecd498e57450ef84c20e39ae604cf (patch)
treea1f4399f011eb2b59e21f0588d322690a6ab14f4 /source
parent84cf672da90dfdaa2dfd2742cc69fa0bad268081 (diff)
downloadmupdf-a6d083bb776ecd498e57450ef84c20e39ae604cf.tar.xz
Add CMap processing scripts, and turn cmapdump into mutool.
A dumping script written in python. A flattening script written in python (for easier editing). A subsetting script written in shell to minimize CMaps by reusing subsets. Use 'mutool cmapdump' to bootstrap or verify cmap dumps.
Diffstat (limited to 'source')
-rw-r--r--source/tools/cmapdump.c147
-rw-r--r--source/tools/mutool.c5
2 files changed, 152 insertions, 0 deletions
diff --git a/source/tools/cmapdump.c b/source/tools/cmapdump.c
new file mode 100644
index 00000000..f1c5450a
--- /dev/null
+++ b/source/tools/cmapdump.c
@@ -0,0 +1,147 @@
+/* cmapdump.c -- parse a CMap file and dump it as a c-struct */
+
+#include "mupdf/fitz.h"
+#include "mupdf/pdf.h"
+
+#include <stdio.h>
+#include <string.h>
+
+static void
+clean(char *p)
+{
+ while (*p)
+ {
+ if ((*p == '/') || (*p == '.') || (*p == '\\') || (*p == '-'))
+ *p = '_';
+ p ++;
+ }
+}
+
+int
+cmapdump_main(int argc, char **argv)
+{
+ pdf_cmap *cmap;
+ fz_stream *fi;
+ char name[256];
+ int i, k;
+ fz_context *ctx;
+
+ if (argc < 2)
+ {
+ fprintf(stderr, "usage: cmapdump > out.c [lots of cmap files]\n");
+ return 1;
+ }
+
+ ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
+ if (!ctx)
+ {
+ fprintf(stderr, "cannot initialise context\n");
+ return 1;
+ }
+
+ printf("/* This is an automatically generated file. Do not edit. */\n");
+
+ for (i = 1; i < argc; i++)
+ {
+ fi = fz_open_file(ctx, argv[i]);
+ cmap = pdf_load_cmap(ctx, fi);
+ fz_drop_stream(ctx, fi);
+
+ strcpy(name, cmap->cmap_name);
+ clean(name);
+
+ printf("\n/* %s */\n\n", cmap->cmap_name);
+
+ if (cmap->rlen)
+ {
+ printf("static const pdf_range cmap_%s_ranges[] = {", name);
+ for (k = 0; k < cmap->rlen; k++)
+ {
+ printf("\n{%u,%u,%u},", cmap->ranges[k].low, cmap->ranges[k].high, cmap->ranges[k].out);
+ }
+ printf("\n};\n\n");
+ }
+
+ if (cmap->xlen)
+ {
+ printf("static const pdf_xrange cmap_%s_xranges[] = {", name);
+ for (k = 0; k < cmap->xlen; k++)
+ {
+ printf("\n{%u,%u,%u},", cmap->xranges[k].low, cmap->xranges[k].high, cmap->xranges[k].out);
+ }
+ printf("\n};\n\n");
+ }
+
+ if (cmap->mlen > 0)
+ {
+ printf("static const pdf_mrange cmap_%s_mranges[] = {", name);
+ for (k = 0; k < cmap->mlen; k++)
+ {
+ printf("\n{%u,%u},", cmap->mranges[k].low, cmap->mranges[k].out);
+ }
+ printf("\n};\n\n");
+ }
+
+ if (cmap->dlen > 0)
+ {
+ int ti = 0, tn = cmap->dict[0];
+ printf("static const int cmap_%s_table[] = {\n", name);
+ for (k = 0; k < cmap->dlen; k++)
+ {
+ if (ti > tn)
+ {
+ printf("\n");
+ ti = 1;
+ tn = cmap->dict[k];
+ }
+ else
+ ++ti;
+ printf("%u,", cmap->dict[k]);
+ }
+ printf("\n};\n\n");
+ }
+
+ printf("static pdf_cmap cmap_%s = {\n", name);
+ printf("\t{ -1, pdf_drop_cmap_imp },\n");
+ printf("\t/* cmapname */ \"%s\",\n", cmap->cmap_name);
+ printf("\t/* usecmap */ \"%s\", NULL,\n", cmap->usecmap_name);
+ printf("\t/* wmode */ %u,\n", cmap->wmode);
+ printf("\t/* codespaces */ %u, {\n", cmap->codespace_len);
+ if (cmap->codespace_len == 0)
+ {
+ printf("\t\t{ 0, 0, 0 },\n");
+ }
+ for (k = 0; k < cmap->codespace_len; k++)
+ {
+ int n = cmap->codespace[k].n;
+ printf("\t\t{ %u, 0x%0*x, 0x%0*x },\n", n,
+ n*2, cmap->codespace[k].low,
+ n*2, cmap->codespace[k].high);
+ }
+ printf("\t},\n");
+
+ if (cmap->rlen)
+ printf("\t%u, %u, (pdf_range*)cmap_%s_ranges,\n", cmap->rlen, cmap->rlen, name);
+ else
+ printf("\t0, 0, NULL, /* ranges */\n");
+ if (cmap->xlen)
+ printf("\t%u, %u, (pdf_xrange*)cmap_%s_xranges,\n", cmap->xlen, cmap->xlen, name);
+ else
+ printf("\t0, 0, NULL, /* xranges */\n");
+ if (cmap->mlen)
+ printf("\t%u, %u, (pdf_mrange*)cmap_%s_mranges,\n", cmap->mlen, cmap->mlen, name);
+ else
+ printf("\t0, 0, NULL, /* mranges */\n");
+ if (cmap->dict)
+ printf("\t%u, %u, (int*)cmap_%s_table,\n", cmap->dlen, cmap->dlen, name);
+ else
+ printf("\t0, 0, NULL, /* table */\n");
+ printf("\t0, 0, 0, NULL /* splay tree */\n");
+ printf("};\n");
+
+ pdf_drop_cmap(ctx, cmap);
+ }
+
+ fz_drop_context(ctx);
+ return 0;
+}
diff --git a/source/tools/mutool.c b/source/tools/mutool.c
index c6b33330..892e7c6c 100644
--- a/source/tools/mutool.c
+++ b/source/tools/mutool.c
@@ -27,6 +27,8 @@ int pdfmerge_main(int argc, char *argv[]);
int pdfportfolio_main(int argc, char *argv[]);
int pdfsign_main(int argc, char *argv[]);
+int cmapdump_main(int argc, char *argv[]);
+
static struct {
int (*func)(int argc, char *argv[]);
char *name;
@@ -57,6 +59,9 @@ static struct {
#endif
#if FZ_ENABLE_PDF
{ pdfshow_main, "show", "show internal pdf objects" },
+#ifndef NDEBUG
+ { cmapdump_main, "cmapdump", "dump CMap resource as C source file" },
+#endif
#endif
};