# HG changeset patch # User Matti Hamalainen # Date 1544440617 -7200 # Node ID 2ca6a13b091b26184cc3cf0c34c326a17fd94944 # Parent 8cd0122609765bbc28a06ba2f89d61f7df5d2c44 Improve fontconv '-s' option parsing. diff -r 8cd012260976 -r 2ca6a13b091b tools/fontconv.c --- 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;