Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions QRCoder/PostscriptQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,17 @@ public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool d
var drawableModulesCount = QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : offset * 2);
var pointsPerModule = (double)Math.Min(viewBox.Width, viewBox.Height) / (double)drawableModulesCount;

var estimatedCapacity = PS_HEADER.Length + PS_FUNCTIONS.Length + PS_FOOTER.Length +
var header = epsFormat ? EPS_HEADER : PS_HEADER;
var footer = epsFormat ? EPS_FOOTER : PS_FOOTER;

var estimatedCapacity = header.Length + PS_FUNCTIONS.Length + footer.Length +
(drawableModulesCount * drawableModulesCount * 2) + // modules (either "f " or "b ")
drawableModulesCount * 3 + // newlines ("nl\n")
200; // embedded numbers
var sb = new StringBuilder(estimatedCapacity);

sb.AppendFormat(CultureInfo.InvariantCulture, PS_HEADER, [
CleanSvgVal(viewBox.Width), CleanSvgVal(pointsPerModule),
epsFormat ? "EPSF-3.0" : string.Empty
sb.AppendFormat(CultureInfo.InvariantCulture, header, [
CleanSvgVal(viewBox.Width), CleanSvgVal(pointsPerModule)
]);
Comment thread
Shane32 marked this conversation as resolved.
sb.AppendFormat(CultureInfo.InvariantCulture, PS_FUNCTIONS, [
CleanSvgVal(darkColor.R /255.0), CleanSvgVal(darkColor.G /255.0), CleanSvgVal(darkColor.B /255.0),
Expand All @@ -127,7 +129,7 @@ public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool d
}
}
sb.Append('\n');
sb.Append(PS_FOOTER);
sb.Append(footer);
return sb.ToString();
}

Expand All @@ -140,15 +142,17 @@ public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool d

// Note: line terminations here will encode differently based on which platform QRCoder was compiled on (CRLF vs LF);
// however, PostScript interpreters should handle both equally well.

// Standard PostScript header — includes media/page setup and setpagedevice (valid for standalone PS documents).
private const string PS_HEADER = """
%!PS-Adobe-3.0 {2}
%!PS-Adobe-3.0
%%Creator: QRCoder.NET
%%Title: QRCode
%%DocumentData: Clean7Bit
%%Origin: 0
%%DocumentMedia: Default {0} {0} 0 () ()
%%BoundingBox: 0 0 {0} {0}
%%LanguageLevel: 2
%%LanguageLevel: 2
%%Pages: 1
%%Page: 1 1
%%EndComments
Expand All @@ -162,8 +166,26 @@ public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool d

""";

// EPS header — omits %%DocumentMedia, %%Pages, %%Page, and setpagedevice, all of which are
// forbidden or inappropriate in Encapsulated PostScript (Adobe Technical Note #5002).
private const string EPS_HEADER = """
%!PS-Adobe-3.0 EPSF-3.0
%%Creator: QRCoder.NET
%%Title: QRCode
%%DocumentData: Clean7Bit
%%Origin: 0
%%BoundingBox: 0 0 {0} {0}
%%LanguageLevel: 2
%%EndComments
%%BeginConstants
/sz {0} def
/sc {1} def
%%EndConstants

""";

private const string PS_FUNCTIONS = """
%%BeginFunctions
%%BeginFunctions
/csquare {{
newpath
0 0 moveto
Expand All @@ -174,15 +196,15 @@ 1 0 rlineto
setrgbcolor
fill
}} def
/f {{
/f {{
{0} {1} {2} csquare
1 0 translate
}} def
/b {{
/b {{
1 0 translate
}} def
/background {{
{3} {4} {5} csquare
}} def
/background {{
{3} {4} {5} csquare
}} def
/nl {{
-{6} -1 translate
Expand All @@ -200,10 +222,19 @@ sc sc scale

""";

// Standard PostScript footer — showpage is required for standalone PS documents.
private const string PS_FOOTER = """
%%EndBody
grestore
showpage
showpage
%%EOF

""";

// EPS footer — showpage is forbidden in EPS files (Adobe Technical Note #5002).
private const string EPS_FOOTER = """
%%EndBody
grestore
%%EOF

""";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
%!PS-Adobe-3.0
%!PS-Adobe-3.0
%%Creator: QRCoder.NET
%%Title: QRCode
%%DocumentData: Clean7Bit
%%Origin: 0
%%DocumentMedia: Default 165 165 0 () ()
%%BoundingBox: 0 0 165 165
%%LanguageLevel: 2
%%LanguageLevel: 2
%%Pages: 1
%%Page: 1 1
%%EndComments
Expand All @@ -16,7 +16,7 @@
%%BeginFeature: *PageSize Default
<< /PageSize [ sz sz ] /ImagingBBox null >> setpagedevice
%%EndFeature
%%BeginFunctions
%%BeginFunctions
/csquare {
newpath
0 0 moveto
Expand All @@ -27,15 +27,15 @@
setrgbcolor
fill
} def
/f {
/f {
1 0 0 csquare
1 0 translate
} def
/b {
/b {
1 0 translate
} def
/background {
0 0 1 csquare
} def
/background {
0 0 1 csquare
} def
/nl {
-33 -1 translate
Expand Down Expand Up @@ -85,5 +85,5 @@ b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b nl
b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b
%%EndBody
grestore
showpage
showpage
%%EOF
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@
%%Title: QRCode
%%DocumentData: Clean7Bit
%%Origin: 0
%%DocumentMedia: Default 165 165 0 () ()
%%BoundingBox: 0 0 165 165
%%LanguageLevel: 2
%%Pages: 1
%%Page: 1 1
%%LanguageLevel: 2
%%EndComments
%%BeginConstants
/sz 165 def
/sc 5 def
%%EndConstants
%%BeginFeature: *PageSize Default
<< /PageSize [ sz sz ] /ImagingBBox null >> setpagedevice
%%EndFeature
%%BeginFunctions
%%BeginFunctions
/csquare {
newpath
0 0 moveto
Expand All @@ -27,15 +21,15 @@
setrgbcolor
fill
} def
/f {
/f {
0 0 0 csquare
1 0 translate
} def
/b {
/b {
1 0 translate
} def
/background {
1 1 1 csquare
} def
/background {
1 1 1 csquare
} def
/nl {
-33 -1 translate
Expand Down Expand Up @@ -85,5 +79,4 @@ b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b nl
b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b
%%EndBody
grestore
showpage
%%EOF
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
%!PS-Adobe-3.0
%!PS-Adobe-3.0
%%Creator: QRCoder.NET
%%Title: QRCode
%%DocumentData: Clean7Bit
%%Origin: 0
%%DocumentMedia: Default 165 165 0 () ()
%%BoundingBox: 0 0 165 165
%%LanguageLevel: 2
%%LanguageLevel: 2
%%Pages: 1
%%Page: 1 1
%%EndComments
Expand All @@ -16,7 +16,7 @@
%%BeginFeature: *PageSize Default
<< /PageSize [ sz sz ] /ImagingBBox null >> setpagedevice
%%EndFeature
%%BeginFunctions
%%BeginFunctions
/csquare {
newpath
0 0 moveto
Expand All @@ -27,15 +27,15 @@
setrgbcolor
fill
} def
/f {
/f {
0 0 0 csquare
1 0 translate
} def
/b {
/b {
1 0 translate
} def
/background {
1 1 1 csquare
} def
/background {
1 1 1 csquare
} def
/nl {
-33 -1 translate
Expand Down Expand Up @@ -85,5 +85,5 @@ b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b nl
b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b
%%EndBody
grestore
showpage
showpage
%%EOF
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
%!PS-Adobe-3.0
%!PS-Adobe-3.0
%%Creator: QRCoder.NET
%%Title: QRCode
%%DocumentData: Clean7Bit
%%Origin: 0
%%DocumentMedia: Default 33 33 0 () ()
%%BoundingBox: 0 0 33 33
%%LanguageLevel: 2
%%LanguageLevel: 2
%%Pages: 1
%%Page: 1 1
%%EndComments
Expand All @@ -16,7 +16,7 @@
%%BeginFeature: *PageSize Default
<< /PageSize [ sz sz ] /ImagingBBox null >> setpagedevice
%%EndFeature
%%BeginFunctions
%%BeginFunctions
/csquare {
newpath
0 0 moveto
Expand All @@ -27,15 +27,15 @@
setrgbcolor
fill
} def
/f {
/f {
0 0 0 csquare
1 0 translate
} def
/b {
/b {
1 0 translate
} def
/background {
1 1 1 csquare
} def
/background {
1 1 1 csquare
} def
/nl {
-33 -1 translate
Expand Down Expand Up @@ -85,5 +85,5 @@ b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b nl
b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b
%%EndBody
grestore
showpage
showpage
%%EOF
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
%!PS-Adobe-3.0
%!PS-Adobe-3.0
%%Creator: QRCoder.NET
%%Title: QRCode
%%DocumentData: Clean7Bit
%%Origin: 0
%%DocumentMedia: Default 50 50 0 () ()
%%BoundingBox: 0 0 50 50
%%LanguageLevel: 2
%%LanguageLevel: 2
%%Pages: 1
%%Page: 1 1
%%EndComments
Expand All @@ -16,7 +16,7 @@
%%BeginFeature: *PageSize Default
<< /PageSize [ sz sz ] /ImagingBBox null >> setpagedevice
%%EndFeature
%%BeginFunctions
%%BeginFunctions
/csquare {
newpath
0 0 moveto
Expand All @@ -27,15 +27,15 @@
setrgbcolor
fill
} def
/f {
/f {
0 0 0 csquare
1 0 translate
} def
/b {
/b {
1 0 translate
} def
/background {
1 1 1 csquare
} def
/background {
1 1 1 csquare
} def
/nl {
-25 -1 translate
Expand Down Expand Up @@ -77,5 +77,5 @@ f b b b b b f b f f f f b f f b b f b b f f f b b nl
f f f f f f f b f b b b b f b b b f f b b b f f f
%%EndBody
grestore
showpage
showpage
%%EOF
Loading