d7b5eeabb772c2f0728cb4cc73120a6364350fa4
[Sone.git] / src / main / java / net / pterodactylus / sone / template / ImageLinkFilter.java
1 /*
2  * Sone - ImageLinkFilter.java - Copyright © 2011 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import java.io.StringReader;
21 import java.io.StringWriter;
22 import java.util.Map;
23
24 import net.pterodactylus.sone.data.Image;
25 import net.pterodactylus.util.number.Numbers;
26 import net.pterodactylus.util.object.Default;
27 import net.pterodactylus.util.template.Filter;
28 import net.pterodactylus.util.template.Template;
29 import net.pterodactylus.util.template.TemplateContext;
30 import net.pterodactylus.util.template.TemplateContextFactory;
31 import net.pterodactylus.util.template.TemplateParser;
32
33 /**
34  * Template filter that turns an {@link Image} into an HTML &lt;img&gt; tag,
35  * using some parameters to influence parameters of the image.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class ImageLinkFilter implements Filter {
40
41         /** The template to render for the &lt;img&gt; tag. */
42         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>\"/>"));
43
44         /** The template context factory. */
45         private final TemplateContextFactory templateContextFactory;
46
47         /**
48          * Creates a new image link filter.
49          *
50          * @param templateContextFactory
51          *            The template context factory
52          */
53         public ImageLinkFilter(TemplateContextFactory templateContextFactory) {
54                 this.templateContextFactory = templateContextFactory;
55         }
56
57         /**
58          * {@inheritDoc}
59          */
60         @Override
61         public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
62                 Image image = (Image) data;
63                 String imageClass = parameters.get("class");
64                 int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE);
65                 int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE);
66                 String mode = String.valueOf(parameters.get("mode"));
67                 String title = parameters.get("title");
68                 if ((title != null) && title.startsWith("=")) {
69                         title = String.valueOf(templateContext.get(title.substring(1)));
70                 }
71
72                 TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext();
73                 linkTemplateContext.set("class", imageClass);
74                 if (image.isInserted()) {
75                         linkTemplateContext.set("src", "/" + image.getKey());
76                         linkTemplateContext.set("forceDownload", true);
77                 } else {
78                         linkTemplateContext.set("src", "getImage.html?image=" + image.getId());
79                 }
80                 int imageWidth = image.getWidth();
81                 int imageHeight = image.getHeight();
82                 if ("enlarge".equals(mode)) {
83                         double scale = Math.max(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
84                         linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
85                         linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
86                         linkTemplateContext.set("left", String.format("%dpx", (int) (maxWidth - (imageWidth * scale)) / 2));
87                         linkTemplateContext.set("top", String.format("%dpx", (int) (maxHeight - (imageHeight * scale)) / 2));
88                 } else {
89                         double scale = 1;
90                         if ((imageWidth > maxWidth) || (imageHeight > maxHeight)) {
91                                 scale = Math.min(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
92                         }
93                         linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
94                         linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
95                 }
96                 linkTemplateContext.set("alt", Default.forNull(title, image.getDescription()));
97                 linkTemplateContext.set("title", Default.forNull(title, image.getTitle()));
98
99                 StringWriter stringWriter = new StringWriter();
100                 linkTemplate.render(linkTemplateContext, stringWriter);
101                 return stringWriter.toString();
102         }
103
104 }