comparison tools/fontconv.c @ 2075:2ca6a13b091b

Improve fontconv '-s' option parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 10 Dec 2018 13:16:57 +0200
parents a8a942c25df3
children e3f0eaf23f4f
comparison
equal deleted inserted replaced
2074:8cd012260976 2075:2ca6a13b091b
24 24
25 static const DMOptArg optList[] = 25 static const DMOptArg optList[] =
26 { 26 {
27 { 0, '?', "help", "Show this help", OPT_NONE }, 27 { 0, '?', "help", "Show this help", OPT_NONE },
28 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, 28 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
29 { 2, 's', "size", "Set glyph dimensions (-s W:H) for image->font conversion", OPT_ARGREQ }, 29 { 2, 's', "size", "Set glyph dimensions (-s W:H or -s N) for image->font conversion", OPT_ARGREQ },
30 #ifdef DM_GFX_TTF_TEXT 30 #ifdef DM_GFX_TTF_TEXT
31 { 3, 'c', "color", "TTF font rendering color (def: 0xFFFFFF)", OPT_ARGREQ }, 31 { 3, 'c', "color", "TTF font rendering color (def: 0xFFFFFF)", OPT_ARGREQ },
32 { 4, 'b', "bpp", "Render font in 8 or 32 bits per pixel (default 32)", OPT_ARGREQ }, 32 { 4, 'b', "bpp", "Render font in 8 or 32 bits per pixel (default 32)", OPT_ARGREQ },
33 #endif 33 #endif
34 }; 34 };
58 dmVerbosity++; 58 dmVerbosity++;
59 break; 59 break;
60 60
61 case 2: 61 case 2:
62 { 62 {
63 int w, h; 63 unsigned int fontW, fontH;
64 if (sscanf(optArg, "%d:%d", &w, &h) != 2) 64 char *sep = strchr(optArg, ':');
65 if (sep != NULL)
65 { 66 {
66 dmErrorMsg("Invalid argument for -s option, '%s'.\n", 67 char *tmpStr = dm_strndup(optArg, sep - optArg);
67 optArg); 68
68 return FALSE; 69 if (!dmGetIntVal(tmpStr, &fontW, NULL) ||
70 !dmGetIntVal(sep + 1, &fontH, NULL))
71 {
72 dmErrorMsg("Invalid font width or height value ('%s')\n",
73 optArg);
74
75 dmFree(tmpStr);
76 return FALSE;
77 }
78
79 dmFree(tmpStr);
69 } 80 }
70 if (w < DMFONT_MIN_WIDTH || w > DMFONT_MAX_WIDTH || 81 else
71 h < DMFONT_MIN_HEIGHT || h > DMFONT_MAX_HEIGHT)
72 { 82 {
73 dmErrorMsg("Invalid dimensions, must be %d < W %d, %d < H < %d.\n", 83 if (!dmGetIntVal(optArg, &fontW, NULL))
84 {
85 dmErrorMsg("Invalid font size value ('%s')\n",
86 optArg);
87
88 return FALSE;
89 }
90 fontH = fontW;
91 }
92
93 if (fontW < DMFONT_MIN_WIDTH || fontW > DMFONT_MAX_WIDTH ||
94 fontH < DMFONT_MIN_HEIGHT || fontH > DMFONT_MAX_HEIGHT)
95 {
96 dmErrorMsg("Invalid font dimensions, must be %d < W %d, %d < H < %d.\n",
74 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH, 97 DMFONT_MIN_WIDTH , DMFONT_MAX_WIDTH,
75 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT); 98 DMFONT_MIN_HEIGHT , DMFONT_MAX_HEIGHT);
76 return FALSE; 99 return FALSE;
77 } 100 }
78 optSplitWidth = w; 101 optSplitWidth = fontW;
79 optSplitHeight = h; 102 optSplitHeight = fontH;
80 } 103 }
81 break; 104 break;
82 105
83 case 3: 106 case 3:
84 { 107 {