Skip to content

Commit ad15301

Browse files
authored
Merge pull request #59 from synercoder/features/small-fixes
A fix with octal literal string encoding, and rewritten pdfname encod…
2 parents 022250a + f4061ff commit ad15301

File tree

5 files changed

+18
-21
lines changed

5 files changed

+18
-21
lines changed

src/Synercoding.FileFormats.Pdf/LowLevel/Extensions/PdfStreamExtensions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,16 @@ public static PdfStream WriteStringLiteral(this PdfStream stream, string value)
255255
stream.Write('\\').Write(')');
256256
else if (c == '\\')
257257
stream.Write('\\').Write('\\');
258-
else if (c > 0x7F && c < 0x200)
259-
stream.Write('\\').Write(_toOctal(c));
258+
else if (c > 0x7F && c <= 0xFF)
259+
{
260+
var octal = _toOctal(c);
261+
_ = octal switch
262+
{
263+
< 10 => stream.Write('\\').Write('0').Write('0').Write(octal),
264+
< 100 => stream.Write('\\').Write('0').Write(octal),
265+
_ => stream.Write('\\').Write(octal)
266+
};
267+
}
260268
else if (c <= 0x7F)
261269
stream.Write(c);
262270
else

src/Synercoding.FileFormats.Pdf/LowLevel/ObjectStream.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public ObjectStream Write(PdfPage page)
121121
.WriteIfNotNull(PdfName.Get("CropBox"), page.CropBox)
122122
.WriteIfNotNull(PdfName.Get("BleedBox"), page.BleedBox)
123123
.WriteIfNotNull(PdfName.Get("TrimBox"), page.TrimBox)
124+
.WriteIfNotNull(PdfName.Get("ArtBox"), page.ArtBox)
124125
.WriteIfNotNull(PdfName.Get("Rotate"), (int?)page.Rotation);
125126

126127
// Resources

src/Synercoding.FileFormats.Pdf/LowLevel/PdfName.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,13 @@ private static string _encode(string input)
9393
// 0xff because only ascii is supported in pdf names
9494
var encoded = c switch
9595
{
96-
// Pdf reserved characters
97-
' ' => "#20",
98-
'#' => "#23",
99-
'%' => "#25",
100-
'(' => "#28",
101-
')' => "#29",
102-
'/' => "#2f",
103-
'<' => "#3c",
104-
'>' => "#3e",
105-
'[' => "#5b",
106-
']' => "#5d",
107-
'{' => "#7b",
108-
'}' => "#7d",
96+
(char)0 => throw new InvalidOperationException("NULL character is not allowed in a pdf name."),
97+
var cc when cc >= 'a' && cc <= 'z' => cc.ToString(),
98+
var cc when cc >= 'A' && cc <= 'Z' => cc.ToString(),
99+
var cc when cc >= '0' && cc <= '9' => cc.ToString(),
109100
// special characters
110101
var cc when cc < 16 => $"#0{Convert.ToString(cc, 16)}",
111-
var cc when cc < 32 => '#' + Convert.ToString(cc, 16),
112-
var cc when cc > 126 => '#' + Convert.ToString(cc, 16),
113-
// "readable characters"
114-
var cc => cc.ToString()
102+
var cc => '#' + Convert.ToString(cc, 16),
115103
};
116104
pdfName.Append(encoded);
117105
}

src/Synercoding.FileFormats.Pdf/PackageDetails.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Product>Synercoding.FileFormats.Pdf</Product>
1111
<Title>Synercoding.FileFormats.Pdf</Title>
1212
<Description>Contains classes which makes it easy to quickly create a pdf file.</Description>
13-
<PackageReleaseNotes>Rewritten most of the API surface to provide a uniform way to interact with the content.</PackageReleaseNotes>
13+
<PackageReleaseNotes>A fix with octal literal string encoding, and rewritten pdfname encoding, renamed art to artbox, and encoded artbox in resulting pdf.</PackageReleaseNotes>
1414
</PropertyGroup>
1515

1616
</Project>

src/Synercoding.FileFormats.Pdf/PdfPage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public PageRotation? Rotation
8888
/// <summary>
8989
/// The art box of the <see cref="PdfPage"/>
9090
/// </summary>
91-
public Rectangle? Art { get; set; }
91+
public Rectangle? ArtBox { get; set; }
9292

9393
/// <inheritdoc />
9494
public void Dispose()

0 commit comments

Comments
 (0)