From e5ecdfae981ca0b603892deffa9ef2892c5d1d9c Mon Sep 17 00:00:00 2001 From: Morten Fangel Date: Thu, 15 Dec 2016 15:59:23 +0100 Subject: Made number of iterations configurable with a flag Introduced a new -n flag where you can set the number of iterations to run. More iterations = longer running time, but smaller output --- sfnt2woff.c | 12 ++++++++++-- woff.c | 9 +++++++-- woff.h | 3 ++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/sfnt2woff.c b/sfnt2woff.c index 2faae4a..943f2b1 100644 --- a/sfnt2woff.c +++ b/sfnt2woff.c @@ -65,6 +65,7 @@ usage(const char * progName) "Options:\n" " -v . set font version number (major and minor, both integers)\n" " -m include metadata from (not validated)\n" + " -n number of zopfli iterations (default = 15)\n" " -p include private data block\n" , progName); } @@ -102,10 +103,11 @@ main(int argc, char * argv[]) const char * metadataFile = NULL; const char * privateFile = NULL; unsigned int maj = 0, min = 0; + int numiterations = 0; uint32_t status = eWOFF_ok; int opt; - while ((opt = getopt(argc, argv, "v:m:p:h")) != -1) { + while ((opt = getopt(argc, argv, "v:m:p:n:h")) != -1) { switch (opt) { case 'v': if (sscanf(optarg, "%u.%u", &maj, &min) < 2 || maj > 0xffff || min > 0xffff) { @@ -119,6 +121,12 @@ main(int argc, char * argv[]) case 'p': privateFile = optarg; break; + case 'n': + if (sscanf(optarg, "%u", &numiterations) < 1 || !numiterations || numiterations > 5000) { + fprintf(stderr, "# invalid number of iterations - please specify number between 0 and 5000\n"); + numiterations = 0; + } + break; case 'h': case '?': usage(progName); @@ -140,7 +148,7 @@ main(int argc, char * argv[]) const uint8_t * sfntData = readFile(argv[0], &sfntLen); uint32_t woffLen; - const uint8_t * woffData = woffEncode(sfntData, sfntLen, maj, min, &woffLen, &status); + const uint8_t * woffData = woffEncode(sfntData, sfntLen, maj, min, numiterations, &woffLen, &status); free((void *)sfntData); if (WOFF_FAILURE(status)) { reportErr(status); diff --git a/woff.c b/woff.c index 03a0baa..c0f1192 100644 --- a/woff.c +++ b/woff.c @@ -121,7 +121,8 @@ calcChecksum(const sfntDirEntry * dirEntry, const uint8_t * woffEncode(const uint8_t * sfntData, uint32_t sfntLen, uint16_t majorVersion, uint16_t minorVersion, - uint32_t * woffLen, uint32_t * pStatus) + int32_t numiterations, uint32_t * woffLen, + uint32_t * pStatus) { uint8_t * woffData = NULL; tableOrderRec * tableOrder = NULL; @@ -255,6 +256,9 @@ woffEncode(const uint8_t * sfntData, uint32_t sfntLen, ZopfliOptions options; ZopfliInitOptions(&options); + if (numiterations) { + options.numiterations = numiterations; + } for (order = 0; order < numTables; ++order) { uLong sourceLen, destLen = 0; @@ -368,7 +372,8 @@ woffEncode(const uint8_t * sfntData, uint32_t sfntLen, free(woffData); woffData = (uint8_t *) woffEncode(cleanSfnt, sfntLen, majorVersion, minorVersion, - &tableOffset, &status); + numiterations, &tableOffset, + &status); free((void *) cleanSfnt); if (WOFF_FAILURE(status)) { FAIL(status); diff --git a/woff.h b/woff.h index d8c6f55..e792526 100644 --- a/woff.h +++ b/woff.h @@ -107,7 +107,8 @@ extern "C" { */ const uint8_t * woffEncode(const uint8_t * sfntData, uint32_t sfntLen, uint16_t majorVersion, uint16_t minorVersion, - uint32_t * woffLen, uint32_t * status); + int32_t numiterations, uint32_t * woffLen, + uint32_t * status); /***************************************************************************** -- cgit v1.2.3