summaryrefslogtreecommitdiff
path: root/src/index.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.c')
-rw-r--r--src/index.c423
1 files changed, 187 insertions, 236 deletions
diff --git a/src/index.c b/src/index.c
index 15dcea1..b0b6923 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1,6 +1,7 @@
/* index.c
Copyright (C) 1999-2003 Tom Gilbert.
+Copyright (C) 2010-2020 Birte Kristina Friesel.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
@@ -27,15 +28,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "filelist.h"
#include "winwidget.h"
#include "options.h"
+#include "index.h"
-static char *create_index_dimension_string(int w, int h);
-static char *create_index_size_string(char *file);
-static char *create_index_title_string(int num, int w, int h);
/* TODO Break this up a bit ;) */
/* TODO s/bit/lot */
void init_index_mode(void)
{
+ Imlib_Load_Error err;
Imlib_Image im_main;
Imlib_Image im_temp;
int w = 800, h = 600, ww = 0, hh = 0, www, hhh, xxx, yyy;
@@ -50,17 +50,16 @@ void init_index_mode(void)
Imlib_Font title_fn = NULL;
int text_area_w = 0;
int tw = 0, th = 0;
- int fw_name, fw_size, fw_dim, fw, fh;
+ int fw, fh;
int vertical = 0;
int max_column_w = 0;
int thumbnailcount = 0;
gib_list *l = NULL, *last = NULL;
feh_file *file = NULL;
- int lines;
+ int lineno;
unsigned char trans_bg = 0;
int index_image_width, index_image_height;
- int x_offset_name = 0, x_offset_dim = 0, x_offset_size = 0;
- char *s;
+ gib_list *line, *lines;
if (opt.montage) {
mode = "montage";
@@ -75,11 +74,11 @@ void init_index_mode(void)
fn = gib_imlib_load_font(DEFAULT_FONT);
if (opt.title_font) {
- int fh, fw;
title_fn = gib_imlib_load_font(opt.title_font);
- if (!fn)
+ if (!title_fn)
title_fn = gib_imlib_load_font(DEFAULT_FONT_TITLE);
+
gib_imlib_get_text_size(title_fn, "W", NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
title_area_h = fh + 4;
} else
@@ -90,8 +89,9 @@ void init_index_mode(void)
/* Work out how tall the font is */
gib_imlib_get_text_size(fn, "W", NULL, &tw, &th, IMLIB_TEXT_TO_RIGHT);
+ get_index_string_dim(NULL, fn, &fw, &fh);
/* For now, allow room for the right number of lines with small gaps */
- text_area_h = ((th + 2) * (opt.index_show_name + opt.index_show_size + opt.index_show_dim)) + 5;
+ text_area_h = fh + 5;
/* This includes the text area for index data */
tot_thumb_h = opt.thumb_h + text_area_h;
@@ -111,174 +111,52 @@ void init_index_mode(void)
if (!opt.limit_w && !opt.limit_h) {
if (bg_im) {
- if (opt.verbose)
- fprintf(stdout,
- PACKAGE
- " - No size restriction specified for index.\n"
- " You did specify a background however, so the\n"
- " index size has defaulted to the size of the image\n");
opt.limit_w = bg_w;
opt.limit_h = bg_h;
- } else {
- if (opt.verbose)
- fprintf(stdout,
- PACKAGE
- " - No size restriction specified for index.\n"
- " Using defaults (width limited to 800)\n");
+ } else
opt.limit_w = 800;
- }
}
/* Here we need to whiz through the files, and look at the filenames and
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_show_name) {
- gib_imlib_get_text_size(fn, file->name, NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw > text_area_w)
- text_area_w = fw;
- }
- if (opt.index_show_dim) {
- gib_imlib_get_text_size(fn,
- create_index_dimension_string
- (1000, 1000), NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw > text_area_w)
- text_area_w = fw;
- }
- if (opt.index_show_size) {
- gib_imlib_get_text_size(fn,
- create_index_size_string
- (file->filename), NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw > text_area_w)
- text_area_w = fw;
- }
- 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, choose a smaller 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_show_name) {
- gib_imlib_get_text_size(fn, file->name, NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw > text_area_w)
- text_area_w = fw;
- }
- if (opt.index_show_dim) {
- gib_imlib_get_text_size(fn,
- create_index_dimension_string
- (1000, 1000), NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw > text_area_w)
- text_area_w = fw;
- }
- if (opt.index_show_size) {
- gib_imlib_get_text_size(fn,
- create_index_size_string
- (file->filename), NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw > text_area_w)
- text_area_w = fw;
- }
- 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_show_name) {
- gib_imlib_get_text_size(fn, file->name, NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw > text_area_w)
- text_area_w = fw;
- }
- if (opt.index_show_dim) {
- gib_imlib_get_text_size(fn,
- create_index_dimension_string
- (1000, 1000), NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw > text_area_w)
- text_area_w = fw;
- }
- if (opt.index_show_size) {
- gib_imlib_get_text_size(fn,
- create_index_size_string
- (file->filename), NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw > text_area_w)
- text_area_w = fw;
- }
-
- 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);
- if (!im_main)
- eprintf("Imlib error creating index image, are you low on RAM?");
+ if (!im_main) {
+ if (index_image_height >= 32768 || index_image_width >= 32768) {
+ eprintf("Failed to create %dx%d pixels (%d MB) index image.\n"
+ "This is probably due to Imlib2 issues when dealing with images larger than 32k x 32k pixels.",
+ index_image_width, index_image_height, index_image_width * index_image_height * 4 / (1024*1024));
+ } else {
+ eprintf("Failed to create %dx%d pixels (%d MB) index image. Do you have enough RAM?",
+ index_image_width, index_image_height, index_image_width * index_image_height * 4 / (1024*1024));
+ }
+ }
if (bg_im)
gib_imlib_blend_image_onto_image(im_main, bg_im,
@@ -292,15 +170,9 @@ void init_index_mode(void)
gib_imlib_image_fill_rectangle(im_main, 0, 0, w, h + title_area_h, 0, 0, 0, 255);
}
- /* Create the window title at this point */
-
- if (!opt.title)
- s = estrdup(PACKAGE " [index mode]");
- else
- s = estrdup(feh_printf(opt.title, NULL));
-
if (opt.display) {
- winwid = winwidget_create_from_image(im_main, s, WIN_TYPE_SINGLE);
+ winwid = winwidget_create_from_image(im_main, WIN_TYPE_SINGLE);
+ winwidget_rename(winwid, PACKAGE " [index mode]");
winwidget_show(winwid);
}
@@ -354,33 +226,14 @@ void init_index_mode(void)
text_area_w = opt.thumb_w;
/* Now draw on the info text */
- if (opt.index_show_name) {
- gib_imlib_get_text_size(fn, file->name, NULL, &fw_name, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw_name > text_area_w)
- text_area_w = fw_name;
- }
- if (opt.index_show_dim) {
- gib_imlib_get_text_size(fn,
- create_index_dimension_string
- (ww, hh), NULL, &fw_dim, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw_dim > text_area_w)
- text_area_w = fw_dim;
- }
- if (opt.index_show_size) {
- gib_imlib_get_text_size(fn,
- create_index_size_string
- (file->filename), NULL, &fw_size, &fh, IMLIB_TEXT_TO_RIGHT);
- if (fw_size > text_area_w)
- text_area_w = fw_size;
+ if (opt.index_info) {
+ get_index_string_dim(file, fn, &fw, &fh);
+ if (fw > text_area_w)
+ text_area_w = fw;
}
if (text_area_w > opt.thumb_w)
text_area_w += 5;
- /* offsets for centering text */
- x_offset_name = (text_area_w - fw_name) / 2;
- x_offset_dim = (text_area_w - fw_dim) / 2;
- x_offset_size = (text_area_w - fw_size) / 2;
-
if (vertical) {
if (text_area_w > max_column_w)
max_column_w = text_area_w;
@@ -416,29 +269,23 @@ void init_index_mode(void)
gib_imlib_free_image_and_decache(im_thumb);
- lines = 0;
- if (opt.index_show_name)
- gib_imlib_text_draw(im_main, fn, NULL,
- x + x_offset_name,
- y + opt.thumb_h +
- (lines++ * (th + 2)) +
- 2, file->name, IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255);
- if (opt.index_show_dim)
- gib_imlib_text_draw(im_main, fn, NULL,
- x + x_offset_dim,
- y + opt.thumb_h +
- (lines++ * (th + 2)) +
- 2,
- create_index_dimension_string
- (ww, hh), IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255);
- if (opt.index_show_size)
- gib_imlib_text_draw(im_main, fn, NULL,
- x + x_offset_size,
- y + opt.thumb_h +
- (lines++ * (th + 2)) +
- 2,
- create_index_size_string
- (file->filename), IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255);
+ lineno = 0;
+ if (opt.index_info) {
+ line = lines = feh_wrap_string(create_index_string(file),
+ opt.thumb_w * 3, fn, NULL);
+
+ while (line) {
+ gib_imlib_get_text_size(fn, (char *) line->data,
+ NULL, &fw, &fh, IMLIB_TEXT_TO_RIGHT);
+ gib_imlib_text_draw(im_main, fn, NULL,
+ x + ((text_area_w - fw) >> 1),
+ y + opt.thumb_h + (lineno++ * (th + 2)) + 2,
+ (char *) line->data,
+ IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255);
+ line = line->next;
+ }
+ gib_list_free_and_data(lines);
+ }
if (vertical)
y += tot_thumb_h;
@@ -457,7 +304,7 @@ void init_index_mode(void)
}
}
if (opt.verbose)
- fprintf(stdout, "\n");
+ putc('\n', stderr);
if (opt.title_font) {
int fw, fh, fx, fy;
@@ -468,6 +315,9 @@ void init_index_mode(void)
fx = (index_image_width - fw) >> 1;
fy = index_image_height - fh - 2;
gib_imlib_text_draw(im_main, title_fn, NULL, fx, fy, s, IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255);
+
+ if (opt.display)
+ winwidget_render_image(winwid, 0, 0);
}
if (opt.output && opt.output_file) {
@@ -475,17 +325,22 @@ void init_index_mode(void)
if (opt.output_dir)
snprintf(output_buf, 1024, "%s/%s", opt.output_dir, opt.output_file);
- else
- strncpy(output_buf, opt.output_file, 1024);
+ else {
+ strncpy(output_buf, opt.output_file, 1023);
+ output_buf[1023] = '\0';
+ }
- gib_imlib_save_image(im_main, output_buf);
- if (opt.verbose) {
+ gib_imlib_save_image_with_error_return(im_main, output_buf, &err);
+ if (err) {
+ feh_print_load_error(output_buf, im_main, err, LOAD_ERROR_IMLIB);
+ }
+ else if (opt.verbose) {
int tw, th;
tw = gib_imlib_image_get_width(im_main);
th = gib_imlib_image_get_height(im_main);
- fprintf(stdout, PACKAGE " - File saved as %s\n", output_buf);
- fprintf(stdout,
+ fprintf(stderr, PACKAGE " - File saved as %s\n", output_buf);
+ fprintf(stderr,
" - Image is %dx%d pixels and contains %d thumbnails\n", tw, th, thumbnailcount);
}
}
@@ -493,37 +348,133 @@ void init_index_mode(void)
if (!opt.display)
gib_imlib_free_image_and_decache(im_main);
- free(s);
return;
}
-static char *create_index_size_string(char *file)
+void index_calculate_height(Imlib_Font fn, int w, int *h, int *tot_thumb_h)
{
- static char str[50];
- int size = 0;
- double kbs = 0.0;
- struct stat st;
-
- if (stat(file, &st))
- kbs = 0.0;
- else {
- size = st.st_size;
- kbs = (double) size / 1000;
+ 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;
+}
- snprintf(str, sizeof(str), "%.2fKb", kbs);
- return(str);
+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;
}
-static char *create_index_dimension_string(int w, int h)
+void get_index_string_dim(feh_file *file, Imlib_Font fn, int *fw, int *fh)
{
- static char str[50];
+ int line_w, line_h;
+ char fake_file = 0;
+ gib_list *line, *lines;
+ int max_w = 0, total_h = 0;
+
+ if (!opt.index_info) {
+ *fw = 0;
+ *fh = 0;
+ return;
+ }
- snprintf(str, sizeof(str), "%dx%d", w, h);
- return(str);
+ /* called with file = NULL in the setup phase.
+ * We need a fake file, otherwise feh_printf will remove format specifiers,
+ * leading e.g. to a 0x0 report for index_dim = "%n".
+ */
+ if (file == NULL) {
+ fake_file = 1;
+ file = feh_file_new("foo");
+ file->info = feh_file_info_new();
+ }
+
+ line = lines = feh_wrap_string(create_index_string(file), opt.thumb_w * 3, fn, NULL);
+
+ while (line) {
+ gib_imlib_get_text_size(fn, (char *) line->data,
+ NULL, &line_w, &line_h, IMLIB_TEXT_TO_RIGHT);
+
+ if (line_w > max_w)
+ max_w = line_w;
+ total_h += line_h + 2;
+
+ line = line->next;
+ }
+
+ gib_list_free_and_data(lines);
+ if (fake_file)
+ feh_file_free(file);
+
+ *fw = max_w;
+ *fh = total_h;
+ return;
+}
+
+char *create_index_string(feh_file * file)
+{
+ return feh_printf(opt.index_info, file, NULL);
}
-static char *create_index_title_string(int num, int w, int h)
+char *create_index_title_string(int num, int w, int h)
{
static char str[50];