From 75bb13e531654a561dc7baa2f2bc594aa4a8fc52 Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Sun, 29 May 2011 19:00:44 +0200 Subject: Introduce new theming system This extends current templating system to another dimension, bringing the possibility to have multiple themes generated at once. The purpose is to be able to switch between multiple designs, from PC to mobile, classic vs. flat view, slideshow, etc. For the moment, only the classic theme is available. Rules: * for switching between themes, place a link inside your template manually - it's not a cgg concern, only your theming infrastructure * don't forget to define different file names for index and album pages across different themes * it's recommended to keep default theme named as "index.*" to avoid showing directory listing on webserver Notes: * some TODOs will be fixed when we have new theme using these advanced features (e.g. pictures in album pages) * TODO: introduce tag/block conditional system, allow custom user defines for each theme/page * TODO: deprecate , and in favor of conditionals (these tags belong to theming) --- src/generators.c | 564 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 300 insertions(+), 264 deletions(-) (limited to 'src/generators.c') diff --git a/src/generators.c b/src/generators.c index 95bed37..051ee28 100644 --- a/src/generators.c +++ b/src/generators.c @@ -35,67 +35,89 @@ #include "stats.h" - -/* Returns newly allocated string */ -static gchar * -make_thumbnail_string (TGallerySetup *setup, unsigned int item_index, const gchar *imgname) -{ - gchar *s; - gchar *num; - - s = g_strdup (setup->thumbnail_name_format); - str_replace (&s, "%s", imgname); - num = g_strdup_printf ("%.3d", item_index); - str_replace (&s, "%d", num); - g_free (num); - - return s; -} - -/* Get full image source path */ -/* Returns newly allocated string */ -static gchar * -item_get_img_src (TGallerySetup *setup, TAlbum *items, TIndexItem *item) +#define IS_NOFULLSIZE(item,parent_items,setup) \ + (! item->force_fullsize && ! setup->override_nofullsize && \ + (! parent_items->fullsize || item->force_nofullsize) && \ + (item->force_nofullsize || parent_items->nofullsize || setup->nofullsize)) + +static void +get_image_paths (TGallerySetup *setup, + TAlbum *items, + TIndexItem *item, + unsigned int item_index, + TPathInfo *path_info, + TImageSize *image_size, + gchar **full_img_src, + gchar **full_img_dst, + gchar **page_img_dst) { - if (items->type == GALLERY_TYPE_INDEX) { - if (item->thumbnail == NULL || strlen (item->thumbnail) == 0) - return NULL; - return g_path_get_basename (item->thumbnail); - } - else - if (items->type == GALLERY_TYPE_ALBUM) { - return g_path_get_basename ((item->path == NULL && item->preview) ? item->preview : item->path); + gboolean nofullsize; + gboolean is_thumbnail; + gboolean is_preview; + gboolean is_original; + gchar *s1, *s2, *s3; + gchar *target_image_dir; + + if (full_img_src) + *full_img_src = NULL; + if (full_img_dst) + *full_img_dst = NULL; + if (page_img_dst) + *page_img_dst = NULL; + + /* ignore combinations that are not valid */ + nofullsize = IS_NOFULLSIZE (item, items, setup); + is_thumbnail = g_ascii_strcasecmp ("thumbnail", image_size->name) == 0; + is_preview = g_ascii_strcasecmp ("preview", image_size->name) == 0; + is_original = g_ascii_strcasecmp ("original", image_size->name) == 0; + if ((items->type == GALLERY_TYPE_INDEX && ! is_thumbnail) || + (is_thumbnail && item->hidden) || + (is_thumbnail && items->type == GALLERY_TYPE_INDEX && (item->thumbnail == NULL || strlen (item->thumbnail) == 0)) /* || + -- in several cases we need to get path to the source image, e.g. to retrieve EXIF metadata; disabling the check for now + (is_original && nofullsize) */ ) + return; + + target_image_dir = g_strdup_printf ("%s%s", TARGET_IMAGE_DIR_PREFIX, image_size->name); + + /* Thumbnail special case */ + if (is_thumbnail) { + s1 = NULL; + if (items->type == GALLERY_TYPE_INDEX) { + s1 = item->thumbnail; + } else + if (items->type == GALLERY_TYPE_ALBUM) { + s1 = (item->path == NULL && item->preview) ? item->preview : item->path; + s1 = (item->thumbnail) ? item->thumbnail : s1; + } + s2 = g_path_get_basename (s1); + if (full_img_src) + *full_img_src = g_build_filename (path_info->src_dir, s1, NULL); + s3 = g_strdup_printf (THUMBNAIL_NAME_FORMAT, item_index, s2); + g_free (s2); + if (full_img_dst) + *full_img_dst = g_build_filename (path_info->dest_dir, target_image_dir, s3, NULL); + if (page_img_dst) + *page_img_dst = g_build_filename (target_image_dir, s3, NULL); + g_free (s3); } - g_assert_not_reached (); - return NULL; -} -/* Get full thumbnail source path */ -/* Returns newly allocated string */ -static gchar * -item_get_thumbnail_src (TGallerySetup *setup, TAlbum *items, TIndexItem *item, unsigned int item_index) -{ - gchar *s1, *ret; - const gchar *gen_img_src; - - if (items->type == GALLERY_TYPE_INDEX) { - if (item->thumbnail == NULL || strlen (item->thumbnail) == 0) - return NULL; - s1 = g_path_get_basename (item->thumbnail); - ret = make_thumbnail_string (setup, item_index, s1); - g_free (s1); - return ret; - } - else - if (items->type == GALLERY_TYPE_ALBUM) { - gen_img_src = (item->path == NULL && item->preview) ? item->preview : item->path; - s1 = g_path_get_basename ((item->thumbnail) ? item->thumbnail : gen_img_src); - ret = make_thumbnail_string (setup, item_index, s1); - g_free (s1); - return ret; + /* Other image sizes */ + else { + if (is_preview) + s1 = (item->preview) ? item->preview : item->path; + else + s1 = (item->path == NULL && item->preview) ? item->preview : item->path; + if (full_img_src) + *full_img_src = g_build_filename (path_info->src_dir, s1, NULL); + s2 = g_path_get_basename (s1); + if (full_img_dst) + *full_img_dst = g_build_filename (path_info->dest_dir, target_image_dir, s2, NULL); + if (page_img_dst) + *page_img_dst = g_build_filename (target_image_dir, s2, NULL); + g_free (s2); } - g_assert_not_reached (); - return NULL; + + g_free (target_image_dir); } @@ -109,145 +131,120 @@ generate_image (TGallerySetup *setup, TIndexItem *item, unsigned int item_index, TPathInfo *path_info, - gboolean update_when_necessary) + gboolean query_update) { - unsigned long new_w, new_h; - unsigned long thumb_w, thumb_h; - gchar *thumb_dst; - gchar *big_dst; - gchar *big_src; - gchar *orig_dst; - gchar *img_src_full; - gchar *thumb_src_full; - gchar *s1; - int bigq; gboolean res; - gboolean source_img_portrait; - - - thumb_src_full = NULL; - img_src_full = NULL; - orig_dst = NULL; - - if (items->type == GALLERY_TYPE_INDEX) { - if (item->thumbnail == NULL || strlen (item->thumbnail) == 0) - return FALSE; - img_src_full = g_build_filename (path_info->src_dir, item->thumbnail, NULL); - thumb_src_full = g_build_filename (path_info->src_dir, item->thumbnail, NULL); - } - else - if (items->type == GALLERY_TYPE_ALBUM) { - s1 = (item->path == NULL && item->preview) ? item->preview : item->path; - thumb_src_full = (item->thumbnail) ? item->thumbnail : s1; - img_src_full = g_build_filename (path_info->src_dir, s1, NULL); - thumb_src_full = g_build_filename (path_info->src_dir, thumb_src_full, NULL); - } - - /* Make paths */ - s1 = item_get_thumbnail_src (setup, items, item, item_index); - thumb_dst = g_build_filename (path_info->dest_dir, setup->thumbnail_dir, s1, NULL); - g_free (s1); - s1 = item_get_img_src (setup, items, item); - big_dst = g_build_filename (path_info->dest_dir, setup->img_big_dir, s1, NULL); - if (item->force_fullsize || setup->override_nofullsize || (items->fullsize && ! item->force_nofullsize) || - (! item->force_nofullsize && ! items->nofullsize && ! setup->nofullsize)) - orig_dst = g_build_filename (path_info->dest_dir, setup->img_orig_dir, s1, NULL); - res = (! update_when_necessary) || (! item->hidden && needs_update (thumb_src_full, thumb_dst)) || - (items->type == GALLERY_TYPE_ALBUM && (needs_update (img_src_full, big_dst) || (orig_dst && needs_update (img_src_full, orig_dst)))); - g_free (s1); - - /* Do something when necessary */ - if (res) { - get_image_sizes (img_src_full, &new_w, &new_h); - - if (new_w > 0 && new_h > 0) { - stats_images_inc (); - source_img_portrait = new_h > new_w; - - /* Generate thumbnail */ - g_assert (thumb_src_full != NULL); - get_image_sizes (thumb_src_full, &thumb_w, &thumb_h); - - if (thumb_w > 0 && thumb_h > 0) { - if (! item->hidden) { - /* Squared thumbnails */ - if (setup->squared_thumbnail_type != THUMBNAIL_SQUARE_TYPE_NONE) { - thumb_w = setup->thumbnail_square_size; - thumb_h = setup->thumbnail_square_size; - } else - /* Portrait / Landscape thumbnails */ - if (thumb_h > thumb_w) - calculate_sizes (setup->thumbnail_portrait_width, setup->thumbnail_portrait_height, &thumb_w, &thumb_h); + gboolean is_thumbnail; + gboolean is_preview; + gboolean is_original; + gchar *img_src; + gchar *img_dst; + unsigned long img_w, img_h; + int quality; + GList *l; + TImageSize *image_size; + + + res = ! query_update; + for (l = g_list_first (setup->design->image_sizes); l; l = g_list_next (l)) { + image_size = l->data; + + is_thumbnail = g_ascii_strcasecmp ("thumbnail", image_size->name) == 0; + is_preview = g_ascii_strcasecmp ("preview", image_size->name) == 0; + is_original = g_ascii_strcasecmp ("original", image_size->name) == 0; + + get_image_paths (setup, items, item, item_index, path_info, image_size, &img_src, &img_dst, NULL); + if (img_src == NULL || img_dst == NULL) + continue; + + /* Do something when required */ + res = res || needs_update (img_src, img_dst); + if (! query_update) { + /* Copy the source file */ + if ((is_preview && item->preview) || (is_original && image_size->no_resize)) { + if (! copy_file (img_src, img_dst)) + log_error ("generate_image: error copying image %s\n", img_src); + } + /* Resize image */ + else { + get_image_sizes (img_src, &img_w, &img_h); + if (img_w > 0 && img_h > 0) { + stats_images_inc (); + + /* Calculate sizes */ + if (is_thumbnail && setup->squared_thumbnail_type != THUMBNAIL_SQUARE_TYPE_NONE) + img_w = img_h = image_size->square_size; else - calculate_sizes (setup->thumbnail_landscape_width, setup->thumbnail_landscape_height, &thumb_w, &thumb_h); - if (! resize_image (thumb_src_full, thumb_dst, thumb_w, thumb_h, setup->thumbnail_quality, TRUE, setup->squared_thumbnail_type)) - log_error ("generate_image: error resizing thumbnail %s\n", thumb_src_full); + if (is_preview && item->width > 0 && item->height > 0) + calculate_sizes (item->width, item->height, &img_w, &img_h); + if (img_w > img_h) { + if (is_preview && items->landscape_width > 0 && items->landscape_height > 0) + calculate_sizes (items->landscape_width, items->landscape_height, &img_w, &img_h); + else + calculate_sizes (image_size->landscape_width, image_size->landscape_height, &img_w, &img_h); + } else { + if (is_preview && items->portrait_width > 0 && items->portrait_height > 0) + calculate_sizes (items->portrait_width, items->portrait_height, &img_w, &img_h); + else + calculate_sizes (image_size->portrait_width, image_size->portrait_height, &img_w, &img_h); + } + /* Calculate quality */ + quality = image_size->quality; + if (is_preview && items->quality > 0 && items->quality <= 100) + quality = items->quality; + if (is_preview && item->quality > 0 && item->quality <= 100) + quality = item->quality; + + /* Perform resize and strip */ + if (! resize_image (img_src, img_dst, img_w, img_h, quality, is_thumbnail, setup->squared_thumbnail_type)) + log_error ("generate_image: error resizing image %s\n", img_src); + } else { + log_error ("generate_image: image %s sizes are %lux%lu\n", img_src, img_w, img_h); } - } else { - log_error ("generate_image: thumbnail %s sizes are %lux%lu\n", thumb_src_full, thumb_w, thumb_h); } + } + if (! is_thumbnail) + modify_exif (img_dst, setup->erase_exif_thumbnail, setup->add_copyright); + g_free (img_src); + g_free (img_dst); + } + return res; +} - /* Generate/copy preview image */ - if (items->type == GALLERY_TYPE_ALBUM) { - if (item->preview == NULL) { - /* No preview image supplied, generate it from original */ - bigq = setup->preview_quality; - if (items->quality > 0 && items->quality <= 100) - bigq = items->quality; - if (item->quality > 0 && item->quality <= 100) - bigq = item->quality; - - if (item->width > 0 && item->height > 0) { - calculate_sizes (item->width, item->height, &new_w, &new_h); - } else { - if (! source_img_portrait) { - if (items->landscape_width > 0 && items->landscape_height > 0) - calculate_sizes (items->landscape_width, items->landscape_height, &new_w, &new_h); - else - calculate_sizes (setup->preview_landscape_width, setup->preview_landscape_height, &new_w, &new_h); - } else { - if (items->portrait_width > 0 && items->portrait_height > 0) - calculate_sizes (items->portrait_width, items->portrait_height, &new_w, &new_h); - else - calculate_sizes (setup->preview_portrait_width, setup->preview_portrait_height, &new_w, &new_h); - } - } - g_assert (img_src_full != NULL); - if (! resize_image (img_src_full, big_dst, new_w, new_h, bigq, FALSE, 0)) - log_error ("generate_image: error resizing big image %s\n", img_src_full); - } - else { - /* Copy the preview (big) image provided */ - big_src = g_build_filename (path_info->src_dir, item->preview, NULL); - if (! copy_file (big_src, big_dst)) - log_error ("generate_image: error copying preview image %s\n", big_src); - g_free (big_src); - } - modify_exif (big_dst, setup->erase_exif_thumbnail, setup->add_copyright); +static TImageSize * +find_image_size_for_name (TGallerySetup *setup, const gchar *name) +{ + GList *l; + TImageSize *image_size; - /* Generate/copy original image */ - if (orig_dst) { - if (! copy_file (img_src_full, orig_dst)) - log_error ("generate_image: error copying original image %s\n", img_src_full); - else - modify_exif (orig_dst, setup->erase_exif_thumbnail, setup->add_copyright); - } - } - } + for (l = g_list_first (setup->design->image_sizes); l; l = g_list_next (l)) { + image_size = l->data; + g_assert (image_size != NULL); + if (g_ascii_strcasecmp (name, image_size->name) == 0) + return image_size; } - g_free (img_src_full); - g_free (thumb_src_full); - g_free (big_dst); - g_free (thumb_dst); - g_free (orig_dst); - - return res; + return NULL; } +static gchar * +get_index_filename (TAlbum *items, TGalleryDesignTheme *theme, TPathInfo *path_info, TIndexItem *retrieve_child) +{ + gchar *s; + TGalleryType gallery_type; + + gallery_type = items->type; + if (retrieve_child) { + s = g_build_filename (path_info->src_dir, retrieve_child->path, "index.xml", NULL); + gallery_type = get_child_gallery_type (s); + g_free (s); + } + + return (gallery_type == GALLERY_TYPE_ALBUM || theme->index_filename == NULL) ? theme->album_filename : theme->index_filename; +} /* * write_html_album: process album and index template files @@ -260,6 +257,7 @@ generate_image (TGallerySetup *setup, gboolean write_html_album (TGallerySetup *setup, TPathInfo *path_info, + TGalleryDesignTheme *theme, const gchar *template_src, const gchar *dst, TAlbum *items) @@ -281,6 +279,7 @@ write_html_album (TGallerySetup *setup, ReplaceTable *global_replace_table; ReplaceTable *local_replace_table; BlockParser *block_parser; + TImageSize *image_size, *thumb_image_size; fin = fopen (template_src, "r"); @@ -335,17 +334,19 @@ write_html_album (TGallerySetup *setup, level += count_dir_levels (tmp_item->path) - 1; } /* Go Up string */ - s3 = make_string ("../", level); - s2 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); - replace_table_add_key_printf (global_replace_table, "GO_UP_LINK", "%s%s%s", s3, setup->index_file_name, s2); - g_free (s2); - g_free (s3); + if (parent) { + s3 = make_string ("../", level); + s2 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); + replace_table_add_key_printf (global_replace_table, "GO_UP_LINK", "%s%s%s", s3, get_index_filename (parent, theme, NULL, NULL), s2); + g_free (s2); + g_free (s3); + } while (parent) { s3 = make_string ("../", level); s4 = g_strdup (parent->ID); s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", old_parent_item_index) : g_strdup (""); - s2 = g_strdup_printf ("%s > %s", s3, setup->index_file_name, s5, s4, s1); + s2 = g_strdup_printf ("%s > %s", s3, get_index_filename (parent, theme, NULL, NULL), s5, s4, s1); g_free (s3); g_free (s1); g_free (s4); @@ -365,7 +366,7 @@ write_html_album (TGallerySetup *setup, /* Supportfiles path */ s1 = make_string ("../", level - 1); - replace_table_add_key (global_replace_table, "TEMPLATES_PATH", setup->support_files_use_common_root ? s1 : ""); + replace_table_add_key (global_replace_table, "TEMPLATES_PATH", setup->supplemental_files_use_common_root ? s1 : ""); g_free (s1); /* META tags */ @@ -397,9 +398,9 @@ write_html_album (TGallerySetup *setup, if (setup->favicon_file && strlen (setup->favicon_file) > 0) { s3 = make_string ("../", level - 1); if (setup->favicon_type) - s2 = g_strdup_printf ("%s\t\n", s1, setup->favicon_type, setup->support_files_use_common_root ? s3 : "", setup->favicon_file); + s2 = g_strdup_printf ("%s\t\n", s1, setup->favicon_type, setup->supplemental_files_use_common_root ? s3 : "", setup->favicon_file); else - s2 = g_strdup_printf ("%s\t\n", s1, setup->support_files_use_common_root ? s3 : "", setup->favicon_file); + s2 = g_strdup_printf ("%s\t\n", s1, setup->supplemental_files_use_common_root ? s3 : "", setup->favicon_file); g_free (s1); g_free (s3); s1 = s2; @@ -416,6 +417,10 @@ write_html_album (TGallerySetup *setup, g_free (s1); } + /* Theming */ + /* TODO: "image_size" will be used for album themes showing pictures inpage */ + image_size = find_image_size_for_name (setup, theme->album_image_size); + thumb_image_size = find_image_size_for_name (setup, "thumbnail"); /* Setup block parser */ block_parser_register_key (block_parser, "IMG_LIST", "IMG_LIST"); @@ -462,37 +467,38 @@ write_html_album (TGallerySetup *setup, case INDEX_ITEM_TYPE_PICTURE: /* Skip HTML code generation if it's a hidden item */ if (! item->hidden) { - s2 = item_get_thumbnail_src (setup, items, item, i); - if (s2 != NULL) { - s4 = g_build_filename (path_info->dest_dir, setup->thumbnail_dir, s2, NULL); - get_image_sizes (s4, &img_thumb_w, &img_thumb_h); - g_free (s4); - s1 = (img_thumb_h > img_thumb_w) ? "IMG_LIST_PORTRAIT" : "IMG_LIST_LANDSCAPE"; - } + img_thumb_w = img_thumb_h = 0; + get_image_paths (setup, items, item, i, path_info, thumb_image_size, NULL, &s2, &s3); + if (s2 != NULL) + get_image_sizes (s2, &img_thumb_w, &img_thumb_h); - if (setup->squared_thumbnail_type != THUMBNAIL_SQUARE_TYPE_NONE) + if (setup->squared_thumbnail_type != THUMBNAIL_SQUARE_TYPE_NONE || img_thumb_w == img_thumb_h) s1 = "IMG_LIST_SQUARED"; - if (! s1) - s1 = "IMG_LIST_LANDSCAPE"; /* fallback case */ + else + s1 = (img_thumb_h > img_thumb_w) ? "IMG_LIST_PORTRAIT" : "IMG_LIST_LANDSCAPE"; s1 = block_parser_get_data (block_parser, s1); - if (s2 != NULL) { - replace_table_add_key_printf (local_replace_table, "IMG_THUMBNAIL", "%s/%s", setup->thumbnail_dir, s2); - replace_table_add_key_int (local_replace_table, "IMG_SIZE_THUMB_W", img_thumb_w); - replace_table_add_key_int (local_replace_table, "IMG_SIZE_THUMB_H", img_thumb_h); + if (s3 != NULL) { + replace_table_add_key (local_replace_table, "IMG_THUMBNAIL", s3); + if (img_thumb_w > 0 && img_thumb_h > 0) { + replace_table_add_key_int (local_replace_table, "IMG_SIZE_THUMB_W", img_thumb_w); + replace_table_add_key_int (local_replace_table, "IMG_SIZE_THUMB_H", img_thumb_h); + } } g_free (s2); - replace_table_add_key_printf (local_replace_table, "ALBUM_SUBPATH", "%s/%s", item->path, setup->index_file_name); - s3 = item_get_img_src (setup, items, item); - replace_table_add_key_printf (local_replace_table, "IMG_SUBPAGE", "%s%s", s3, GET_EXT (setup->index_file_name)); - replace_table_add_key (local_replace_table, "IMG_FILENAME", s3); g_free (s3); + + s2 = GET_ITEM_TARGET_FILENAME (item); + replace_table_add_key_printf (local_replace_table, "IMG_SUBPAGE", theme->picture_filename, s2); + replace_table_add_key (local_replace_table, "IMG_FILENAME", s2); + g_free (s2); replace_table_add_key (local_replace_table, "IMG_TITLE", item->title); replace_table_add_key (local_replace_table, "IMG_DESCRIPTION", item->title_description); replace_table_add_key_printf (local_replace_table, "IMG_LIST_ID", "i%d", i + 1); if (items->type == GALLERY_TYPE_INDEX) { s3 = g_build_filename (path_info->src_dir, item->path, "index.xml", NULL); replace_table_add_key_int (local_replace_table, "ALBUM_NUM_ITEMS", get_album_objects_count (s3)); + replace_table_add_key_printf (local_replace_table, "ALBUM_SUBPATH", "%s/%s", item->path, get_index_filename (items, theme, path_info, item)); g_free (s3); } } @@ -521,6 +527,8 @@ write_html_album (TGallerySetup *setup, replace_table_process (&block, global_replace_table); replace_table_add_key (global_replace_table, "IMG_LIST", block); g_free (block); + /* We don't use data from this key directly, let's mark it as used since we've built the structure we needed. */ + block_parser_set_as_used (block_parser, "IMG_LIST"); } /* Replace all known tags */ @@ -556,6 +564,7 @@ write_html_album (TGallerySetup *setup, gboolean write_html_image (TGallerySetup *setup, TPathInfo *path_info, + TGalleryDesignTheme *theme, const gchar *template_src, const gchar *dst, TIndexItem *item, @@ -563,11 +572,15 @@ write_html_image (TGallerySetup *setup, { FILE *fin; FILE *fout; - gchar *big_dst; - gchar *orig_dst; + gchar *img_dst; + gchar *img_dst_page; + gchar *img_orig_src; + gchar *img_orig_dst; + gchar *img_orig_dst_page; TExifData *exif; - unsigned long img_big_w, img_big_h, img_orig_w, img_orig_h; - unsigned int item_index, real_item_index, real_total_items; + unsigned long img_w, img_h; + unsigned long img_orig_w, img_orig_h; + unsigned int item_index, next_item_index, real_item_index, real_total_items; TIndexItem *previous_item; TIndexItem *next_item; TIndexItem *tmp_item; @@ -583,8 +596,10 @@ write_html_image (TGallerySetup *setup, int level, old_parent_item_index; gboolean override_title_meta; gboolean image_fullsize; + gboolean theme_size_is_original; ReplaceTable *replace_table; BlockParser *block_parser; + TImageSize *image_size; fin = fopen (template_src, "r"); @@ -599,7 +614,11 @@ write_html_image (TGallerySetup *setup, return FALSE; } + img_orig_src = NULL; + img_orig_dst = NULL; + img_orig_dst_page = NULL; preload_imgname = NULL; + res = TRUE; replace_table = replace_table_new (); block_parser = block_parser_new (); @@ -621,45 +640,55 @@ write_html_image (TGallerySetup *setup, /* Get previous and next items */ previous_item = NULL; next_item = NULL; + next_item_index = 0; for (i = item_index - 2; i >= 0 && (previous_item == NULL || previous_item->type != INDEX_ITEM_TYPE_PICTURE); i--) previous_item = g_ptr_array_index (parent_items->items, i); if (previous_item && previous_item->type != INDEX_ITEM_TYPE_PICTURE) previous_item = NULL; - for (i = item_index; i < parent_items->items->len && (next_item == NULL || next_item->type != INDEX_ITEM_TYPE_PICTURE); i++) + for (i = item_index; i < parent_items->items->len && (next_item == NULL || next_item->type != INDEX_ITEM_TYPE_PICTURE); i++) { next_item = g_ptr_array_index (parent_items->items, i); - if (next_item && next_item->type != INDEX_ITEM_TYPE_PICTURE) + next_item_index = i; + } + if (next_item && next_item->type != INDEX_ITEM_TYPE_PICTURE) { next_item = NULL; - - /* Paths setup */ - imgname = g_path_get_basename ((item->path == NULL && item->preview) ? item->preview : item->path); - if (next_item && setup->preload) { - s1 = g_path_get_basename ((next_item->path == NULL && next_item->preview) ? next_item->preview : next_item->path); - preload_imgname = g_build_filename (setup->img_big_dir, s1, NULL); - g_free (s1); + next_item_index = 0; } - big_dst = g_build_filename (path_info->dest_dir, setup->img_big_dir, imgname, NULL); - orig_dst = g_build_filename (path_info->dest_dir, setup->img_orig_dir, imgname, NULL); - res = TRUE; + + /* If currently processed picture size is original, disable fullsize tags */ + theme_size_is_original = g_ascii_strcasecmp (theme->picture_image_size, "original") == 0; + /* Original size is needed for several things, like EXIF and IPTC metadata */ + image_size = find_image_size_for_name (setup, "original"); + get_image_paths (setup, parent_items, item, item_index, path_info, image_size, &img_orig_src, &img_orig_dst, &img_orig_dst_page); + image_size = find_image_size_for_name (setup, theme->picture_image_size); + get_image_paths (setup, parent_items, item, item_index, path_info, image_size, NULL, &img_dst, &img_dst_page); + imgname = g_path_get_basename (img_dst); + if (next_item && setup->preload) + get_image_paths (setup, parent_items, next_item, next_item_index, path_info, image_size, NULL, NULL, &preload_imgname); /* Get EXIF data from the original image */ - s1 = g_build_filename (path_info->src_dir, (item->path == NULL && item->preview) ? item->preview : item->path, NULL); - exif = get_exif (s1); - g_free (s1); - if (exif == NULL) - log_error ("write_html_image: error getting exif data from file \"%s\"\n", orig_dst); + exif = NULL; + if (img_orig_src) { + exif = get_exif (img_orig_src); + if (exif == NULL) + log_error ("write_html_image: error getting exif data from file \"%s\"\n", img_orig_src); + } + /* Try destination image instead, though it might have the metadata stripped */ + if (exif == NULL) { + exif = get_exif (img_dst); + if (exif == NULL) + log_error ("write_html_image: error getting exif data from file \"%s\"\n", img_dst); + } /* Retrieve image sizes of preview and original image */ - get_image_sizes (big_dst, &img_big_w, &img_big_h); - image_fullsize = item->force_fullsize || setup->override_nofullsize || - (parent_items->fullsize && ! item->force_nofullsize) || - (! item->force_nofullsize && ! parent_items->nofullsize && ! setup->nofullsize); + get_image_sizes (img_dst, &img_w, &img_h); + image_fullsize = ! theme_size_is_original && img_orig_dst != NULL && ! IS_NOFULLSIZE (item, parent_items, setup); if (image_fullsize) - get_image_sizes (orig_dst, &img_orig_w, &img_orig_h); + get_image_sizes (img_orig_dst, &img_orig_w, &img_orig_h); /* Get title and description from IPTC/EXIF/JPEG if not defined */ title = g_strdup (item->title); title_desc = g_strdup (item->title_description); - if (setup->use_iptc_exif && title == NULL && title_desc == NULL) { + if (setup->use_iptc_exif && title == NULL && title_desc == NULL && exif != NULL) { if (exif->iptc_caption) title = g_strdup (exif->iptc_caption); if (exif->jpeg_comment) { @@ -713,13 +742,15 @@ write_html_image (TGallerySetup *setup, replace_table_add_key (replace_table, "FOOTER", setup->footer); replace_table_add_key_int (replace_table, "TOTAL_ITEMS", real_total_items); replace_table_add_key_int (replace_table, "FILE_NO", real_item_index); - replace_table_add_key_int (replace_table, "IMG_SIZE_BIG_W", img_big_w); - replace_table_add_key_int (replace_table, "IMG_SIZE_BIG_H", img_big_h); - replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_W", img_orig_w); - replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_H", img_orig_h); - replace_table_add_key_printf (replace_table, "IMG_SRC_BIG", "%s/%s", setup->img_big_dir, imgname); - replace_table_add_key_printf (replace_table, "IMG_SRC_FULL", "%s/%s", setup->img_orig_dir, imgname); + replace_table_add_key_int (replace_table, "IMG_SIZE_W", img_w); + replace_table_add_key_int (replace_table, "IMG_SIZE_H", img_h); + replace_table_add_key_printf (replace_table, "IMG_SRC", "%s%s/%s", TARGET_IMAGE_DIR_PREFIX, image_size->name, imgname); replace_table_add_key (replace_table, "IMG_SRC_PRELOAD", preload_imgname ? preload_imgname : ""); + if (image_fullsize) { + replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_W", img_orig_w); + replace_table_add_key_int (replace_table, "IMG_SIZE_ORIG_H", img_orig_h); + replace_table_add_key (replace_table, "IMG_SRC_FULL", img_orig_dst_page); + } /* Navigation bar (NOTE: 'int level' is used below + favicon) */ s1 = g_strdup (imgname); @@ -730,7 +761,7 @@ write_html_image (TGallerySetup *setup, s3 = make_string ("../", level); s4 = g_strdup (parent->ID); s5 = setup->use_inpage_links ? g_strdup_printf ("#i%d", parent == parent_items ? item_index : old_parent_item_index) : g_strdup (""); - s2 = g_strdup_printf ("%s > %s", s3, setup->index_file_name, s5, s4, s1); + s2 = g_strdup_printf ("%s > %s", s3, get_index_filename (parent, theme, NULL, NULL), s5, s4, s1); g_free (s3); g_free (s1); g_free (s4); @@ -750,20 +781,22 @@ write_html_image (TGallerySetup *setup, /* Supportfiles path */ s1 = make_string ("../", level - 1); - replace_table_add_key (replace_table, "TEMPLATES_PATH", setup->support_files_use_common_root ? s1 : ""); + replace_table_add_key (replace_table, "TEMPLATES_PATH", setup->supplemental_files_use_common_root ? s1 : ""); g_free (s1); /* EXIF values */ - replace_table_add_key (replace_table, "EXIF_ISO", exif->iso ? exif->iso : "??"); - replace_table_add_key (replace_table, "EXIF_TIME", exif->exposure ? exif->exposure : "??"); - replace_table_add_key (replace_table, "EXIF_APERTURE", exif->aperture ? exif->aperture : "??"); - replace_table_add_key (replace_table, "EXIF_FOCAL_LENGTH", exif->focal_length ? exif->focal_length : "??"); - replace_table_add_key (replace_table, "EXIF_FLASH", exif->flash ? exif->flash : "??"); - replace_table_add_key (replace_table, "EXIF_DATE", exif->datetime ? exif->datetime : "??"); - replace_table_add_key (replace_table, "EXIF_CAMERA_MODEL", exif->camera_model ? exif->camera_model : "??"); - s1 = g_strdup_printf ("(%s)", exif->focal_length_35mm); - replace_table_add_key (replace_table, "EXIF_FOCAL_35", exif->focal_length_35mm ? s1 : ""); - g_free (s1); + if (exif) { + replace_table_add_key (replace_table, "EXIF_ISO", exif->iso ? exif->iso : "??"); + replace_table_add_key (replace_table, "EXIF_TIME", exif->exposure ? exif->exposure : "??"); + replace_table_add_key (replace_table, "EXIF_APERTURE", exif->aperture ? exif->aperture : "??"); + replace_table_add_key (replace_table, "EXIF_FOCAL_LENGTH", exif->focal_length ? exif->focal_length : "??"); + replace_table_add_key (replace_table, "EXIF_FLASH", exif->flash ? exif->flash : "??"); + replace_table_add_key (replace_table, "EXIF_DATE", exif->datetime ? exif->datetime : "??"); + replace_table_add_key (replace_table, "EXIF_CAMERA_MODEL", exif->camera_model ? exif->camera_model : "??"); + s1 = g_strdup_printf ("(%s)", exif->focal_length_35mm); + replace_table_add_key (replace_table, "EXIF_FOCAL_35", exif->focal_length_35mm ? s1 : ""); + g_free (s1); + } /* Border style */ s1 = item->border_style; @@ -777,19 +810,19 @@ write_html_image (TGallerySetup *setup, /* Next/Previous links */ if (next_item) { - s2 = g_path_get_basename ((next_item->path == NULL && next_item->preview) ? next_item->preview : next_item->path); - replace_table_add_key_printf (replace_table, "LINK_NEXT", "%s%s", s2, GET_EXT (setup->index_file_name)); + s2 = GET_ITEM_TARGET_FILENAME (next_item); + replace_table_add_key_printf (replace_table, "LINK_NEXT", theme->picture_filename, s2); g_free (s2); } else - replace_table_add_key (replace_table, "LINK_NEXT", setup->index_file_name); + replace_table_add_key (replace_table, "LINK_NEXT", get_index_filename (parent_items, theme, NULL, NULL)); if (previous_item) { - s2 = g_path_get_basename ((previous_item->path == NULL && previous_item->preview) ? previous_item->preview : previous_item->path); - replace_table_add_key_printf (replace_table, "LINK_PREV", "%s%s", s2, GET_EXT (setup->index_file_name)); + s2 = GET_ITEM_TARGET_FILENAME (previous_item); + replace_table_add_key_printf (replace_table, "LINK_PREV", theme->picture_filename, s2); g_free (s2); } else - replace_table_add_key (replace_table, "LINK_PREV", setup->index_file_name); + replace_table_add_key (replace_table, "LINK_PREV", get_index_filename (parent_items, theme, NULL, NULL)); /* META tags */ override_title_meta = setup->use_title_as_meta && title && (strlen (title) > 0); @@ -821,9 +854,9 @@ write_html_image (TGallerySetup *setup, if (setup->favicon_file && strlen (setup->favicon_file) > 0) { s3 = make_string ("../", level - 1); if (setup->favicon_type) - s2 = g_strdup_printf ("%s\t\n", s1, setup->favicon_type, setup->support_files_use_common_root ? s3 : "", setup->favicon_file); + s2 = g_strdup_printf ("%s\t\n", s1, setup->favicon_type, setup->supplemental_files_use_common_root ? s3 : "", setup->favicon_file); else - s2 = g_strdup_printf ("%s\t\n", s1, setup->support_files_use_common_root ? s3 : "", setup->favicon_file); + s2 = g_strdup_printf ("%s\t\n", s1, setup->supplemental_files_use_common_root ? s3 : "", setup->favicon_file); g_free (s1); g_free (s3); s1 = s2; @@ -887,8 +920,11 @@ write_html_image (TGallerySetup *setup, fclose (fin); g_free (title); g_free (title_desc); - g_free (big_dst); - g_free (orig_dst); + g_free (img_dst); + g_free (img_dst_page); + g_free (img_orig_src); + g_free (img_orig_dst); + g_free (img_orig_dst_page); g_free (imgname); g_free (preload_imgname); free_exif_data (exif); -- cgit v1.2.3