changeset 100:55f7140efa2f

Support for generating shellscript for using imagemagick to add labels into image format maps.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 17 Dec 2006 16:49:20 +0000
parents 140dda7f853d
children 8952704f0966
files mkloc.c
diffstat 1 files changed, 155 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/mkloc.c	Sun Dec 17 16:47:33 2006 +0000
+++ b/mkloc.c	Sun Dec 17 16:49:20 2006 +0000
@@ -46,6 +46,13 @@
 } fileinfo_t;
 
 
+enum {
+	OUTFMT_MAP = 0,
+	OUTFMT_LOCFILE,
+	OUTFMT_SCRIPT
+};
+
+
 /* Variables
  */
 char	*srcFile = NULL,
@@ -54,21 +61,26 @@
 
 int	optOffsetX = 0,
 	optOffsetY = 0;
-BOOL	optGetLoc = FALSE;
+BOOL	optGetUpdateLoc = FALSE,
+	optOutput = OUTFMT_MAP;
+float	optScale = -1;
 
 
 /* Arguments
  */
 optarg_t optList[] = {
-	{ 0, '?', "help",	"Show this help", 0 },
-	{ 2, 'v', "verbose",	"Be more verbose", 0 },
-	{ 3, 'q', "quiet",	"Be quiet", 0 },
-	{ 1, 'o', "output",	"Output file (default stdout)", 1 },
-	{ 5, 'm', "map",	"Input map file", 1 },
-	{ 6, 'l', "locinfo",	"Input location info file", 1 },
-	{ 4, 'g', "getloc",	"Generate/update location info", 0 },
-	{ 7, 'x', "offset-x",	"Location X offset", 1 },
-	{ 8, 'y', "offset-y",	"Location Y offset", 1 },
+	{ 0, '?', "help",	"Show this help", OPT_NONE },
+	{ 2, 'v', "verbose",	"Be more verbose", OPT_NONE },
+	{ 3, 'q', "quiet",	"Be quiet", OPT_NONE },
+	{ 1, 'o', "output",	"Output file (default stdout)", OPT_ARGREQ },
+	{ 5, 'm', "map",	"Input map file", OPT_ARGREQ },
+	{ 6, 'l', "locinfo",	"Input location info file", OPT_ARGREQ },
+	{ 4, 'g', "getloc",	"Generate/update location info", OPT_NONE },
+	{ 7, 'x', "offset-x",	"Location X offset", OPT_ARGREQ },
+	{ 8, 'y', "offset-y",	"Location Y offset", OPT_ARGREQ },
+	{ 9, 's', "scale",	"Scale coordinates by", OPT_ARGREQ },
+	{ 10,'S', "out-script",	"Output script for ImageMagick", OPT_NONE },
+	{ 11,'L', "out-locinfo","Output location info file", OPT_NONE },
 };
 
 const int optListN = (sizeof(optList) / sizeof(optarg_t));
@@ -113,21 +125,37 @@
 		break;
 	
 	case 4:
-		THMSG(1, "Location generation mode\n");
-		optGetLoc = TRUE;
+		THMSG(1, "Location updating/generation mode\n");
+		optGetUpdateLoc = TRUE;
 		break;
 
 	case 7:
 		optOffsetX = atoi(optArg);
+		THMSG(1, "Location X offset = %d\n", optOffsetX);
 		break;
 
 	case 8:
 		optOffsetY = atoi(optArg);
-		THMSG(1, "Location Y offset = %d\n", optOffsetX);
+		THMSG(1, "Location Y offset = %d\n", optOffsetY);
+		break;
+
+	case 9:
+		optScale = atof(optArg);
+		THMSG(1, "Location scale factor = %1.3f\n", optScale);
+		break;
+
+	case 10:
+		THMSG(1, "Script output mode\n");
+		optOutput = OUTFMT_SCRIPT;
+		break;
+
+	case 11:
+		THMSG(1, "Location file output mode\n");
+		optOutput = OUTFMT_LOCFILE;
 		break;
 
 	default:
-		THERR("Unknown argument '%s'.\n", currArg);
+		THERR("Unknown option '%s'.\n", currArg);
 		return FALSE;
 	}
 	
@@ -494,11 +522,26 @@
 }
 
 
