X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FImageLinkFilter.java;h=4742ae35a07d17ec001e98d9504094f730c113a2;hp=3d6c731ac995ff4e46e26b412732ef0a3bff1094;hb=62573c314957b1851f4fbe693b8746686caa940a;hpb=0df5e91852f737d760c5a9f54c5667309fbadcc2 diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java index 3d6c731..4742ae3 100644 --- a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java @@ -1,5 +1,5 @@ /* - * Sone - ImageLinkFilter.java - Copyright © 2011 David Roden + * Sone - ImageLinkFilter.java - Copyright © 2011–2016 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,28 +17,36 @@ package net.pterodactylus.sone.template; +import static java.lang.Integer.MAX_VALUE; +import static java.lang.String.valueOf; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + import java.io.StringReader; import java.io.StringWriter; import java.util.Map; +import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Image; -import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Filter; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.template.TemplateContextFactory; import net.pterodactylus.util.template.TemplateParser; +import com.google.common.base.Function; +import com.google.common.base.Optional; + /** * Template filter that turns an {@link Image} into an HTML <img> tag, * using some parameters to influence parameters of the image. - * - * @author David ‘Bombe’ Roden */ 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 core. */ + private final Core core; /** The template context factory. */ private final TemplateContextFactory templateContextFactory; @@ -46,10 +54,13 @@ public class ImageLinkFilter implements Filter { /** * Creates a new image link filter. * + * @param core + * The core * @param templateContextFactory * The template context factory */ - public ImageLinkFilter(TemplateContextFactory templateContextFactory) { + public ImageLinkFilter(Core core, TemplateContextFactory templateContextFactory) { + this.core = core; this.templateContextFactory = templateContextFactory; } @@ -57,11 +68,21 @@ public class ImageLinkFilter implements Filter { * {@inheritDoc} */ @Override - public Object format(TemplateContext templateContext, Object data, Map parameters) { - Image image = (Image) data; - 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); + public Object format(TemplateContext templateContext, Object data, Map parameters) { + Image image = null; + if (data instanceof String) { + image = core.getImage((String) data, false); + } else if (data instanceof Image) { + image = (Image) data; + } + if (image == null) { + return null; + } + String imageClass = Optional.fromNullable(parameters.get("class")).transform(getStringValue()).orNull(); + int maxWidth = parseInt(valueOf(parameters.get("max-width")), MAX_VALUE); + int maxHeight = parseInt(valueOf(parameters.get("max-height")), MAX_VALUE); + String mode = valueOf(parameters.get("mode")); + String title = Optional.fromNullable(parameters.get("title")).transform(getStringValue()).orNull(); TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext(); linkTemplateContext.set("class", imageClass); @@ -72,18 +93,35 @@ 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)); + 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", Optional.fromNullable(title).or(image.getDescription())); + linkTemplateContext.set("title", Optional.fromNullable(title).or(image.getTitle())); StringWriter stringWriter = new StringWriter(); linkTemplate.render(linkTemplateContext, stringWriter); return stringWriter.toString(); } + private Function getStringValue() { + return new Function() { + @Override + public String apply(Object input) { + return (input != null) ? input.toString() : null; + } + }; + } + }