Exemplo n.º 1
0
        public virtual void ParseText(TextRenderInfo textRenderInfo)
        {
            var         text     = textRenderInfo.GetText();
            LineSegment baseline = textRenderInfo.GetBaseline();

            if (textRenderInfo.GetRise() != 0)
            {
                Matrix m = new Matrix(0.0f, -textRenderInfo.GetRise());
                baseline = baseline.TransformBy(m);
            }
            var          start      = baseline.GetStartPoint();
            LineSegment  ascentLine = textRenderInfo.GetAscentLine();
            PdfTextBlock item       = new PdfTextBlock
            {
                Value        = text,
                Bottom       = pageContext.PageHeight - start.Get(Vector.I2),
                Top          = pageContext.PageHeight - ascentLine.GetStartPoint().Get(Vector.I2),
                Left         = start.Get(Vector.I1),
                Width        = baseline.GetEndPoint().Get(Vector.I1) - start.Get(Vector.I1),
                FontSize     = FontManager.Instance.GetFontSize(textRenderInfo, baseline, ascentLine),
                StrokeColore = ColorManager.Instance.GetColor(textRenderInfo),
                CharSpacing  = textRenderInfo.GetSingleSpaceWidth(),
                Font         = GetFont(textRenderInfo),
            };

            RightToLeftManager.Instance.AssignRtl(item, textRenderInfo.GetUnscaledWidth() < 0);
            pageContext.LinkManager.AssignLink(item);
            texts.Add(item);
        }
Exemplo n.º 2
0
 private TextChunkInfo(Vector startLocation, Vector endLocation, TextRenderInfo info)
     : base(info.GetText(), startLocation, endLocation, info.GetSingleSpaceWidth())
 {
     RenderInfo    = info;
     StartLocation = startLocation;
     EndLocation   = endLocation;
 }
Exemplo n.º 3
0
        public virtual void RenderText(TextRenderInfo renderInfo)
        {
            bool firstRender = results.Count == 0;

            LineSegment segment = renderInfo.GetBaseline();
            Vector      start   = segment.GetStartPoint();
            Vector      end     = segment.GetEndPoint();

            //Use the Y value of the bottom left corner of the text for the key
            int currentLineKey = (int)start[1];

            if (!firstRender)
            {
                Vector x0 = start;
                Vector x1 = lastStart;
                Vector x2 = lastEnd;

                float dist = (x2.Subtract(x1)).Cross((x1.Subtract(x0))).LengthSquared / x2.Subtract(x1).LengthSquared;

                float sameLineThreshold = 1f;
                //If we've detected that we're still on the same
                if (dist <= sameLineThreshold)
                {
                    //Use the previous Y coordinate
                    currentLineKey = (int)lastStart[1];
                }
            }
            //Hack: PDFs start with zero at the bottom so our keys will be upside down. Using negative keys cheats this.
            currentLineKey = currentLineKey * -1;

            //If this line hasn't been used before add a new line to our collection
            if (!results.ContainsKey(currentLineKey))
            {
                results.Add(currentLineKey, new StringBuilder());
            }

            //Insert a space between blocks of text if it appears there should be
            if (!firstRender &&                                      //First pass never needs a leading space
                results[currentLineKey].Length != 0 &&               //Don't append a space to the begining of a line
                !results[currentLineKey].ToString().EndsWith(" ") && //Don't append if the current buffer ends in a space already
                renderInfo.GetText().Length > 0 &&                   //Don't append if the new next is empty
                !renderInfo.GetText().StartsWith(" "))
            {                                                        //Don't append if the new text starts with a space
                //Calculate the distance between the two blocks
                float spacing = lastEnd.Subtract(start).Length;
                //If it "looks" like it should be a space
                if (spacing > renderInfo.GetSingleSpaceWidth() / 2f)
                {
                    //Add a space
                    results[currentLineKey].Append(" ");
                }
            }

            //Add the text to the line in our collection
            results[currentLineKey].Append(renderInfo.GetText());

            lastStart = start;
            lastEnd   = end;
        }
 public virtual void EventOccurred(IEventData data, EventType type)
 {
     if (type.Equals(EventType.RENDER_TEXT))
     {
         TextRenderInfo renderInfo  = (TextRenderInfo)data;
         bool           firstRender = result.Length == 0;
         bool           hardReturn  = false;
         LineSegment    segment     = renderInfo.GetBaseline();
         Vector         start       = segment.GetStartPoint();
         Vector         end         = segment.GetEndPoint();
         if (!firstRender)
         {
             Vector x1 = lastStart;
             Vector x2 = lastEnd;
             // see http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
             float dist = (x2.Subtract(x1)).Cross((x1.Subtract(start))).LengthSquared() / x2.Subtract(x1).LengthSquared
                              ();
             float sameLineThreshold = 1f;
             // we should probably base this on the current font metrics, but 1 pt seems to be sufficient for the time being
             if (dist > sameLineThreshold)
             {
                 hardReturn = true;
             }
         }
         // Note:  Technically, we should check both the start and end positions, in case the angle of the text changed without any displacement
         // but this sort of thing probably doesn't happen much in reality, so we'll leave it alone for now
         if (hardReturn)
         {
             //System.out.println("<< Hard Return >>");
             AppendTextChunk("\n");
         }
         else
         {
             if (!firstRender)
             {
                 if (result[result.Length - 1] != ' ' && renderInfo.GetText().Length > 0 && renderInfo.GetText()[0] != ' ')
                 {
                     // we only insert a blank space if the trailing character of the previous string wasn't a space, and the leading character of the current string isn't a space
                     float spacing = lastEnd.Subtract(start).Length();
                     if (spacing > renderInfo.GetSingleSpaceWidth() / 2f)
                     {
                         AppendTextChunk(" ");
                     }
                 }
             }
         }
         //System.out.println("Inserting implied space before '" + renderInfo.getText() + "'");
         //System.out.println("Displaying first string of content '" + text + "' :: x1 = " + x1);
         //System.out.println("[" + renderInfo.getStartPoint() + "]->[" + renderInfo.getEndPoint() + "] " + renderInfo.getText());
         AppendTextChunk(renderInfo.GetText());
         lastStart = start;
         lastEnd   = end;
     }
 }
