Ejemplo n.º 1
0
 private bool Match(Link link, PdfTextBlock text)
 {
     return(link.bottom <= text.Bottom &&
            link.top >= text.Top &&
            link.left <= text.Left &&
            link.right >= text.Right);
 }
        public void AssignRtl(PdfTextBlock item, bool reversed)
        {
            var text = item.Value;

            if (IsRTL(text))
            {
                item.IsRightToLeft = true;
                if (!reversed)
                {
                    ReverseText(item);
                }
            }
            else if (!IsNeutral(text))
            {
                item.IsRightToLeft = false;
                if (reversed)
                {
                    ReverseText(item);
                }
            }

//            else if (IsDigit(text))
//            {
//                item.IsDigit = true;
//            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 void InitProperties()
 {
     blocks = new List <PdfTextBlock>();
     left   = int.MaxValue;
     right  = 0;
     top    = group.Key;
     last   = null;
 }
Ejemplo n.º 5
0
        public void AssignLink(PdfTextBlock text)
        {
            var link = links.FirstOrDefault(l => Match(l, text));

            if (link != null)
            {
                text.Link = link.uri;
            }
        }
Ejemplo n.º 6
0
        private IList <PdfTextResult> MergeTextInLine()
        {
            var result = new LinkedList <PdfTextResult> {
            };
            LinkedListNode <PdfTextResult> firstNode = null;
            PdfTextBlock  lastBlock = null;
            PdfTextResult text      = null;
            PdfLinkResult link      = null;

            for (var index = 0; index < blocks.Count; index++)
            {
                var  current    = blocks[index];
                bool currentRTL =
                    RightToLeftManager.Instance.AssignNeutral(pageContext.PageRTL, current, blocks, index);
                bool opositeDirection = pageContext.PageRTL != currentRTL;
//                bool digitLtr = current.IsDigit && last?.IsDigit==true;
                if (lastBlock != null &&
                    Equals(lastBlock.StrokeColore, current.StrokeColore) &&
                    lastBlock.FontSize == current.FontSize &&
                    lastBlock.Font == current.Font &&
                    lastBlock.Link == current.Link &&
                    lastBlock.IsRightToLeft == current.IsRightToLeft)
                {
                    if (opositeDirection) //&&!digitLtr)
                    {
                        text.Value = current.Value + text.Value;
                    }
                    else
                    {
                        text.Value += current.Value;
                    }
                }
                else
                {
//                    var stateRtl =
//                        RightToLeftManager.Instance.PageElemtRtl(pageRTL, currentRightToLeft); // && !digitLtr);
                    SeperateRtlLtr(opositeDirection, pageContext.PageRTL, current, lastBlock, text);
                    text = new PdfTextResult
                    {
                        FontSize     = current.FontSize,
                        Font         = current.Font,
                        StrokeColore = current.StrokeColore,
                        Value        = current.Value,
                    };
                    pageContext.LinkManager.AssignLink(current, text, ref link);

                    AddNewText(opositeDirection, result, text, ref firstNode);
                }

                lastBlock = current;
            }

            return(result.ToArray());
        }
        public bool AssignNeutral(bool pageRightToLeft, PdfTextBlock current, IList <PdfTextBlock> blocks, int index)
        {
            if (current.IsRightToLeft != null)
            {
                return(current.IsRightToLeft.Value);
            }
            var currentRightToLeft = FindRtlOfNeutral(pageRightToLeft, blocks, index);

            current.IsRightToLeft = currentRightToLeft;
            if (currentRightToLeft) // && !current.IsDigit)
            {
                var chars = new char[current.Value.Length];
                for (var i = 0; i < current.Value.Length; i++)
                {
                    var c = current.Value[i];
                    switch (c)
                    {
                    case '[':
                        c = ']';
                        break;

                    case ']':
                        c = '[';
                        break;

                    case '(':
                        c = ')';
                        break;

                    case ')':
                        c = '(';
                        break;

                    case '{':
                        c = '}';
                        break;

                    case '}':
                        c = '{';
                        break;
                    }

                    chars[chars.Length - 1 - i] = c;
                }

                current.Value = new string(chars);
            }

            return(currentRightToLeft);
        }
Ejemplo n.º 8
0
        private void CalculateLines()
        {
            lines = new List <PdfTextLineDetails>();
            InitProperties();
            foreach (var current in group)
            {
                if (string.IsNullOrWhiteSpace(current.Value) &&
                    (last == null || last.End >= current.End || string.IsNullOrWhiteSpace(last.Value)))
                {
                    continue;
                }
                if (last != null)
                {
                    if (last.End + last.CharSpacing < current.Start)
                    {
                        if (last.End + last.CharSpacing * 2 < current.Start)
                        {
                            AddLine();
                            InitProperties();
                        }
                        else
                        {
                            AddSpace(current);
                        }
                    }
                }

                blocks.Add(current);
                if (left > current.Left)
                {
                    left = current.Left;
                }
                if (right < current.Right)
                {
                    right = current.Right;
                }
                if (top > current.Top)
                {
                    top = current.Top;
                }
                last = current;
            }

            AddLine();
        }
Ejemplo n.º 9
0
 public void AssignLink(PdfTextBlock current, PdfTextResult text, ref PdfLinkResult link)
 {
     if (current.Link != null)
     {
         if (!string.Equals(link?.Link, current.Link))
         {
             if (link != null && Log.DebugSupported)
             {
                 Log.Debug("link:" + link);
             }
             link = new PdfLinkResult {
                 Link = current.Link
             };
         }
         link.Children.Add(text);
         text.LinkParent = link;
     }
 }
Ejemplo n.º 10
0
 private void AddSpace(PdfTextBlock current)
 {
     if (last.Value == " ")
     {
         last.Width = current.Start - last.Start;
         if (pageContext.PageRTL)
         {
             last.Left = current.Right;
         }
     }
     else if (current.Value == " ")
     {
         current.Width = current.End - last.End;
         if (!pageContext.PageRTL)
         {
             current.Left = last.Right;
         }
     }
     else
     {
         Log.Debug($"adding space between '{last.Value}'-'{current.Value}'");
         blocks.Add(new PdfTextBlock
         {
             Bottom        = current.Bottom,
             CharSpacing   = current.CharSpacing,
             Font          = current.Font,
             FontSize      = current.FontSize,
             StrokeColore  = current.StrokeColore,
             IsRightToLeft = current.IsRightToLeft,
             Left          = pageContext.PageRTL ? current.Right : last.Right,
             Width         = current.Start - last.End,
             Top           = current.Top,
             Link          = current.Link,
             Value         = " "
         });
     }
 }
Ejemplo n.º 11
0
 private void SeperateRtlLtr(bool opositeDirection, bool pageRtl,
                             PdfTextBlock current, PdfTextBlock lastBlock, PdfTextResult lastText)
 {
     if (opositeDirection)
     {
         if (lastBlock?.IsRightToLeft == pageRtl &&
             //!lastBlock.IsDigit &&
             !RightToLeftManager.Instance.IsNeutral(lastBlock.Value[lastBlock.Value.Length - 1]))
         {
             lastText.Value = lastText.Value + " ";
             Log.Debug("seperated:" + lastBlock.Value + "-" + current.Value);
         }
     }
     else
     {
         if (lastBlock?.IsRightToLeft == !pageRtl &&
             //  !lastBlock.IsDigit &&
             !RightToLeftManager.Instance.IsNeutral(current.Value[0]))
         {
             current.Value = " " + current.Value;
             Log.Debug("seperated:" + lastText.Value + "-" + current.Value);
         }
     }
 }
Ejemplo n.º 12
0
 private void ReverseText(PdfTextBlock item)
 {
     item.Value = new string(item.Value.Reverse().ToArray());
 }