/// <summary>
 /// Retruns true if the current line has no entries or non-character runs
 /// </summary>
 /// <returns></returns>
 private bool IsEmptyLine()
 {
     if (this.CurrentLine.IsEmpty)
     {
         return(true);
     }
     else
     {
         for (int i = 0; i < this.CurrentLine.Runs.Count; i++)
         {
             PDFLayoutRun run = this.CurrentLine.Runs[i];
             if (run is PDFTextRunBegin)
             {
                 continue;
             }
             else if (run is PDFTextRunSpacer)
             {
                 continue;
             }
             else if (run is PDFLayoutPositionedRegionRun)
             {
                 continue;
             }
             else if (run is PDFLayoutInlineBegin)
             {
                 continue;
             }
             else
             {
                 return(false);
             }
         }
         return(true);
     }
 }
Example #2
0
        /// <summary>
        /// Overrides default behaviour to calculate the maximum height.
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected override bool DoClose(ref string msg)
        {
            PDFLayoutRun last = this.LastRun();

            if (null != last && last.IsClosed == false)
            {
                last.Close();
            }
            //Return the base value
            return(base.DoClose(ref msg));
        }
Example #3
0
 /// <summary>
 /// Justs adds a run
 /// </summary>
 /// <param name="run"></param>
 public virtual void AddRun(PDFLayoutRun run)
 {
     if (run is PDFTextRunBegin)
     {
         PDFUnit ascent = ((PDFTextRunBegin)run).TextRenderOptions.GetAscent();
         if (ascent > this.BaseLineOffset)
         {
             this.BaseLineOffset = ascent;
         }
     }
     else if (run is PDFTextRunEnd)
     {
         PDFTextRunEnd end    = (PDFTextRunEnd)run;
         PDFUnit       ascent = end.Start.TextRenderOptions.GetAscent();
         if (ascent > this.BaseLineOffset)
         {
             this.BaseLineOffset = ascent;
         }
     }
     this.Runs.Add(run);
 }
Example #4
0
        internal bool JustifyContent(PDFUnit total, PDFUnit current, PDFUnit available, bool all, List <PDFTextRunCharacter> runCache, ref PDFTextRenderOptions currOptions)
        {
            if (this.Runs.Count < 1)
            {
                return(false);
            }

            bool shouldJustify = all;

            PDFLayoutRun last = this.Runs[this.Runs.Count - 1];

            if (last is PDFTextRunNewLine && (last as PDFTextRunNewLine).IsHardReturn == false)
            {
                shouldJustify = true;
            }


            if (shouldJustify)
            {
                runCache.Clear();
                bool intext     = (null != currOptions); //if we have text render options then even if we are the first run we can be considered as inside a text block
                int  charCount  = 0;
                int  spaceCount = 0;
                PDFTextRunCharacter lastchars = null;

                for (int i = 0; i < this.Runs.Count; i++)
                {
                    PDFLayoutRun cur = this.Runs[i];

                    if (cur is PDFTextRunBegin)
                    {
                        currOptions = ((PDFTextRunBegin)cur).TextRenderOptions;
                        if (!intext)
                        {
                            intext = true;
                        }
                    }
                    else if (cur is PDFTextRunCharacter && intext)
                    {
                        PDFTextRunCharacter chars = cur as PDFTextRunCharacter;
                        if (!(currOptions.WordSpacing.HasValue || currOptions.CharacterSpacing.HasValue))
                        {
                            AddCharactersAndSpaces(chars.Characters, ref charCount, ref spaceCount);

                            lastchars = chars;
                        }
                    }
                    else if (cur is PDFLayoutComponentRun)
                    {
                        lastchars = null;
                    }
                }



                // Post process to calculate the required spacing
                // if we have some text in our line.

                if (intext && (spaceCount + charCount > 0))
                {
                    if (null != lastchars && lastchars.Characters.EndsWith(" "))
                    {
                        lastchars.Characters = lastchars.Characters.Substring(0, lastchars.Characters.Length - 1);
                        spaceCount          -= 1;
                    }

                    double spaceToCharFactor = 10; // apply ten times more to spaces than characters.
                    double full = (spaceCount * spaceToCharFactor) + charCount;
                    this._linespacingOptions = new ExtraSpacingOptions()
                    {
                        CharSpace = available / full, WordSpace = (available / full) * spaceToCharFactor, Options = currOptions
                    };


                    charCount  = 0;
                    spaceCount = 0;
                    PDFUnit currWidth = 0;
                    PDFUnit change    = PDFUnit.Zero;

                    for (int i = 0; i < this.Runs.Count; i++)
                    {
                        PDFLayoutRun cur = this.Runs[i];
                        if (cur is PDFTextRunBegin)
                        {
                            PDFTextRunBegin begin = (PDFTextRunBegin)cur;
                            currOptions = begin.TextRenderOptions;
                            if (i > 0)
                            {
                                change           = (_linespacingOptions.WordSpace * spaceCount) + (_linespacingOptions.CharSpace * charCount);
                                begin.LineInset += change;
                            }
                        }
                        else if (cur is PDFTextRunCharacter)
                        {
                            if (!currOptions.WordSpacing.HasValue || !currOptions.CharacterSpacing.HasValue)
                            {
                                PDFTextRunCharacter chars = cur as PDFTextRunCharacter;
                                int runChars  = 0;
                                int runSpaces = 0;
                                AddCharactersAndSpaces(chars.Characters, ref runChars, ref runSpaces);
                                charCount       += runChars;
                                spaceCount      += runSpaces;
                                chars.ExtraSpace = (_linespacingOptions.WordSpace * runSpaces) + (_linespacingOptions.CharSpace * runChars);
                            }
                        }
                        else if (cur is PDFLayoutComponentRun)
                        {
                            if (i > 0)
                            {
                                PDFLayoutComponentRun comprun = (cur as PDFLayoutComponentRun);
                                PDFRect bounds = comprun.TotalBounds;
                                bounds.X           += (_linespacingOptions.WordSpace * spaceCount) + (_linespacingOptions.CharSpace * charCount);
                                comprun.TotalBounds = bounds;
                            }
                        }
                        else if (cur is PDFTextRunNewLine)
                        {
                            PDFTextRunNewLine newLine = (cur as PDFTextRunNewLine);
                            newLine.Offset = new PDFSize(newLine.Offset.Width + change, newLine.Offset.Height);
                        }
                    }
                }
            }

            return(shouldJustify);
        }