Skip to content

Commit 7bd4291

Browse files
FOP-3135: Allow source and target resolution configuration by João André Gonçalves
1 parent 7e516d7 commit 7bd4291

File tree

10 files changed

+299
-8
lines changed

10 files changed

+299
-8
lines changed

Diff for: batik-bridge/pom.xml

+12
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@
106106
<version>${rhino.version}</version>
107107
<optional>true</optional>
108108
</dependency>
109+
<dependency>
110+
<groupId>junit</groupId>
111+
<artifactId>junit</artifactId>
112+
<version>${junit.version}</version>
113+
<scope>test</scope>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.mockito</groupId>
117+
<artifactId>mockito-core</artifactId>
118+
<version>2.28.2</version>
119+
<scope>test</scope>
120+
</dependency>
109121
</dependencies>
110122

111123
<build>

Diff for: batik-bridge/src/main/java/org/apache/batik/bridge/UserAgent.java

+8
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,12 @@ void checkLoadExternalResource(ParsedURL resourceURL,
309309

310310
/** Returns the Font Family Resolver */
311311
FontFamilyResolver getFontFamilyResolver();
312+
313+
float getTargetResolution();
314+
315+
float getSourceResolution();
316+
317+
void setTargetResolution(float targetResolution);
318+
319+
void setSourceResolution(float sourceResolution);
312320
}

Diff for: batik-bridge/src/main/java/org/apache/batik/bridge/UserAgentAdapter.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3232
import org.apache.batik.util.SVGFeatureStrings;
3333
import org.apache.batik.util.XMLResourceDescriptor;
3434

