Skip to content

Instantly share code, notes, and snippets.

@ramSeraph
Last active July 29, 2022 18:18
Show Gist options
  • Save ramSeraph/5f595263c88d5cc8116d36da13d1e857 to your computer and use it in GitHub Desktop.
Save ramSeraph/5f595263c88d5cc8116d36da13d1e857 to your computer and use it in GitHub Desktop.
mutool draw patch

A patch for mutool draw from mupdf to pick up fonts from an external font directory for non embedded fonts

Based on version 1.20.0-rc1 of mupdf

build instructions

git clone https://github.com/ArtifexSoftware/mupdf
cd mupdf
git checkout 1.20.0-rc1
git submodule update --init
git apply ../mupdf.patch
make HAVE_X11=no HAVE_GLUT=no

the patched mutool will appear at build/release/mutool

Pick your own build flags as required.

diff --git a/source/tools/mudraw.c b/source/tools/mudraw.c
index 2af369bd8..467e88917 100644
--- a/source/tools/mudraw.c
+++ b/source/tools/mudraw.c
@@ -368,6 +368,7 @@ static int layer_off_len;
static const char ocr_language_default[] = "eng";
static const char *ocr_language = ocr_language_default;
static const char *ocr_datadir = NULL;
+static const char *fontdir = NULL;
static struct {
int active;
@@ -488,6 +489,7 @@ static int usage(void)
"\t-Y\tList individual layers to stderr\n"
"\t-z -\tHide individual layer\n"
"\t-Z -\tShow individual layer\n"
+ "\t-n -\tFolder to load additional fonts from\n"
"\n"
"\tpages\tcomma separated list of page numbers and ranges\n"
);
@@ -1909,6 +1911,34 @@ static void save_accelerator(fz_context *ctx, fz_document *doc, const char *fnam
fz_save_accelerator(ctx, doc, absname);
}
+fz_font *load_font_from_dir(fz_context *ctx, const char *name, int bold, int italic, int needs_exact_metrics) {
+ if (fontdir == NULL) {
+ return NULL;
+ }
+ char buf[500];
+ fz_font *font = NULL;
+ fz_try(ctx)
+ {
+ fz_snprintf(buf, sizeof buf, "%s/%s.ttf", fontdir, name);
+ if (!fz_file_exists(ctx, buf))
+ fz_snprintf(buf, sizeof buf, "%s/%s.TTF", fontdir, name);
+ if (!fz_file_exists(ctx, buf))
+ fz_snprintf(buf, sizeof buf, "%s/%s.otf", fontdir, name);
+ if (!fz_file_exists(ctx, buf))
+ fz_snprintf(buf, sizeof buf, "%s/%s.OTF", fontdir, name);
+ if (!fz_file_exists(ctx, buf))
+ fz_snprintf(buf, sizeof buf, "%s/%s.ttc", fontdir, name);
+ if (!fz_file_exists(ctx, buf))
+ fz_snprintf(buf, sizeof buf, "%s/%s.TTC", fontdir, name);
+ if (fz_file_exists(ctx, buf))
+ font = fz_new_font_from_file(ctx, NULL, buf, 0, 0);
+ }
+ fz_catch(ctx)
+ return NULL;
+ return font;
+
+}
+
#ifdef MUDRAW_STANDALONE
int main(int argc, char **argv)
#else
@@ -1927,7 +1957,7 @@ int mudraw_main(int argc, char **argv)
fz_var(doc);
- while ((c = fz_getopt(argc, argv, "qp:o:F:R:r:w:h:fB:c:e:G:Is:A:DiW:H:S:T:t:d:U:XLvPl:y:Yz:Z:NO:am:K")) != -1)
+ while ((c = fz_getopt(argc, argv, "qp:o:F:n:R:r:w:h:fB:c:e:G:Is:A:DiW:H:S:T:t:d:U:XLvPl:y:Yz:Z:NO:am:K")) != -1)
{
switch (c)
{
@@ -1939,6 +1969,7 @@ int mudraw_main(int argc, char **argv)
case 'o': output = fz_optarg; break;
case 'F': format = fz_optarg; break;
+ case 'n': fontdir = fz_optarg; break;
case 'R': rotation = fz_atof(fz_optarg); break;
case 'r': resolution = fz_atof(fz_optarg); res_specified = 1; break;
@@ -2082,6 +2113,10 @@ int mudraw_main(int argc, char **argv)
exit(1);
}
+ if (fontdir != NULL) {
+ fz_install_load_system_font_funcs(ctx, load_font_from_dir, NULL, NULL);
+ }
+
fz_try(ctx)
{
if (proof_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment