public void Add(float number)
 {
     if (number.ApproxNotEqual(0))
     {
         if (!float.IsNaN(_lastNum))
         {
             _lastNum += number;
             if (_lastNum.ApproxNotEqual(0))
             {
                 replaceLast(_lastNum);
             }
             else
             {
                 ArrayList.RemoveAt(ArrayList.Count - 1);
             }
         }
         else
         {
             _lastNum = number;
             ArrayList.Add(_lastNum);
         }
         _lastStr = null;
     }
     // adding zero doesn't modify the TextArray at all
 }
 private float getVariableBorderWidth(float variableWidthValue, int side)
 {
     if ((border & side) != 0)
     {
         return(variableWidthValue.ApproxNotEqual(UNDEFINED) ? variableWidthValue : borderWidth);
     }
     return(0);
 }
示例#3
0
        /// <summary>
        /// Compares this Font with another
        /// </summary>
        /// <param name="obj">the other Font</param>
        /// <returns>a value</returns>
        public virtual int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(-1);
            }
            Font font;

            try
            {
                font = (Font)obj;
                if (_baseFont != null && !_baseFont.Equals(font.BaseFont))
                {
                    return(-2);
                }
                if (_family != font.Family)
                {
                    return(1);
                }
                if (_size.ApproxNotEqual(font.Size))
                {
                    return(2);
                }
                if (_style != font.Style)
                {
                    return(3);
                }
                if (_color == null)
                {
                    if (font.Color == null)
                    {
                        return(0);
                    }
                    return(4);
                }
                if (font.Color == null)
                {
                    return(4);
                }
                if (_color.Equals(font.Color))
                {
                    return(0);
                }
                return(4);
            }
            catch
            {
                return(-3);
            }
        }
示例#4
0
        /// <summary>
        /// Writes the content of this RtfChunk. First the font information
        /// is written, then the content, and then more font information
        /// </summary>
        public override void WriteContent(Stream result)
        {
            byte[] t;
            if (_background != null)
            {
                result.Write(OpenGroup, 0, OpenGroup.Length);
            }

            _font.WriteBegin(result);
            if (_superSubScript < 0)
            {
                result.Write(_fontSubscript, 0, _fontSubscript.Length);
            }
            else if (_superSubScript > 0)
            {
                result.Write(_fontSuperscript, 0, _fontSuperscript.Length);
            }
            if (_background != null)
            {
                result.Write(_backgroundColor, 0, _backgroundColor.Length);
                result.Write(t = IntToByteArray(_background.GetColorNumber()), 0, t.Length);
            }
            result.Write(Delimiter, 0, Delimiter.Length);

            Document.FilterSpecialChar(result, _content, false, _softLineBreaks || Document.GetDocumentSettings().IsAlwaysGenerateSoftLinebreaks());

            if (_superSubScript.ApproxNotEqual(0))
            {
                result.Write(_fontEndSuperSubscript, 0, _fontEndSuperSubscript.Length);
            }
            _font.WriteEnd(result);

            if (_background != null)
            {
                result.Write(CloseGroup, 0, CloseGroup.Length);
            }
        }
示例#5
0
        public void SetMkIconFit(PdfName scale, PdfName scalingType, float leftoverLeft, float leftoverBottom, bool fitInBounds)
        {
            PdfDictionary dic = new PdfDictionary();

            if (!scale.Equals(PdfName.A))
            {
                dic.Put(PdfName.Sw, scale);
            }
            if (!scalingType.Equals(PdfName.P))
            {
                dic.Put(PdfName.S, scalingType);
            }
            if (leftoverLeft.ApproxNotEqual(0.5f) || leftoverBottom.ApproxNotEqual(0.5f))
            {
                PdfArray array = new PdfArray(new PdfNumber(leftoverLeft));
                array.Add(new PdfNumber(leftoverBottom));
                dic.Put(PdfName.A, array);
            }
            if (fitInBounds)
            {
                dic.Put(PdfName.Fb, PdfBoolean.Pdftrue);
            }
            Mk.Put(PdfName.If, dic);
        }
示例#6
0
        /// <summary>
        /// Reads a page from a TIFF image.
        /// by direct byte copying. It's faster but may not work
        /// every time
        /// </summary>
        /// <param name="s">the file source</param>
        /// <param name="page">the page to get. The first page is 1</param>
        /// <param name="direct">for single strip, CCITT images, generate the image</param>
        /// <returns>the  Image </returns>
        public static Image GetTiffImage(RandomAccessFileOrArray s, int page, bool direct)
        {
            if (page < 1)
            {
                throw new InvalidOperationException("The page number must be >= 1.");
            }

            var dir = new TiffDirectory(s, page - 1);

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_TILEWIDTH))
            {
                throw new InvalidOperationException("Tiles are not supported.");
            }

            var compression = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_COMPRESSION);

            switch (compression)
            {
            case TiffConstants.COMPRESSION_CCITTRLEW:
            case TiffConstants.COMPRESSION_CCITTRLE:
            case TiffConstants.COMPRESSION_CCITTFAX3:
            case TiffConstants.COMPRESSION_CCITTFAX4:
                break;

            default:
                return(GetTiffImageColor(dir, s));
            }
            float rotation = 0;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_ORIENTATION))
            {
                var rot = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_ORIENTATION);
                if (rot == TiffConstants.ORIENTATION_BOTRIGHT || rot == TiffConstants.ORIENTATION_BOTLEFT)
                {
                    rotation = (float)Math.PI;
                }
                else if (rot == TiffConstants.ORIENTATION_LEFTTOP || rot == TiffConstants.ORIENTATION_LEFTBOT)
                {
                    rotation = (float)(Math.PI / 2.0);
                }
                else if (rot == TiffConstants.ORIENTATION_RIGHTTOP || rot == TiffConstants.ORIENTATION_RIGHTBOT)
                {
                    rotation = -(float)(Math.PI / 2.0);
                }
            }

            Image img            = null;
            long  tiffT4Options  = 0;
            long  tiffT6Options  = 0;
            var   fillOrder      = 1;
            var   h              = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_IMAGELENGTH);
            var   w              = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_IMAGEWIDTH);
            var   dpiX           = 0;
            var   dpiY           = 0;
            float xyRatio        = 0;
            var   resolutionUnit = TiffConstants.RESUNIT_INCH;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_RESOLUTIONUNIT))
            {
                resolutionUnit = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_RESOLUTIONUNIT);
            }

            dpiX = getDpi(dir.GetField(TiffConstants.TIFFTAG_XRESOLUTION), resolutionUnit);
            dpiY = getDpi(dir.GetField(TiffConstants.TIFFTAG_YRESOLUTION), resolutionUnit);
            if (resolutionUnit == TiffConstants.RESUNIT_NONE)
            {
                if (dpiY != 0)
                {
                    xyRatio = dpiX / (float)dpiY;
                }

                dpiX = 0;
                dpiY = 0;
            }
            var rowsStrip = h;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_ROWSPERSTRIP))
            {
                rowsStrip = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_ROWSPERSTRIP);
            }

            if (rowsStrip <= 0 || rowsStrip > h)
            {
                rowsStrip = h;
            }

            var offset = getArrayLongShort(dir, TiffConstants.TIFFTAG_STRIPOFFSETS);
            var size   = getArrayLongShort(dir, TiffConstants.TIFFTAG_STRIPBYTECOUNTS);

            if ((size == null || (size.Length == 1 && (size[0] == 0 || size[0] + offset[0] > s.Length))) && h == rowsStrip)
            { // some TIFF producers are really lousy, so...
                size = new long[] { s.Length - (int)offset[0] };
            }
            var reverse        = false;
            var fillOrderField = dir.GetField(TiffConstants.TIFFTAG_FILLORDER);

            if (fillOrderField != null)
            {
                fillOrder = fillOrderField.GetAsInt(0);
            }

            reverse = (fillOrder == TiffConstants.FILLORDER_LSB2MSB);
            var paramsn = 0;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_PHOTOMETRIC))
            {
                var photo = dir.GetFieldAsLong(TiffConstants.TIFFTAG_PHOTOMETRIC);
                if (photo == TiffConstants.PHOTOMETRIC_MINISBLACK)
                {
                    paramsn |= Element.CCITT_BLACKIS1;
                }
            }
            var imagecomp = 0;

            switch (compression)
            {
            case TiffConstants.COMPRESSION_CCITTRLEW:
            case TiffConstants.COMPRESSION_CCITTRLE:
                imagecomp = Element.CCITTG3_1D;
                paramsn  |= Element.CCITT_ENCODEDBYTEALIGN | Element.CCITT_ENDOFBLOCK;
                break;

            case TiffConstants.COMPRESSION_CCITTFAX3:
                imagecomp = Element.CCITTG3_1D;
                paramsn  |= Element.CCITT_ENDOFLINE | Element.CCITT_ENDOFBLOCK;
                var t4OptionsField = dir.GetField(TiffConstants.TIFFTAG_GROUP3OPTIONS);
                if (t4OptionsField != null)
                {
                    tiffT4Options = t4OptionsField.GetAsLong(0);
                    if ((tiffT4Options & TiffConstants.GROUP3OPT_2DENCODING) != 0)
                    {
                        imagecomp = Element.CCITTG3_2D;
                    }

                    if ((tiffT4Options & TiffConstants.GROUP3OPT_FILLBITS) != 0)
                    {
                        paramsn |= Element.CCITT_ENCODEDBYTEALIGN;
                    }
                }
                break;

            case TiffConstants.COMPRESSION_CCITTFAX4:
                imagecomp = Element.CCITTG4;
                var t6OptionsField = dir.GetField(TiffConstants.TIFFTAG_GROUP4OPTIONS);
                if (t6OptionsField != null)
                {
                    tiffT6Options = t6OptionsField.GetAsLong(0);
                }

                break;
            }
            if (direct && rowsStrip == h)
            { //single strip, direct
                var im = new byte[(int)size[0]];
                s.Seek(offset[0]);
                s.ReadFully(im);
                img          = Image.GetInstance(w, h, false, imagecomp, paramsn, im);
                img.Inverted = true;
            }
            else
            {
                var rowsLeft = h;
                var g4       = new Ccittg4Encoder(w);
                for (var k = 0; k < offset.Length; ++k)
                {
                    var im = new byte[(int)size[k]];
                    s.Seek(offset[k]);
                    s.ReadFully(im);
                    var height  = Math.Min(rowsStrip, rowsLeft);
                    var decoder = new TiffFaxDecoder(fillOrder, w, height);
                    var outBuf  = new byte[(w + 7) / 8 * height];
                    switch (compression)
                    {
                    case TiffConstants.COMPRESSION_CCITTRLEW:
                    case TiffConstants.COMPRESSION_CCITTRLE:
                        decoder.Decode1D(outBuf, im, 0, height);
                        g4.Fax4Encode(outBuf, height);
                        break;

                    case TiffConstants.COMPRESSION_CCITTFAX3:
                        try
                        {
                            decoder.Decode2D(outBuf, im, 0, height, tiffT4Options);
                        }
                        catch (Exception e)
                        {
                            // let's flip the fill bits and try again...
                            tiffT4Options ^= TiffConstants.GROUP3OPT_FILLBITS;
                            try
                            {
                                decoder.Decode2D(outBuf, im, 0, height, tiffT4Options);
                            }
                            catch
                            {
                                throw e;
                            }
                        }
                        g4.Fax4Encode(outBuf, height);
                        break;

                    case TiffConstants.COMPRESSION_CCITTFAX4:
                        decoder.DecodeT6(outBuf, im, 0, height, tiffT6Options);
                        g4.Fax4Encode(outBuf, height);
                        break;
                    }
                    rowsLeft -= rowsStrip;
                }
                var g4Pic = g4.Close();
                img = Image.GetInstance(w, h, false, Element.CCITTG4, paramsn & Element.CCITT_BLACKIS1, g4Pic);
            }
            img.SetDpi(dpiX, dpiY);
            img.XyRatio = xyRatio;
            if (dir.IsTagPresent(TiffConstants.TIFFTAG_ICCPROFILE))
            {
                try
                {
                    var fd      = dir.GetField(TiffConstants.TIFFTAG_ICCPROFILE);
                    var iccProf = IccProfile.GetInstance(fd.GetAsBytes());
                    if (iccProf.NumComponents == 1)
                    {
                        img.TagIcc = iccProf;
                    }
                }
                catch
                {
                    //empty
                }
            }
            img.OriginalType = Image.ORIGINAL_TIFF;
            if (rotation.ApproxNotEqual(0))
            {
                img.InitialRotation = rotation;
            }

            return(img);
        }
示例#7
0
        protected static Image GetTiffImageColor(TiffDirectory dir, RandomAccessFileOrArray s)
        {
            var            predictor   = 1;
            TifflzwDecoder lzwDecoder  = null;
            var            compression = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_COMPRESSION);

            switch (compression)
            {
            case TiffConstants.COMPRESSION_NONE:
            case TiffConstants.COMPRESSION_LZW:
            case TiffConstants.COMPRESSION_PACKBITS:
            case TiffConstants.COMPRESSION_DEFLATE:
            case TiffConstants.COMPRESSION_ADOBE_DEFLATE:
            case TiffConstants.COMPRESSION_OJPEG:
            case TiffConstants.COMPRESSION_JPEG:
                break;

            default:
                throw new InvalidOperationException("The compression " + compression + " is not supported.");
            }
            var photometric = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_PHOTOMETRIC);

            switch (photometric)
            {
            case TiffConstants.PHOTOMETRIC_MINISWHITE:
            case TiffConstants.PHOTOMETRIC_MINISBLACK:
            case TiffConstants.PHOTOMETRIC_RGB:
            case TiffConstants.PHOTOMETRIC_SEPARATED:
            case TiffConstants.PHOTOMETRIC_PALETTE:
                break;

            default:
                if (compression != TiffConstants.COMPRESSION_OJPEG && compression != TiffConstants.COMPRESSION_JPEG)
                {
                    throw new InvalidOperationException("The photometric " + photometric + " is not supported.");
                }

                break;
            }
            float rotation = 0;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_ORIENTATION))
            {
                var rot = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_ORIENTATION);
                if (rot == TiffConstants.ORIENTATION_BOTRIGHT || rot == TiffConstants.ORIENTATION_BOTLEFT)
                {
                    rotation = (float)Math.PI;
                }
                else if (rot == TiffConstants.ORIENTATION_LEFTTOP || rot == TiffConstants.ORIENTATION_LEFTBOT)
                {
                    rotation = (float)(Math.PI / 2.0);
                }
                else if (rot == TiffConstants.ORIENTATION_RIGHTTOP || rot == TiffConstants.ORIENTATION_RIGHTBOT)
                {
                    rotation = -(float)(Math.PI / 2.0);
                }
            }

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_PLANARCONFIG) &&
                dir.GetFieldAsLong(TiffConstants.TIFFTAG_PLANARCONFIG) == TiffConstants.PLANARCONFIG_SEPARATE)
            {
                throw new InvalidOperationException("Planar images are not supported.");
            }

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_EXTRASAMPLES))
            {
                throw new InvalidOperationException("Extra samples are not supported.");
            }

            var samplePerPixel = 1;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_SAMPLESPERPIXEL)) // 1,3,4
            {
                samplePerPixel = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_SAMPLESPERPIXEL);
            }

            var bitsPerSample = 1;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_BITSPERSAMPLE))
            {
                bitsPerSample = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_BITSPERSAMPLE);
            }

            switch (bitsPerSample)
            {
            case 1:
            case 2:
            case 4:
            case 8:
                break;

            default:
                throw new InvalidOperationException("Bits per sample " + bitsPerSample + " is not supported.");
            }
            Image img = null;

            var h              = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_IMAGELENGTH);
            var w              = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_IMAGEWIDTH);
            var dpiX           = 0;
            var dpiY           = 0;
            var resolutionUnit = TiffConstants.RESUNIT_INCH;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_RESOLUTIONUNIT))
            {
                resolutionUnit = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_RESOLUTIONUNIT);
            }

            dpiX = getDpi(dir.GetField(TiffConstants.TIFFTAG_XRESOLUTION), resolutionUnit);
            dpiY = getDpi(dir.GetField(TiffConstants.TIFFTAG_YRESOLUTION), resolutionUnit);
            var fillOrder      = 1;
            var reverse        = false;
            var fillOrderField = dir.GetField(TiffConstants.TIFFTAG_FILLORDER);

            if (fillOrderField != null)
            {
                fillOrder = fillOrderField.GetAsInt(0);
            }

            reverse = (fillOrder == TiffConstants.FILLORDER_LSB2MSB);
            var rowsStrip = h;

            if (dir.IsTagPresent(TiffConstants.TIFFTAG_ROWSPERSTRIP)) //another hack for broken tiffs
            {
                rowsStrip = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_ROWSPERSTRIP);
            }

            if (rowsStrip <= 0 || rowsStrip > h)
            {
                rowsStrip = h;
            }

            var offset = getArrayLongShort(dir, TiffConstants.TIFFTAG_STRIPOFFSETS);
            var size   = getArrayLongShort(dir, TiffConstants.TIFFTAG_STRIPBYTECOUNTS);

            if ((size == null || (size.Length == 1 && (size[0] == 0 || size[0] + offset[0] > s.Length))) && h == rowsStrip)
            { // some TIFF producers are really lousy, so...
                size = new long[] { s.Length - (int)offset[0] };
            }
            if (compression == TiffConstants.COMPRESSION_LZW)
            {
                var predictorField = dir.GetField(TiffConstants.TIFFTAG_PREDICTOR);
                if (predictorField != null)
                {
                    predictor = predictorField.GetAsInt(0);
                    if (predictor != 1 && predictor != 2)
                    {
                        throw new InvalidOperationException("Illegal value for Predictor in TIFF file.");
                    }
                    if (predictor == 2 && bitsPerSample != 8)
                    {
                        throw new InvalidOperationException(bitsPerSample + "-bit samples are not supported for Horizontal differencing Predictor.");
                    }
                }
                lzwDecoder = new TifflzwDecoder(w, predictor,
                                                samplePerPixel);
            }
            var                   rowsLeft = h;
            MemoryStream          stream   = null;
            ZDeflaterOutputStream zip      = null;
            Ccittg4Encoder        g4       = null;

            if (bitsPerSample == 1 && samplePerPixel == 1)
            {
                g4 = new Ccittg4Encoder(w);
            }
            else
            {
                stream = new MemoryStream();
                if (compression != TiffConstants.COMPRESSION_OJPEG && compression != TiffConstants.COMPRESSION_JPEG)
                {
                    zip = new ZDeflaterOutputStream(stream);
                }
            }
            if (compression == TiffConstants.COMPRESSION_OJPEG)
            {
                // Assume that the TIFFTAG_JPEGIFBYTECOUNT tag is optional, since it's obsolete and
                // is often missing

                if ((!dir.IsTagPresent(TiffConstants.TIFFTAG_JPEGIFOFFSET)))
                {
                    throw new IOException("Missing tag(s) for OJPEG compression.");
                }
                var jpegOffset = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_JPEGIFOFFSET);
                var jpegLength = s.Length - jpegOffset;

                if (dir.IsTagPresent(TiffConstants.TIFFTAG_JPEGIFBYTECOUNT))
                {
                    jpegLength = (int)dir.GetFieldAsLong(TiffConstants.TIFFTAG_JPEGIFBYTECOUNT) +
                                 (int)size[0];
                }

                var jpeg = new byte[Math.Min(jpegLength, s.Length - jpegOffset)];

                var posFilePointer = s.FilePointer;
                posFilePointer += jpegOffset;
                s.Seek(posFilePointer);
                s.ReadFully(jpeg);
                img = new Jpeg(jpeg);
            }
            else if (compression == TiffConstants.COMPRESSION_JPEG)
            {
                if (size.Length > 1)
                {
                    throw new IOException("Compression JPEG is only supported with a single strip. This image has " + size.Length + " strips.");
                }

                var jpeg = new byte[(int)size[0]];
                s.Seek(offset[0]);
                s.ReadFully(jpeg);
                img = new Jpeg(jpeg);
            }
            else
            {
                for (var k = 0; k < offset.Length; ++k)
                {
                    var im = new byte[(int)size[k]];
                    s.Seek(offset[k]);
                    s.ReadFully(im);
                    var    height = Math.Min(rowsStrip, rowsLeft);
                    byte[] outBuf = null;
                    if (compression != TiffConstants.COMPRESSION_NONE)
                    {
                        outBuf = new byte[(w * bitsPerSample * samplePerPixel + 7) / 8 * height];
                    }

                    if (reverse)
                    {
                        TiffFaxDecoder.ReverseBits(im);
                    }

                    switch (compression)
                    {
                    case TiffConstants.COMPRESSION_DEFLATE:
                    case TiffConstants.COMPRESSION_ADOBE_DEFLATE:
                        Inflate(im, outBuf);
                        break;

                    case TiffConstants.COMPRESSION_NONE:
                        outBuf = im;
                        break;

                    case TiffConstants.COMPRESSION_PACKBITS:
                        DecodePackbits(im, outBuf);
                        break;

                    case TiffConstants.COMPRESSION_LZW:
                        lzwDecoder.Decode(im, outBuf, height);
                        break;
                    }
                    if (bitsPerSample == 1 && samplePerPixel == 1)
                    {
                        g4.Fax4Encode(outBuf, height);
                    }
                    else
                    {
                        zip.Write(outBuf, 0, outBuf.Length);
                    }
                    rowsLeft -= rowsStrip;
                }
                if (bitsPerSample == 1 && samplePerPixel == 1)
                {
                    img = Image.GetInstance(w, h, false, Element.CCITTG4,
                                            photometric == TiffConstants.PHOTOMETRIC_MINISBLACK ? Element.CCITT_BLACKIS1 : 0, g4.Close());
                }
                else
                {
                    zip.Close();
                    img          = Image.GetInstance(w, h, samplePerPixel, bitsPerSample, stream.ToArray());
                    img.Deflated = true;
                }
            }
            img.SetDpi(dpiX, dpiY);
            if (compression != TiffConstants.COMPRESSION_OJPEG && compression != TiffConstants.COMPRESSION_JPEG)
            {
                if (dir.IsTagPresent(TiffConstants.TIFFTAG_ICCPROFILE))
                {
                    try
                    {
                        var fd      = dir.GetField(TiffConstants.TIFFTAG_ICCPROFILE);
                        var iccProf = IccProfile.GetInstance(fd.GetAsBytes());
                        if (samplePerPixel == iccProf.NumComponents)
                        {
                            img.TagIcc = iccProf;
                        }
                    }
                    catch
                    {
                        //empty
                    }
                }
                if (dir.IsTagPresent(TiffConstants.TIFFTAG_COLORMAP))
                {
                    var fd      = dir.GetField(TiffConstants.TIFFTAG_COLORMAP);
                    var rgb     = fd.GetAsChars();
                    var palette = new byte[rgb.Length];
                    var gColor  = rgb.Length / 3;
                    var bColor  = gColor * 2;
                    for (var k = 0; k < gColor; ++k)
                    {
                        palette[k * 3]     = (byte)(rgb[k] >> 8);
                        palette[k * 3 + 1] = (byte)(rgb[k + gColor] >> 8);
                        palette[k * 3 + 2] = (byte)(rgb[k + bColor] >> 8);
                    }
                    var indexed = new PdfArray();
                    indexed.Add(PdfName.Indexed);
                    indexed.Add(PdfName.Devicergb);
                    indexed.Add(new PdfNumber(gColor - 1));
                    indexed.Add(new PdfString(palette));
                    var additional = new PdfDictionary();
                    additional.Put(PdfName.Colorspace, indexed);
                    img.Additional = additional;
                }
                img.OriginalType = Image.ORIGINAL_TIFF;
            }
            if (photometric == TiffConstants.PHOTOMETRIC_MINISWHITE)
            {
                img.Inverted = true;
            }

            if (rotation.ApproxNotEqual(0))
            {
                img.InitialRotation = rotation;
            }

            return(img);
        }
        /// <summary>
        /// Writes the RtfImage content
        /// </summary>
        public override void WriteContent(Stream result)
        {
            byte[] t;
            if (_topLevelElement)
            {
                result.Write(RtfPhrase.ParagraphDefaults, 0, RtfPhrase.ParagraphDefaults.Length);
                switch (_alignment)
                {
                case Element.ALIGN_LEFT:
                    result.Write(RtfParagraphStyle.AlignLeft, 0, RtfParagraphStyle.AlignLeft.Length);
                    break;

                case Element.ALIGN_RIGHT:
                    result.Write(RtfParagraphStyle.AlignRight, 0, RtfParagraphStyle.AlignRight.Length);
                    break;

                case Element.ALIGN_CENTER:
                    result.Write(RtfParagraphStyle.AlignCenter, 0, RtfParagraphStyle.AlignCenter.Length);
                    break;

                case Element.ALIGN_JUSTIFIED:
                    result.Write(RtfParagraphStyle.AlignJustify, 0, RtfParagraphStyle.AlignJustify.Length);
                    break;
                }
            }
            result.Write(OpenGroup, 0, OpenGroup.Length);
            result.Write(_pictureGroup, 0, _pictureGroup.Length);
            result.Write(OpenGroup, 0, OpenGroup.Length);
            result.Write(_picture, 0, _picture.Length);
            switch (_imageType)
            {
            case Image.ORIGINAL_JPEG:
                result.Write(_pictureJpeg, 0, _pictureJpeg.Length);
                break;

            case Image.ORIGINAL_PNG:
            case Image.ORIGINAL_GIF:
                result.Write(_picturePng, 0, _picturePng.Length);
                break;

            case Image.ORIGINAL_WMF:
            case Image.ORIGINAL_BMP:
                result.Write(_pictureWmf, 0, _pictureWmf.Length);
                break;
            }
            result.Write(_pictureWidth, 0, _pictureWidth.Length);
            result.Write(t = IntToByteArray((int)_width), 0, t.Length);
            result.Write(_pictureHeight, 0, _pictureHeight.Length);
            result.Write(t = IntToByteArray((int)_height), 0, t.Length);
            if (Document.GetDocumentSettings().IsWriteImageScalingInformation())
            {
                result.Write(_pictureScaleX, 0, _pictureScaleX.Length);
                result.Write(t = IntToByteArray((int)(100 * _plainWidth / _width)), 0, t.Length);
                result.Write(_pictureScaleY, 0, _pictureScaleY.Length);
                result.Write(t = IntToByteArray((int)(100 * _plainHeight / _height)), 0, t.Length);
            }
            if (Document.GetDocumentSettings().IsImagePdfConformance())
            {
                result.Write(_pictureScaledWidth, 0, _pictureScaledWidth.Length);
                result.Write(t = IntToByteArray((int)(_plainWidth * TWIPS_FACTOR)), 0, t.Length);
                result.Write(_pictureScaledHeight, 0, _pictureScaledHeight.Length);
                result.Write(t = IntToByteArray((int)(_plainHeight * TWIPS_FACTOR)), 0, t.Length);
            }
            else
            {
                if (_width.ApproxNotEqual(_plainWidth) || _imageType == Image.ORIGINAL_BMP)
                {
                    result.Write(_pictureScaledWidth, 0, _pictureScaledWidth.Length);
                    result.Write(t = IntToByteArray((int)(_plainWidth * PixelTwipsFactor)), 0, t.Length);
                }
                if (_height.ApproxNotEqual(_plainHeight) || _imageType == Image.ORIGINAL_BMP)
                {
                    result.Write(_pictureScaledHeight, 0, _pictureScaledHeight.Length);
                    result.Write(t = IntToByteArray((int)(_plainHeight * PixelTwipsFactor)), 0, t.Length);
                }
            }

            if (Document.GetDocumentSettings().IsImageWrittenAsBinary())
            {
                //binary
                result.WriteByte((byte)'\n');
                result.Write(_pictureBinaryData, 0, _pictureBinaryData.Length);
                result.Write(t = IntToByteArray(imageDataSize()), 0, t.Length);
                result.Write(Delimiter, 0, Delimiter.Length);
                if (result is RtfByteArrayBuffer)
                {
                    ((RtfByteArrayBuffer)result).Append(_imageData);
                }
                else
                {
                    for (int k = 0; k < _imageData.Length; k++)
                    {
                        result.Write(_imageData[k], 0, _imageData[k].Length);
                    }
                }
            }
            else
            {
                //hex encoded
                result.Write(Delimiter, 0, Delimiter.Length);
                result.WriteByte((byte)'\n');
                writeImageDataHexEncoded(result);
            }
            result.Write(CloseGroup, 0, CloseGroup.Length);
            result.Write(CloseGroup, 0, CloseGroup.Length);
            if (_topLevelElement)
            {
                result.Write(RtfParagraph.Paragraph, 0, RtfParagraph.Paragraph.Length);
            }
            result.WriteByte((byte)'\n');
        }
示例#9
0
        private PdfObject getColorspace()
        {
            if (_iccProfile != null)
            {
                if ((_colorType & 2) == 0)
                {
                    return(PdfName.Devicegray);
                }
                else
                {
                    return(PdfName.Devicergb);
                }
            }
            if (_gamma.ApproxEquals(1f) && !_hasChrm)
            {
                if ((_colorType & 2) == 0)
                {
                    return(PdfName.Devicegray);
                }
                else
                {
                    return(PdfName.Devicergb);
                }
            }
            else
            {
                var array = new PdfArray();
                var dic   = new PdfDictionary();
                if ((_colorType & 2) == 0)
                {
                    if (_gamma.ApproxEquals(1f))
                    {
                        return(PdfName.Devicegray);
                    }

                    array.Add(PdfName.Calgray);
                    dic.Put(PdfName.Gamma, new PdfNumber(_gamma));
                    dic.Put(PdfName.Whitepoint, new PdfLiteral("[1 1 1]"));
                    array.Add(dic);
                }
                else
                {
                    PdfObject wp = new PdfLiteral("[1 1 1]");
                    array.Add(PdfName.Calrgb);
                    if (_gamma.ApproxNotEqual(1f))
                    {
                        var gm = new PdfArray();
                        var n  = new PdfNumber(_gamma);
                        gm.Add(n);
                        gm.Add(n);
                        gm.Add(n);
                        dic.Put(PdfName.Gamma, gm);
                    }
                    if (_hasChrm)
                    {
                        var   z   = _yW * ((_xG - _xB) * _yR - (_xR - _xB) * _yG + (_xR - _xG) * _yB);
                        var   ya  = _yR * ((_xG - _xB) * _yW - (_xW - _xB) * _yG + (_xW - _xG) * _yB) / z;
                        var   xa  = ya * _xR / _yR;
                        var   za  = ya * ((1 - _xR) / _yR - 1);
                        var   yb  = -_yG * ((_xR - _xB) * _yW - (_xW - _xB) * _yR + (_xW - _xR) * _yB) / z;
                        var   xb  = yb * _xG / _yG;
                        var   zb  = yb * ((1 - _xG) / _yG - 1);
                        var   yc  = _yB * ((_xR - _xG) * _yW - (_xW - _xG) * _yW + (_xW - _xR) * _yG) / z;
                        var   xc  = yc * _xB / _yB;
                        var   zc  = yc * ((1 - _xB) / _yB - 1);
                        var   xw  = xa + xb + xc;
                        float yw  = 1;//YA+YB+YC;
                        var   zw  = za + zb + zc;
                        var   wpa = new PdfArray();
                        wpa.Add(new PdfNumber(xw));
                        wpa.Add(new PdfNumber(yw));
                        wpa.Add(new PdfNumber(zw));
                        wp = wpa;
                        var matrix = new PdfArray();
                        matrix.Add(new PdfNumber(xa));
                        matrix.Add(new PdfNumber(ya));
                        matrix.Add(new PdfNumber(za));
                        matrix.Add(new PdfNumber(xb));
                        matrix.Add(new PdfNumber(yb));
                        matrix.Add(new PdfNumber(zb));
                        matrix.Add(new PdfNumber(xc));
                        matrix.Add(new PdfNumber(yc));
                        matrix.Add(new PdfNumber(zc));
                        dic.Put(PdfName.Matrix, matrix);
                    }
                    dic.Put(PdfName.Whitepoint, wp);
                    array.Add(dic);
                }
                return(array);
            }
        }