+void printString(FILE *f, char *s)
+{
+	char *p = s;
+	
+	while (*p) {
+		switch (*p) {
+		case '\'': fputs("\\'", f); break;
+		case '"': fputs("\\\"", f); break;
+		default: fputc(*p, f);
+		}
+		p++;
+	}
+}
+
+
 int main(int argc, char *argv[])
 {
 	FILE *outFile;
 	mapblock_t *worldMap = NULL;
-	locations_t worldLocations;
+	locations_t worldLoc;
 
 	/* Initialize */
 	th_init("mkloc", "Add locations to ASCII map", "0.1", NULL, NULL);
@@ -508,7 +551,7 @@
 	th_args_process(argc, argv, optList, optListN,
 		argHandleOpt, NULL, FALSE);
 	
-	if (srcFile == NULL || (locFile == NULL && !optGetLoc)) {
+	if (srcFile == NULL || (locFile == NULL && !optGetUpdateLoc)) {
 		THERR("Nothing to do. (try --help)\n");
 		exit(0);
 	}
@@ -526,14 +569,14 @@
 	/* Read location info */
 	if (locFile) {
 		THMSG(2, "Reading location info '%s'\n", locFile);
-		if (!parseLocationFile(locFile, &worldLocations, optOffsetX, optOffsetY)) {
+		if (!parseLocationFile(locFile, &worldLoc, optOffsetX, optOffsetY)) {
 			THERR("Error reading location file!\n");
 			exit(1);
 		}
 	}
 	
 	/* What mode are we in? */
-	if (optGetLoc) {
+	if (optGetUpdateLoc) {
 		int x, y, n, numNewLoc = 0, numInvLoc = 0;
 		char *d = worldMap->d;
 		
@@ -544,7 +587,7 @@
 		for (x = 0; x < worldMap->w; x++) {
 			if (*d == '?' || *d == '%') {
 				/* Check for new locations */
-				if (searchLocation(&worldLocations, x, y, TRUE) < 0) {
+				if (searchLocation(&worldLoc, x, y, TRUE) < 0) {
 					char tmps[512];
 					int tmpf;
 					
@@ -557,13 +600,13 @@
 					else
 						tmpf = LOCF_NONE;
 					
-					addLocation(&worldLocations,
+					addLocation(&worldLoc,
 						x, y, DIR_NONE, tmps, tmpf);
 				}
 			} else {
 				/* Check for misplaced locations */
-				if ((n = searchLocation(&worldLocations, x, y, TRUE)) >= 0) {
-					locinfo_t *tloc = worldLocations.locations[n];
+				if ((n = searchLocation(&worldLoc, x, y, TRUE)) >= 0) {
+					locinfo_t *tloc = worldLoc.locations[n];
 					
 					if ((tloc->flags & LOCF_MARKMASK) == 0) {
 						/* Mark as possibly invalid */
@@ -579,6 +622,20 @@
 			numNewLoc, numInvLoc);
 	}
 	
+	/* Scale locations */
+	if (optScale >= 0) {
+		int i;
+		
+		THMSG(1, "Scaling locations ..\n");
+		
+		for (i = 0; i < worldLoc.numLocations; i++) {
+			locinfo_t *tmp = worldLoc.locations[i];
+			
+			tmp->x = ((float) tmp->x) * optScale;
+			tmp->y = ((float) tmp->y) * optScale;
+		}
+	}
+	
 	/* Open output file */
 	if (destFile == NULL)
 		outFile = stdout;
@@ -589,22 +646,93 @@
 	}
 
 	/* Output results */
-	if (optGetLoc) {
+	if (optOutput == OUTFMT_SCRIPT) {
+		int i, unitSize = 5;
+		
+		THMSG(1, "Generating ImageMagick script ...\n");
+		
+		fprintf(outFile,
+		"#!/bin/sh\n"
+		"convert \"$1\" -font /usr/share/fonts/arial.ttf -pointsize %d\\\n",
+		(int) (5.0f * optScale));
+		
+		for (i = 0; i < worldLoc.numLocations; i++) {
+			locinfo_t *tmp = worldLoc.locations[i];
+			int x, y;
+			
+			y = tmp->y;
+			switch (tmp->dir) {
+			case DIR_LEFTDOWN:
+				y = tmp->y + unitSize;
+			case DIR_LEFT:
+				x = tmp->x - strlen(tmp->desc) * unitSize;
+				break;
+
+			case DIR_DOWN:
+				y = tmp->y + unitSize;
+			case DIR_NONE:
+			default:
+				x = tmp->x + unitSize;
+				break;
+			}
+			
+			fprintf(outFile,
+			"\t-fill black -draw \"circle %d,%d %d,%d\" ",
+			tmp->x, tmp->y, tmp->x + unitSize/2, tmp->y + unitSize/2);
+			
+
+			fprintf(outFile,
+			"-fill red -draw \"circle %d,%d %d,%d\" ",
+			tmp->x, tmp->y, tmp->x + unitSize/2-1, tmp->y + unitSize/2-1);
+
+#if 0
+			fprintf(outFile,
+			"-fill black -draw \"text %d,%d '", x-1, y-1);
+			printString(outFile, tmp->desc);
+			fprintf(outFile, "'\" ");
+
+			fprintf(outFile,
+			"-fill black -draw \"text %d,%d '", x+1, y+1);
+			printString(outFile, tmp->desc);
+			fprintf(outFile, "'\" ");
+
+			fprintf(outFile,
+			"-fill black -draw \"text %d,%d '", x-1, y+1);
+			printString(outFile, tmp->desc);
+			fprintf(outFile, "'\" ");
+
+			fprintf(outFile,
+			"-fill black -draw \"text %d,%d '", x+1, y-1);
+			printString(outFile, tmp->desc);
+			fprintf(outFile, "'\" ");
+
+			fprintf(outFile,
+			"-fill white -draw \"text %d,%d '", x, y);
+			printString(outFile, tmp->desc);
+			fprintf(outFile, "'\" ");
+#endif
+
+			fprintf(outFile, " \\\n");
+		}
+		
+		fprintf(outFile, "\t\"$2\"\n");
+	} else
+	if (optOutput == OUTFMT_LOCFILE) {
 		/* Output location list */
 		THMSG(1, "Outputting generated location list ...\n");
 
-		outputLocationFile(outFile, &worldLocations);
+		outputLocationFile(outFile, &worldLoc);
 		
-		THMSG(2, "%d locations\n", worldLocations.numLocations);
+		THMSG(2, "%d locations\n", worldLoc.numLocations);
 	} else {
 		/* Output map in selected format */
 		THMSG(1, "Outputting generated map of (%d x %d) ...\n",
 			worldMap->w, worldMap->h);
 
 		THMSG(2, "Adjusting location labels ..\n");
-		adjustLocInfoCoords(worldMap, &worldLocations);
+		adjustLocInfoCoords(worldMap, &worldLoc);
 		
-		outputMapCTRL(outFile, worldMap, &worldLocations);
+		outputMapCTRL(outFile, worldMap, &worldLoc);
 	}
 	
 	fclose(outFile);