(svn r6462) -Codechange: Have GetStringWidth() return width as well as the height bounding
authorDarkvater
Sat, 16 Sep 2006 13:20:14 +0000
changeset 4609 6c337b3fbf4b
parent 4608 b5470981ed85
child 4610 45ac1d25fd62
(svn r6462) -Codechange: Have GetStringWidth() return width as well as the height bounding
box of the string. Therefore rename the function to GetStringBoundingRect()
and have it return a BoundingRect type of width/height
genworld_gui.c
gfx.c
gfx.h
misc_gui.c
openttd.h
players.c
texteff.c
town_cmd.c
viewport.c
--- a/genworld_gui.c	Sat Sep 16 13:02:35 2006 +0000
+++ b/genworld_gui.c	Sat Sep 16 13:20:14 2006 +0000
@@ -304,7 +304,7 @@
 
 			DrawString( 12,  91, STR_HEIGHTMAP_NAME, 0x10);
 			SetDParam(0, _heightmap_str);
-			DrawStringTruncated(114,  91, STR_ORANGE, 0x10, 326 - 114 - GetStringWidth(buffer) - 5);
+			DrawStringTruncated(114,  91, STR_ORANGE, 0x10, 326 - 114 - GetStringBoundingBox(buffer).width - 5);
 
 			/* TODO -- Remove next 2 lines if 32 widget limit is removed */
 			DrawFrameRect(114, 196, 219, 207, 12, 0);
--- a/gfx.c	Sat Sep 16 13:02:35 2006 +0000
+++ b/gfx.c	Sat Sep 16 13:20:14 2006 +0000
@@ -358,7 +358,7 @@
 	int w;
 
 	GetString(buffer, str);
-	w = GetStringWidth(buffer);
+	w = GetStringBoundingBox(buffer).width;
 	DoDrawString(buffer, x - w, y, color);
 
 	return w;
@@ -369,7 +369,7 @@
 	char buffer[512];
 
 	TruncateStringID(str, buffer, maxw);
-	DoDrawString(buffer, x - GetStringWidth(buffer), y, color);
+	DoDrawString(buffer, x - GetStringBoundingBox(buffer).width, y, color);
 }
 
 void DrawStringRightAlignedUnderline(int x, int y, StringID str, uint16 color)
@@ -386,7 +386,7 @@
 
 	GetString(buffer, str);
 
-	w = GetStringWidth(buffer);
+	w = GetStringBoundingBox(buffer).width;
 	DoDrawString(buffer, x - w / 2, y, color);
 
 	return w;
@@ -401,7 +401,7 @@
 
 int DoDrawStringCentered(int x, int y, const char *str, uint16 color)
 {
-	int w = GetStringWidth(str);
+	int w = GetStringBoundingBox(str).width;
 	DoDrawString(str, x - w / 2, y, color);
 	return w;
 }
@@ -478,7 +478,7 @@
 	src = buffer;
 
 	for (;;) {
-		w = GetStringWidth(src);
+		w = GetStringBoundingBox(src).width;
 		DoDrawString(src, x - (w>>1), y, 0xFE);
 		_cur_fontsize = _last_fontsize;
 
@@ -539,26 +539,47 @@
 	}
 }
 
-int GetStringWidth(const char *str)
+/** Return the string dimension in pixels. The height and width are returned
+ * in a single BoundingRect value. TINYFONT, BIGFONT modifiers are only
+ * supported as the first character of the string. The returned dimensions
+ * are therefore a rough estimation correct for all the current strings
+ * but not every possible combination
+ * @param str string to calculate pixel-width
+ * @return string width and height in pixels */
+BoundingRect GetStringBoundingBox(const char *str)
 {
 	FontSize size = _cur_fontsize;
-	int w, max_w;
+	BoundingRect br;
+	int max_width;
 	byte c;
 
-	w = max_w = 0;
+	br.width = br.height = max_width = 0;
 	for (c = *str; c != '\0'; c = *(++str)) {
 		if (c >= ASCII_LETTERSTART) {
-			w += GetCharacterWidth(size, c);
+			br.width += GetCharacterWidth(size, c);
 		} else {
-			if (c == ASCII_SETX) str++;
-			else if (c == ASCII_SETXY) str += 2;
-			else if (c == ASCII_TINYFONT) size = FS_SMALL;
-			else if (c == ASCII_BIGFONT) size = FS_LARGE;
-			else if (c == ASCII_NL && w > max_w) {max_w = w; w = 0;}
+			switch (c) {
+				case ASCII_SETX: br.width += (byte)*str++; break;
+				case ASCII_SETXY:
+					br.width += (byte)*str++;
+					br.height += (byte)*str++;
+					break;
+				case ASCII_TINYFONT: size = FS_SMALL; break;
+				case ASCII_BIGFONT:  size = FS_LARGE; break;
+				case ASCII_NL:
+					br.height += GetCharacterHeight(size);
+					if (br.width > max_width) {
+						max_width = br.width;
+						br.width = 0;
+					}
+					break;
+			}
 		}
 	}
+	br.height += GetCharacterHeight(size);
 
-	return max(w, max_w);
+	br.width  = max(br.width, max_width);
+	return br;
 }
 
 
