Factor out function to get value with broadest digits

This commit is contained in:
Jonathan G Rennison
2023-06-01 20:34:48 +01:00
parent 18a79a35d0
commit a10c5a62c9
3 changed files with 20 additions and 8 deletions

View File

@@ -1528,6 +1528,24 @@ byte GetDigitWidth(FontSize size)
return width; return width;
} }
/**
* Return some number that is suitable for string size computations.
* @param count Number of digits which shall be displayable.
* @param size Font of the number
* @return The number.
*/
uint64 GetBroadestDigitsValue(uint count, FontSize size)
{
uint front = 0;
uint next = 0;
GetBroadestDigit(&front, &next, size);
uint64 val = count > 1 ? front : next;
for (; count > 1; count--) {
val = 10 * val + next;
}
return val;
}
/** /**
* Determine the broadest digits for guessing the maximum width of a n-digit number. * Determine the broadest digits for guessing the maximum width of a n-digit number.
* @param[out] front Broadest digit, which is not 0. (Use this digit as first digit for numbers with more than one digit.) * @param[out] front Broadest digit, which is not 0. (Use this digit as first digit for numbers with more than one digit.)

View File

@@ -220,6 +220,7 @@ bool ToggleFullScreen(bool fs);
byte GetCharacterWidth(FontSize size, WChar key); byte GetCharacterWidth(FontSize size, WChar key);
byte GetDigitWidth(FontSize size = FS_NORMAL); byte GetDigitWidth(FontSize size = FS_NORMAL);
void GetBroadestDigit(uint *front, uint *next, FontSize size = FS_NORMAL); void GetBroadestDigit(uint *front, uint *next, FontSize size = FS_NORMAL);
uint64 GetBroadestDigitsValue(uint count, FontSize size = FS_NORMAL);
extern int font_height_cache[FS_END]; extern int font_height_cache[FS_END];

View File

@@ -116,14 +116,7 @@ void SetDParamMaxValue(uint n, uint64 max_value, uint min_count, FontSize size)
*/ */
void SetDParamMaxDigits(uint n, uint count, FontSize size) void SetDParamMaxDigits(uint n, uint count, FontSize size)
{ {
uint front = 0; SetDParam(n, GetBroadestDigitsValue(count, size));
uint next = 0;
GetBroadestDigit(&front, &next, size);
uint64 val = count > 1 ? front : next;
for (; count > 1; count--) {
val = 10 * val + next;
}
SetDParam(n, val);
} }
/** /**