Skip to content

Commit 94f49b7

Browse files
committed
always write colors with 3 decimal places
1 parent b576096 commit 94f49b7

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

src/IxMilia.Pdf.Test/PdfWriterTests.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ 3 0 obj
5353
<</Type /Page /Parent 2 0 R /Contents 4 0 R /MediaBox [0 0 612.00 792.00] /Resources <<>>>>
5454
endobj
5555
4 0 obj
56-
<</Length 28>>
56+
<</Length 52>>
5757
stream
5858
0 w
59-
0 0 0 RG
60-
0 0 0 rg
59+
0.000 0.000 0.000 RG
60+
0.000 0.000 0.000 rg
6161
S
6262
endstream
6363
endobj
@@ -70,7 +70,7 @@ 0000000125 00000 n
7070
0000000235 00000 n
7171
trailer <</Size 5 /Root 1 0 R>>
7272
startxref
73-
315
73+
339
7474
%%EOF
7575
";
7676
AssertFileEquals(file, expected);
@@ -131,8 +131,8 @@ public void VerifyLineStrokeTest()
131131
};
132132
AssertPathBuilderContains(builder, @"
133133
0 w
134-
0 0 0 RG
135-
0 0 0 rg
134+
0.000 0.000 0.000 RG
135+
0.000 0.000 0.000 rg
136136
0.00 0.00 m
137137
1.00 1.00 l
138138
S
@@ -141,12 +141,12 @@ 2.00 2.00 m
141141
3.00 3.00 l
142142
S
143143
0 w
144-
1 0 0 RG
144+
1.000 0.000 0.000 RG
145145
4.00 4.00 m
146146
5.00 5.00 l
147147
S
148148
2.2 w
149-
0 1 0 RG
149+
0.000 1.000 0.000 RG
150150
6.00 6.00 m
151151
7.00 7.00 l
152152
S
@@ -176,8 +176,8 @@ public void VerifyFillTest()
176176
};
177177
AssertPathBuilderContains(builder, @"
178178
0 w
179-
0 0 0 RG
180-
0 0 0 rg
179+
0.000 0.000 0.000 RG
180+
0.000 0.000 0.000 rg
181181
0.00 0.00 m
182182
1.00 1.00 l
183183
S
@@ -186,12 +186,12 @@ 2.00 2.00 m
186186
3.00 3.00 l
187187
S
188188
0 w
189-
1 0 0 rg
189+
1.000 0.000 0.000 rg
190190
4.00 4.00 m
191191
5.00 5.00 l
192192
S
193193
2.2 w
194-
0 1 0 rg
194+
0.000 1.000 0.000 rg
195195
6.00 6.00 m
196196
7.00 7.00 l
197197
S
@@ -478,12 +478,12 @@ public void VerifyStreamFilterTest()
478478
page.Items.Add(text);
479479

480480
var expected = @"
481-
<</Length 185
481+
<</Length 233
482482
/Filter [/ASCIIHexDecode]
483483
>>
484484
stream
485-
3020770D0A30203020302052470D0A30203020302072670D0A42540D0A202020202F46312031322E30302054660D0A2020202037322E30302037322E30302054
486-
640D0A202020205B28666F6F295D20544A0D0A45540D0A530D0A>
485+
3020770D0A302E30303020302E30303020302E3030302052470D0A302E30303020302E30303020302E3030302072670D0A42540D0A202020202F46312031322E
486+
30302054660D0A2020202037322E30302037322E30302054640D0A202020205B28666F6F295D20544A0D0A45540D0A530D0A>
487487
endstream
488488
endobj
489489
";

src/IxMilia.Pdf/Extensions/WriterExtensions.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using System.Globalization;
2+
using System.Text.RegularExpressions;
23

34
namespace IxMilia.Pdf.Extensions
45
{
56
internal static class WriterExtensions
67
{
8+
private static readonly Regex NegativeZero = new Regex(@"^-0(\.0+)?$", RegexOptions.Compiled);
9+
710
public static string AsObjectReference(this int objectId)
811
{
912
return $"{objectId} 0 R";
@@ -14,13 +17,14 @@ public static string AsInvariant(this double value)
1417
return value.ToString(CultureInfo.InvariantCulture);
1518
}
1619

17-
public static string AsFixed(this double value)
20+
public static string AsFixed(this double value, int decimalPlaces = 2)
1821
{
19-
var str = value.ToString("f2", CultureInfo.InvariantCulture);
22+
var str = value.ToString($"f{decimalPlaces}", CultureInfo.InvariantCulture);
23+
2024
// special case for really small negative values that round to 0
21-
if (str == "-0.00")
25+
if (NegativeZero.IsMatch(str))
2226
{
23-
str = "0.00";
27+
str = str.Substring(1);
2428
}
2529

2630
return str;

src/IxMilia.Pdf/PdfColor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace IxMilia.Pdf
44
{
55
public struct PdfColor
66
{
7+
private static readonly int DecimalPlaces = 3;
8+
79
public double R { get; set; }
810
public double G { get; set; }
911
public double B { get; set; }
@@ -17,7 +19,7 @@ public PdfColor(double r, double g, double b)
1719

1820
public override string ToString()
1921
{
20-
return $"{R.AsInvariant()} {G.AsInvariant()} {B.AsInvariant()}";
22+
return $"{R.AsFixed(DecimalPlaces)} {G.AsFixed(DecimalPlaces)} {B.AsFixed(DecimalPlaces)}";
2123
}
2224

2325
public static bool operator ==(PdfColor a, PdfColor b)

0 commit comments

Comments
 (0)