--- a/gfx.h	Sat Sep 16 13:02:35 2006 +0000
+++ b/gfx.h	Sat Sep 16 13:20:14 2006 +0000
@@ -67,7 +67,7 @@
 void GfxFillRect(int left, int top, int right, int bottom, int color);
 void GfxDrawLine(int left, int top, int right, int bottom, int color);
 
-int GetStringWidth(const char *str);
+BoundingRect GetStringBoundingBox(const char *str);
 void LoadStringWidthTable(void);
 void DrawStringMultiCenter(int x, int y, StringID str, int maxw);
 void DrawStringMultiLine(int x, int y, StringID str, int maxw);
--- a/misc_gui.c	Sat Sep 16 13:02:35 2006 +0000
+++ b/misc_gui.c	Sat Sep 16 13:20:14 2006 +0000
@@ -666,7 +666,7 @@
 
 	GetString(buffer, string_id);
 
-	right = GetStringWidth(buffer) + 6;
+	right = GetStringBoundingBox(buffer).width + 6;
 
 	/* Cut tooltip length to 200 pixels max, wrap to new line if longer */
 	bottom = 14;
@@ -1776,7 +1776,7 @@
 				case STR_CHEAT_CHANGE_PLAYER:
 					SetDParam(0, val);
 					GetString(buf, STR_CHEAT_CHANGE_PLAYER);
-					DrawPlayerIcon(_current_player, 60 + GetStringWidth(buf), y + 2);
+					DrawPlayerIcon(_current_player, 60 + GetStringBoundingBox(buf).width, y + 2);
 					break;
 				/* Set correct string for switch climate cheat */
 				case STR_CHEAT_SWITCH_CLIMATE: val += STR_TEMPERATE_LANDSCAPE;
--- a/openttd.h	Sat Sep 16 13:02:35 2006 +0000
+++ b/openttd.h	Sat Sep 16 13:20:14 2006 +0000
@@ -12,6 +12,11 @@
 	int left,top,right,bottom;
 } Rect;
 
+typedef struct BoundingRect {
+	int width;
+	int height;
+} BoundingRect;
+
 typedef struct Point {
 	int x,y;
 } Point;
--- a/players.c	Sat Sep 16 13:02:35 2006 +0000
+++ b/players.c	Sat Sep 16 13:20:14 2006 +0000
@@ -344,7 +344,7 @@
 		}
 
 		GetString(buffer, str);
-		if (strlen(buffer) >= 32 || GetStringWidth(buffer) >= 150)
+		if (strlen(buffer) >= 32 || GetStringBoundingBox(buffer).width >= 150)
 			goto bad_town_name;
 
 set_name:;
@@ -451,7 +451,7 @@
 
 		SetDParam(0, p->president_name_2);
 		GetString(buffer, p->president_name_1);
-		if (strlen(buffer) >= 32 || GetStringWidth(buffer) >= 94)
+		if (strlen(buffer) >= 32 || GetStringBoundingBox(buffer).width >= 94)
 			continue;
 
 		FOR_ALL_PLAYERS(pp) {
--- a/texteff.c	Sat Sep 16 13:02:35 2006 +0000
+++ b/texteff.c	Sat Sep 16 13:20:14 2006 +0000
@@ -70,7 +70,7 @@
 
 	/* Cut the message till it fits inside the chatbox */
 	length = strlen(buf);
-	while (GetStringWidth(buf) > _textmessage_width - 9) buf[--length] = '\0';
+	while (GetStringBoundingBox(buf).width > _textmessage_width - 9) buf[--length] = '\0';
 
 	/* Find an empty spot and put the message there */
 	for (i = 0; i < MAX_CHAT_MESSAGES; i++) {
@@ -247,7 +247,7 @@
 	te->params_2 = GetDParam(4);
 
 	GetString(buffer, msg);
-	w = GetStringWidth(buffer);
+	w = GetStringBoundingBox(buffer).width;
 
 	te->x = x - (w >> 1);
 	te->right = x + (w >> 1) - 1;
--- a/town_cmd.c	Sat Sep 16 13:02:35 2006 +0000
+++ b/town_cmd.c	Sat Sep 16 13:20:14 2006 +0000
@@ -889,7 +889,7 @@
 		GetString(buf1, townnametype);
 
 		// Check size and width
-		if (strlen(buf1) >= 31 || GetStringWidth(buf1) > 130) continue;
+		if (strlen(buf1) >= 31 || GetStringBoundingBox(buf1).width > 130) continue;
 
 		FOR_ALL_TOWNS(t2) {
 			// We can't just compare the numbers since
--- a/viewport.c	Sat Sep 16 13:02:35 2006 +0000
+++ b/viewport.c	Sat Sep 16 13:20:14 2006 +0000
@@ -1018,13 +1018,13 @@
 	sign->top = top;
 
 	GetString(buffer, str);
-	w = GetStringWidth(buffer) + 3;
+	w = GetStringBoundingBox(buffer).width + 3;
 	sign->width_1 = w;
 	sign->left = left - w / 2;
 
-	// zoomed out version
+	/* zoomed out version */
 	_cur_fontsize = FS_SMALL;
-	w = GetStringWidth(buffer) + 3;
+	w = GetStringBoundingBox(buffer).width + 3;
 	_cur_fontsize = FS_NORMAL;
 	sign->width_2 = w;
 }