Skip to content

Commit fc70b08

Browse files
committed
Fix #114: Add orientation to outputs
1 parent 34c2efd commit fc70b08

13 files changed

+671
-130
lines changed

gradlew.bat

+92-92
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,92 @@
1-
@rem
2-
@rem Copyright 2015 the original author or authors.
3-
@rem
4-
@rem Licensed under the Apache License, Version 2.0 (the "License");
5-
@rem you may not use this file except in compliance with the License.
6-
@rem You may obtain a copy of the License at
7-
@rem
8-
@rem https://www.apache.org/licenses/LICENSE-2.0
9-
@rem
10-
@rem Unless required by applicable law or agreed to in writing, software
11-
@rem distributed under the License is distributed on an "AS IS" BASIS,
12-
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
@rem See the License for the specific language governing permissions and
14-
@rem limitations under the License.
15-
@rem
16-
17-
@if "%DEBUG%"=="" @echo off
18-
@rem ##########################################################################
19-
@rem
20-
@rem Gradle startup script for Windows
21-
@rem
22-
@rem ##########################################################################
23-
24-
@rem Set local scope for the variables with windows NT shell
25-
if "%OS%"=="Windows_NT" setlocal
26-
27-
set DIRNAME=%~dp0
28-
if "%DIRNAME%"=="" set DIRNAME=.
29-
@rem This is normally unused
30-
set APP_BASE_NAME=%~n0
31-
set APP_HOME=%DIRNAME%
32-
33-
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
34-
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
35-
36-
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
37-
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
38-
39-
@rem Find java.exe
40-
if defined JAVA_HOME goto findJavaFromJavaHome
41-
42-
set JAVA_EXE=java.exe
43-
%JAVA_EXE% -version >NUL 2>&1
44-
if %ERRORLEVEL% equ 0 goto execute
45-
46-
echo.
47-
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
48-
echo.
49-
echo Please set the JAVA_HOME variable in your environment to match the
50-
echo location of your Java installation.
51-
52-
goto fail
53-
54-
:findJavaFromJavaHome
55-
set JAVA_HOME=%JAVA_HOME:"=%
56-
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
57-
58-
if exist "%JAVA_EXE%" goto execute
59-
60-
echo.
61-
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
62-
echo.
63-
echo Please set the JAVA_HOME variable in your environment to match the
64-
echo location of your Java installation.
65-
66-
goto fail
67-
68-
:execute
69-
@rem Setup the command line
70-
71-
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
72-
73-
74-
@rem Execute Gradle
75-
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
76-
77-
:end
78-
@rem End local scope for the variables with windows NT shell
79-
if %ERRORLEVEL% equ 0 goto mainEnd
80-
81-
:fail
82-
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83-
rem the _cmd.exe /c_ return code!
84-
set EXIT_CODE=%ERRORLEVEL%
85-
if %EXIT_CODE% equ 0 set EXIT_CODE=1
86-
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
87-
exit /b %EXIT_CODE%
88-
89-
:mainEnd
90-
if "%OS%"=="Windows_NT" endlocal
91-
92-
:omega
1+
@rem
2+
@rem Copyright 2015 the original author or authors.
3+
@rem
4+
@rem Licensed under the Apache License, Version 2.0 (the "License");
5+
@rem you may not use this file except in compliance with the License.
6+
@rem You may obtain a copy of the License at
7+
@rem
8+
@rem https://www.apache.org/licenses/LICENSE-2.0
9+
@rem
10+
@rem Unless required by applicable law or agreed to in writing, software
11+
@rem distributed under the License is distributed on an "AS IS" BASIS,
12+
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
@rem See the License for the specific language governing permissions and
14+
@rem limitations under the License.
15+
@rem
16+
17+
@if "%DEBUG%"=="" @echo off
18+
@rem ##########################################################################
19+
@rem
20+
@rem Gradle startup script for Windows
21+
@rem
22+
@rem ##########################################################################
23+
24+
@rem Set local scope for the variables with windows NT shell
25+
if "%OS%"=="Windows_NT" setlocal
26+
27+
set DIRNAME=%~dp0
28+
if "%DIRNAME%"=="" set DIRNAME=.
29+
@rem This is normally unused
30+
set APP_BASE_NAME=%~n0
31+
set APP_HOME=%DIRNAME%
32+
33+
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
34+
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
35+
36+
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
37+
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
38+
39+
@rem Find java.exe
40+
if defined JAVA_HOME goto findJavaFromJavaHome
41+
42+
set JAVA_EXE=java.exe
43+
%JAVA_EXE% -version >NUL 2>&1
44+
if %ERRORLEVEL% equ 0 goto execute
45+
46+
echo.
47+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
48+
echo.
49+
echo Please set the JAVA_HOME variable in your environment to match the
50+
echo location of your Java installation.
51+
52+
goto fail
53+
54+
:findJavaFromJavaHome
55+
set JAVA_HOME=%JAVA_HOME:"=%
56+
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
57+
58+
if exist "%JAVA_EXE%" goto execute
59+
60+
echo.
61+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
62+
echo.
63+
echo Please set the JAVA_HOME variable in your environment to match the
64+
echo location of your Java installation.
65+
66+
goto fail
67+
68+
:execute
69+
@rem Setup the command line
70+
71+
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
72+
73+
74+
@rem Execute Gradle
75+
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
76+
77+
:end
78+
@rem End local scope for the variables with windows NT shell
79+
if %ERRORLEVEL% equ 0 goto mainEnd
80+
81+
:fail
82+
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83+
rem the _cmd.exe /c_ return code!
84+
set EXIT_CODE=%ERRORLEVEL%
85+
if %EXIT_CODE% equ 0 set EXIT_CODE=1
86+
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
87+
exit /b %EXIT_CODE%
88+
89+
:mainEnd
90+
if "%OS%"=="Windows_NT" endlocal
91+
92+
:omega

