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