2 * Sone - ImageLinkFilter.java - Copyright © 2011–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.template;
20 import static java.lang.Integer.MAX_VALUE;
21 import static java.lang.String.valueOf;
22 import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
24 import java.io.StringReader;
25 import java.io.StringWriter;
28 import net.pterodactylus.sone.core.Core;
29 import net.pterodactylus.sone.data.Image;
30 import net.pterodactylus.util.template.Filter;
31 import net.pterodactylus.util.template.Template;
32 import net.pterodactylus.util.template.TemplateContext;
33 import net.pterodactylus.util.template.TemplateContextFactory;
34 import net.pterodactylus.util.template.TemplateParser;
36 import com.google.common.base.Optional;
39 * Template filter that turns an {@link Image} into an HTML <img> tag,
40 * using some parameters to influence parameters of the image.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class ImageLinkFilter implements Filter {
46 /** The template to render for the <img> tag. */
47 private static final Template linkTemplate = TemplateParser.parse(new StringReader("<img<%ifnull !class> class=\"<%class|css>\"<%/if> src=\"<%src|html><%if forceDownload>?forcedownload=true<%/if>\" alt=\"<%alt|html>\" title=\"<%title|html>\" width=\"<%width|html>\" height=\"<%height|html>\" style=\"position: relative;<%ifnull ! top>top: <% top|html>;<%/if><%ifnull ! left>left: <% left|html>;<%/if>\"/>"));
50 private final Core core;
52 /** The template context factory. */
53 private final TemplateContextFactory templateContextFactory;
56 * Creates a new image link filter.
60 * @param templateContextFactory
61 * The template context factory
63 public ImageLinkFilter(Core core, TemplateContextFactory templateContextFactory) {
65 this.templateContextFactory = templateContextFactory;
72 public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
74 if (data instanceof String) {
75 image = core.getImage((String) data, false);
76 } else if (data instanceof Image) {
82 String imageClass = valueOf(parameters.get("class"));
83 int maxWidth = parseInt(valueOf(parameters.get("max-width")), MAX_VALUE);
84 int maxHeight = parseInt(valueOf(parameters.get("max-height")), MAX_VALUE);
85 String mode = valueOf(parameters.get("mode"));
86 String title = valueOf(parameters.get("title"));
88 TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext();
89 linkTemplateContext.set("class", imageClass);
90 if (image.isInserted()) {
91 linkTemplateContext.set("src", "/" + image.getKey());
92 linkTemplateContext.set("forceDownload", true);
94 linkTemplateContext.set("src", "getImage.html?image=" + image.getId());
96 int imageWidth = image.getWidth();
97 int imageHeight = image.getHeight();
98 if ("enlarge".equals(mode)) {
99 double scale = Math.max(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
100 linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
101 linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
102 linkTemplateContext.set("left", String.format("%dpx", (int) (maxWidth - (imageWidth * scale)) / 2));
103 linkTemplateContext.set("top", String.format("%dpx", (int) (maxHeight - (imageHeight * scale)) / 2));
106 if ((imageWidth > maxWidth) || (imageHeight > maxHeight)) {
107 scale = Math.min(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
109 linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
110 linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
112 linkTemplateContext.set("alt", Optional.fromNullable(title).or(image.getDescription()));
113 linkTemplateContext.set("title", Optional.fromNullable(title).or(image.getTitle()));
115 StringWriter stringWriter = new StringWriter();
116 linkTemplate.render(linkTemplateContext, stringWriter);
117 return stringWriter.toString();