Test
This commit is contained in:
292
PrintPDF/PdfSharp/Pdf.Internal/AnsiEncoding.cs
Normal file
292
PrintPDF/PdfSharp/Pdf.Internal/AnsiEncoding.cs
Normal file
@@ -0,0 +1,292 @@
|
||||
#region PDFsharp - A .NET library for processing PDF
|
||||
//
|
||||
// Authors:
|
||||
// Stefan Lange
|
||||
//
|
||||
// Copyright (c) 2005-2017 empira Software GmbH, Cologne Area (Germany)
|
||||
//
|
||||
// http://www.pdfsharp.com
|
||||
// http://sourceforge.net/projects/pdfsharp
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace PdfSharp.Pdf.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// An encoder for PDF AnsiEncoding.
|
||||
/// </summary>
|
||||
public sealed class AnsiEncoding : Encoding
|
||||
{
|
||||
#if DEBUG_ && !(SILVERLIGHT || NETFX_CORE)
|
||||
public static void ProofImplementation()
|
||||
{
|
||||
// Implementation was verified with .NET Ansi encoding.
|
||||
Encoding dotnetImplementation = Encoding.GetEncoding(1252);
|
||||
Encoding thisImplementation = new AnsiEncoding();
|
||||
|
||||
// Check ANSI chars.
|
||||
for (int i = 0; i <= 255; i++)
|
||||
{
|
||||
byte[] b = { (byte) i };
|
||||
char[] ch1 = dotnetImplementation.GetChars(b, 0, 1);
|
||||
char[] ch2 = thisImplementation.GetChars(b, 0, 1);
|
||||
if (ch1[0] != ch2[0])
|
||||
Debug.Print("Error");
|
||||
byte[] b1 = dotnetImplementation.GetBytes(ch1, 0, 1);
|
||||
byte[] b2 = thisImplementation.GetBytes(ch1, 0, 1);
|
||||
if (b1.Length != b2.Length || b1.Length > 1 || b1[0] != b2[0])
|
||||
Debug.Print("Error");
|
||||
}
|
||||
|
||||
// Check Unicode chars.
|
||||
for (int i = 0; i <= 65535; i++)
|
||||
{
|
||||
if (i >= 256)
|
||||
break;
|
||||
if (i == 0x80)
|
||||
Debug.Print("");
|
||||
char[] ch = new char[] { (char)i };
|
||||
byte[] b1 = dotnetImplementation.GetBytes(ch, 0, 1);
|
||||
byte[] b2 = thisImplementation.GetBytes(ch, 0, 1);
|
||||
if (b1.Length != b2.Length || b1.Length > 1 || b1[0] != b2[0])
|
||||
Debug.Print("Error");
|
||||
//byte[] b = new byte[] { (byte)i };
|
||||
//char ch = (char)i;
|
||||
char[] ch1 = dotnetImplementation.GetChars(b1, 0, 1);
|
||||
char[] ch2 = thisImplementation.GetChars(b2, 0, 1);
|
||||
if (ch1[0] != ch2[0])
|
||||
Debug.Print("Error");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Gets the byte count.
|
||||
/// </summary>
|
||||
public override int GetByteCount(char[] chars, int index, int count)
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bytes.
|
||||
/// </summary>
|
||||
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
|
||||
{
|
||||
int count = charCount;
|
||||
for (; charCount > 0; byteIndex++, charIndex++, charCount--)
|
||||
bytes[byteIndex] = (byte)UnicodeToAnsi(chars[charIndex]);
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the character count.
|
||||
/// </summary>
|
||||
public override int GetCharCount(byte[] bytes, int index, int count)
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the chars.
|
||||
/// </summary>
|
||||
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
|
||||
{
|
||||
for (int idx = byteCount; idx > 0; byteIndex++, charIndex++, idx--)
|
||||
chars[charIndex] = AnsiToUnicode[bytes[byteIndex]];
|
||||
return byteCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, calculates the maximum number of bytes produced by encoding the specified number of characters.
|
||||
/// </summary>
|
||||
/// <param name="charCount">The number of characters to encode.</param>
|
||||
/// <returns>
|
||||
/// The maximum number of bytes produced by encoding the specified number of characters.
|
||||
/// </returns>
|
||||
public override int GetMaxByteCount(int charCount)
|
||||
{
|
||||
return charCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, calculates the maximum number of characters produced by decoding the specified number of bytes.
|
||||
/// </summary>
|
||||
/// <param name="byteCount">The number of bytes to decode.</param>
|
||||
/// <returns>
|
||||
/// The maximum number of characters produced by decoding the specified number of bytes.
|
||||
/// </returns>
|
||||
public override int GetMaxCharCount(int byteCount)
|
||||
{
|
||||
return byteCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the specified Unicode character is available in the ANSI code page 1252.
|
||||
/// </summary>
|
||||
public static bool IsAnsi1252Char(char ch)
|
||||
{
|
||||
if (ch < '\u0080' || (ch >= '\u00A0' && ch <= '\u00FF'))
|
||||
return true;
|
||||
|
||||
switch (ch)
|
||||
{
|
||||
case '\u20AC':
|
||||
case '\u0081':
|
||||
case '\u201A':
|
||||
case '\u0192':
|
||||
case '\u201E':
|
||||
case '\u2026':
|
||||
case '\u2020':
|
||||
case '\u2021':
|
||||
case '\u02C6':
|
||||
case '\u2030':
|
||||
case '\u0160':
|
||||
case '\u2039':
|
||||
case '\u0152':
|
||||
case '\u008D':
|
||||
case '\u017D':
|
||||
case '\u008F':
|
||||
case '\u0090':
|
||||
case '\u2018':
|
||||
case '\u2019':
|
||||
case '\u201C':
|
||||
case '\u201D':
|
||||
case '\u2022':
|
||||
case '\u2013':
|
||||
case '\u2014':
|
||||
case '\u02DC':
|
||||
case '\u2122':
|
||||
case '\u0161':
|
||||
case '\u203A':
|
||||
case '\u0153':
|
||||
case '\u009D':
|
||||
case '\u017E':
|
||||
case '\u0178':
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps Unicode to ANSI code page 1252.
|
||||
/// </summary>
|
||||
public static char UnicodeToAnsi(char ch)
|
||||
{
|
||||
if (ch < '\u0080' || (ch >= '\u00A0' && ch <= '\u00FF'))
|
||||
return ch;
|
||||
|
||||
switch (ch)
|
||||
{
|
||||
case '\u20AC':
|
||||
return '\u0080';
|
||||
case '\u0081':
|
||||
return '\u0081';
|
||||
case '\u201A':
|
||||
return '\u0082';
|
||||
case '\u0192':
|
||||
return '\u0083';
|
||||
case '\u201E':
|
||||
return '\u0084';
|
||||
case '\u2026':
|
||||
return '\u0085';
|
||||
case '\u2020':
|
||||
return '\u0086';
|
||||
case '\u2021':
|
||||
return '\u0087';
|
||||
case '\u02C6':
|
||||
return '\u0088';
|
||||
case '\u2030':
|
||||
return '\u0089';
|
||||
case '\u0160':
|
||||
return '\u008A';
|
||||
case '\u2039':
|
||||
return '\u008B';
|
||||
case '\u0152':
|
||||
return '\u008C';
|
||||
case '\u008D':
|
||||
return '\u008D';
|
||||
case '\u017D':
|
||||
return '\u008E';
|
||||
case '\u008F':
|
||||
return '\u008F';
|
||||
case '\u0090':
|
||||
return '\u0090';
|
||||
case '\u2018':
|
||||
return '\u0091';
|
||||
case '\u2019':
|
||||
return '\u0092';
|
||||
case '\u201C':
|
||||
return '\u0093';
|
||||
case '\u201D':
|
||||
return '\u0094';
|
||||
case '\u2022':
|
||||
return '\u0095';
|
||||
case '\u2013':
|
||||
return '\u0096';
|
||||
case '\u2014':
|
||||
return '\u0097';
|
||||
case '\u02DC':
|
||||
return '\u0098';
|
||||
case '\u2122':
|
||||
return '\u0099';
|
||||
case '\u0161':
|
||||
return '\u009A';
|
||||
case '\u203A':
|
||||
return '\u009B';
|
||||
case '\u0153':
|
||||
return '\u009C';
|
||||
case '\u009D':
|
||||
return '\u009D';
|
||||
case '\u017E':
|
||||
return '\u009E';
|
||||
case '\u0178':
|
||||
return '\u009F';
|
||||
}
|
||||
return '\u00A4'; // Char 164 is ANSI value of '<27>'.
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps WinAnsi to Unicode characters.
|
||||
/// </summary>
|
||||
static readonly char[] AnsiToUnicode = // new char[/*256*/]
|
||||
{
|
||||
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
|
||||
/* 00 */ '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', '\u000C', '\u000D', '\u000E', '\u000F',
|
||||
/* 10 */ '\u0010', '\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', '\u001E', '\u001F',
|
||||
/* 20 */ '\u0020', '\u0021', '\u0022', '\u0023', '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F',
|
||||
/* 30 */ '\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', '\u003C', '\u003D', '\u003E', '\u003F',
|
||||
/* 40 */ '\u0040', '\u0041', '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', '\u004E', '\u004F',
|
||||
/* 50 */ '\u0050', '\u0051', '\u0052', '\u0053', '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F',
|
||||
/* 60 */ '\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', '\u006C', '\u006D', '\u006E', '\u006F',
|
||||
/* 70 */ '\u0070', '\u0071', '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', '\u007E', '\u007F',
|
||||
/* 80 */ '\u20AC', '\u0081', '\u201A', '\u0192', '\u201E', '\u2026', '\u2020', '\u2021', '\u02C6', '\u2030', '\u0160', '\u2039', '\u0152', '\u008D', '\u017D', '\u008F',
|
||||
/* 90 */ '\u0090', '\u2018', '\u2019', '\u201C', '\u201D', '\u2022', '\u2013', '\u2014', '\u02DC', '\u2122', '\u0161', '\u203A', '\u0153', '\u009D', '\u017E', '\u0178',
|
||||
/* A0 */ '\u00A0', '\u00A1', '\u00A2', '\u00A3', '\u00A4', '\u00A5', '\u00A6', '\u00A7', '\u00A8', '\u00A9', '\u00AA', '\u00AB', '\u00AC', '\u00AD', '\u00AE', '\u00AF',
|
||||
/* B0 */ '\u00B0', '\u00B1', '\u00B2', '\u00B3', '\u00B4', '\u00B5', '\u00B6', '\u00B7', '\u00B8', '\u00B9', '\u00BA', '\u00BB', '\u00BC', '\u00BD', '\u00BE', '\u00BF',
|
||||
/* C0 */ '\u00C0', '\u00C1', '\u00C2', '\u00C3', '\u00C4', '\u00C5', '\u00C6', '\u00C7', '\u00C8', '\u00C9', '\u00CA', '\u00CB', '\u00CC', '\u00CD', '\u00CE', '\u00CF',
|
||||
/* D0 */ '\u00D0', '\u00D1', '\u00D2', '\u00D3', '\u00D4', '\u00D5', '\u00D6', '\u00D7', '\u00D8', '\u00D9', '\u00DA', '\u00DB', '\u00DC', '\u00DD', '\u00DE', '\u00DF',
|
||||
/* E0 */ '\u00E0', '\u00E1', '\u00E2', '\u00E3', '\u00E4', '\u00E5', '\u00E6', '\u00E7', '\u00E8', '\u00E9', '\u00EA', '\u00EB', '\u00EC', '\u00ED', '\u00EE', '\u00EF',
|
||||
/* F0 */ '\u00F0', '\u00F1', '\u00F2', '\u00F3', '\u00F4', '\u00F5', '\u00F6', '\u00F7', '\u00F8', '\u00F9', '\u00FA', '\u00FB', '\u00FC', '\u00FD', '\u00FE', '\u00FF'
|
||||
};
|
||||
}
|
||||
}
|
83
PrintPDF/PdfSharp/Pdf.Internal/ColorSpaceHelper.cs
Normal file
83
PrintPDF/PdfSharp/Pdf.Internal/ColorSpaceHelper.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
#region PDFsharp - A .NET library for processing PDF
|
||||
//
|
||||
// Authors:
|
||||
// Stefan Lange
|
||||
//
|
||||
// Copyright (c) 2005-2017 empira Software GmbH, Cologne Area (Germany)
|
||||
//
|
||||
// http://www.pdfsharp.com
|
||||
// http://sourceforge.net/projects/pdfsharp
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using PdfSharp.Drawing;
|
||||
|
||||
namespace PdfSharp.Pdf.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper functions for RGB and CMYK colors.
|
||||
/// </summary>
|
||||
static class ColorSpaceHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks whether a color mode and a color match.
|
||||
/// </summary>
|
||||
public static XColor EnsureColorMode(PdfColorMode colorMode, XColor color)
|
||||
{
|
||||
#if true
|
||||
if (colorMode == PdfColorMode.Rgb && color.ColorSpace != XColorSpace.Rgb)
|
||||
return XColor.FromArgb((int)(color.A * 255), color.R, color.G, color.B);
|
||||
|
||||
if (colorMode == PdfColorMode.Cmyk && color.ColorSpace != XColorSpace.Cmyk)
|
||||
return XColor.FromCmyk(color.A, color.C, color.M, color.Y, color.K);
|
||||
|
||||
return color;
|
||||
#else
|
||||
if (colorMode == PdfColorMode.Rgb && color.ColorSpace != XColorSpace.Rgb)
|
||||
throw new InvalidOperationException(PSSR.InappropriateColorSpace(colorMode, color.ColorSpace));
|
||||
|
||||
if (colorMode == PdfColorMode.Cmyk && color.ColorSpace != XColorSpace.Cmyk)
|
||||
throw new InvalidOperationException(PSSR.InappropriateColorSpace(colorMode, color.ColorSpace));
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the color mode of a document and a color match.
|
||||
/// </summary>
|
||||
public static XColor EnsureColorMode(PdfDocument document, XColor color)
|
||||
{
|
||||
if (document == null)
|
||||
throw new ArgumentNullException("document");
|
||||
|
||||
return EnsureColorMode(document.Options.ColorMode, color);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether two colors are equal referring to their CMYK color values.
|
||||
/// </summary>
|
||||
public static bool IsEqualCmyk(XColor x, XColor y)
|
||||
{
|
||||
if (x.ColorSpace != XColorSpace.Cmyk || y.ColorSpace != XColorSpace.Cmyk)
|
||||
return false;
|
||||
return x.C == y.C && x.M == y.M && x.Y == y.Y && x.K == y.K;
|
||||
}
|
||||
}
|
||||
}
|
147
PrintPDF/PdfSharp/Pdf.Internal/DocEncoding.cs
Normal file
147
PrintPDF/PdfSharp/Pdf.Internal/DocEncoding.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
#region PDFsharp - A .NET library for processing PDF
|
||||
//
|
||||
// Authors:
|
||||
// Stefan Lange
|
||||
//
|
||||
// Copyright (c) 2005-2017 empira Software GmbH, Cologne Area (Germany)
|
||||
//
|
||||
// http://www.pdfsharp.com
|
||||
// http://sourceforge.net/projects/pdfsharp
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace PdfSharp.Pdf.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// An encoder for PDF DocEncoding.
|
||||
/// </summary>
|
||||
internal sealed class DocEncoding : Encoding
|
||||
{
|
||||
public DocEncoding()
|
||||
{ }
|
||||
|
||||
public override int GetByteCount(char[] chars, int index, int count)
|
||||
{
|
||||
return PdfEncoders.WinAnsiEncoding.GetByteCount(chars, index, count);
|
||||
}
|
||||
|
||||
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
|
||||
{
|
||||
byte[] ansi = PdfEncoders.WinAnsiEncoding.GetBytes(chars, charIndex, charCount);
|
||||
for (int idx = 0, count = ansi.Length; count > 0; idx++, byteIndex++, count--)
|
||||
bytes[byteIndex] = AnsiToDoc[ansi[idx]];
|
||||
return ansi.Length;
|
||||
}
|
||||
|
||||
public override int GetCharCount(byte[] bytes, int index, int count)
|
||||
{
|
||||
//return PdfEncoders.WinAnsiEncoding.GetCharCount(bytes, index, count);
|
||||
Debug.Assert(PdfEncoders.WinAnsiEncoding.GetCharCount(bytes, index, count) == count);
|
||||
return count;
|
||||
}
|
||||
|
||||
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
|
||||
{
|
||||
PdfDocToUnicode.GetType();
|
||||
throw new NotImplementedException("GetChars");
|
||||
//for (; byteCount > 0; byteIndex++, charIndex++, byteCount--)
|
||||
// chars[charIndex] = (char)bytes[byteIndex];
|
||||
//return byteCount;
|
||||
}
|
||||
|
||||
public override int GetMaxByteCount(int charCount)
|
||||
{
|
||||
return charCount;
|
||||
}
|
||||
|
||||
public override int GetMaxCharCount(int byteCount)
|
||||
{
|
||||
return byteCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts WinAnsi to DocEncode characters. Based upon PDF Reference 1.6.
|
||||
/// </summary>
|
||||
static readonly byte[] AnsiToDoc = new byte[256]
|
||||
{
|
||||
// x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf
|
||||
/* 00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
|
||||
/* 10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
|
||||
/* 20 */ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
|
||||
/* 30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
|
||||
/* 40 */ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
|
||||
/* 50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
|
||||
/* 60 */ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
|
||||
/* 70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
|
||||
/* 80 */ 0xA0, 0x81, 0x91, 0x83, 0x8C, 0x83, 0x81, 0x82, 0x1A, 0x89, 0x97, 0x88, 0x96, 0x8D, 0x99, 0x8F,
|
||||
/* 90 */ 0x90, 0x8F, 0x90, 0x8D, 0x8E, 0x80, 0x85, 0x84, 0x1F, 0x92, 0x90, 0x89, 0x9C, 0x9D, 0x9E, 0x98,
|
||||
/* a0 */ 0x20, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
|
||||
/* b0 */ 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
|
||||
/* c0 */ 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
|
||||
/* d0 */ 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
|
||||
/* e0 */ 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
|
||||
/* f0 */ 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
|
||||
|
||||
////// Converts WinAnsi to DocEncode characters. Incomplete, just maps € and some other characters.
|
||||
////// x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf
|
||||
/////* 00 */ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
|
||||
/////* 10 */ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
|
||||
/////* 20 */ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
|
||||
/////* 30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
|
||||
/////* 40 */ 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
|
||||
/////* 50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
|
||||
/////* 60 */ 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
|
||||
/////* 70 */ 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
|
||||
/////* 80 */ 0xA0,#0x7F, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
|
||||
/////* 90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,#0x8A,#0x8C, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
|
||||
/////* a0 */ 0x20, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
|
||||
/////* b0 */ 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
|
||||
/////* c0 */ 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
|
||||
/////* d0 */ 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
|
||||
/////* e0 */ 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
|
||||
/////* f0 */ 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
|
||||
};
|
||||
|
||||
// TODO: use this table
|
||||
static readonly char[] PdfDocToUnicode = new char[]
|
||||
{
|
||||
'\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F',
|
||||
'\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F',
|
||||
'\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27', '\x28', '\x29', '\x2A', '\x2B', '\x2C', '\x2D', '\x2E', '\x2F',
|
||||
'\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37', '\x38', '\x39', '\x3A', '\x3B', '\x3C', '\x3D', '\x3E', '\x3F',
|
||||
'\x40', '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47', '\x48', '\x49', '\x4A', '\x4B', '\x4C', '\x4D', '\x4E', '\x4F',
|
||||
'\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57', '\x58', '\x59', '\x5A', '\x5B', '\x5C', '\x5D', '\x5E', '\x5F',
|
||||
'\x60', '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', '\x68', '\x69', '\x6A', '\x6B', '\x6C', '\x6D', '\x6E', '\x6F',
|
||||
'\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', '\x78', '\x79', '\x7A', '\x7B', '\x7C', '\x7D', '\x7E', '\x7F',
|
||||
'\x2022', '\x2020', '\x2021', '\x2026', '\x2014', '\x2013', '\x0192', '\x2044', '\x2039', '\x203A', '\x2212', '\x2030', '\x201E', '\x201C', '\x201D', '\x2018',
|
||||
'\x2019', '\x201A', '\x2122', '\xFB01', '\xFB02', '\x0141', '\x0152', '\x0160', '\x0178', '\x017D', '\x0131', '\x0142', '\x0153', '\x0161', '\x017E', '\xFFFD',
|
||||
'\x20AC', '\xA1', '\xA2', '\xA3', '\xA4', '\xA5', '\xA6', '\xA7', '\xA8', '\xA9', '\xAA', '\xAB', '\xAC', '\xAD', '\xAE', '\xAF',
|
||||
'\xB0', '\xB1', '\xB2', '\xB3', '\xB4', '\xB5', '\xB6', '\xB7', '\xB8', '\xB9', '\xBA', '\xBB', '\xBC', '\xBD', '\xBE', '\xBF',
|
||||
'\xC0', '\xC1', '\xC2', '\xC3', '\xC4', '\xC5', '\xC6', '\xC7', '\xC8', '\xC9', '\xCA', '\xCB', '\xCC', '\xCD', '\xCE', '\xCF',
|
||||
'\xD0', '\xD1', '\xD2', '\xD3', '\xD4', '\xD5', '\xD6', '\xD7', '\xD8', '\xD9', '\xDA', '\xDB', '\xDC', '\xDD', '\xDE', '\xDF',
|
||||
'\xE0', '\xE1', '\xE2', '\xE3', '\xE4', '\xE5', '\xE6', '\xE7', '\xE8', '\xE9', '\xEA', '\xEB', '\xEC', '\xED', '\xEE', '\xEF',
|
||||
'\xF0', '\xF1', '\xF2', '\xF3', '\xF4', '\xF5', '\xF6', '\xF7', '\xF8', '\xF9', '\xFA', '\xFB', '\xFC', '\xFD', '\xFE', '\xFF',
|
||||
};
|
||||
}
|
||||
}
|
125
PrintPDF/PdfSharp/Pdf.Internal/GlobalObjectTable.cs
Normal file
125
PrintPDF/PdfSharp/Pdf.Internal/GlobalObjectTable.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
#region PDFsharp - A .NET library for processing PDF
|
||||
//
|
||||
// Authors:
|
||||
// Stefan Lange
|
||||
//
|
||||
// Copyright (c) 2005-2017 empira Software GmbH, Cologne Area (Germany)
|
||||
//
|
||||
// http://www.pdfsharp.com
|
||||
// http://sourceforge.net/projects/pdfsharp
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace PdfSharp.Pdf.Internal
|
||||
{
|
||||
#if true_
|
||||
/// <summary>
|
||||
/// Provides a thread-local cache for large objects.
|
||||
/// </summary>
|
||||
internal class GlobalObjectTable_not_in_use
|
||||
{
|
||||
public GlobalObjectTable_not_in_use()
|
||||
{ }
|
||||
|
||||
public void AttatchDocument(PdfDocument.DocumentHandle handle)
|
||||
{
|
||||
lo ck (_documentHandles)
|
||||
{
|
||||
_documentHandles.Add(handle);
|
||||
}
|
||||
}
|
||||
|
||||
public void DetatchDocument(PdfDocument.DocumentHandle handle)
|
||||
{
|
||||
lo ck (_documentHandles)
|
||||
{
|
||||
// Notify other documents about detach
|
||||
int count = _documentHandles.Count;
|
||||
for (int idx = 0; idx < count; idx++)
|
||||
{
|
||||
if (((PdfDocument.DocumentHandle)_documentHandles[idx]).IsAlive)
|
||||
{
|
||||
PdfDocument target = ((PdfDocument.DocumentHandle)_documentHandles[idx]).Target;
|
||||
if (target != null)
|
||||
target.OnExternalDocumentFinalized(handle);
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up table
|
||||
for (int idx = 0; idx < _documentHandles.Count; idx++)
|
||||
{
|
||||
PdfDocument target = ((PdfDocument.DocumentHandle)_documentHandles[idx]).Target;
|
||||
if (target == null)
|
||||
{
|
||||
_documentHandles.RemoveAt(idx);
|
||||
idx--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//lo ck (documents)
|
||||
//{
|
||||
// int index = IndexOf(document);
|
||||
// if (index != -1)
|
||||
// {
|
||||
// documents.RemoveAt(index);
|
||||
// int count = documents.Count;
|
||||
// for (int idx = 0; idx < count; idx++)
|
||||
// {
|
||||
// PdfDocument target = ((WeakReference)documents[idx]).Target as PdfDocument;
|
||||
// if (target != null)
|
||||
// target.OnExternalDocumentFinalized(document);
|
||||
// }
|
||||
|
||||
// for (int idx = 0; idx < documents.Count; idx++)
|
||||
// {
|
||||
// PdfDocument target = ((WeakReference)documents[idx]).Target as PdfDocument;
|
||||
// if (target == null)
|
||||
// {
|
||||
// documents.RemoveAt(idx);
|
||||
// idx--;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
//int IndexOf(PdfDocument.Handle handle)
|
||||
//{
|
||||
// int count = documents.Count;
|
||||
// for (int idx = 0; idx < count; idx++)
|
||||
// {
|
||||
// if ((PdfDocument.Handle)documents[idx] == handle)
|
||||
// return idx;
|
||||
// //if (Object.ReferenceEquals(((WeakReference)documents[idx]).Target, document))
|
||||
// // return idx;
|
||||
// }
|
||||
// return -1;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// Array of handles to all documents.
|
||||
/// </summary>
|
||||
readonly List<object> _documentHandles = new List<object>();
|
||||
}
|
||||
#endif
|
||||
}
|
55
PrintPDF/PdfSharp/Pdf.Internal/PdfDiagnostics.cs
Normal file
55
PrintPDF/PdfSharp/Pdf.Internal/PdfDiagnostics.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
#region PDFsharp - A .NET library for processing PDF
|
||||
//
|
||||
// Authors:
|
||||
// Stefan Lange
|
||||
//
|
||||
// Copyright (c) 2005-2017 empira Software GmbH, Cologne Area (Germany)
|
||||
//
|
||||
// http://www.pdfsharp.com
|
||||
// http://sourceforge.net/projects/pdfsharp
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
namespace PdfSharp.Pdf.Internal
|
||||
{
|
||||
class PdfDiagnostics
|
||||
{
|
||||
public static bool TraceCompressedObjects
|
||||
{
|
||||
get { return _traceCompressedObjects; }
|
||||
set { _traceCompressedObjects = value; }
|
||||
}
|
||||
static bool _traceCompressedObjects = true;
|
||||
|
||||
public static bool TraceXrefStreams
|
||||
{
|
||||
get { return _traceXrefStreams && TraceCompressedObjects; }
|
||||
set { _traceXrefStreams = value; }
|
||||
}
|
||||
static bool _traceXrefStreams = true;
|
||||
|
||||
public static bool TraceObjectStreams
|
||||
{
|
||||
get { return _traceObjectStreams && TraceCompressedObjects; }
|
||||
set { _traceObjectStreams = value; }
|
||||
}
|
||||
static bool _traceObjectStreams = true;
|
||||
}
|
||||
}
|
662
PrintPDF/PdfSharp/Pdf.Internal/PdfEncoders.cs
Normal file
662
PrintPDF/PdfSharp/Pdf.Internal/PdfEncoders.cs
Normal file
@@ -0,0 +1,662 @@
|
||||
#region PDFsharp - A .NET library for processing PDF
|
||||
//
|
||||
// Authors:
|
||||
// Stefan Lange
|
||||
//
|
||||
// Copyright (c) 2005-2017 empira Software GmbH, Cologne Area (Germany)
|
||||
//
|
||||
// http://www.pdfsharp.com
|
||||
// http://sourceforge.net/projects/pdfsharp
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using PdfSharp.Drawing;
|
||||
using PdfSharp.Pdf.Security;
|
||||
|
||||
namespace PdfSharp.Pdf.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// Groups a set of static encoding helper functions.
|
||||
/// </summary>
|
||||
internal static class PdfEncoders
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the raw encoding.
|
||||
/// </summary>
|
||||
public static Encoding RawEncoding
|
||||
{
|
||||
get { return _rawEncoding ?? (_rawEncoding = new RawEncoding()); }
|
||||
}
|
||||
static Encoding _rawEncoding;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the raw Unicode encoding.
|
||||
/// </summary>
|
||||
public static Encoding RawUnicodeEncoding
|
||||
{
|
||||
get { return _rawUnicodeEncoding ?? (_rawUnicodeEncoding = new RawUnicodeEncoding()); }
|
||||
}
|
||||
static Encoding _rawUnicodeEncoding;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Windows 1252 (ANSI) encoding.
|
||||
/// </summary>
|
||||
public static Encoding WinAnsiEncoding
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_winAnsiEncoding == null)
|
||||
{
|
||||
#if !SILVERLIGHT && !NETFX_CORE && !UWP && !CORE
|
||||
// Use .net encoder if available.
|
||||
_winAnsiEncoding = Encoding.GetEncoding(1252);
|
||||
#else
|
||||
// Use own implementation in Silverlight and WinRT
|
||||
_winAnsiEncoding = new AnsiEncoding();
|
||||
#endif
|
||||
}
|
||||
return _winAnsiEncoding;
|
||||
}
|
||||
}
|
||||
static Encoding _winAnsiEncoding;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the PDF DocEncoding encoding.
|
||||
/// </summary>
|
||||
public static Encoding DocEncoding
|
||||
{
|
||||
get { return _docEncoding ?? (_docEncoding = new DocEncoding()); }
|
||||
}
|
||||
static Encoding _docEncoding;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the UNICODE little-endian encoding.
|
||||
/// </summary>
|
||||
public static Encoding UnicodeEncoding
|
||||
{
|
||||
get { return _unicodeEncoding ?? (_unicodeEncoding = Encoding.Unicode); }
|
||||
}
|
||||
static Encoding _unicodeEncoding;
|
||||
|
||||
///// <summary>
|
||||
///// Encodes a string from a byte array. Each character gets the code of the corresponding byte.
|
||||
///// </summary>
|
||||
//public static string RawString(byte[] bytes, int offset, int length)
|
||||
//{
|
||||
// char[] chars = new char[length];
|
||||
// for (int idx = offset, ch = 0; idx < offset + length; idx++, ch++)
|
||||
// chars[ch] = (char)bytes[idx];
|
||||
// return new string(chars, 0, length);
|
||||
//}
|
||||
//
|
||||
//public static string RawString(byte[] bytes)
|
||||
//{
|
||||
// return RawString(bytes, 0, bytes.Length);
|
||||
//}
|
||||
|
||||
#if true_
|
||||
public static string EncodeAsLiteral(string text, bool unicode)
|
||||
{
|
||||
if (text == null || text == "")
|
||||
return "<>";
|
||||
|
||||
StringBuilder pdf = new StringBuilder("");
|
||||
if (!unicode)
|
||||
{
|
||||
byte[] bytes = WinAnsiEncoding.GetBytes(text);
|
||||
int count = bytes.Length;
|
||||
pdf.Append("(");
|
||||
for (int idx = 0; idx < count; idx++)
|
||||
{
|
||||
char ch = (char)bytes[idx];
|
||||
if (ch < 32)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '\n':
|
||||
pdf.Append("\\n");
|
||||
break;
|
||||
|
||||
case '\r':
|
||||
pdf.Append("\\r");
|
||||
break;
|
||||
|
||||
case '\t':
|
||||
pdf.Append("\\t");
|
||||
break;
|
||||
|
||||
case '\f':
|
||||
pdf.Append("\\f");
|
||||
break;
|
||||
|
||||
default:
|
||||
pdf.Append(InvalidChar); // TODO
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '(':
|
||||
pdf.Append("\\(");
|
||||
break;
|
||||
|
||||
case ')':
|
||||
pdf.Append("\\)");
|
||||
break;
|
||||
|
||||
case '\\':
|
||||
pdf.Append("\\\\");
|
||||
break;
|
||||
|
||||
default:
|
||||
pdf.Append(ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
pdf.Append(')');
|
||||
}
|
||||
else
|
||||
{
|
||||
pdf.Append("<");
|
||||
byte[] bytes = UnicodeEncoding.GetBytes(text);
|
||||
int count = bytes.Length;
|
||||
for (int idx = 0; idx < count; idx += 2)
|
||||
{
|
||||
pdf.AppendFormat("{0:X2}{1:X2}", bytes[idx + 1], bytes[idx]);
|
||||
if (idx != 0 && (idx % 48) == 0)
|
||||
pdf.Append("\n");
|
||||
}
|
||||
pdf.Append(">");
|
||||
}
|
||||
return pdf.ToString();
|
||||
}
|
||||
#endif
|
||||
|
||||
//public static string EncodeAsLiteral(string text)
|
||||
//{
|
||||
// return EncodeAsLiteral(text, false);
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a raw string into a raw string literal, possibly encrypted.
|
||||
/// </summary>
|
||||
public static string ToStringLiteral(string text, PdfStringEncoding encoding, PdfStandardSecurityHandler securityHandler)
|
||||
{
|
||||
if (String.IsNullOrEmpty(text))
|
||||
return "()";
|
||||
|
||||
byte[] bytes;
|
||||
switch (encoding)
|
||||
{
|
||||
case PdfStringEncoding.RawEncoding:
|
||||
bytes = RawEncoding.GetBytes(text);
|
||||
break;
|
||||
|
||||
case PdfStringEncoding.WinAnsiEncoding:
|
||||
bytes = WinAnsiEncoding.GetBytes(text);
|
||||
break;
|
||||
|
||||
case PdfStringEncoding.PDFDocEncoding:
|
||||
bytes = DocEncoding.GetBytes(text);
|
||||
break;
|
||||
|
||||
case PdfStringEncoding.Unicode:
|
||||
bytes = RawUnicodeEncoding.GetBytes(text);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException(encoding.ToString());
|
||||
}
|
||||
byte[] temp = FormatStringLiteral(bytes, encoding == PdfStringEncoding.Unicode, true, false, securityHandler);
|
||||
return RawEncoding.GetString(temp, 0, temp.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a raw string into a raw string literal, possibly encrypted.
|
||||
/// </summary>
|
||||
public static string ToStringLiteral(byte[] bytes, bool unicode, PdfStandardSecurityHandler securityHandler)
|
||||
{
|
||||
if (bytes == null || bytes.Length == 0)
|
||||
return "()";
|
||||
|
||||
byte[] temp = FormatStringLiteral(bytes, unicode, true, false, securityHandler);
|
||||
return RawEncoding.GetString(temp, 0, temp.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a raw string into a raw hexadecimal string literal, possibly encrypted.
|
||||
/// </summary>
|
||||
public static string ToHexStringLiteral(string text, PdfStringEncoding encoding, PdfStandardSecurityHandler securityHandler)
|
||||
{
|
||||
if (String.IsNullOrEmpty(text))
|
||||
return "<>";
|
||||
|
||||
byte[] bytes;
|
||||
switch (encoding)
|
||||
{
|
||||
case PdfStringEncoding.RawEncoding:
|
||||
bytes = RawEncoding.GetBytes(text);
|
||||
break;
|
||||
|
||||
case PdfStringEncoding.WinAnsiEncoding:
|
||||
bytes = WinAnsiEncoding.GetBytes(text);
|
||||
break;
|
||||
|
||||
case PdfStringEncoding.PDFDocEncoding:
|
||||
bytes = DocEncoding.GetBytes(text);
|
||||
break;
|
||||
|
||||
case PdfStringEncoding.Unicode:
|
||||
//bytes = UnicodeEncoding.GetBytes(text);
|
||||
bytes = RawUnicodeEncoding.GetBytes(text);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotImplementedException(encoding.ToString());
|
||||
}
|
||||
|
||||
byte[] agTemp = FormatStringLiteral(bytes, encoding == PdfStringEncoding.Unicode, true, true, securityHandler);
|
||||
return RawEncoding.GetString(agTemp, 0, agTemp.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a raw string into a raw hexadecimal string literal, possibly encrypted.
|
||||
/// </summary>
|
||||
public static string ToHexStringLiteral(byte[] bytes, bool unicode, PdfStandardSecurityHandler securityHandler)
|
||||
{
|
||||
if (bytes == null || bytes.Length == 0)
|
||||
return "<>";
|
||||
|
||||
byte[] agTemp = FormatStringLiteral(bytes, unicode, true, true, securityHandler);
|
||||
return RawEncoding.GetString(agTemp, 0, agTemp.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the specified byte array into a byte array representing a string literal.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The bytes of the string.</param>
|
||||
/// <param name="unicode">Indicates whether one or two bytes are one character.</param>
|
||||
/// <param name="prefix">Indicates whether to use Unicode prefix.</param>
|
||||
/// <param name="hex">Indicates whether to create a hexadecimal string literal.</param>
|
||||
/// <param name="securityHandler">Encrypts the bytes if specified.</param>
|
||||
/// <returns>The PDF bytes.</returns>
|
||||
public static byte[] FormatStringLiteral(byte[] bytes, bool unicode, bool prefix, bool hex, PdfStandardSecurityHandler securityHandler)
|
||||
{
|
||||
if (bytes == null || bytes.Length == 0)
|
||||
return hex ? new byte[] { (byte)'<', (byte)'>' } : new byte[] { (byte)'(', (byte)')' };
|
||||
|
||||
Debug.Assert(!unicode || bytes.Length % 2 == 0, "Odd number of bytes in Unicode string.");
|
||||
|
||||
byte[] originalBytes = null;
|
||||
|
||||
bool encrypted = false;
|
||||
if (securityHandler != null && !hex)
|
||||
{
|
||||
originalBytes = bytes;
|
||||
bytes = (byte[])bytes.Clone();
|
||||
bytes = securityHandler.EncryptBytes(bytes);
|
||||
encrypted = true;
|
||||
}
|
||||
|
||||
int count = bytes.Length;
|
||||
StringBuilder pdf = new StringBuilder();
|
||||
if (!unicode)
|
||||
{
|
||||
if (!hex)
|
||||
{
|
||||
pdf.Append("(");
|
||||
for (int idx = 0; idx < count; idx++)
|
||||
{
|
||||
char ch = (char)bytes[idx];
|
||||
if (ch < 32)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '\n':
|
||||
pdf.Append("\\n");
|
||||
break;
|
||||
|
||||
case '\r':
|
||||
pdf.Append("\\r");
|
||||
break;
|
||||
|
||||
case '\t':
|
||||
pdf.Append("\\t");
|
||||
break;
|
||||
|
||||
case '\b':
|
||||
pdf.Append("\\b");
|
||||
break;
|
||||
|
||||
// Corrupts encrypted text.
|
||||
//case '\f':
|
||||
// pdf.Append("\\f");
|
||||
// break;
|
||||
|
||||
default:
|
||||
// Don't escape characters less than 32 if the string is encrypted, because it is
|
||||
// unreadable anyway.
|
||||
encrypted = true;
|
||||
if (!encrypted)
|
||||
{
|
||||
pdf.Append("\\0");
|
||||
pdf.Append((char)(ch % 8 + '0'));
|
||||
pdf.Append((char)(ch / 8 + '0'));
|
||||
}
|
||||
else
|
||||
pdf.Append(ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '(':
|
||||
pdf.Append("\\(");
|
||||
break;
|
||||
|
||||
case ')':
|
||||
pdf.Append("\\)");
|
||||
break;
|
||||
|
||||
case '\\':
|
||||
pdf.Append("\\\\");
|
||||
break;
|
||||
|
||||
default:
|
||||
pdf.Append(ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
pdf.Append(')');
|
||||
}
|
||||
else
|
||||
{
|
||||
pdf.Append('<');
|
||||
for (int idx = 0; idx < count; idx++)
|
||||
pdf.AppendFormat("{0:X2}", bytes[idx]);
|
||||
pdf.Append('>');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Hex:
|
||||
if (hex)
|
||||
{
|
||||
if (securityHandler != null && prefix)
|
||||
{
|
||||
// TODO Reduce redundancy.
|
||||
// Encrypt data after padding BOM.
|
||||
var bytes2 = new byte[bytes.Length + 2];
|
||||
// Add BOM.
|
||||
bytes2[0] = 0xfe;
|
||||
bytes2[1] = 0xff;
|
||||
// Copy bytes.
|
||||
Array.Copy(bytes, 0, bytes2, 2, bytes.Length);
|
||||
// Encyption.
|
||||
bytes2 = securityHandler.EncryptBytes(bytes2);
|
||||
encrypted = true;
|
||||
pdf.Append("<");
|
||||
var count2 = bytes2.Length;
|
||||
for (int idx = 0; idx < count2; idx += 2)
|
||||
{
|
||||
pdf.AppendFormat("{0:X2}{1:X2}", bytes2[idx], bytes2[idx + 1]);
|
||||
if (idx != 0 && (idx % 48) == 0)
|
||||
pdf.Append("\n");
|
||||
}
|
||||
pdf.Append(">");
|
||||
}
|
||||
else
|
||||
{
|
||||
// No prefix or no encryption.
|
||||
pdf.Append(prefix ? "<FEFF" : "<");
|
||||
for (int idx = 0; idx < count; idx += 2)
|
||||
{
|
||||
pdf.AppendFormat("{0:X2}{1:X2}", bytes[idx], bytes[idx + 1]);
|
||||
if (idx != 0 && (idx % 48) == 0)
|
||||
pdf.Append("\n");
|
||||
}
|
||||
pdf.Append(">");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO non hex literals... not sure how to treat linefeeds, '(', '\' etc.
|
||||
if (encrypted)
|
||||
{
|
||||
// Hack: Call self with hex := true.
|
||||
return FormatStringLiteral(originalBytes, unicode, prefix, true, securityHandler);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Hack: Call self with hex := true.
|
||||
return FormatStringLiteral(bytes, true, prefix, true, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
return RawEncoding.GetBytes(pdf.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts WinAnsi to DocEncode characters. Incomplete, just maps and some other characters.
|
||||
/// </summary>
|
||||
static byte[] docencode_______ = new byte[256]
|
||||
{
|
||||
// TODO:
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
|
||||
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
|
||||
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
|
||||
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
|
||||
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
|
||||
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
|
||||
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
|
||||
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
|
||||
0xA0, 0x7F, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
|
||||
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x8A, 0x8C, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
|
||||
0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
|
||||
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
|
||||
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
|
||||
0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
|
||||
0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
|
||||
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
|
||||
};
|
||||
|
||||
//public static string DocEncode(string text, bool unicode)//, PdfStandardSecurityHandler securityHandler)
|
||||
//{
|
||||
// if (text == null || text == "")
|
||||
// return "()";
|
||||
//
|
||||
// int length = text.Length;
|
||||
// StringBuilder encoded = new StringBuilder(2 * length);
|
||||
// if (!unicode)
|
||||
// {
|
||||
// byte[] bytes = WinAnsiEncoding.GetBytes(text);
|
||||
// encoded.Append('(');
|
||||
// for (int idx = 0; idx < length; idx++)
|
||||
// {
|
||||
// char ch = (char)bytes[idx];
|
||||
// if (ch > 255)
|
||||
// {
|
||||
// //TODO unicode?
|
||||
// encoded.Append(InvalidChar);
|
||||
// //encoded.Append(ch);
|
||||
// continue;
|
||||
// }
|
||||
// ch = (char)docencode[(int)ch];
|
||||
// if (ch < 32)
|
||||
// {
|
||||
// switch (ch)
|
||||
// {
|
||||
// case '\n':
|
||||
// encoded.Append("\\n");
|
||||
// break;
|
||||
//
|
||||
// case '\r':
|
||||
// encoded.Append("\\r");
|
||||
// break;
|
||||
//
|
||||
// case '\t':
|
||||
// encoded.Append("\\t");
|
||||
// break;
|
||||
//
|
||||
// case '\f':
|
||||
// encoded.Append("\\f");
|
||||
// break;
|
||||
//
|
||||
// default:
|
||||
// encoded.Append(InvalidChar); // TODO
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// switch (ch)
|
||||
// {
|
||||
// case '(':
|
||||
// encoded.Append("\\(");
|
||||
// break;
|
||||
//
|
||||
// case ')':
|
||||
// encoded.Append("\\)");
|
||||
// break;
|
||||
//
|
||||
// case '\\':
|
||||
// encoded.Append("\\\\");
|
||||
// break;
|
||||
//
|
||||
// default:
|
||||
// encoded.Append(ch);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// encoded.Append(')');
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// encoded.Append("<FEFF");
|
||||
// for (int idx = 0; idx < length; idx++)
|
||||
// encoded.AppendFormat("{0:X4}", (int)text[idx]);
|
||||
// encoded.Append('>');
|
||||
// }
|
||||
// return encoded.ToString();
|
||||
//}
|
||||
|
||||
//public static string DocEncode(string text)
|
||||
//{
|
||||
// return DocEncode(text, false);
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// Encodes a hexadecimal doc-encoded string literal.
|
||||
///// </summary>
|
||||
//public static string DocEncodeHex(string text, bool unicode)
|
||||
//{
|
||||
// if (text == null || text == "")
|
||||
// return "<>";
|
||||
//
|
||||
// int length = text.Length;
|
||||
// StringBuilder encoded = new StringBuilder(3 * length);
|
||||
// if (!unicode)
|
||||
// {
|
||||
// byte[] bytes = WinAnsiEncoding.GetBytes(text);
|
||||
// encoded.Append('<');
|
||||
// for (int idx = 0; idx < length; idx++)
|
||||
// encoded.AppendFormat("{0:X2}", docencode[bytes[idx]]);
|
||||
// encoded.Append('>');
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// encoded.Append("<FEFF");
|
||||
// for (int idx = 0; idx < length; idx++)
|
||||
// {
|
||||
// encoded.AppendFormat("{0:X4}", (int)text[idx]);
|
||||
// }
|
||||
// encoded.Append('>');
|
||||
// }
|
||||
// return encoded.ToString();
|
||||
//}
|
||||
|
||||
//public static string DocEncodeHex(string text)
|
||||
//{
|
||||
// return DocEncodeHex(text, false);
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// ...because I always forget CultureInfo.InvariantCulture and wonder why Acrobat
|
||||
/// cannot understand my German decimal separator...
|
||||
/// </summary>
|
||||
public static string Format(string format, params object[] args)
|
||||
{
|
||||
return String.Format(CultureInfo.InvariantCulture, format, args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a float into a string with up to 3 decimal digits and a decimal point.
|
||||
/// </summary>
|
||||
public static string ToString(double val)
|
||||
{
|
||||
return val.ToString(Config.SignificantFigures3, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an XColor into a string with up to 3 decimal digits and a decimal point.
|
||||
/// </summary>
|
||||
public static string ToString(XColor color, PdfColorMode colorMode)
|
||||
{
|
||||
const string format = Config.SignificantFigures3;
|
||||
|
||||
// If not defined let color decide
|
||||
if (colorMode == PdfColorMode.Undefined)
|
||||
colorMode = color.ColorSpace == XColorSpace.Cmyk ? PdfColorMode.Cmyk : PdfColorMode.Rgb;
|
||||
|
||||
switch (colorMode)
|
||||
{
|
||||
case PdfColorMode.Cmyk:
|
||||
return String.Format(CultureInfo.InvariantCulture, "{0:" + format + "} {1:" + format + "} {2:" + format + "} {3:" + format + "}",
|
||||
color.C, color.M, color.Y, color.K);
|
||||
|
||||
default:
|
||||
return String.Format(CultureInfo.InvariantCulture, "{0:" + format + "} {1:" + format + "} {2:" + format + "}",
|
||||
color.R / 255.0, color.G / 255.0, color.B / 255.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an XMatrix into a string with up to 4 decimal digits and a decimal point.
|
||||
/// </summary>
|
||||
public static string ToString(XMatrix matrix)
|
||||
{
|
||||
const string format = Config.SignificantFigures4;
|
||||
return String.Format(CultureInfo.InvariantCulture,
|
||||
"{0:" + format + "} {1:" + format + "} {2:" + format + "} {3:" + format + "} {4:" + format + "} {5:" + format + "}",
|
||||
matrix.M11, matrix.M12, matrix.M21, matrix.M22, matrix.OffsetX, matrix.OffsetY);
|
||||
}
|
||||
}
|
||||
}
|
165
PrintPDF/PdfSharp/Pdf.Internal/RawEncoding.cs
Normal file
165
PrintPDF/PdfSharp/Pdf.Internal/RawEncoding.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
#region PDFsharp - A .NET library for processing PDF
|
||||
//
|
||||
// Authors:
|
||||
// Stefan Lange
|
||||
//
|
||||
// Copyright (c) 2005-2017 empira Software GmbH, Cologne Area (Germany)
|
||||
//
|
||||
// http://www.pdfsharp.com
|
||||
// http://sourceforge.net/projects/pdfsharp
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace PdfSharp.Pdf.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// An encoder for raw strings. The raw encoding is simply the identity relation between
|
||||
/// characters and bytes. PDFsharp internally works with raw encoded strings instead of
|
||||
/// byte arrays because strings are much more handy than byte arrays.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Raw encoded strings represent an array of bytes. Therefore a character greater than
|
||||
/// 255 is not valid in a raw encoded string.
|
||||
/// </remarks>
|
||||
public sealed class RawEncoding : Encoding
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RawEncoding"/> class.
|
||||
/// </summary>
|
||||
// ReSharper disable EmptyConstructor
|
||||
public RawEncoding()
|
||||
{ }
|
||||
// ReSharper restore EmptyConstructor
|
||||
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified character array.
|
||||
/// </summary>
|
||||
/// <param name="chars">The character array containing the set of characters to encode.</param>
|
||||
/// <param name="index">The index of the first character to encode.</param>
|
||||
/// <param name="count">The number of characters to encode.</param>
|
||||
/// <returns>
|
||||
/// The number of bytes produced by encoding the specified characters.
|
||||
/// </returns>
|
||||
public override int GetByteCount(char[] chars, int index, int count)
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, encodes a set of characters from the specified character array into the specified byte array.
|
||||
/// </summary>
|
||||
/// <param name="chars">The character array containing the set of characters to encode.</param>
|
||||
/// <param name="charIndex">The index of the first character to encode.</param>
|
||||
/// <param name="charCount">The number of characters to encode.</param>
|
||||
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
|
||||
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
|
||||
/// <returns>
|
||||
/// The actual number of bytes written into <paramref name="bytes"/>.
|
||||
/// </returns>
|
||||
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
|
||||
{
|
||||
for (int count = charCount; count > 0; charIndex++, byteIndex++, count--)
|
||||
{
|
||||
#if DEBUG_
|
||||
if ((uint) chars[charIndex] > 255)
|
||||
Debug-Break.Break(true);
|
||||
#endif
|
||||
//Debug.Assert((uint)chars[charIndex] < 256, "Raw string contains invalid character with a value > 255.");
|
||||
bytes[byteIndex] = (byte)chars[charIndex];
|
||||
//#warning Here is a HACK that must not be ignored!
|
||||
// HACK:
|
||||
// bytes[byteIndex] = (byte)chars[charIndex];
|
||||
}
|
||||
return charCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
|
||||
/// <param name="index">The index of the first byte to decode.</param>
|
||||
/// <param name="count">The number of bytes to decode.</param>
|
||||
/// <returns>
|
||||
/// The number of characters produced by decoding the specified sequence of bytes.
|
||||
/// </returns>
|
||||
public override int GetCharCount(byte[] bytes, int index, int count)
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, decodes a sequence of bytes from the specified byte array into the specified character array.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
|
||||
/// <param name="byteIndex">The index of the first byte to decode.</param>
|
||||
/// <param name="byteCount">The number of bytes to decode.</param>
|
||||
/// <param name="chars">The character array to contain the resulting set of characters.</param>
|
||||
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
|
||||
/// <returns>
|
||||
/// The actual number of characters written into <paramref name="chars"/>.
|
||||
/// </returns>
|
||||
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
|
||||
{
|
||||
for (int count = byteCount; count > 0; byteIndex++, charIndex++, count--)
|
||||
chars[charIndex] = (char)bytes[byteIndex];
|
||||
return byteCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, calculates the maximum number of bytes produced by encoding the specified number of characters.
|
||||
/// </summary>
|
||||
/// <param name="charCount">The number of characters to encode.</param>
|
||||
/// <returns>
|
||||
/// The maximum number of bytes produced by encoding the specified number of characters.
|
||||
/// </returns>
|
||||
public override int GetMaxByteCount(int charCount)
|
||||
{
|
||||
return charCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, calculates the maximum number of characters produced by decoding the specified number of bytes.
|
||||
/// </summary>
|
||||
/// <param name="byteCount">The number of bytes to decode.</param>
|
||||
/// <returns>
|
||||
/// The maximum number of characters produced by decoding the specified number of bytes.
|
||||
/// </returns>
|
||||
public override int GetMaxCharCount(int byteCount)
|
||||
{
|
||||
return byteCount;
|
||||
}
|
||||
|
||||
#if SILVERLIGHT
|
||||
/// <summary>
|
||||
/// When overridden in a derived class, decodes all the bytes in the specified byte array into a string.
|
||||
/// </summary>
|
||||
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
|
||||
/// <returns>
|
||||
/// A <see cref="T:System.String"/> containing the results of decoding the specified sequence of bytes.
|
||||
/// </returns>
|
||||
public string GetString(byte[] bytes)
|
||||
{
|
||||
return GetString(bytes, 0, bytes.Length);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
84
PrintPDF/PdfSharp/Pdf.Internal/RawUnicodeEncoding.cs
Normal file
84
PrintPDF/PdfSharp/Pdf.Internal/RawUnicodeEncoding.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
#region PDFsharp - A .NET library for processing PDF
|
||||
//
|
||||
// Authors:
|
||||
// Stefan Lange
|
||||
//
|
||||
// Copyright (c) 2005-2017 empira Software GmbH, Cologne Area (Germany)
|
||||
//
|
||||
// http://www.pdfsharp.com
|
||||
// http://sourceforge.net/projects/pdfsharp
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System.Text;
|
||||
|
||||
namespace PdfSharp.Pdf.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// An encoder for Unicode strings.
|
||||
/// (That means, a character represents a glyph index.)
|
||||
/// </summary>
|
||||
internal sealed class RawUnicodeEncoding : Encoding
|
||||
{
|
||||
public RawUnicodeEncoding()
|
||||
{ }
|
||||
|
||||
public override int GetByteCount(char[] chars, int index, int count)
|
||||
{
|
||||
// Each character represents exactly an ushort value, which is a glyph index.
|
||||
return 2 * count;
|
||||
}
|
||||
|
||||
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
|
||||
{
|
||||
for (int count = charCount; count > 0; charIndex++, count--)
|
||||
{
|
||||
char ch = chars[charIndex];
|
||||
bytes[byteIndex++] = (byte)(ch >> 8);
|
||||
bytes[byteIndex++] = (byte)ch;
|
||||
}
|
||||
return charCount * 2;
|
||||
}
|
||||
|
||||
public override int GetCharCount(byte[] bytes, int index, int count)
|
||||
{
|
||||
return count / 2;
|
||||
}
|
||||
|
||||
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
|
||||
{
|
||||
for (int count = byteCount; count > 0; byteIndex += 2, charIndex++, count--)
|
||||
{
|
||||
chars[charIndex] = (char)((int)bytes[byteIndex] << 8 + (int)bytes[byteIndex + 1]);
|
||||
}
|
||||
return byteCount;
|
||||
}
|
||||
|
||||
public override int GetMaxByteCount(int charCount)
|
||||
{
|
||||
return charCount * 2;
|
||||
}
|
||||
|
||||
public override int GetMaxCharCount(int byteCount)
|
||||
{
|
||||
return byteCount / 2;
|
||||
}
|
||||
}
|
||||
}
|
128
PrintPDF/PdfSharp/Pdf.Internal/ThreadLocalStorage.cs
Normal file
128
PrintPDF/PdfSharp/Pdf.Internal/ThreadLocalStorage.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
#region PDFsharp - A .NET library for processing PDF
|
||||
//
|
||||
// Authors:
|
||||
// Stefan Lange
|
||||
//
|
||||
// Copyright (c) 2005-2017 empira Software GmbH, Cologne Area (Germany)
|
||||
//
|
||||
// http://www.pdfsharp.com
|
||||
// http://sourceforge.net/projects/pdfsharp
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the "Software"),
|
||||
// to deal in the Software without restriction, including without limitation
|
||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
// and/or sell copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using PdfSharp.Pdf.IO;
|
||||
|
||||
namespace PdfSharp.Pdf.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a thread-local cache for large objects.
|
||||
/// </summary>
|
||||
internal class ThreadLocalStorage // #???
|
||||
{
|
||||
public ThreadLocalStorage()
|
||||
{
|
||||
_importedDocuments = new Dictionary<string, PdfDocument.DocumentHandle>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public void AddDocument(string path, PdfDocument document)
|
||||
{
|
||||
_importedDocuments.Add(path, document.Handle);
|
||||
}
|
||||
|
||||
public void RemoveDocument(string path)
|
||||
{
|
||||
_importedDocuments.Remove(path);
|
||||
}
|
||||
|
||||
public PdfDocument GetDocument(string path)
|
||||
{
|
||||
Debug.Assert(path.StartsWith("*") || Path.IsPathRooted(path), "Path must be full qualified.");
|
||||
|
||||
PdfDocument document = null;
|
||||
PdfDocument.DocumentHandle handle;
|
||||
if (_importedDocuments.TryGetValue(path, out handle))
|
||||
{
|
||||
document = handle.Target;
|
||||
if (document == null)
|
||||
RemoveDocument(path);
|
||||
}
|
||||
if (document == null)
|
||||
{
|
||||
document = PdfReader.Open(path, PdfDocumentOpenMode.Import);
|
||||
_importedDocuments.Add(path, document.Handle);
|
||||
}
|
||||
return document;
|
||||
}
|
||||
|
||||
public PdfDocument[] Documents
|
||||
{
|
||||
get
|
||||
{
|
||||
List<PdfDocument> list = new List<PdfDocument>();
|
||||
foreach (PdfDocument.DocumentHandle handle in _importedDocuments.Values)
|
||||
{
|
||||
if (handle.IsAlive)
|
||||
list.Add(handle.Target);
|
||||
}
|
||||
return list.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public void DetachDocument(PdfDocument.DocumentHandle handle)
|
||||
{
|
||||
if (handle.IsAlive)
|
||||
{
|
||||
foreach (string path in _importedDocuments.Keys)
|
||||
{
|
||||
if (_importedDocuments[path] == handle)
|
||||
{
|
||||
_importedDocuments.Remove(path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clean table
|
||||
bool itemRemoved = true;
|
||||
while (itemRemoved)
|
||||
{
|
||||
itemRemoved = false;
|
||||
foreach (string path in _importedDocuments.Keys)
|
||||
{
|
||||
if (!_importedDocuments[path].IsAlive)
|
||||
{
|
||||
_importedDocuments.Remove(path);
|
||||
itemRemoved = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps path to document handle.
|
||||
/// </summary>
|
||||
readonly Dictionary<string, PdfDocument.DocumentHandle> _importedDocuments;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user