Exemplo n.º 5
0
        public virtual void RenderText(TextRenderInfo renderInfo)
        {
            bool firstRender = results.Count == 0;

            LineSegment segment = renderInfo.GetBaseline();
            Vector      start   = segment.GetStartPoint();
            Vector      end     = segment.GetEndPoint();

            int currentLineKey = (int)start[1];

            if (!firstRender)
            {
                Vector x0 = start;
                Vector x1 = lastStart;
                Vector x2 = lastEnd;

                float distance = (x2.Subtract(x1)).Cross((x1.Subtract(x0))).LengthSquared / x2.Subtract(x1).LengthSquared;

                float sameLineThreshold = 1f;
                if (distance <= sameLineThreshold)
                {
                    currentLineKey = (int)lastStart[1];
                }
            }
            currentLineKey = currentLineKey * -1;

            if (!results.ContainsKey(currentLineKey))
            {
                results.Add(currentLineKey, new StringBuilder());
            }

            if (!firstRender &&
                results[currentLineKey].Length != 0 &&
                !results[currentLineKey].ToString().EndsWith(" ") &&
                renderInfo.GetText().Length > 0 &&
                !renderInfo.GetText().StartsWith(" "))
            {
                float spacing = lastEnd.Subtract(start).Length;
                if (spacing > renderInfo.GetSingleSpaceWidth() / 2f)
                {
                    results[currentLineKey].Append(" ");
                }
            }

            results[currentLineKey].Append(renderInfo.GetText());

            lastStart = start;
            lastEnd   = end;
        }
Exemplo n.º 6
0
        public virtual void TextExtractGlyphAverageWidth()
        {
            String              text     = "i";
            PdfDictionary       fontDict = InitFontDictWithNotSpecifiedWidthOfSpaceChar();
            CanvasGraphicsState gs       = new CanvasGraphicsState();

            gs.SetFont(PdfFontFactory.CreateFont(fontDict));
            gs.SetFontSize(12);
            TextRenderInfo testTRI          = new TextRenderInfo(new PdfString(text), gs, new Matrix(), new Stack <CanvasTag>());
            float          singleSpaceWidth = testTRI.GetSingleSpaceWidth();
            float          iWidth           = testTRI.GetUnscaledWidth();

            NUnit.Framework.Assert.IsTrue(iWidth < singleSpaceWidth);
            NUnit.Framework.Assert.AreEqual(6.671999931335449, singleSpaceWidth, 0);
        }
Exemplo n.º 7
0
            public void RenderText(TextRenderInfo renderInfo)
            {
                float rise = renderInfo.GetRise();

                if (rise != 0)
                {
                    throw new NotImplementedException();
                }
                if (!m_textRenderInfoSimple.TrySet(renderInfo))
                {
                    throw new NotImplementedException();
                }
                var         text             = renderInfo.GetText();
                float       descentY         = m_textRenderInfoSimple.OriginY - m_textRenderInfoSimple.Descent;
                float       ascentY          = m_textRenderInfoSimple.OriginY + m_textRenderInfoSimple.Ascent;
                int         lineBuilderIndex = GetLineBuilderIndex(descentY, ascentY);
                LineBuilder lineBuilder;

                if (lineBuilderIndex < 0)
                {
                    lineBuilderIndex = ~lineBuilderIndex;
                    if (0 < lineBuilderIndex && descentY <= m_lineBuilders[lineBuilderIndex - 1].MaxY)
                    {
                        throw new NotImplementedException();
                    }
                    if (lineBuilderIndex < m_lineBuilders.Count && m_lineBuilders[lineBuilderIndex].MinY <= ascentY)
                    {
                        throw new NotImplementedException();
                    }
                    lineBuilder      = new LineBuilder();
                    lineBuilder.MinY = descentY;
                    lineBuilder.MaxY = ascentY;
                    m_lineBuilders.Insert(lineBuilderIndex, lineBuilder);
                }
                lineBuilder = m_lineBuilders[lineBuilderIndex];
                var charGroup = lineBuilder.AddCharGroup(m_textRenderInfoSimple.OriginX);

                charGroup.OriginXPlusAdvancement = m_textRenderInfoSimple.OriginX + m_textRenderInfoSimple.Advancement;
                charGroup.Value            = text;
                charGroup.SingleSpaceWidth = renderInfo.GetSingleSpaceWidth();
                charGroup.CanBeSuperscript = lineBuilder.MinY < descentY && !IsCloseTo(lineBuilder.MinY, descentY) && IsCloseTo(lineBuilder.MaxY, ascentY);
            }
