Skip to content

Commit 814b25e

Browse files
authored
Fixed "new line" parsing issue for PS and EPS files generated by Apple.
When Ghostscript.NET parses comments like "%%BoundingBox", it expects line to end with either \n (Unix) or \r\n (Windows). This patch adds support for files generated on Apple devices where lines ends with '\r'.
1 parent e89bb0f commit 814b25e

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

Ghostscript.NET/Viewer/DSC/DSCTokenizer.cs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
//
1+
//
22
// DSCTokenizer.cs
33
// This file is part of Ghostscript.NET library
44
//
55
// Author: Josip Habjan ([email protected], http://www.linkedin.com/in/habjan)
6-
// Copyright (c) 2013-2016 by Josip Habjan. All rights reserved.
6+
// Copyright (c) 2013-2023 by Josip Habjan. All rights reserved.
77
//
88
// Author ported some parts of this code from GSView.
99
//
@@ -164,20 +164,32 @@ private DSCToken ReadUntil(string prefix, DSCTokenEnding end)
164164

165165
while ((c = this.ReadChar()) > -1)
166166
{
167+
// unix new line
167168
if (c == '\n' && (end & DSCTokenEnding.LineEnd) == DSCTokenEnding.LineEnd)
168169
{
169170
token.Length = _bufferedStream.Position - 1 - token.StartPosition;
170171
token.Text = text.ToString().Trim();
171172
token.Ending = DSCTokenEnding.LineEnd;
172173
return token;
173174
}
174-
else if (c == '\r' && this.ReadChar() == '\n' && (end & DSCTokenEnding.LineEnd) == DSCTokenEnding.LineEnd)
175+
// windows new line
176+
else if (c == '\r' && this.PeekChar() == '\n' && (end & DSCTokenEnding.LineEnd) == DSCTokenEnding.LineEnd)
175177
{
178+
this.ReadChar();
179+
176180
token.Length = _bufferedStream.Position - 2 - token.StartPosition;
177181
token.Text = text.ToString().Trim();
178182
token.Ending = DSCTokenEnding.LineEnd;
179183
return token;
180184
}
185+
// mac new line
186+
else if (c == '\r' && (end & DSCTokenEnding.LineEnd) == DSCTokenEnding.LineEnd)
187+
{
188+
token.Length = _bufferedStream.Position - 1 - token.StartPosition;
189+
token.Text = text.ToString().Trim();
190+
token.Ending = DSCTokenEnding.LineEnd;
191+
return token;
192+
}
181193
else if (c == ' ' && text.Length > 0 && lastAppendedChar != ' ' && (end & DSCTokenEnding.Whitespace) == DSCTokenEnding.Whitespace)
182194
{
183195
token.Length = _bufferedStream.Position - 1 - token.StartPosition;
@@ -223,7 +235,7 @@ public long FileSize
223235

224236
#endregion
225237

226-
#region ReadByte
238+
#region ReadChar
227239

228240
private int ReadChar()
229241
{
@@ -254,6 +266,49 @@ private int ReadChar()
254266
}
255267

256268
#endregion
269+
270+
#region PeekChar
271+
272+
private int PeekChar()
273+
{
274+
if (_bufferedStream.Position == _bufferedStream.Length)
275+
{
276+
return -1;
277+
}
278+
279+
if (_isUnicode)
280+
{
281+
byte[] b = new byte[2];
282+
283+
_bufferedStream.Read(b, 0, 2);
284+
285+
int res = 0;
286+
287+
if (_isLittleEndian)
288+
{
289+
res = (int)(b[0] | b[1] << 8);
290+
}
291+
else
292+
{
293+
res = (int)(b[0] << 8 | b[1]);
294+
}
295+
296+
_bufferedStream.Position -= 2;
297+
298+
return res;
299+
}
300+
else
301+
{
302+
int res = _bufferedStream.ReadByte();
303+
304+
_bufferedStream.Position--;
305+
306+
return res;
307+
}
308+
}
309+
310+
#endregion
311+
257312
}
258313

259314
}

0 commit comments

Comments
 (0)