示例#10
0
        /// <summary>
        /// Checks if this line has to be justified.
        /// </summary>
        /// <returns> true  if the alignment equals <VAR>ALIGN_JUSTIFIED</VAR> and there is some width left.</returns>

        public bool HasToBeJustified()
        {
            return((Alignment == Element.ALIGN_JUSTIFIED || Alignment == Element.ALIGN_JUSTIFIED_ALL) && Width.ApproxNotEqual(0));
        }
示例#11
0
        /// <summary>
        /// Write out the columns.  After writing, use
        /// {@link #isOverflow()} to see if all text was written.
        /// @throws DocumentException on error
        /// </summary>
        /// <param name="canvas">PdfContentByte to write with</param>
        /// <param name="document">document to write to (only used to get page limit info)</param>
        /// <param name="documentY">starting y position to begin writing at</param>
        /// <returns>the current height (y position) after writing the columns</returns>
        public float Write(PdfContentByte canvas, PdfDocument document, float documentY)
        {
            _document          = document;
            _columnText.Canvas = canvas;
            if (_columnDefs.Count == 0)
            {
                throw new DocumentException("MultiColumnText has no columns");
            }
            _overflow = false;
            float currentHeight = 0;
            var   done          = false;

            while (!done)
            {
                if (_top.ApproxEquals(AUTOMATIC))
                {
                    _top = document.GetVerticalPosition(true);
                }
                else if (_nextY.ApproxEquals(AUTOMATIC))
                {
                    _nextY = document.GetVerticalPosition(true); // RS - 07/07/2005 - - Get current doc writing position for top of columns on new page.
                }

                var currentDef = (ColumnDef)_columnDefs[CurrentColumn];
                _columnText.YLine = _top;

                var left  = currentDef.ResolvePositions(Rectangle.LEFT_BORDER);
                var right = currentDef.ResolvePositions(Rectangle.RIGHT_BORDER);
                if (document.IsMarginMirroring() && document.PageNumber % 2 == 0)
                {
                    var delta = document.RightMargin - document.Left;
                    left  = (float[])left.Clone();
                    right = (float[])right.Clone();
                    for (var i = 0; i < left.Length; i += 2)
                    {
                        left[i] -= delta;
                    }
                    for (var i = 0; i < right.Length; i += 2)
                    {
                        right[i] -= delta;
                    }
                }
                currentHeight = Math.Max(currentHeight, getHeight(left, right));

                if (currentDef.IsSimple())
                {
                    _columnText.SetSimpleColumn(left[2], left[3], right[0], right[1]);
                }
                else
                {
                    _columnText.SetColumns(left, right);
                }

                var result = _columnText.Go();
                if ((result & ColumnText.NO_MORE_TEXT) != 0)
                {
                    done = true;
                    _top = _columnText.YLine;
                }
                else if (ShiftCurrentColumn())
                {
                    _top = _nextY;
                }
                else
                {  // check if we are done because of height
                    _totalHeight += currentHeight;

                    if ((_desiredHeight.ApproxNotEqual(AUTOMATIC)) && (_totalHeight >= _desiredHeight))
                    {
                        _overflow = true;
                        break;
                    }
                    else
                    {  // need to start new page and reset the columns
                        documentY = _nextY;
                        newPage();
                        currentHeight = 0;
                    }
                }
            }
            if (_desiredHeight.ApproxEquals(AUTOMATIC) && _columnDefs.Count == 1)
            {
                currentHeight = documentY - _columnText.YLine;
            }
            return(currentHeight);
        }
 protected PdfAppearance GetBorderAppearance()
 {
     PdfAppearance app = PdfAppearance.CreateAppearance(writer, box.Width, box.Height);
     switch (rotation)
     {
         case 90:
             app.SetMatrix(0, 1, -1, 0, box.Height, 0);
             break;
         case 180:
             app.SetMatrix(-1, 0, 0, -1, box.Width, box.Height);
             break;
         case 270:
             app.SetMatrix(0, -1, 1, 0, 0, box.Width);
             break;
     }
     app.SaveState();
     // background
     if (backgroundColor != null)
     {
         app.SetColorFill(backgroundColor);
         app.Rectangle(0, 0, box.Width, box.Height);
         app.Fill();
     }
     // border
     if (borderStyle == PdfBorderDictionary.STYLE_UNDERLINE)
     {
         if (borderWidth.ApproxNotEqual(0) && borderColor != null)
         {
             app.SetColorStroke(borderColor);
             app.SetLineWidth(borderWidth);
             app.MoveTo(0, borderWidth / 2);
             app.LineTo(box.Width, borderWidth / 2);
             app.Stroke();
         }
     }
     else if (borderStyle == PdfBorderDictionary.STYLE_BEVELED)
     {
         if (borderWidth.ApproxNotEqual(0) && borderColor != null)
         {
             app.SetColorStroke(borderColor);
             app.SetLineWidth(borderWidth);
             app.Rectangle(borderWidth / 2, borderWidth / 2, box.Width - borderWidth, box.Height - borderWidth);
             app.Stroke();
         }
         // beveled
         BaseColor actual = backgroundColor;
         if (actual == null)
             actual = BaseColor.White;
         app.SetGrayFill(1);
         drawTopFrame(app);
         app.SetColorFill(actual.Darker());
         drawBottomFrame(app);
     }
     else if (borderStyle == PdfBorderDictionary.STYLE_INSET)
     {
         if (borderWidth.ApproxNotEqual(0) && borderColor != null)
         {
             app.SetColorStroke(borderColor);
             app.SetLineWidth(borderWidth);
             app.Rectangle(borderWidth / 2, borderWidth / 2, box.Width - borderWidth, box.Height - borderWidth);
             app.Stroke();
         }
         // inset
         app.SetGrayFill(0.5f);
         drawTopFrame(app);
         app.SetGrayFill(0.75f);
         drawBottomFrame(app);
     }
     else
     {
         if (borderWidth.ApproxNotEqual(0) && borderColor != null)
         {
             if (borderStyle == PdfBorderDictionary.STYLE_DASHED)
                 app.SetLineDash(3, 0);
             app.SetColorStroke(borderColor);
             app.SetLineWidth(borderWidth);
             app.Rectangle(borderWidth / 2, borderWidth / 2, box.Width - borderWidth, box.Height - borderWidth);
             app.Stroke();
             if ((options & COMB) != 0 && maxCharacterLength > 1)
             {
                 float step = box.Width / maxCharacterLength;
                 float yb = borderWidth / 2;
                 float yt = box.Height - borderWidth / 2;
                 for (int k = 1; k < maxCharacterLength; ++k)
                 {
                     float x = step * k;
                     app.MoveTo(x, yb);
                     app.LineTo(x, yt);
                 }
                 app.Stroke();
             }
         }
     }
     app.RestoreState();
     return app;
 }