Exemplo n.º 8
0
        public void RenderText(TextRenderInfo renderInfo)
        {
            var a           = renderInfo.GetBaseline();
            var ascentLine  = renderInfo.GetAscentLine();
            var a2          = renderInfo.GetSingleSpaceWidth();
            var descentLine = renderInfo.GetDescentLine();
            var a4          = renderInfo.GetUnscaledBaseline();
            var a5          = renderInfo.GetFont();
            var a6          = renderInfo.GetRise();
            var a7          = renderInfo.GetText();

            bool        flag1      = this.result.Length == 0;
            bool        flag2      = false;
            LineSegment baseline   = renderInfo.GetBaseline();
            Vector      startPoint = baseline.GetStartPoint();
            Vector      endPoint   = baseline.GetEndPoint();

            if (!flag1)
            {
                Vector v         = startPoint;
                Vector lastStart = this.lastStart;
                Vector lastEnd   = this.lastEnd;
                if ((double)(lastEnd.Subtract(lastStart).Cross(lastStart.Subtract(v)).LengthSquared / lastEnd.Subtract(lastStart).LengthSquared) > 1.0)
                {
                    flag2 = true;
                }
            }

            RectangleF rectCurrent = getRenderRectangleF(renderInfo);


            if (flag2)
            {
                this.AppendTextChunk('\n');
            }
            //else if (!flag1 && this.result[this.result.Length - 1] != ' ' && (renderInfo.GetText().Length > 0 && renderInfo.GetText()[0] != ' ') && (double)this.lastEnd.Subtract(startPoint).Length > (double)renderInfo.GetSingleSpaceWidth() / 2.0)
            //    this.AppendTextChunk(' ');
            this.AppendTextChunk(renderInfo.GetText());
            this.lastStart = startPoint;
            this.lastEnd   = endPoint;
        }
Exemplo n.º 9
0
        private static ITextChunkLocation GetLocation(TextRenderInfo tri)
        {
            LineSegment baseline = tri.GetBaseline();

            return(new TextChunkLocationDefaultImp(baseline.GetStartPoint(), baseline.GetEndPoint(), tri.GetSingleSpaceWidth
                                                       ()));
        }
Exemplo n.º 10
0
            public override void RenderText(TextRenderInfo renderInfo)
            {
                base.RenderText(renderInfo);

                //Vector bottomLeft = renderInfo.GetDescentLine().GetStartPoint();
                Vector    bottomLeft = renderInfo.GetBaseline().GetStartPoint();
                Vector    topRight   = renderInfo.GetAscentLine().GetEndPoint();
                TextChunk tc         = new TextChunk(renderInfo.GetText(), bottomLeft, topRight, renderInfo.GetSingleSpaceWidth());

                TextChunks.Add(tc);
            }
Exemplo n.º 11
0
        public virtual void RenderText(TextRenderInfo renderInfo)
        {
            LineSegment segment  = renderInfo.GetBaseline();
            TextChunk   location = new TextChunk(renderInfo.GetText(), segment.GetStartPoint(), segment.GetEndPoint(), renderInfo.GetSingleSpaceWidth());

            var _with1 = location;

            //Chunk Location:
            //			Debug.Print(renderInfo.GetText);
            _with1.PosLeft   = renderInfo.GetDescentLine().GetStartPoint()[Vector.I1];
            _with1.PosRight  = renderInfo.GetAscentLine().GetEndPoint()[Vector.I1];
            _with1.PosBottom = renderInfo.GetDescentLine().GetStartPoint()[Vector.I2];
            _with1.PosTop    = renderInfo.GetAscentLine().GetEndPoint()[Vector.I2];
            //Chunk Font Size: (Height)
            _with1.curFontSize = _with1.PosTop - segment.GetStartPoint()[Vector.I2];
            //Use Font name  and Size as Key in the SortedList
            string StrKey = renderInfo.GetFont().PostscriptFontName + _with1.curFontSize.ToString();

            //Add this font to ThisPdfDocFonts SortedList if it's not already present
            if (!ThisPdfDocFonts.ContainsKey(StrKey))
            {
                ThisPdfDocFonts.Add(StrKey, renderInfo.GetFont());
            }
            //Store the SortedList index in this Chunk, so we can get it later
            _with1.FontIndex = ThisPdfDocFonts.IndexOfKey(StrKey);
            locationalResult.Add(location);
        }
