changeset 2075:2ca6a13b091b

Improve fontconv '-s' option parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 10 Dec 2018 13:16:57 +0200
parents 8cd012260976
children eeddaf411083
files tools/fontconv.c
diffstat 1 files changed, 34 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/tools/fontconv.c	Mon Dec 10 12:55:29 2018 +0200
+++ b/tools/fontconv.c	Mon Dec 10 13:16:57 2018 +0200
@@ -26,7 +26,7 @@
 {
     {  0, '?', "help",     "Show this help", OPT_NONE },
     {  1, 'v', "verbose",  "Be more verbose", OPT_NONE },
-    {  2, 's', "size",     "Set glyph dimensions (-s W:H) for image->font conversion", OPT_ARGREQ },
+    {  2, 's', "size",     "Set glyph dimensions (-s W:H or -s N) for image->font conversion", OPT_ARGREQ },
 #ifdef DM_GFX_TTF_TEXT
     {  3, 'c', "color",    "TTF font rendering color (def: 0xFFFFFF)", OPT_ARGREQ },
     {  4, 'b', "bpp",      "Render font in 8 or 32 bits per pixel (default 32)", OPT_ARGREQ },
@@ -60,23 +60,46 @@
 
         case 2:
             {
-                int w, h;
-                if (sscanf(optArg, "%d:%d", &w, &h) != 2)
+                unsigned int fontW, fontH;
+                char *sep = strchr(optArg, ':');
+                if (sep != NULL)
                 {
-                    dmErrorMsg("Invalid argument for -s option, '%s'.\n",
-                        optArg);
-                    return FALSE;
+                    char *tmpStr = dm_strndup(optArg, sep - optArg);
+
+                    if (!dmGetIntVal(tmpStr, &fontW, NULL) ||
+                        !dmGetIntVal(sep + 1, &fontH, NULL))
+                    {
+                        dmErrorMsg("Invalid font width or height value ('%s')\n",
+                            optArg);
+
+                        dmFree(tmpStr);
+                        return FALSE;
+                    }
+
+                    dmFree(tmpStr);
                 }
-                if (w < DMFONT_MIN_WIDTH  || w > DMFONT_MAX_WIDTH ||
-                    h < DMFONT_MIN_HEIGHT || h > DMFONT_MAX_HEIGHT)
+                else
                 {
-                    dmErrorMsg("Invalid dimensions, must be %d < W %d, %d < H < %d.\n",
+                    if (!dmGetIntVal(optArg, &fontW, NULL))
+                    {
+                        dmErrorMsg("Invalid font size value ('%s')\n",
+                            optArg);
+
+                        return FALSE;
+                    }
+                    fontH = fontW;
+                }
+
+                if (fontW < DMFONT_MIN_WIDTH  || fontW > DMFONT_MAX_WIDTH ||
+                    fontH < DMFONT_MIN_HEIGHT || fontH > DMFONT_MAX_HEIGHT)
+                {
+                    dmErrorMsg("Invalid font dimensions, must be %d < W %d, %d < H < %d.\n",
                         DMFONT_MIN_WIDTH  , DMFONT_MAX_WIDTH,
                         DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT);
                     return FALSE;
                 }
-                optSplitWidth = w;
-                optSplitHeight = h;
+                optSplitWidth = fontW;
+                optSplitHeight = fontH;
             }
             break;