src/main/java/uk/org/okapibarcode/output/Java2DRenderer.java

+18
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,28 @@ public class Java2DRenderer implements SymbolRenderer {
6767
* @param ink the ink (foreground) color
6868
*/
6969
public Java2DRenderer(Graphics2D g2d, double magnification, Color paper, Color ink) {
70+
this(g2d, magnification, paper, ink, 0);
71+
}
72+
73+
/**
74+
* Creates a new Java 2D renderer with the specified orientation. If the specified paper color is {@code null}, the symbol is drawn without clearing the
75+
* existing {@code g2d} background.
76+
*
77+
* @param g2d the graphics to render to
78+
* @param magnification the magnification factor to apply
79+
* @param paper the paper (background) color (may be {@code null})
80+
* @param ink the ink (foreground) color
81+
* @param orientation the orientation of the symbol (0, 90, 180, or 270)
82+
*/
83+
public Java2DRenderer(Graphics2D g2d, double magnification, Color paper, Color ink, int orientation) {
7084
this.g2d = g2d;
7185
this.magnification = magnification;
7286
this.paper = paper;
7387
this.ink = ink;
88+
orientation = normalizeOrientation(orientation);
89+
if (orientation != 0) {
90+
g2d.rotate(Math.toRadians(orientation));
91+
}
7492
}
7593

7694
/** {@inheritDoc} */

src/main/java/uk/org/okapibarcode/output/PostScriptRenderer.java

+30
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class PostScriptRenderer implements SymbolRenderer {
5252
/** The ink (foreground) color. */
5353
private final Color ink;
5454

55+
/** The orientation of the symbol. */
56+
private final int orientation;
57+
5558
/**
5659
* Creates a new PostScript renderer.
5760
*
@@ -61,10 +64,24 @@ public class PostScriptRenderer implements SymbolRenderer {
6164
* @param ink the ink (foreground) color
6265
*/
6366
public PostScriptRenderer(OutputStream out, double magnification, Color paper, Color ink) {
67+
this(out, magnification, paper, ink, 0);
68+
}
69+
70+
/**
71+
* Creates a new PostScript renderer with the specified orientation.
72+
*
73+
* @param out the output stream to render to
74+
* @param magnification the magnification factor to apply
75+
* @param paper the paper (background) color
76+
* @param ink the ink (foreground) color
77+
* @param orientation the orientation of the symbol (0, 90, 180, or 270)
78+
*/
79+
public PostScriptRenderer(OutputStream out, double magnification, Color paper, Color ink, int orientation) {
6480
this.out = out;
6581
this.magnification = magnification;
6682
this.paper = paper;
6783
this.ink = ink;
84+
this.orientation = normalizeOrientation(orientation);
6885
}
6986

7087
/** {@inheritDoc} */
@@ -104,6 +121,14 @@ public void render(Symbol symbol) throws IOException {
104121
writer.append("/TR { newpath 4 1 roll exch moveto 1 index 0 rlineto 0 exch rlineto neg 0 rlineto closepath fill } bind def\n");
105122
writer.append("/TE { pop pop } bind def\n");
106123

124+
// Set orientation
125+
if (orientation != 0) {
126+
writer.append("gsave\n");
127+
writer.append(width/2).append(" ").append(height/2).append(" translate\n");
128+
writer.append(orientation).append(" rotate\n");
129+
writer.append(-width/2).append(" ").append(-height/2).append(" translate\n");
130+
}
131+
107132
// Background
108133
writer.append("newpath\n");
109134
writer.append(ink.red / 255.0).append(" ")
@@ -235,6 +260,11 @@ public void render(Symbol symbol) throws IOException {
235260
writer.append(" TH\n");
236261
}
237262

263+
// Restore original transformation if rotated
264+
if (orientation != 0) {
265+
writer.append("grestore\n");
266+
}
267+
238268
// Footer
239269
writer.append("\nshowpage\n");
240270
}

src/main/java/uk/org/okapibarcode/output/SvgRenderer.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public class SvgRenderer implements SymbolRenderer {
6767

6868
/** Whether or not to include the XML prolog in the output. */
6969
private final boolean xmlProlog;
70+
71+
/** The orientation of the symbol. */
72+
private final int orientation;
7073

7174
/**
7275
* Creates a new SVG renderer.
@@ -79,11 +82,27 @@ public class SvgRenderer implements SymbolRenderer {
7982
* standalone SVG documents, {@code false} for SVG content embedded directly in HTML documents)
8083
*/
8184
public SvgRenderer(OutputStream out, double magnification, Color paper, Color ink, boolean xmlProlog) {
85+
this(out, magnification, paper, ink, xmlProlog, 0);
86+
}
87+
88+
/**
89+
* Creates a new SVG renderer with the specified orientation.
90+
*
91+
* @param out the output stream to render to
92+
* @param magnification the magnification factor to apply
93+
* @param paper the paper (background) color
94+
* @param ink the ink (foreground) color
95+
* @param xmlProlog whether or not to include the XML prolog in the output (usually {@code true} for
96+
* standalone SVG documents, {@code false} for SVG content embedded directly in HTML documents)
97+
* @param orientation the orientation of the symbol (0, 90, 180, or 270)
98+
*/
99+
public SvgRenderer(OutputStream out, double magnification, Color paper, Color ink, boolean xmlProlog, int orientation) {
82100
this.out = out;
83101
this.magnification = magnification;
84102
this.paper = paper;
85103
this.ink = ink;
86104
this.xmlProlog = xmlProlog;
105+
this.orientation = normalizeOrientation(orientation);
87106
}
88107

89108
/** {@inheritDoc} */
@@ -126,7 +145,17 @@ public void render(Symbol symbol) throws IOException {
126145
.append("\" version=\"1.1")
127146
.append("\" xmlns=\"http://www.w3.org/2000/svg\">\n");
128147
writer.append(" <desc>").append(clean(title)).append("</desc>\n");
129-
writer.append(" <g id=\"barcode\" fill=\"#").append(fgColour).append("\">\n");
148+
149+
String transform = "";
150+
if (orientation == 90) {
151+
transform = " transform=\"rotate(90," + (width/2) + "," + (height/2) + ")\"";
152+
} else if (orientation == 180) {
153+
transform = " transform=\"rotate(180," + (width/2) + "," + (height/2) + ")\"";
154+
} else if (orientation == 270) {
155+
transform = " transform=\"rotate(270," + (width/2) + "," + (height/2) + ")\"";
156+
}
157+
158+
writer.append(" <g id=\"barcode\" fill=\"#").append(fgColour).append("\"").append(transform).append(">\n");
130159
writer.append(" <rect x=\"0\" y=\"0\" width=\"").appendInt(width)
131160
.append("\" height=\"").appendInt(height)
132161
.append("\" fill=\"#").append(bgColour).append("\" />\n");

src/main/java/uk/org/okapibarcode/output/SymbolRenderer.java

+25
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,29 @@ public interface SymbolRenderer {
3333
*/
3434
void render(Symbol symbol) throws IOException;
3535

36+
/**
37+
* Normalizes orientation values to 0, 90, 180, or 270 degrees.
38+
*
39+
* @param orientation the orientation to normalize
40+
* @return the normalized orientation (0, 90, 180, or 270)
41+
* @throws IllegalArgumentException if the orientation is not valid
42+
*/
43+
default int normalizeOrientation(int orientation) {
44+
switch (orientation) {
45+
case 0:
46+
return 0;
47+
case 90:
48+
case -270:
49+
return 90;
50+
case 180:
51+
case -180:
52+
return 180;
53+
case 270:
54+
case -90:
55+
return 270;
56+
default:
57+
throw new IllegalArgumentException(
58+
"Orientation must be 0, 90, 180, 270, -90, -180 or -270");
59+
}
60+
}
3661
}

0 commit comments

Comments
 (0)