Exemplo n.º 12
0
        /**
         *
         * @see com.itextpdf.text.pdf.parser.RenderListener#renderText(com.itextpdf.text.pdf.parser.TextRenderInfo)
         */
        public virtual void RenderText(TextRenderInfo renderInfo)
        {
            LineSegment segment = renderInfo.GetBaseline();

            if (renderInfo.GetRise() != 0)
            { // remove the rise from the baseline - we do this because the text from a super/subscript render operations should probably be considered as part of the baseline of the text the super/sub is relative to
                Matrix riseOffsetTransform = new Matrix(0, -renderInfo.GetRise());
                segment = segment.TransformBy(riseOffsetTransform);
            }
            //TextChunk location = new TextChunk(renderInfo.GetText(), segment.GetStartPoint(), segment.GetEndPoint(), renderInfo.GetSingleSpaceWidth());
            //locationalResult.Add(location);

            //base.RenderText(renderInfo);


            //Get the bounding box for the chunk of text
            var bottomLeft = renderInfo.GetDescentLine().GetStartPoint();
            var topRight   = renderInfo.GetAscentLine().GetEndPoint();

            //Create a rectangle from it
            var rect = new iTextSharp.text.Rectangle(
                bottomLeft[Vector.I1],
                bottomLeft[Vector.I2],
                topRight[Vector.I1],
                topRight[Vector.I2]
                );

            TextChunk location = new TextChunk(renderInfo.GetText(), segment.GetStartPoint(), segment.GetEndPoint(), renderInfo.GetSingleSpaceWidth(), rect);

            locationalResult.Add(location);
        }
Exemplo n.º 13
0
 public ITextChunkLocation CreateLocation(TextRenderInfo renderInfo, LineSegment baseline)
 {
     return(new TextChunkLocationDefaultImp(baseline.GetStartPoint(), baseline.GetEndPoint(), renderInfo.GetSingleSpaceWidth
                                                ()));
 }
Exemplo n.º 14
0
        public void RenderText(TextRenderInfo renderInfo)
        {
            LineSegment baseline = renderInfo.GetBaseline();

            this.locationalResult.Add(new MyLocationTextExtractionStrategy.TextChunk(renderInfo.GetText(), baseline.GetStartPoint(), baseline.GetEndPoint(), renderInfo.GetSingleSpaceWidth()));
        }
Exemplo n.º 15
0
        /// <summary>
        ///     places text in text rectangles as pdf might render each character separetly
        /// </summary>
        /// <param name="renderInfo"></param>
        public override void RenderText(TextRenderInfo renderInfo)
        {
            var segment = renderInfo.GetBaseline();

            _charWidth = renderInfo.GetSingleSpaceWidth() / 2f;
            var text = renderInfo.GetText();

            _log.Debug("RenderText " + text);

            var bottomLeftCoordinate = renderInfo.GetDescentLine().GetStartPoint();
            var topRightCoordinate   = renderInfo.GetAscentLine().GetEndPoint();

            var textRectangle = new TextRectangle
            {
                Text   = text,
                X      = bottomLeftCoordinate[0],
                Y      = bottomLeftCoordinate[1],
                Width  = topRightCoordinate[0] - bottomLeftCoordinate[0],
                Height = Math.Abs(bottomLeftCoordinate[1] - topRightCoordinate[1])
            };

            if (_pageRotation == 0)
            {
                textRectangle.Y = _pageHeight - textRectangle.Y;
            }
            if (_pageRotation == 90)
            {
                textRectangle.X      = bottomLeftCoordinate[1];
                textRectangle.Y      = bottomLeftCoordinate[0];
                textRectangle.Width  = Math.Abs(topRightCoordinate[1] - bottomLeftCoordinate[1]);
                textRectangle.Height = Math.Abs(bottomLeftCoordinate[0] - topRightCoordinate[0]);
            }

            var startLocation = segment.GetStartPoint();
            var endLocation   = segment.GetEndPoint();


            var oVector = endLocation.Subtract(startLocation);

            if (oVector.Length == 0)
            {
                oVector = new Vector(1, 0, 0);
            }

            var orientationVector    = oVector.Normalize();
            var orientationMagnitude =
                (int)(Math.Atan2(orientationVector[Vector.I2], orientationVector[Vector.I1]) * 1000);

            if (orientationMagnitude != 0)
            {
                if (orientationVector[1] == -1)
                {
                    textRectangle.Y = textRectangle.Y + textRectangle.Height;
                }
            }


            if ((_lastChunk != null) && (Math.Abs(_lastChunk.Y - textRectangle.Y) < TOLERANCE))
            {
                var spacing = Math.Abs(textRectangle.X - (_lastChunk.X + _lastChunk.Width));
                if (spacing < renderInfo.GetSingleSpaceWidth() / 2f)
                {
                    _lastChunk.Width += textRectangle.Width;
                    _lastChunk.Text  += textRectangle.Text;
                    return;
                }
            }
            else if ((_lastChunk != null) && (orientationVector[1] == -1) &&
                     (Math.Abs(_lastChunk.X - textRectangle.X) < TOLERANCE))
            {
                var spacing = Math.Abs(textRectangle.Y - (_lastChunk.Y + textRectangle.Height));
                if (spacing < renderInfo.GetSingleSpaceWidth() / 2f)
                {
                    _lastChunk.Height += textRectangle.Height;
                    _lastChunk.Text   += textRectangle.Text;
                    return;
                }
            }


            _lastChunk = textRectangle;

            _textRectangles.Add(textRectangle);
        }
        public override void RenderText(TextRenderInfo renderInfo)
        {
            base.RenderText(renderInfo);
            LineSegment baseline = renderInfo.GetBaseline();
            //Create ExtendedChunk
            ExtendedTextChunk aExtendedChunk = new ExtendedTextChunk(renderInfo.GetText(), baseline.GetStartPoint(), baseline.GetEndPoint(), renderInfo.GetSingleSpaceWidth(), renderInfo.GetCharacterRenderInfos().ToList());

            this.m_DocChunks.Add(aExtendedChunk);
        }
