jasperreports icon indicating copy to clipboard operation
jasperreports copied to clipboard

Images scaled with RetainShape are sometimes rendered too small

Open mfg92 opened this issue 5 months ago • 3 comments

I have noticed that some SVGs are rendered too small when using the RetainShape scaling option. I tried to find out what distinguishes the SVGs that are scaled correctly from those that are scaled too small. My conclusion is that if the optional attributes width and height in the root tag svg are missing, then the problem can occur. It seems that without these attributes, the scaling algorithm assumes that the image is a square, where each side has a length of MAX(viewBox.width, viewBox.height).

I think RetainShape should work the same regardless of whether width and height are in the root tag or not.

Examples: grafik grafik

Template of the example:

<!-- Created with Jaspersoft Studio version 7.0.0.final using JasperReports Library version 7.0.0-b478feaa9aab4375eba71de77b4ca138ad2f62aa  -->
<!-- 2024-09-23T14:00:01 -->
<jasperReport name="image" language="java" pageWidth="500" pageHeight="800" columnWidth="400" leftMargin="50" rightMargin="50" topMargin="0" bottomMargin="0" uuid="890c1c55-90c2-4100-bc34-807a0fe94789">
	<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
	<parameter name="horizontalBox" forPrompting="false" class="java.lang.String">
		<defaultValueExpression><![CDATA["PHN2ZyB3aWR0aD0iMzYiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAzNiAyNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxnPgogICAgICAgIDxyZWN0CiAgICAgICAgICAgICAgICB4PSIwLjUiIHk9IjAuNSIKICAgICAgICAgICAgICAgIHdpZHRoPSIzNSIgaGVpZ2h0PSIyMyIKICAgICAgICAgICAgICAgIHJ4PSI1IgogICAgICAgICAgICAgICAgZmlsbD0iI2ZmMCIgc3Ryb2tlPSIjZjQ4ZTAwIgogICAgICAgIC8+CiAgICA8L2c+Cjwvc3ZnPg=="]]></defaultValueExpression>
	</parameter>
	<parameter name="verticalBox" forPrompting="false" class="java.lang.String">
		<defaultValueExpression><![CDATA["PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMzYiIHZpZXdCb3g9IjAgMCAyNCAzNiIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICAgIDxnPgogICAgICAgIDxyZWN0CiAgICAgICAgICAgICAgICB4PSIwLjUiIHk9IjAuNSIKICAgICAgICAgICAgICAgIHdpZHRoPSIyMyIgaGVpZ2h0PSIzNSIKICAgICAgICAgICAgICAgIHJ4PSI1IgogICAgICAgICAgICAgICAgZmlsbD0iI2ZmMCIgc3Ryb2tlPSIjZjQ4ZTAwIgogICAgICAgIC8+CiAgICA8L2c+Cjwvc3ZnPg=="]]></defaultValueExpression>
	</parameter>
	<parameter name="image" forPrompting="false" class="java.lang.String">
		<defaultValueExpression><![CDATA[new String(org.apache.commons.codec.binary.Base64.decodeBase64($P{horizontalBox}.getBytes()), "UTF-8").replaceFirst("<svg.*?viewBox", "<svg viewBox")]]></defaultValueExpression>
	</parameter>
	<parameter name="image2" forPrompting="false" class="java.lang.String">
		<defaultValueExpression><![CDATA[new String(org.apache.commons.codec.binary.Base64.decodeBase64($P{horizontalBox}.getBytes()), "UTF-8")]]></defaultValueExpression>
	</parameter>
	<group name="Main">
		<groupHeader>
			<band height="800" splitType="Prevent">
				<element kind="staticText" uuid="3cd785d5-a435-4964-aee8-446203ac8b82" x="0" y="0" width="400" height="30" fontSize="20.0" bold="true" hTextAlign="Center">
					<text><![CDATA[only viewBox]]></text>
				</element>
				<element kind="textField" uuid="9048b3eb-76d9-43e4-af86-61b5d2402099" x="0" y="40" width="400" height="100" textAdjust="ScaleFont">
					<expression><![CDATA[$P{image}]]></expression>
				</element>
				<element kind="image" uuid="4a78d751-4ed2-41ce-8365-4305b44034ab" positionType="Float" x="0" y="150" width="200" height="100" scaleImage="RetainShape" hImageAlign="Center" vImageAlign="Top">
					<expression><![CDATA[new java.io.ByteArrayInputStream($P{image}.getBytes())]]></expression>
					<box>
						<pen lineWidth="2.0" lineStyle="Dashed"/>
					</box>
				</element>
				<element kind="image" uuid="e675f9a9-78cc-4b40-81e2-333a75f612ce" positionType="Float" x="250" y="150" width="100" height="200" scaleImage="RetainShape" hImageAlign="Center" vImageAlign="Top">
					<expression><![CDATA[new java.io.ByteArrayInputStream($P{image}.getBytes())]]></expression>
					<box>
						<pen lineWidth="2.0" lineStyle="Dashed"/>
					</box>
				</element>
				<element kind="staticText" uuid="29c991fb-e8a9-4910-832a-f4ec0c516784" x="0" y="400" width="400" height="30" fontSize="20.0" bold="true" hTextAlign="Center">
					<text><![CDATA[viewBox + width + height]]></text>
				</element>
				<element kind="textField" uuid="09d79785-ddfb-4719-ab91-56afee41fe94" x="0" y="440" width="400" height="100" textAdjust="ScaleFont">
					<expression><![CDATA[$P{image2}]]></expression>
				</element>
				<element kind="image" uuid="4a396040-4cfa-4f2a-8cdd-d8c14448a27e" positionType="Float" x="0" y="550" width="200" height="100" scaleImage="RetainShape" hImageAlign="Center" vImageAlign="Top">
					<expression><![CDATA[new java.io.ByteArrayInputStream($P{image2}.getBytes())]]></expression>
					<box>
						<pen lineWidth="2.0" lineStyle="Dashed"/>
					</box>
				</element>
				<element kind="image" uuid="e3a1e7f5-33b1-4ca6-a946-5fb9145f5c58" positionType="Float" x="250" y="550" width="100" height="200" scaleImage="RetainShape" hImageAlign="Center" vImageAlign="Top">
					<expression><![CDATA[new java.io.ByteArrayInputStream($P{image2}.getBytes())]]></expression>
					<box>
						<pen lineWidth="2.0" lineStyle="Dashed"/>
					</box>
				</element>
			</band>
		</groupHeader>
	</group>
</jasperReport>

mfg92 avatar Sep 23 '24 12:09 mfg92