internal static void GetFontData(Font font, Stream inputStream) { int len = inputStream.ReadByte(); byte[] fontName = new byte[len]; inputStream.Read(fontName, 0, len); font.name = System.Text.Encoding.UTF8.GetString(fontName, 0, len); len = GetInt24(inputStream); byte[] fontInfo = new byte[len]; inputStream.Read(fontInfo, 0, len); font.info = System.Text.Encoding.UTF8.GetString(fontInfo, 0, len); byte[] buf = new byte[GetInt32(inputStream)]; inputStream.Read(buf, 0, buf.Length); MemoryStream stream = new MemoryStream(Decompressor.inflate(buf)); font.unitsPerEm = GetInt32(stream); font.bBoxLLx = GetInt32(stream); font.bBoxLLy = GetInt32(stream); font.bBoxURx = GetInt32(stream); font.bBoxURy = GetInt32(stream); font.fontAscent = GetInt32(stream); font.fontDescent = GetInt32(stream); font.firstChar = GetInt32(stream); font.lastChar = GetInt32(stream); font.capHeight = GetInt32(stream); font.fontUnderlinePosition = GetInt32(stream); font.fontUnderlineThickness = GetInt32(stream); len = GetInt32(stream); font.advanceWidth = new int[len]; for (int i = 0; i < len; i++) { font.advanceWidth[i] = GetInt16(stream); } len = GetInt32(stream); font.glyphWidth = new int[len]; for (int i = 0; i < len; i++) { font.glyphWidth[i] = GetInt16(stream); } len = GetInt32(stream); font.unicodeToGID = new int[len]; for (int i = 0; i < len; i++) { font.unicodeToGID[i] = GetInt16(stream); } font.cff = (inputStream.ReadByte() == 'Y') ? true : false; font.uncompressedSize = GetInt32(inputStream); font.compressedSize = GetInt32(inputStream); }
internal void SetStreamAndData(byte[] buf, int length) { if (this.stream == null) { this.stream = new byte[length]; Array.Copy(buf, streamOffset, stream, 0, length); if (GetValue("/Filter").Equals("/FlateDecode")) { this.data = Decompressor.inflate(stream); } else { // Assume no compression for now. // In the future we may handle LZW decompression ... this.data = stream; } } }
/** * Used to embed PNG images in the PDF document. * */ public PNGImage(Stream inputStream) { ValidatePNG(inputStream); List <Chunk> chunks = ProcessPNG(inputStream); for (int i = 0; i < chunks.Count; i++) { Chunk chunk = chunks[i]; String chunkType = System.Text.Encoding.UTF8.GetString(chunk.type); if (chunkType.Equals("IHDR")) { this.w = (int)ToUInt32(chunk.GetData(), 0); // Width this.h = (int)ToUInt32(chunk.GetData(), 4); // Height this.bitDepth = chunk.GetData()[8]; // Bit Depth this.colorType = chunk.GetData()[9]; // Color Type // Console.WriteLine( // "Bit Depth == " + chunk.GetData()[8]); // Console.WriteLine( // "Color Type == " + chunk.GetData()[9]); // Console.WriteLine(chunk.GetData()[10]); // Console.WriteLine(chunk.GetData()[11]); // Console.WriteLine(chunk.GetData()[12]); if (chunk.GetData()[12] == 1) { Console.WriteLine("Interlaced PNG images are not supported."); Console.WriteLine("Convert the image using OptiPNG:\noptipng -i0 -o7 myimage.png\n"); } } else if (chunkType.Equals("IDAT")) { iDAT = AppendIdatChunk(iDAT, chunk.GetData()); } else if (chunkType.Equals("PLTE")) { pLTE = chunk.GetData(); if (pLTE.Length % 3 != 0) { throw new Exception("Incorrect palette length."); } } else if (chunkType.Equals("gAMA")) { // TODO: // Console.WriteLine("gAMA chunk found!"); } else if (chunkType.Equals("tRNS")) { // Console.WriteLine("tRNS chunk found!"); if (colorType == 3) { tRNS = chunk.GetData(); } } else if (chunkType.Equals("cHRM")) { // TODO: // Console.WriteLine("cHRM chunk found!"); } else if (chunkType.Equals("sBIT")) { // TODO: // Console.WriteLine("sBIT chunk found!"); } else if (chunkType.Equals("bKGD")) { // TODO: // Console.WriteLine("bKGD chunk found!"); } } byte[] inflatedImageData = Decompressor.inflate(iDAT); byte[] imageData; if (colorType == 0) { // Grayscale Image if (bitDepth == 16) { imageData = GetImageColorType0BitDepth16(inflatedImageData); } else if (bitDepth == 8) { imageData = GetImageColorType0BitDepth8(inflatedImageData); } else if (bitDepth == 4) { imageData = GetImageColorType0BitDepth4(inflatedImageData); } else if (bitDepth == 2) { imageData = GetImageColorType0BitDepth2(inflatedImageData); } else if (bitDepth == 1) { imageData = GetImageColorType0BitDepth1(inflatedImageData); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } else if (colorType == 6) { if (bitDepth == 8) { imageData = GetImageColorType6BitDepth8(inflatedImageData); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } else { // Color Image if (pLTE == null) { // Trucolor Image if (bitDepth == 16) { imageData = GetImageColorType2BitDepth16(inflatedImageData); } else { imageData = GetImageColorType2BitDepth8(inflatedImageData); } } else { // Indexed Image if (bitDepth == 8) { imageData = GetImageColorType3BitDepth8(inflatedImageData); } else if (bitDepth == 4) { imageData = GetImageColorType3BitDepth4(inflatedImageData); } else if (bitDepth == 2) { imageData = GetImageColorType3BitDepth2(inflatedImageData); } else if (bitDepth == 1) { imageData = GetImageColorType3BitDepth1(inflatedImageData); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } } deflatedImageData = Compressor.deflate(imageData); }