Exemplo n.º 17
0
            //--------------------------------------------------------------------------------------------------
            public void RenderText(TextRenderInfo renderInfo)
            {
                LineSegment segment  = renderInfo.GetBaseline();
                TextChunk   location = new TextChunk(renderInfo.GetText(), segment.GetStartPoint(), segment.GetEndPoint(), renderInfo.GetSingleSpaceWidth());

                location.iPage = Page;

                int renderInfoTextLength = new StringInfo(renderInfo.GetText()).LengthInTextElements;

                if (renderInfoTextLength == 1)
                {
                    location.AscentLines.Add(renderInfo.GetAscentLine());
                    location.DescentLines.Add(renderInfo.GetDescentLine());
                }
                else
                {
                    IList <TextRenderInfo> infos = renderInfo.GetCharacterRenderInfos();
                    System.Diagnostics.Debug.Assert(infos != null);
                    System.Diagnostics.Debug.Assert(renderInfoTextLength == infos.Count);
                    foreach (TextRenderInfo info in infos)
                    {
                        location.AscentLines.Add(info.GetAscentLine());
                        location.DescentLines.Add(info.GetDescentLine());
                    }
                }
                m_LocationalResult.Add(location);
            }
Exemplo n.º 18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="renderInfo"></param>
        public override void RenderText(TextRenderInfo renderInfo)
        {
            LineSegment segment  = renderInfo.GetBaseline();
            MyTextChunk location = new MyTextChunk(renderInfo.GetText(), segment.GetStartPoint(), segment.GetEndPoint(), renderInfo.GetSingleSpaceWidth(), renderInfo.GetAscentLine(), renderInfo.GetDescentLine());

            m_locationResult.Add(location);
        }
Exemplo n.º 19
0
        public override void RenderText(TextRenderInfo renderInfo)
        {
            DocumentFont font = renderInfo.GetFont();

            byte[]        bytes   = font.ConvertToBytes(renderInfo.GetText());
            StringBuilder partStr = new StringBuilder();

            //PdfDictionary dict = font.FontDictionary;
            //PdfDictionary encoding = dict.GetAsDict(PdfName.ENCODING);
            //if (encoding != null)
            {
                string renderTxt = renderInfo.GetText();
                foreach (char c in renderTxt)
                {
                    string name = c.ToString();
                    if (CharMaps.ContainsKey(name))
                    {
                        string chat = CharMaps[name];
                        partStr.Append(chat);
                    }
                    else
                    {
                        /*
                         * String s = name.ToString().Substring(4);
                         * byte[] nameBytes = this.HexStringToBytes(s);
                         * string text = Encoding.BigEndianUnicode.GetString(nameBytes);
                         * partStr.Append(text);
                         */
                        partStr.Append(name);
                    }
                }

                /*
                 * PdfArray diffs = encoding.GetAsArray(PdfName.DIFFERENCES);
                 * StringBuilder builder = new StringBuilder();
                 * foreach (byte b in renderInfo.PdfString.GetBytes())
                 * {
                 *  string name = "";
                 *  try
                 *  {
                 *      name = diffs.GetAsName((char)b).ToString();
                 *  }
                 *  catch (Exception ex)
                 *  {
                 *      //throw ex;
                 *  }
                 *  if (CharMaps.ContainsKey(name))
                 *  {
                 *      string chat = CharMaps[name];
                 *      //partStr.Append(chat);
                 *  }
                 *  else
                 *  {
                 *      //String s = name.ToString().Substring(4);
                 *      //byte[] nameBytes = this.HexStringToBytes(s);
                 *      //string text = Encoding.BigEndianUnicode.GetString(nameBytes);
                 *      //partStr.Append(text);
                 *  }
                 * }
                 */
            }
            //else
            //{
            //    partStr.Append(renderInfo.GetText());
            //}

            if (partStr.ToString().Trim().Length > 0)
            {
                LineSegment segment    = renderInfo.GetAscentLine();
                Vector      startPoint = segment.GetStartPoint();
                if (LastEndPoint != null)
                {
                    int      charCount    = partStr.Length;
                    float    singleWidth  = renderInfo.GetSingleSpaceWidth();// segment.GetEndPoint().Subtract(segment.GetStartPoint()).Length / (float)charCount;
                    string[] pStartStrs   = startPoint.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    string[] pLastEndStrs = LastEndPoint.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    float    verticalSpan = Math.Abs(float.Parse(pStartStrs[1]) - float.Parse(pLastEndStrs[1]));
                    if (verticalSpan > renderInfo.GetSingleSpaceWidth())
                    {
                        if (verticalSpan > 4 * renderInfo.GetSingleSpaceWidth())
                        {
                            TextBuilder.Append("\r\n\r\n");
                        }
                        else
                        {
                            TextBuilder.Append("\r\n");
                        }
                    }
                    else
                    {
                        Vector spanVector = startPoint.Subtract(LastEndPoint);
                        float  span       = spanVector.Length;
                        if (span > singleWidth / 2f)
                        {
                            TextBuilder.Append(" ");
                        }
                    }
                }
                TextBuilder.Append(partStr.ToString());
                LastEndPoint = segment.GetEndPoint();
            }

            base.RenderText(renderInfo);
        }
