Beispiel #1
0
        public void TestCrossVector()
        {
            Vector v = new Vector(2, 3, 4);
            Matrix m = new Matrix(5, 6, 7, 8, 9, 10);
            Vector shouldBe = new Vector(67, 76, 4);

            Vector rslt = v.Cross(m);
            Assert.AreEqual(shouldBe, rslt);
        }
        public void RenderText(TextRenderInfo renderInfo)
        {
            var curFont = renderInfo.GetFont().PostscriptFontName;

              //Check if faux bold is used
              if ((renderInfo.GetTextRenderMode() == (int) TextRenderMode.FillThenStrokeText))
            curFont += "-Bold";

              //This code assumes that if the baseline changes then we're on a newline
              var curBaseline = renderInfo.GetBaseline().GetStartPoint();
              var topRight = renderInfo.GetAscentLine().GetEndPoint();
              var rect = new Rectangle(curBaseline[Vector.I1], curBaseline[Vector.I2], topRight[Vector.I1], topRight[Vector.I2]);
              var curFontSize = rect.Height;

              //See if something has changed, either the baseline, the font or the font size
              if ((lastBaseLine == null) || (curBaseline[Vector.I2] != lastBaseLine[Vector.I2]) || (curFontSize != lastFontSize) ||
              (curFont != lastFont))
              {
            //if we've put down at least one span tag close it
            if ((lastBaseLine != null))
              result.AppendLine("</span>");

            //If the baseline has changed then insert a line break
            if ((lastBaseLine != null) && curBaseline[Vector.I2] != lastBaseLine[Vector.I2])
              result.AppendLine("<br />");

            //Create an HTML tag with appropriate styles
            result.AppendFormat("<span style=\"font-family:{0};font-size:{1}; position: relative; top: {2}; left: {3};\">",
              curFont, curFontSize, 850 - rect.Top, rect.Left);
              }

              //Append the current text
              result.Append(renderInfo.GetText());

              //Set currently used properties
              lastBaseLine = curBaseline;
              lastFontSize = curFontSize;
              lastFont = curFont;
        }
        public void RenderText(iTextSharp.text.pdf.parser.TextRenderInfo renderInfo)
        {
            string curFont = renderInfo.GetFont().PostscriptFontName;
              //Check if faux bold is used
              if ((renderInfo.GetTextRenderMode() == (int)TextRenderMode.FillThenStrokeText))
              {
            curFont += "-Bold";
              }

              //This code assumes that if the baseline changes then we're on a newline
              Vector curBaseline = renderInfo.GetBaseline().GetStartPoint();
              Vector topRight = renderInfo.GetAscentLine().GetEndPoint();
              iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(curBaseline[Vector.I1], curBaseline[Vector.I2], topRight[Vector.I1], topRight[Vector.I2]);
              Single curFontSize = rect.Height;

              //See if something has changed, either the baseline, the font or the font size
              if ((this.lastBaseLine == null) || (curBaseline[Vector.I2] != lastBaseLine[Vector.I2]) || (curFontSize != lastFontSize) || (curFont != lastFont))
              {
            //if we've put down at least one span tag close it
            if ((this.lastBaseLine != null))
            {
              this.result.AppendLine("</span>");
            }
            //If the baseline has changed then insert a line break
            if ((this.lastBaseLine != null) && curBaseline[Vector.I2] != lastBaseLine[Vector.I2])
            {
              this.result.AppendLine("<br />");
            }
            //Create an HTML tag with appropriate styles
            this.result.AppendFormat("<span style=\"font-family:{0};font-size:{1}\">", curFont, curFontSize);
              }

              //Append the current text
              this.result.Append(renderInfo.GetText());

              //Set currently used properties
              this.lastBaseLine = curBaseline;
              this.lastFontSize = curFontSize;
              this.lastFont = curFont;
        }
 private void AssertVectorsEqual(Vector v1, Vector v2, String message)
 {
     Assert.AreEqual(v1[0], v2[0], 1 / 72f, message);
     Assert.AreEqual(v1[1], v2[1], 1 / 72f, message);
 }
