diff options
author | Daniel Friesel <derf@finalrewind.org> | 2011-10-03 11:27:19 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2011-10-03 11:27:19 +0200 |
commit | 5631e4ac406ebfb165613311bac559dab481ec12 (patch) | |
tree | c209cf1290038d29c254b4d2c529ba0ed18c443c /src | |
parent | 1409766b09b188a1139982a6cab06554c5a4ca62 (diff) |
Refactor --limit-width / --limit-height code
Diffstat (limited to 'src')
-rw-r--r-- | src/index.c | 187 | ||||
-rw-r--r-- | src/index.h | 2 | ||||
-rw-r--r-- | src/thumbnail.c | 104 |
3 files changed, 103 insertions, 190 deletions
diff --git a/src/index.c b/src/index.c index d9e068a..ba95642 100644 --- a/src/index.c +++ b/src/index.c @@ -133,116 +133,28 @@ void init_index_mode(void) info in the selected font, work out how much space we need, and calculate the size of the image we will require */ - if (opt.limit_w && opt.limit_h) { - int rec_h = 0; - + if (opt.limit_w) { w = opt.limit_w; - h = opt.limit_h; - - /* Work out if this is big enough, and give a warning if not */ - - /* Pretend we are limiting width by that specified, loop through, and - see it we fit in the height specified. If not, continue the loop, - and recommend the final value instead. Carry on and make the index - anyway. */ - - for (l = filelist; l; l = l->next) { - file = FEH_FILE(l->data); - text_area_w = opt.thumb_w; - if (opt.index_info) { - get_index_string_dim(file, fn, &fw, &fh); - if (fw > text_area_w) - text_area_w = fw; - if (fh > text_area_h) { - text_area_h = fh + 5; - tot_thumb_h = opt.thumb_h + text_area_h; - } - } - if (text_area_w > opt.thumb_w) - text_area_w += 5; - - if ((x > w - text_area_w)) { - x = 0; - y += tot_thumb_h; - } - x += text_area_w; - } - rec_h = y + tot_thumb_h; - - if (h < rec_h) { - weprintf( - "The image size you specified (%d by %d) is not large\n" - "enough to hold all the thumnails you specified (%d). To fit all\n" - "the thumnails, either decrease their size, choose a smaller font,\n" - "or use a larger image (may I recommend %d by %d?)", - opt.limit_w, opt.limit_h, filelist_len, opt.limit_w, rec_h); + index_calculate_height(fn, w, &h, &tot_thumb_h); + + if (opt.limit_h) { + if (h > opt.limit_h) + weprintf( + "The image size you specified (%dx%d) is not large\n" + "enough to hold all %d thumbnails. To fit all the thumbnails,\n" + "either decrease their size, choos e asmaller font,\n" + "or use a larger image (like %dx%d)", + opt.limit_w, opt.limit_h, filelist_len, w, h); + h = opt.limit_h; } } else if (opt.limit_h) { vertical = 1; h = opt.limit_h; - /* calc w */ - for (l = filelist; l; l = l->next) { - file = FEH_FILE(l->data); - text_area_w = opt.thumb_w; - /* Calc width of text */ - if (opt.index_info) { - get_index_string_dim(file, fn, &fw, &fh); - if (fw > text_area_w) - text_area_w = fw; - if (fh > text_area_h) { - text_area_h = fh + 5; - tot_thumb_h = opt.thumb_h + text_area_h; - } - } - if (text_area_w > opt.thumb_w) - text_area_w += 5; - - if (text_area_w > max_column_w) - max_column_w = text_area_w; - - if ((y > h - tot_thumb_h)) { - y = 0; - x += max_column_w; - max_column_w = 0; - } - - y += tot_thumb_h; - } - w = x + text_area_w; - max_column_w = 0; - } else if (opt.limit_w) { - w = opt.limit_w; - /* calc h */ - - for (l = filelist; l; l = l->next) { - file = FEH_FILE(l->data); - text_area_w = opt.thumb_w; - if (opt.index_info) { - get_index_string_dim(file, fn, &fw, &fh); - if (fw > text_area_w) - text_area_w = fw; - if (fh > text_area_h) { - text_area_h = fh + 5; - tot_thumb_h = opt.thumb_h + text_area_h; - } - } - - if (text_area_w > opt.thumb_w) - text_area_w += 5; - - if ((x > w - text_area_w)) { - x = 0; - y += tot_thumb_h; - } - x += text_area_w; - } - h = y + tot_thumb_h; + index_calculate_width(fn, &w, h, &tot_thumb_h); } - x = y = 0; - index_image_width = w; index_image_height = h + title_area_h; im_main = imlib_create_image(index_image_width, index_image_height); @@ -445,6 +357,79 @@ void init_index_mode(void) return; } +void index_calculate_height(Imlib_Font fn, int w, int *h, int *tot_thumb_h) +{ + gib_list *l; + feh_file *file = NULL; + int x = 0, y = 0; + int fw = 0, fh = 0; + int text_area_w = 0, text_area_h = 0; + + for (l = filelist; l; l = l->next) { + file = FEH_FILE(l->data); + text_area_w = opt.thumb_w; + if (opt.index_info) { + get_index_string_dim(file, fn, &fw, &fh); + if (fw > text_area_w) + text_area_w = fw; + if (fh > text_area_h) { + text_area_h = fh + 5; + *tot_thumb_h = opt.thumb_h + text_area_h; + } + } + + if (text_area_w > opt.thumb_w) + text_area_w += 5; + + if ((x > w - text_area_w)) { + x = 0; + y += *tot_thumb_h; + } + + x += text_area_w; + } + *h = y + *tot_thumb_h; +} + +void index_calculate_width(Imlib_Font fn, int *w, int h, int *tot_thumb_h) +{ + gib_list *l; + feh_file *file = NULL; + int x = 0, y = 0; + int fw = 0, fh = 0; + int text_area_w = 0, text_area_h = 0; + int max_column_w = 0; + + for (l = filelist; l; l = l->next) { + file = FEH_FILE(l->data); + text_area_w = opt.thumb_w; + /* Calc width of text */ + if (opt.index_info) { + get_index_string_dim(file, fn, &fw, &fh); + if (fw > text_area_w) + text_area_w = fw; + if (fh > text_area_h) { + text_area_h = fh + 5; + *tot_thumb_h = opt.thumb_h + text_area_h; + } + } + if (text_area_w > opt.thumb_w) + text_area_w += 5; + + if (text_area_w > max_column_w) + max_column_w = text_area_w; + + if ((y > h - *tot_thumb_h)) { + y = 0; + x += max_column_w; + max_column_w = 0; + } + + y += *tot_thumb_h; + } + *w = x + text_area_w; +} + void get_index_string_dim(feh_file *file, Imlib_Font fn, int *fw, int *fh) { int line_w, line_h; diff --git a/src/index.h b/src/index.h index 288b0ee..08ab337 100644 --- a/src/index.h +++ b/src/index.h @@ -29,5 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. char *create_index_string(feh_file *file); char *create_index_title_string(int num, int w, int h); void get_index_string_dim(feh_file *file, Imlib_Font fn, int *w, int *h); +void index_calculate_height(Imlib_Font fn, int w, int *h, int *tot_thumb_w); +void index_calculate_width(Imlib_Font fn, int *w, int h, int *tot_thumb_h); #endif diff --git a/src/thumbnail.c b/src/thumbnail.c index 42e2a82..4e32dfa 100644 --- a/src/thumbnail.c +++ b/src/thumbnail.c @@ -522,9 +522,9 @@ void feh_thumbnail_calculate_geometry(void) if (opt.verbose) fputs(PACKAGE " - No size restriction specified for index.\n" - " Using defaults (width limited to 640)\n", + " Using defaults (width limited to 800)\n", stdout); - opt.limit_w = 640; + opt.limit_w = 800; } } @@ -532,100 +532,26 @@ void feh_thumbnail_calculate_geometry(void) info in the selected font, work out how much space we need, and calculate the size of the image we will require */ - if (opt.limit_w && opt.limit_h) { - int rec_h = 0; - + if (opt.limit_w) { td.w = opt.limit_w; - td.h = opt.limit_h; - - /* Work out if this is big enough, and give a warning if not */ - /* Pretend we are limiting width by that specified, loop through, and - see it we fit in the height specified. If not, continue the loop, - and recommend the final value instead. Carry on and make the index - anyway. */ - - for (l = filelist; l; l = l->next) { - file = FEH_FILE(l->data); - td.text_area_w = opt.thumb_w; - if (opt.index_info) { - get_index_string_dim(file, td.font_main, &fw, &fh); - if (fw > td.text_area_w) - td.text_area_w = fw; - } - if (td.text_area_w > opt.thumb_w) - td.text_area_w += 5; - - if ((x > td.w - td.text_area_w)) { - x = 0; - y += td.thumb_tot_h; - } - - x += td.text_area_w; - } - rec_h = y + td.thumb_tot_h; - - if (td.h < rec_h) { - weprintf - ("The image size you specified (%d by %d) is not large\n" - "enough to hold all the thumnails you specified (%d). To fit all\n" - "the thumnails, either decrease their size, choose a smaller font,\n" - "or use a larger image (may I recommend %d by %d?)", - opt.limit_w, opt.limit_h, filelist_len, opt.limit_w, rec_h); + index_calculate_height(td.font_main, td.w, &td.h, &td.thumb_tot_h); + + if (opt.limit_h) { + if (td.h> opt.limit_h) + weprintf( + "The image size you specified (%dx%d) is not large\n" + "enough to hold all %d thumbnails. To fit all\n" + "the thumnails, either decrease their size, choose a smaller font,\n" + "or use a larger image (like %dx%d)", + opt.limit_w, opt.limit_h, filelist_len, opt.limit_w, td.h); + td.h = opt.limit_h; } } else if (opt.limit_h) { td.vertical = 1; td.h = opt.limit_h; - /* calc w */ - for (l = filelist; l; l = l->next) { - file = FEH_FILE(l->data); - td.text_area_w = opt.thumb_w; - /* Calc width of text */ - if (opt.index_info) { - get_index_string_dim(file, td.font_main, &fw, &fh); - if (fw > td.text_area_w) - td.text_area_w = fw; - } - if (td.text_area_w > opt.thumb_w) - td.text_area_w += 5; - - if (td.text_area_w > td.max_column_w) - td.max_column_w = td.text_area_w; - - if ((y > td.h - td.thumb_tot_h)) { - y = 0; - x += td.max_column_w; - td.max_column_w = 0; - } - - y += td.thumb_tot_h; - } - td.w = x + td.text_area_w; - td.max_column_w = 0; - } else if (opt.limit_w) { - td.w = opt.limit_w; - /* calc h */ - - for (l = filelist; l; l = l->next) { - file = FEH_FILE(l->data); - td.text_area_w = opt.thumb_w; - if (opt.index_info) { - get_index_string_dim(file, td.font_main, &fw, &fh); - if (fw > td.text_area_w) - td.text_area_w = fw; - } - if (td.text_area_w > opt.thumb_w) - td.text_area_w += 5; - - if ((x > td.w - td.text_area_w)) { - x = 0; - y += td.thumb_tot_h; - } - - x += td.text_area_w; - } - td.h = y + td.thumb_tot_h; + index_calculate_width(td.font_main, &td.w, td.h, &td.thumb_tot_h); } } |