35+
import org.apache.xmlgraphics.util.UnitConv;
3536
import org.w3c.dom.Element;
3637
import org.w3c.dom.svg.SVGAElement;
3738
import org.w3c.dom.svg.SVGDocument;
@@ -52,6 +53,10 @@ public class UserAgentAdapter implements UserAgent {
5253
*/
5354
protected BridgeContext ctx;
5455

56+
private float sourceResolution = 96;
57+
58+
private float targetResolution = UnitConv.IN2PT;
59+
5560
/**
5661
* Sets the BridgeContext to be used for error information.
5762
*/
@@ -125,7 +130,7 @@ public boolean showConfirm(String message) {
125130
* Returns the size of a px CSS unit in millimeters.
126131
*/
127132
public float getPixelUnitToMillimeter() {
128-
return 0.26458333333333333333333333333333f; // 96dpi
133+
return UnitConv.IN2MM / getSourceResolution();
129134
}
130135

131136
/**
@@ -149,8 +154,7 @@ public String getDefaultFontFamily() {
149154
* Returns the medium font size.
150155
*/
151156
public float getMediumFontSize() {
152-
// 9pt (72pt = 1in)
153-
return 9f * 25.4f / (72f * getPixelUnitToMillimeter());
157+
return 9f * UnitConv.IN2MM / (getTargetResolution() * getPixelUnitToMillimeter());
154158
}
155159

156160
/**
@@ -464,4 +468,16 @@ public void loadDocument(String url) {
464468
public FontFamilyResolver getFontFamilyResolver() {
465469
return DefaultFontFamilyResolver.SINGLETON;
466470
}
471+
472+
public float getSourceResolution() { return sourceResolution; }
473+
474+
public float getTargetResolution() { return targetResolution; }
475+
476+
public void setSourceResolution(float sourceResolution) {
477+
this.sourceResolution = sourceResolution;
478+
}
479+
480+
public void setTargetResolution(float targetResolution) {
481+
this.targetResolution = targetResolution;
482+
}
467483
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
*/
19+
package org.apache.batik.bridge;
20+
21+
import org.apache.xmlgraphics.util.UnitConv;
22+
import org.junit.Test;
23+
24+
import static org.junit.Assert.assertEquals;
25+
26+
public class UserAgentAdapterTestCase {
27+
28+
@Test
29+
public void testEqualResolution_72() {
30+
checkGetMediumFontSize(72f, 72f, 9f);
31+
}
32+
33+
@Test
34+
public void testEqualResolution_96() {
35+
checkGetMediumFontSize(96f, 96f, 9f);
36+
}
37+
38+
@Test
39+
public void testDiffResolution_72_96() {
40+
checkGetMediumFontSize(72f, 96f, 6.74f);
41+
}
42+
43+
@Test
44+
public void testDiffResolution_96_72() {
45+
checkGetMediumFontSize(96f, 72f, 12f);
46+
}
47+
48+
@Test
49+
public void testPixelMM_72() {
50+
checkGetPixelUnitToMillimeter(72f, 72f);
51+
}
52+
53+
@Test
54+
public void testPixelMM_96() { checkGetPixelUnitToMillimeter(96f, 96f); }
55+
56+
@Test
57+
public void testPixelMM_72_96() { checkGetPixelUnitToMillimeter(72f, 96f); }
58+
59+
@Test
60+
public void testPixelMM_96_72() {
61+
checkGetPixelUnitToMillimeter(96f, 72f);
62+
}
63+
64+
private void checkGetMediumFontSize(float sourceRes, float targetRes, float expectedSize) {
65+
UserAgentAdapter adapter = new UserAgentAdapter();
66+
adapter.setSourceResolution(sourceRes);
67+
adapter.setTargetResolution(targetRes);
68+
69+
// Size must be calculated based on the dpi settings
70+
assertEquals(expectedSize, adapter.getMediumFontSize(), 0.01);
71+
}
72+
73+
private void checkGetPixelUnitToMillimeter(float sourceRes, float targetRes) {
74+
UserAgentAdapter adapter = new UserAgentAdapter();
75+
adapter.setSourceResolution(sourceRes);
76+
adapter.setTargetResolution(targetRes);
77+
78+
// Pixel unit to mm must be calculated using the resolution set in the conf
79+
// instead of assuming what the resolution is
80+
assertEquals(UnitConv.IN2MM / sourceRes, adapter.getPixelUnitToMillimeter(), 0.01);
81+
}
82+
}

Diff for: batik-svgbrowser/src/main/java/org/apache/batik/apps/svgbrowser/JSVGViewerFrame.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
145145
import org.apache.batik.util.resources.ResourceManager;
146146
import org.apache.batik.xml.XMLUtilities;
147147

148+
import org.apache.xmlgraphics.util.UnitConv;
148149
import org.w3c.dom.Element;
149150
import org.w3c.dom.Node;
150151
import org.w3c.dom.css.ViewCSS;
@@ -2744,6 +2745,10 @@ public void updateFailed(UpdateManagerEvent e) {
27442745
*/
27452746
protected class UserAgent implements SVGUserAgent {
27462747

2748+
private float sourceResolution = 96;
2749+
2750+
private float targetResolution = UnitConv.IN2PT;
2751+
27472752
/**
27482753
* Creates a new SVGUserAgent.
27492754
*/
@@ -2817,7 +2822,7 @@ public boolean showConfirm(String message) {
28172822
* Returns the size of a px CSS unit in millimeters.
28182823
*/
28192824
public float getPixelUnitToMillimeter() {
2820-
return 0.26458333333333333333333333333333f; // 96dpi
2825+
return UnitConv.IN2MM / getSourceResolution();
28212826
}
28222827

28232828
/**
@@ -2842,7 +2847,7 @@ public String getDefaultFontFamily() {
28422847
*/
28432848
public float getMediumFontSize() {
28442849
// 9pt (72pt == 1in)
2845-
return 9f * 25.4f / (72f * getPixelUnitToMillimeter());
2850+
return 9f * UnitConv.IN2MM / (getTargetResolution() * getPixelUnitToMillimeter());
28462851
}
28472852

28482853
/**
@@ -3078,6 +3083,26 @@ public void checkLoadScript(String scriptType,
30783083
s.checkLoadExternalResource();
30793084
}
30803085
}
3086+
3087+
@Override
3088+
public float getTargetResolution() {
3089+
return targetResolution;
3090+
}
3091+
3092+
@Override
3093+
public float getSourceResolution() {
3094+
return sourceResolution;
3095+
}
3096+
3097+
@Override
3098+
public void setTargetResolution(float targetResolution) {
3099+
this.targetResolution = targetResolution;
3100+
}
3101+
3102+
@Override
3103+
public void setSourceResolution(float sourceResolution) {
3104+
this.sourceResolution = sourceResolution;
3105+
}
30813106
}
30823107

30833108
/**

Diff for: batik-swing/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@
9595
<artifactId>xml-apis-ext</artifactId>
9696
<version>${xmlapisext.version}</version>
9797
</dependency>
98+
<dependency>
99+
<groupId>junit</groupId>
100+
<artifactId>junit</artifactId>
101+
<version>${junit.version}</version>
102+
<scope>test</scope>
103+
</dependency>
98104
</dependencies>
99105

100106
<build>

Diff for: batik-swing/src/main/java/org/apache/batik/swing/svg/JSVGComponent.java

+32
Original file line numberDiff line numberDiff line change
@@ -3097,6 +3097,22 @@ public void loadDocument(String url) {
30973097
public FontFamilyResolver getFontFamilyResolver() {
30983098
return userAgent.getFontFamilyResolver();
30993099
}
3100+
3101+
public float getSourceResolution() {
3102+
return userAgent.getSourceResolution();
3103+
}
3104+
3105+
public float getTargetResolution() {
3106+
return userAgent.getTargetResolution();
3107+
}
3108+
3109+
public void setSourceResolution(float sourceResolution) {
3110+
userAgent.setSourceResolution(sourceResolution);
3111+
}
3112+
3113+
public void setTargetResolution(float targetResolution) {
3114+
userAgent.setTargetResolution(targetResolution);
3115+
}
31003116
}
31013117

31023118
/**
@@ -3705,6 +3721,22 @@ public void loadDocument(String url) {
37053721
public FontFamilyResolver getFontFamilyResolver() {
37063722
return DefaultFontFamilyResolver.SINGLETON;
37073723
}
3724+
3725+
public float getSourceResolution() {
3726+
return svgUserAgent.getSourceResolution();
3727+
}
3728+
3729+
public float getTargetResolution() {
3730+
return svgUserAgent.getTargetResolution();
3731+
}
3732+
3733+
public void setSourceResolution(float sourceResolution) {
3734+
svgUserAgent.setSourceResolution(sourceResolution);
3735+
}
3736+
3737+
public void setTargetResolution(float targetResolution) {
3738+
svgUserAgent.setTargetResolution(targetResolution);
3739+
}
37083740
}
37093741

37103742
protected static final Set FEATURES = new HashSet();

Diff for: batik-swing/src/main/java/org/apache/batik/swing/svg/SVGUserAgent.java

+7
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,11 @@ void checkLoadScript(String scriptType,
228228
void checkLoadExternalResource(ParsedURL resourceURL,
229229
ParsedURL docURL) throws SecurityException;
230230

231+
float getTargetResolution();
232+
233+
float getSourceResolution();
234+
235+
void setTargetResolution(float targetResolution);
236+
237+
void setSourceResolution(float sourceResolution);
231238
}

Diff for: batik-swing/src/main/java/org/apache/batik/swing/svg/SVGUserAgentAdapter.java

+24-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2525
import org.apache.batik.bridge.RelaxedScriptSecurity;
2626
import org.apache.batik.bridge.ScriptSecurity;
2727
import org.apache.batik.util.ParsedURL;
28+
import org.apache.xmlgraphics.util.UnitConv;
2829
import org.w3c.dom.Element;
2930

3031
/*
@@ -50,6 +51,11 @@ Licensed to the Apache Software Foundation (ASF) under one or more
5051
* @version $Id$
5152
*/
5253
public class SVGUserAgentAdapter implements SVGUserAgent {
54+
55+
private float sourceResolution = 96;
56+
57+
private float targetResolution = UnitConv.IN2PT;
58+
5359
public SVGUserAgentAdapter() { }
5460

5561
/**
@@ -106,7 +112,7 @@ public boolean showConfirm(String message) {
106112
* Returns the size of a px CSS unit in millimeters.
107113
*/
108114
public float getPixelUnitToMillimeter() {
109-
return 0.26458333333333333333333333333333f; // 96dpi
115+
return UnitConv.IN2MM / getSourceResolution();
110116
}
111117

112118
/**
@@ -130,8 +136,7 @@ public String getDefaultFontFamily() {
130136
* Returns the medium font size.
131137
*/
132138
public float getMediumFontSize() {
133-
// 9pt (72pt == 1in)
134-
return 9f * 25.4f / (72f * getPixelUnitToMillimeter());
139+
return 9f * UnitConv.IN2MM / (getTargetResolution() * getPixelUnitToMillimeter());
135140
}
136141

137142
/**
@@ -350,4 +355,20 @@ public void checkLoadScript(String scriptType,
350355
s.checkLoadExternalResource();
351356
}
352357
}
358+
359+
public float getSourceResolution() {
360+
return sourceResolution;
361+
}
362+
363+
public float getTargetResolution() {
364+
return targetResolution;
365+
}
366+
367+
public void setSourceResolution(float sourceResolution) {
368+
this.sourceResolution = sourceResolution;
369+
}
370+
371+
public void setTargetResolution(float targetResolution) {
372+
this.targetResolution = targetResolution;
373+
}
353374
}

0 commit comments

Comments
 (0)