X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FImageLinkFilter.java;h=9e758cfd525312fd64178f2351e589acc620a825;hb=8ded406fabed389e0f9dc304232f2ab7f3cd65d7;hp=3d6c731ac995ff4e46e26b412732ef0a3bff1094;hpb=a23c4f218c3adf236d89d5927cae37d6e6e4feda;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java
index 3d6c731..9e758cf 100644
--- a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java
+++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java
@@ -23,6 +23,7 @@ import java.util.Map;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.util.number.Numbers;
+import net.pterodactylus.util.object.Default;
import net.pterodactylus.util.template.Filter;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
@@ -37,8 +38,8 @@ import net.pterodactylus.util.template.TemplateParser;
*/
public class ImageLinkFilter implements Filter {
- /** The template to render for the <img%gt; tag. */
- private static final Template linkTemplate = TemplateParser.parse(new StringReader(" class=\"<%class|css>\"<%/if> src=\"<%src|html>\" alt=\"<%alt|html>\" title=\"<%title|html>\" width=\"<%width|html>\" height=\"<%height|html>\" />"));
+ /** The template to render for the <img> tag. */
+ private static final Template linkTemplate = TemplateParser.parse(new StringReader(" class=\"<%class|css>\"<%/if> src=\"<%src|html>\" 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>\"/>"));
/** The template context factory. */
private final TemplateContextFactory templateContextFactory;
@@ -62,6 +63,11 @@ public class ImageLinkFilter implements Filter {
String imageClass = parameters.get("class");
int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE);
int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE);
+ String mode = String.valueOf(parameters.get("mode"));
+ String title = parameters.get("title");
+ if ((title != null) && title.startsWith("=")) {
+ title = String.valueOf(templateContext.get(title.substring(1)));
+ }
TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext();
linkTemplateContext.set("class", imageClass);
@@ -72,14 +78,27 @@ public class ImageLinkFilter implements Filter {
}
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
- double scale = 1;
- if ((imageWidth > maxWidth) || (imageHeight > maxHeight)) {
- scale = Math.min(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
+ if ("enlarge".equals(mode)) {
+ double scale = Math.max(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
+ linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
+ linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
+ if (scale >= 1) {
+ linkTemplateContext.set("left", String.format("%dpx", (int) ((imageWidth * scale) - maxWidth) / 2));
+ linkTemplateContext.set("top", String.format("%dpx", (int) ((imageHeight * scale) - maxHeight) / 2));
+ } else {
+ linkTemplateContext.set("left", String.format("%dpx", (int) (maxWidth - (imageWidth * scale)) / 2));
+ linkTemplateContext.set("top", String.format("%dpx", (int) (maxHeight - (imageHeight * scale)) / 2));
+ }
+ } else {
+ double scale = 1;
+ if ((imageWidth > maxWidth) || (imageHeight > maxHeight)) {
+ scale = Math.min(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
+ }
+ linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
+ linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
}
- linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
- linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
- linkTemplateContext.set("alt", image.getDescription());
- linkTemplateContext.set("title", image.getTitle());
+ linkTemplateContext.set("alt", Default.forNull(title, image.getDescription()));
+ linkTemplateContext.set("title", Default.forNull(title, image.getTitle()));
StringWriter stringWriter = new StringWriter();
linkTemplate.render(linkTemplateContext, stringWriter);