Exemplo n.º 20
0
    public override void RenderText(TextRenderInfo renderInfo)
    {
        textLineFinder.RenderText(renderInfo);
        LineSegment segment = renderInfo.GetBaseline();

        if (renderInfo.GetRise() != 0)      // remove the rise from the baseline - we do this because the text from a super/subscript render operations should probably be considered as part of the baseline of the text the super/sub is relative to
        {
            Matrix riseOffsetTransform = new Matrix(0, -renderInfo.GetRise());
            segment = segment.TransformBy(riseOffsetTransform);
        }
        TextChunk location = new HorizontalTextChunk(renderInfo.GetText(), segment.GetStartPoint(), segment.GetEndPoint(), renderInfo.GetSingleSpaceWidth(), textLineFinder);

        getLocationalResult().Add(location);
    }
Exemplo n.º 21
0
        public virtual void RenderText(TextRenderInfo renderInfo)
        {
            LineSegment baseline     = renderInfo.GetBaseline();
            TextChunk   renderedItem = new TextChunk(renderInfo.GetText(), baseline.GetStartPoint(), baseline.GetEndPoint(), renderInfo.GetSingleSpaceWidth());
            TextChunk   foundChunk   = renderedItem;

            Debug.Print(renderInfo.GetText());
            foundChunk.PosLeft     = renderInfo.GetDescentLine().GetStartPoint()[0];
            foundChunk.PosRight    = renderInfo.GetAscentLine().GetEndPoint()[0];
            foundChunk.PosBottom   = renderInfo.GetDescentLine().GetStartPoint()[1];
            foundChunk.PosTop      = renderInfo.GetAscentLine().GetEndPoint()[1];
            foundChunk.curFontSize = foundChunk.PosTop - baseline.GetStartPoint()[1];
            string key = renderInfo.GetFont().PostscriptFontName + foundChunk.curFontSize.ToString();

            if (!this.ThisPdfDocFonts.ContainsKey(key))
            {
                this.ThisPdfDocFonts.Add(key, renderInfo.GetFont());
            }
            foundChunk.FontIndex = this.ThisPdfDocFonts.IndexOfKey(key);
            foundChunk           = null;
            this.locationalResult.Add(renderedItem);
        }
        /// <summary>
        /// Render text.
        /// </summary>
        /// <param name="renderInfo">render text information</param>
        public void RenderText(TextRenderInfo renderInfo)
        {
            var segment  = renderInfo.GetBaseline();
            var location = new TextChunk(renderInfo.GetText(), segment.GetStartPoint(), segment.GetEndPoint(), renderInfo.GetSingleSpaceWidth())
            {
                PosLeft     = renderInfo.GetDescentLine().GetStartPoint()[Vector.I1],
                PosRight    = renderInfo.GetAscentLine().GetEndPoint()[Vector.I1],
                PosBottom   = renderInfo.GetDescentLine().GetStartPoint()[Vector.I2],
                PosTop      = renderInfo.GetAscentLine().GetEndPoint()[Vector.I2],
                FillColor   = renderInfo.GetFillColor(),
                StrokeColor = renderInfo.GetStrokeColor(),
                Font        = renderInfo.GetFont()
            };

            // Chunk Font Size: (Height)
            location.CurFontSize = location.PosTop - segment.GetStartPoint()[Vector.I2];

            // Use Font name  and Size as Key in the SortedList
            var strKey = renderInfo.GetFont().PostscriptFontName + location.CurFontSize;

            // Add this font to ThisPdfDocFonts SortedList if it's not already present
            if (!_thisPdfDocFonts.ContainsKey(strKey))
            {
                _thisPdfDocFonts.Add(strKey, renderInfo.GetFont());
            }

            // Store the SortedList index in this Chunk, so we can get it later
            location.FontIndex = _thisPdfDocFonts.IndexOfKey(strKey);

            _locationalResult.Add(location);
        }