Beispiel #5
0
 /**
  * Creates a new line segment.
  * @param startPoint the start point of a line segment.
  * @param endPoint the end point of a line segment.
  */
 public LineSegment(Vector startPoint, Vector endPoint) {
     this.startPoint = startPoint;
     this.endPoint = endPoint;
 }
 /// <summary>
 /// Create a TextInfo.
 /// </summary>
 /// <param name="initialTextChunk"></param>
 public TextInfo(TextChunk initialTextChunk)
 {
     TopLeft = initialTextChunk.AscentLine.GetStartPoint();
     BottomRight = initialTextChunk.DecentLine.GetEndPoint();
     rectangle = initialTextChunk.AscentLine.GetBoundingRectange();
     m_Text = initialTextChunk.Text;
 }
Beispiel #7
0
        /**
         * Captures text using a simplified algorithm for inserting hard returns and spaces
         * @param   renderInfo  render info
         */
        public virtual 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 >>");
                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() + "'");
                    }
                }
            } else {
                //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;            
        }
Beispiel #8
0
        /**
         * Computes the cross product of this vector and the specified vector
         * @param with the vector to cross this vector with
         * @return the cross product
         */
        public Vector Cross(Vector with)
        {
            float x = vals[I2]*with.vals[I3] - vals[I3]*with.vals[I2];
            float y = vals[I3]*with.vals[I1] - vals[I1]*with.vals[I3];
            float z = vals[I1]*with.vals[I2] - vals[I2]*with.vals[I1];

            return new Vector(x, y, z);
        }
Beispiel #9
0
        public void TestSanityCheckOnVectorMath()
        {
            Vector start = new Vector(0, 0, 1);
            Vector end = new Vector(1, 0, 1);
            Vector antiparallelStart = new Vector(0.9f, 0, 1);
            Vector parallelStart = new Vector(1.1f, 0, 1);

            float rsltAntiParallel = antiparallelStart.Subtract(end).Dot(end.Subtract(start).Normalize());
            Assert.AreEqual(-0.1f, rsltAntiParallel, 0.0001);

            float rsltParallel = parallelStart.Subtract(end).Dot(end.Subtract(start).Normalize());
            Assert.AreEqual(0.1f, rsltParallel, 0.0001);

        }
 public Text(PdfString text, Vector startLocation, Vector endLocation, bool visible, int numOfStrTextBelongsTo) : base(visible) {
     this.text = text;
     this.startX = startLocation[0];
     this.endX = endLocation[0];
     this.numOfStrTextBelongsTo = numOfStrTextBelongsTo;
 }
 /// <summary>
 /// Create a TextInfo.
 /// </summary>
 /// <param name="initialTextChunk"></param>
 public TextInfo(TextChunkEx initialTextChunk)
 {
     TopLeft = initialTextChunk.AscentLine.GetStartPoint();
     BottomRight = initialTextChunk.DecentLine.GetEndPoint();
     m_Text = initialTextChunk.Text;
 }
 /// <summary>
 /// Add more text to this TextInfo.
 /// </summary>
 /// <param name="additionalTextChunk"></param>
 public void appendText(TextChunkEx additionalTextChunk)
 {
     BottomRight = additionalTextChunk.DecentLine.GetEndPoint();
     m_Text += additionalTextChunk.Text;
 }
            /// <summary>
            /// Represents a chunk of text, it's orientation, and location relative to the orientation vector
            /// </summary>
            /// <param name="txt"></param>
            /// <param name="startLoc"></param>
            /// <param name="endLoc"></param>
            /// <param name="charSpaceWidth"></param>
            public TextChunkEx(string txt, iTextSharp.text.pdf.parser.Vector startLoc, iTextSharp.text.pdf.parser.Vector endLoc, float charSpaceWidth, iTextSharp.text.pdf.parser.LineSegment ascentLine, iTextSharp.text.pdf.parser.LineSegment decentLine)
            {
                m_text = txt;
                m_startLocation = startLoc;
                m_endLocation = endLoc;
                m_charSpaceWidth = charSpaceWidth;
                AscentLine = ascentLine;
                DecentLine = decentLine;

                m_orientationVector = m_endLocation.Subtract(m_startLocation);
                if (m_orientationVector.Length == 0)
                    m_orientationVector = new Vector(1, 0, 0);
                m_orientationVector = m_orientationVector.Normalize();

                m_orientationMagnitude = (int)(Math.Atan2(m_orientationVector[iTextSharp.text.pdf.parser.Vector.I2], m_orientationVector[iTextSharp.text.pdf.parser.Vector.I1]) * 1000);

                // see http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
                // the two vectors we are crossing are in the same plane, so the result will be purely
                // in the z-axis (out of plane) direction, so we just take the I3 component of the result
                iTextSharp.text.pdf.parser.Vector origin = new iTextSharp.text.pdf.parser.Vector(0, 0, 1);
                m_distPerpendicular = (int)(m_startLocation.Subtract(origin)).Cross(m_orientationVector)[iTextSharp.text.pdf.parser.Vector.I3];

                m_distParallelStart = m_orientationVector.Dot(m_startLocation);
                m_distParallelEnd = m_orientationVector.Dot(m_endLocation);
            }
