| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.nodes; |
| 23 |
|
| 24 |
import java.awt.geom.AffineTransform; |
| 25 |
import java.io.IOException; |
| 26 |
import java.net.URI; |
| 27 |
import java.util.Optional; |
| 28 |
|
| 29 |
import org.jetbrains.annotations.NotNull; |
| 30 |
import org.jetbrains.annotations.Nullable; |
| 31 |
|
| 32 |
import com.github.weisj.jsvg.attributes.Overflow; |
| 33 |
import com.github.weisj.jsvg.attributes.PreserveAspectRatio; |
| 34 |
import com.github.weisj.jsvg.attributes.value.PercentageDimension; |
| 35 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 36 |
import com.github.weisj.jsvg.logging.Logger; |
| 37 |
import com.github.weisj.jsvg.logging.Logger.Level; |
| 38 |
import com.github.weisj.jsvg.logging.impl.LogFactory; |
| 39 |
import com.github.weisj.jsvg.nodes.prototype.spec.Category; |
| 40 |
import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories; |
| 41 |
import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent; |
| 42 |
import com.github.weisj.jsvg.parser.impl.AttributeNode; |
| 43 |
import com.github.weisj.jsvg.parser.impl.Url; |
| 44 |
import com.github.weisj.jsvg.parser.resources.RenderableResource; |
| 45 |
import com.github.weisj.jsvg.parser.resources.ResourceSupplier; |
| 46 |
import com.github.weisj.jsvg.parser.resources.impl.MissingImageResource; |
| 47 |
import com.github.weisj.jsvg.parser.resources.impl.ValueResourceSupplier; |
| 48 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 49 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 50 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 51 |
import com.github.weisj.jsvg.view.FloatSize; |
| 52 |
import com.github.weisj.jsvg.view.ViewBox; |
| 53 |
|
| 54 |
@ElementCategories({Category.Graphic, Category.GraphicsReferencing}) |
| 55 |
@PermittedContent( |
| 56 |
categories = {Category.Animation, Category.Descriptive} |
| 57 |
) |
| 58 |
public final class Image extends RenderableSVGNode { |
| 59 |
private static final Logger LOGGER = LogFactory.createLogger(Image.class); |
| 60 |
|
| 61 |
public static final String TAG = "image"; |
| 62 |
private Length x; |
| 63 |
private Length y; |
| 64 |
private Length width; |
| 65 |
private Length height; |
| 66 |
private PreserveAspectRatio preserveAspectRatio; |
| 67 |
private Overflow overflow; |
| 68 |
|
| 69 |
private ResourceSupplier<RenderableResource> imgResource; |
| 70 |
|
| 71 |
|
| 72 |
@Override |
| 73 |
public @NotNull String tagName() { |
| 74 |
return TAG; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public boolean isVisible(@NotNull RenderContext context) { |
| 79 |
return imgResource != null && super.isVisible(context); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void build(@NotNull AttributeNode attributeNode) { |
| 84 |
super.build(attributeNode); |
| 85 |
x = attributeNode.getLength("x", PercentageDimension.WIDTH, 0); |
| 86 |
y = attributeNode.getLength("y", PercentageDimension.HEIGHT, 0); |
| 87 |
width = attributeNode.getLength("width", PercentageDimension.WIDTH, Length.UNSPECIFIED); |
| 88 |
height = attributeNode.getLength("height", PercentageDimension.HEIGHT, Length.UNSPECIFIED); |
| 89 |
preserveAspectRatio = PreserveAspectRatio.parse( |
| 90 |
attributeNode.getValue("preserveAspectRatio"), attributeNode.parser()); |
| 91 |
overflow = attributeNode.getEnum("overflow", Overflow.Hidden); |
| 92 |
|
| 93 |
Url url = Url.parse(attributeNode.getHref(), Url.RequireFragment.NO); |
| 94 |
if (url == null) return; |
| 95 |
URI resolvedUri = attributeNode.resolveResourceURI(url.rawUrl()); |
| 96 |
if (resolvedUri == null) return; |
| 97 |
|
| 98 |
try { |
| 99 |
imgResource = attributeNode.resourceLoader().loadImage(attributeNode.document(), resolvedUri); |
| 100 |
} catch (IOException e) { |
| 101 |
LOGGER.log(Level.INFO, e.getMessage(), e); |
| 102 |
imgResource = null; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
private @Nullable RenderableResource fetchImage(@NotNull RenderContext context) { |
| 107 |
if (imgResource == null) return null; |
| 108 |
if (imgResource instanceof ValueResourceSupplier) { |
| 109 |
return ((ValueResourceSupplier<RenderableResource>) imgResource).get(); |
| 110 |
} |
| 111 |
Optional<@Nullable RenderableResource> optionalResource = imgResource.get(context.platformSupport()); |
| 112 |
if (!optionalResource.isPresent()) return null; |
| 113 |
RenderableResource resource = optionalResource.get(); |
| 114 |
imgResource = new ValueResourceSupplier<>(resource); |
| 115 |
return resource; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void render(@NotNull RenderContext context, @NotNull Output output) { |
| 120 |
RenderableResource resource = fetchImage(context); |
| 121 |
if (resource == null) { |
| 122 |
resource = new MissingImageResource(); |
| 123 |
} |
| 124 |
|
| 125 |
MeasureContext measure = context.measureContext(); |
| 126 |
FloatSize intrinsicResourceSize = resource.intrinsicSize(context); |
| 127 |
float resourceWidth = intrinsicResourceSize.width; |
| 128 |
float resourceHeight = intrinsicResourceSize.height; |
| 129 |
if (resourceWidth == 0 || resourceHeight == 0) return; |
| 130 |
|
| 131 |
float viewWidth = width.orElseIfUnspecified(resourceWidth).resolve(measure); |
| 132 |
float viewHeight = height.orElseIfUnspecified(resourceHeight).resolve(measure); |
| 133 |
|
| 134 |
output.translate(x.resolve(measure), y.resolve(measure)); |
| 135 |
|
| 136 |
if (overflow.establishesClip()) output.applyClip(new ViewBox(viewWidth, viewHeight)); |
| 137 |
|
| 138 |
|
| 139 |
AffineTransform imgTransform = preserveAspectRatio.computeViewportTransform( |
| 140 |
new FloatSize(viewWidth, viewHeight), |
| 141 |
new ViewBox(resourceWidth, resourceHeight)); |
| 142 |
|
| 143 |
resource.render(output, context, imgTransform); |
| 144 |
} |
| 145 |
} |