diff options
Diffstat (limited to 'navit/util.c')
| -rw-r--r-- | navit/util.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/navit/util.c b/navit/util.c index 9398e316c..944dc2a21 100644 --- a/navit/util.c +++ b/navit/util.c @@ -529,6 +529,70 @@ char * newSysString(const char *toconvert) { #endif #endif +/** + * @brief Optimizes the format of a string, adding carriage returns so that when displayed, the result text zone is roughly as wide as high + * + * @param[in,out] s The string to proces (will be modified by this function, but length will be unchanged) + */ +void square_shape_str(char *s) { + char *c; + char *last_break; + unsigned int max_cols = 0; + unsigned int cur_cols = 0; + unsigned int max_rows = 0; + unsigned int surface; + unsigned int target_cols; + + if (!s) + return; + for (c=s; *c!='\0'; c++) { + if (*c==' ') { + if (max_cols < cur_cols) + max_cols = cur_cols; + cur_cols = 0; + max_rows++; + } else + cur_cols++; + } + if (max_cols < cur_cols) + max_cols = cur_cols; + if (cur_cols) /* If last line does not end with CR, add it to line numbers anyway */ + max_rows++; + /* Give twice more room for rows (hence the factor 2 below) + * This will render as a rectangular shape, taking more horizontal space than vertical */ + surface = max_rows * 2 * max_cols; + target_cols = uint_sqrt(surface); + + if (target_cols < max_cols) + target_cols = max_cols; + + target_cols = target_cols + target_cols/10; /* Allow 10% extra on columns */ + dbg(lvl_debug, "square_shape_str(): analyzing input text=\"%s\". max_rows=%u, max_cols=%u, surface=%u, target_cols=%u", + s, max_rows, max_cols, max_rows * 2 * max_cols, target_cols); + + cur_cols = 0; + last_break = NULL; + for (c=s; *c!='\0'; c++) { + if (*c==' ') { + if (cur_cols>=target_cols) { /* This line is too long, break at the previous non alnum character */ + if (last_break) { + *last_break = + '\n'; /* Replace the previous non alnum character with a line break, this creates a new line and prevents the previous line from being too long */ + cur_cols = c-last_break; + } + } + last_break = c; /* Record this position as a candidate to insert a line break */ + } + cur_cols++; + } + if (cur_cols>=target_cols && last_break) { + *last_break = + '\n'; /* Replace the previous non alnum character with a line break, this creates a new line and prevents the previous line from being too long */ + } + + dbg(lvl_debug, "square_shape_str(): output text=\"%s\"", s); +} + #if defined(_MSC_VER) || (!defined(HAVE_GETTIMEOFDAY) && defined(HAVE_API_WIN32_BASE)) /** * Impements a simple incomplete version of gettimeofday. Only usefull for messuring |