Beispiel #14
0
        /**
         * Computes the difference between this vector and the specified vector
         * @param v the vector to subtract from this one
         * @return the results of the subtraction
         */
        public Vector Subtract(Vector v)
        {
            float x = vals[I1] - v.vals[I1];
            float y = vals[I2] - v.vals[I2];
            float z = vals[I3] - v.vals[I3];

            return new Vector(x, y, z);
        }
Beispiel #15
0
 /**
  * Computes the dot product of this vector with the specified vector
  * @param with the vector to dot product this vector with
  * @return the dot product
  */
 public float Dot(Vector with)
 {
     return vals[I1]*with.vals[I1] + vals[I2]*with.vals[I2] + vals[I3]*with.vals[I3];
 }
Beispiel #16
0
        public 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 dist = (x2.Subtract(x1)).Cross((x1.Subtract(x0))).LengthSquared / x2.Subtract(x1).LengthSquared;

                float sameLineThreshold = 1f;
                if (dist <= 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;
        }
Beispiel #17
-2
        //Automatically called for each chunk of text in the PDF
        public override void RenderText(TextRenderInfo renderInfo)
        {
            base.RenderText(renderInfo);

            var startPosition = System.Globalization.CultureInfo.CurrentCulture.CompareInfo.IndexOf(renderInfo.GetText(), this.TextToSearchFor, this.CompareOptions);
            var texto = renderInfo.GetText();
            var x = renderInfo.GetText().Contains(TextToSearchFor);
            //Get the bounding box for the chunk of text
            /*if (x)
            {
                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]
                                                        );

                this.myPoints.Add(new RectAndText(rect, renderInfo.GetText()));
            }*/

            //This code assumes that if the baseline changes then we're on a newline
            Vector curBaseline = renderInfo.GetBaseline().GetStartPoint();
            Vector curBaseline2 = renderInfo.GetAscentLine().GetEndPoint();

            //See if the baseline has changed
            if ((this.lastBaseLine != null) && (curBaseline[Vector.I2] != lastBaseLine[Vector.I2]))
            {
                //See if we have text and not just whitespace
                if ((!String.IsNullOrWhiteSpace(this.result.ToString())))
                {
                    //Mark the previous line as done by adding it to our buffers
                    this.baselines.Add(this.lastBaseLine[Vector.I2]);
                    this.strings.Add(this.result.ToString());

                    if (this.result.ToString().ToLower().Contains(TextToSearchFor.ToLower()))
                    {
                        //Create a rectangle from it
                        var rect = new iTextSharp.text.Rectangle(startPositionWord[Vector.I1],
                                                            startPositionWord[Vector.I2],
                                                            lastAscentLine[Vector.I1],
                                                            lastAscentLine[Vector.I2]);

                        this.myPoints.Add(new RectAndText(rect, this.result.ToString()));
                        startPositionWord = null;
                        endPositionWord = null;
                    }
                }
                //Reset our "line" buffer
                this.result.Clear();
                wordended = true;
            }

            //Append the current text to our line buffer
            //if (!string.IsNullOrWhiteSpace(renderInfo.GetText()))
                this.result.Append(renderInfo.GetText());

            if (!string.IsNullOrWhiteSpace(result.ToString()) && wordended)
            {
                wordended = false;
                startPositionWord = renderInfo.GetDescentLine().GetStartPoint();
            }
            //Reset the last used line
            this.lastBaseLine = curBaseline;
            this.lastAscentLine = curBaseline2;
        }