Exemplo n.º 23
0
        /**
         * Captures text using a simplified algorithm for inserting hard returns and spaces
         * @param   renderInfo  render info
         */
        public void RenderText(TextRenderInfo renderInfo)
        {
            bool firstRender = result.Length == 0;
            bool hardReturn  = false;

            LineSegment segment = renderInfo.GetBaseline();
            Vector      start   = segment.GetStartPoint();
            Vector      end     = segment.GetEndPoint();

            if (!firstRender)
            {
                Vector x0 = start;
                Vector x1 = lastStart;
                Vector x2 = lastEnd;

                // see http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
                float dist = (x2.Subtract(x1)).Cross((x1.Subtract(x0))).LengthSquared / x2.Subtract(x1).LengthSquared;

                float sameLineThreshold = 1f; // we should probably base this on the current font metrics, but 1 pt seems to be sufficient for the time being
                if (dist > sameLineThreshold)
                {
                    hardReturn = true;
                }

                // Note:  Technically, we should check both the start and end positions, in case the angle of the text changed without any displacement
                // but this sort of thing probably doesn't happen much in reality, so we'll leave it alone for now
            }

            if (hardReturn)
            {
                //System.out.Println("<< Hard Return >>");
                result.Append(Environment.NewLine);
            }
            else if (!firstRender)
            {
                if (result[result.Length - 1] != ' ' && renderInfo.GetText().Length > 0 && renderInfo.GetText()[0] != ' ')
                { // we only insert a blank space if the trailing character of the previous string wasn't a space, and the leading character of the current string isn't a space
                    float spacing = lastEnd.Subtract(start).Length;
                    if (spacing > renderInfo.GetSingleSpaceWidth() / 2f)
                    {
                        result.Append(',');
                        //System.out.Println("Inserting implied space before '" + renderInfo.GetText() + "'");
                    }
                }
            }
            else
            {
                //System.out.Println("Displaying first string of content '" + text + "' :: x1 = " + x1);
            }

            //System.out.Println("[" + renderInfo.GetStartPoint() + "]->[" + renderInfo.GetEndPoint() + "] " + renderInfo.GetText());
            //strings can be rendered in contiguous bits, so check last character for " and remove it if we need
            //to stick two rendered strings together to form one string in the output
            if ((!firstRender) && (result[result.Length - 1] == '\"'))
            {
                result.Remove(result.Length - 1, 1);
                result.Append(renderInfo.GetText() + "\"");
            }
            else
            {
                result.Append("\"" + renderInfo.GetText() + "\"");
            }

            lastStart = start;
            lastEnd   = end;
        }
    public override void RenderText(TextRenderInfo renderInfo)
    {
        LineSegment segment  = renderInfo.GetBaseline();
        TextChunk   location = new TextChunk(renderInfo.GetText(), segment.GetStartPoint(), segment.GetEndPoint(), renderInfo.GetSingleSpaceWidth());

        location.PosLeft     = renderInfo.GetDescentLine().GetStartPoint()[Vector.I1];
        location.PosRight    = renderInfo.GetAscentLine().GetEndPoint()[Vector.I1];
        location.PosBottom   = renderInfo.GetDescentLine().GetStartPoint()[Vector.I2];
        location.PosTop      = renderInfo.GetAscentLine().GetEndPoint()[Vector.I2];
        location.curFontSize = location.PosTop - segment.GetStartPoint()[Vector.I2];
        string StrKey = renderInfo.GetFont().PostscriptFontName + location.curFontSize.ToString();

        if (!ThisPdfDocFonts.ContainsKey(StrKey))
        {
            ThisPdfDocFonts.Add(StrKey, renderInfo.GetFont());
        }
        location.FontIndex = ThisPdfDocFonts.IndexOfKey(StrKey);
        locationalResult.Add(location);
    }
        /// Captures text using a simplified algorithm for inserting hard returns and spaces
        ///     @param   renderInfo  render info
        public virtual void RenderText(TextRenderInfo renderInfo)
        {
            // todo handle adding last line

            string text = renderInfo.GetText().Replace('\t', ' ');

            if (!Utils.FloatsNearlyEqual(renderInfo.GetBaseline().GetStartPoint().Get(Vector.I2), baseline, 0.01f) &&
                !Utils.FloatsNearlyEqual(renderInfo.GetAscentLine().GetStartPoint().Get(Vector.I2), ascent, 0.01f))
            {
                startOfNewline = true;
            }

            if (startOfNewline)
            {
                if (currLine != null)
                {
                    if (currChunk.ChunkStr.Length > 0)
                    {
                        StartNewWord();
                        startOfChunk = true;
                    }
                    currLine.RightMostPos     = bottomRight.Get(Vector.I1);
                    currLine.LineSpacingBelow =
                        currLine.Descent - renderInfo.GetAscentLine().GetStartPoint().Get(Vector.I2);
                }

                currLine = new LineMetrics();
                pageMetrics.AddLine(currLine);
                currLine.Ascent           = renderInfo.GetAscentLine().GetStartPoint().Get(Vector.I2);
                currLine.Descent          = renderInfo.GetDescentLine().GetStartPoint().Get(Vector.I2);
                currLine.Baseline         = renderInfo.GetBaseline().GetStartPoint().Get(Vector.I2);
                currLine.LeftMostPos      = renderInfo.GetBaseline().GetStartPoint().Get(Vector.I1);
                currLine.LineSpacingAbove = descent - currLine.Ascent;

                startOfNewline = false;
            }

            CharMetrics charMetrics;

            // Check if there is space char required between chunks
            if (GetResultantText().Length > 0 &&
                IsSpaceRequired(bottomRight.Get(Vector.I1), renderInfo.GetBaseline().GetStartPoint().Get(Vector.I1),
                                renderInfo.GetBaseline().GetStartPoint().Get(Vector.I2),
                                renderInfo.GetSingleSpaceWidth())) // todo switch order

            {
                charMetrics             = new CharMetrics(' ');
                charMetrics.BottomLeft  = new Vector3D(bottomLeft);
                charMetrics.TopLeft     = new Vector3D(topLeft);
                charMetrics.BottomRight = new Vector3D(renderInfo.GetBaseline().GetStartPoint());
                charMetrics.TopRight    = new Vector3D(renderInfo.GetAscentLine().GetStartPoint());
                this.charMetrices.Add(charMetrics);
                text = " " + text; // todo extra spacing? e.g. handle if it is 4x singlespace width
            }

            TextRenderInfo lastChar = null;
            int            charInd  = 0;

            foreach (TextRenderInfo charInfo in renderInfo.GetCharacterRenderInfos())
            {
                if (charInfo.GetText().Length == 0)
                {
                    continue;
                }

                if (lastChar != null && IsSpaceRequired(lastChar.GetBaseline().GetEndPoint().Get(Vector.I1),
                                                        charInfo.GetBaseline().GetStartPoint().Get(Vector.I1),
                                                        renderInfo.GetSingleSpaceWidth()))
                {
                    charMetrics             = new CharMetrics(' ');
                    charMetrics.FontSize    = charInfo.GetFontSize();
                    charMetrics.BottomLeft  = new Vector3D(lastChar.GetBaseline().GetEndPoint());
                    charMetrics.TopLeft     = new Vector3D(lastChar.GetAscentLine().GetEndPoint());
                    charMetrics.BottomRight = new Vector3D(charInfo.GetBaseline().GetStartPoint());
                    charMetrics.TopRight    = new Vector3D(charInfo.GetAscentLine().GetStartPoint());
                    charMetrices.Add(charMetrics);
                    text = text.Insert(charInd + 1, " ");
                }

                char c = charInfo.GetText()[0] == '\t' ? ' ' : charInfo.GetText()[0];
                charMetrics             = new CharMetrics(c);
                charMetrics.FontSize    = charInfo.GetFontSize();
                charMetrics.BottomLeft  = new Vector3D(charInfo.GetBaseline().GetStartPoint());
                charMetrics.TopLeft     = new Vector3D(charInfo.GetAscentLine().GetStartPoint());
                charMetrics.BottomRight = new Vector3D(charInfo.GetBaseline().GetEndPoint());
                charMetrics.TopRight    = new Vector3D(charInfo.GetAscentLine().GetEndPoint());
                charMetrices.Add(charMetrics);

                lastChar = charInfo;
                charInd++;
            }

            if (startOfChunk)
            {
                if (Utils.FloatsNearlyEqual(currLine.Baseline, baseline, 0.001f))
                {
                    float horSpacing = Math.Abs(
                        bottomRight.Get(Vector.I1) - renderInfo.GetBaseline().GetStartPoint().Get(Vector.I1));
                    if (horSpacing > spaceWidth)
                    {
                        Console.Write("");
                    }
                }

                bottomLeft           = renderInfo.GetBaseline().GetStartPoint();
                topLeft              = renderInfo.GetAscentLine().GetStartPoint();
                currChunk.BottomLeft = new Vector3D(bottomLeft);
                currChunk.TopLeft    = new Vector3D(topLeft);
                baseline             = renderInfo.GetBaseline().GetStartPoint().Get(Vector.I2);
                ascent     = renderInfo.GetAscentLine().GetStartPoint().Get(Vector.I2);
                descent    = renderInfo.GetDescentLine().GetStartPoint().Get(Vector.I2);
                spaceWidth = renderInfo.GetSingleSpaceWidth();

                startOfChunk = false;
            }

            bottomRight = renderInfo.GetBaseline().GetEndPoint();
            topRight    = renderInfo.GetAscentLine().GetEndPoint();

            string currFont = renderInfo.GetFont() != null?renderInfo.GetFont().ToString() : "";

            if (currChunk.FontFamily == null && currFont.Length > 0)
            {
                currChunk.FontFamily = currFont;
            }

            //Check if faux bold is used
            if ((renderInfo.GetTextRenderMode() == (int)TextRenderMode.FillThenStrokeText) || // todo include render mode for outsets etc?
                renderInfo.GetFont().GetFontProgram().GetFontNames().IsBold())
            {
                currChunk.Bold = true;
            }

            if (!(Utils.FloatsNearlyEqual(renderInfo.GetFont().GetFontProgram().GetFontMetrics().GetItalicAngle(), 0, 0.001f)) ||
                renderInfo.GetFont().GetFontProgram().GetFontNames().IsItalic())
            {
                currChunk.Italic = true;
            }

            if (currChunk.FillColor == null && renderInfo.GetFillColor() != null)
            {
                currChunk.FillColor = ProcessColorInfo(renderInfo.GetFillColor());
            }

            if (currChunk.StrokeColor == null && renderInfo.GetStrokeColor() != null)
            {
                currChunk.StrokeColor = ProcessColorInfo(renderInfo.GetStrokeColor());
            }

            currChunk.Append(text);
            result.Append(text); // todo include newlines
        }