Пример #1
0
 /**
  * Copy constructor for <CODE>Phrase</CODE>.
  */
 public Phrase(Phrase phrase) : base()
 {
     this.AddAll(phrase);
     leading     = phrase.Leading;
     font        = phrase.Font;
     hyphenation = phrase.hyphenation;
 }
Пример #2
0
 /// <summary>
 /// Constructs a Phrase with a certain Chunk and a certain leading.
 /// </summary>
 /// <param name="leading">the leading</param>
 /// <param name="chunk">a Chunk</param>
 public Phrase(float leading, Chunk chunk)
 {
     this.leading = leading;
     base.Add(chunk);
     font        = chunk.Font;
     hyphenation = chunk.GetHyphenation();
 }
Пример #3
0
 /**
  * Copy constructor for <CODE>Phrase</CODE>.
  */
 public Phrase(Phrase phrase) : base()
 {
     this.AddAll(phrase);
     SetLeading(phrase.Leading, phrase.MultipliedLeading);
     font        = phrase.Font;
     tabSettings = phrase.TabSettings;
     hyphenation = phrase.hyphenation;
 }
Пример #4
0
 /**
 * Copy constructor for <CODE>Phrase</CODE>.
 */
 public Phrase(Phrase phrase)
     : base()
 {
     this.AddAll(phrase);
     leading = phrase.Leading;
     font = phrase.Font;
     hyphenation = phrase.hyphenation;
 }
Пример #5
0
 /// <summary>
 /// Constructs a Phrase with a certain Chunk and a certain leading.
 /// </summary>
 /// <param name="leading">the leading</param>
 /// <param name="chunk">a Chunk</param>
 public Phrase(float leading, Chunk chunk) {
     this.leading = leading;
     base.Add(chunk);
     font = chunk.Font;
     hyphenation = chunk.GetHyphenation();
 }
Пример #6
0
 /// <summary>
 /// Constructs a Phrase with a certain Chunk.
 /// </summary>
 /// <param name="chunk">a Chunk</param>
 public Phrase(Chunk chunk) {
     base.Add(chunk);
     font = chunk.Font;
     hyphenation = chunk.GetHyphenation();
 }
Пример #7
0
        /**
         * Splits this <CODE>PdfChunk</CODE> if it's too long for the given width.
         * <P>
         * Returns <VAR>null</VAR> if the <CODE>PdfChunk</CODE> wasn't truncated.
         *
         * @param       width       a given width
         * @return      the <CODE>PdfChunk</CODE> that doesn't fit into the width.
         */

        internal PdfChunk Split(float width)
        {
            newlineSplit = false;
            if (image != null)
            {
                if (image.ScaledWidth > width)
                {
                    PdfChunk pc = new PdfChunk(Chunk.OBJECT_REPLACEMENT_CHARACTER, this);
                    value      = "";
                    attributes = new Dictionary <string, object>();
                    image      = null;
                    font       = PdfFont.DefaultFont;
                    return(pc);
                }
                else
                {
                    return(null);
                }
            }
            IHyphenationEvent hyphenationEvent = null;

            if (noStroke.ContainsKey(Chunk.HYPHENATION))
            {
                hyphenationEvent = (IHyphenationEvent)noStroke[Chunk.HYPHENATION];
            }
            int   currentPosition = 0;
            int   splitPosition   = -1;
            float currentWidth    = 0;

            // loop over all the characters of a string
            // or until the totalWidth is reached
            int   lastSpace      = -1;
            float lastSpaceWidth = 0;
            int   length         = value.Length;

            char[]   valueArray = value.ToCharArray();
            char     character  = (char)0;
            BaseFont ft         = font.Font;
            bool     surrogate  = false;

            if (ft.FontType == BaseFont.FONT_TYPE_CJK && ft.GetUnicodeEquivalent(' ') != ' ')
            {
                while (currentPosition < length)
                {
                    // the width of every character is added to the currentWidth
                    char cidChar = valueArray[currentPosition];
                    character = (char)ft.GetUnicodeEquivalent(cidChar);
                    // if a newLine or carriageReturn is encountered
                    if (character == '\n')
                    {
                        newlineSplit = true;
                        string returnValue = value.Substring(currentPosition + 1);
                        value = value.Substring(0, currentPosition);
                        if (value.Length < 1)
                        {
                            value = "\u0001";
                        }
                        PdfChunk pc = new PdfChunk(returnValue, this);
                        return(pc);
                    }
                    currentWidth += GetCharWidth(cidChar);
                    if (character == ' ')
                    {
                        lastSpace      = currentPosition + 1;
                        lastSpaceWidth = currentWidth;
                    }
                    if (currentWidth > width)
                    {
                        break;
                    }
                    // if a split-character is encountered, the splitPosition is altered
                    if (splitCharacter.IsSplitCharacter(0, currentPosition, length, valueArray, new PdfChunk[] { this }))
                    {
                        splitPosition = currentPosition + 1;
                    }
                    currentPosition++;
                }
            }
            else
            {
                while (currentPosition < length)
                {
                    // the width of every character is added to the currentWidth
                    character = valueArray[currentPosition];
                    // if a newLine or carriageReturn is encountered
                    if (character == '\r' || character == '\n')
                    {
                        newlineSplit = true;
                        int inc = 1;
                        if (character == '\r' && currentPosition + 1 < length && valueArray[currentPosition + 1] == '\n')
                        {
                            inc = 2;
                        }
                        string returnValue = value.Substring(currentPosition + inc);
                        value = value.Substring(0, currentPosition);
                        if (value.Length < 1)
                        {
                            value = " ";
                        }
                        PdfChunk pc = new PdfChunk(returnValue, this);
                        return(pc);
                    }
                    surrogate = Utilities.IsSurrogatePair(valueArray, currentPosition);
                    if (surrogate)
                    {
                        currentWidth += GetCharWidth(Utilities.ConvertToUtf32(valueArray[currentPosition], valueArray[currentPosition + 1]));
                    }
                    else
                    {
                        currentWidth += GetCharWidth(character);
                    }
                    if (character == ' ')
                    {
                        lastSpace      = currentPosition + 1;
                        lastSpaceWidth = currentWidth;
                    }
                    if (surrogate)
                    {
                        currentPosition++;
                    }
                    if (currentWidth > width)
                    {
                        break;
                    }
                    // if a split-character is encountered, the splitPosition is altered
                    if (splitCharacter.IsSplitCharacter(0, currentPosition, length, valueArray, null))
                    {
                        splitPosition = currentPosition + 1;
                    }
                    currentPosition++;
                }
            }

            // if all the characters fit in the total width, null is returned (there is no overflow)
            if (currentPosition == length)
            {
                return(null);
            }
            // otherwise, the string has to be truncated
            if (splitPosition < 0)
            {
                string returnValue = value;
                value = "";
                PdfChunk pc = new PdfChunk(returnValue, this);
                return(pc);
            }
            if (lastSpace > splitPosition && splitCharacter.IsSplitCharacter(0, 0, 1, singleSpace, null))
            {
                splitPosition = lastSpace;
            }
            if (hyphenationEvent != null && lastSpace >= 0 && lastSpace < currentPosition)
            {
                int wordIdx = GetWord(value, lastSpace);
                if (wordIdx > lastSpace)
                {
                    string pre  = hyphenationEvent.GetHyphenatedWordPre(value.Substring(lastSpace, wordIdx - lastSpace), font.Font, font.Size, width - lastSpaceWidth);
                    string post = hyphenationEvent.HyphenatedWordPost;
                    if (pre.Length > 0)
                    {
                        string returnValue = post + value.Substring(wordIdx);
                        value = Trim(value.Substring(0, lastSpace) + pre);
                        PdfChunk pc = new PdfChunk(returnValue, this);
                        return(pc);
                    }
                }
            }
            string retVal = value.Substring(splitPosition);

            value = Trim(value.Substring(0, splitPosition));
            PdfChunk tmp = new PdfChunk(retVal, this);

            return(tmp);
        }
Пример #8
0
 /// <summary>
 /// Constructs a Phrase with a certain Chunk.
 /// </summary>
 /// <param name="chunk">a Chunk</param>
 public Phrase(Chunk chunk)
 {
     base.Add(chunk);
     font        = chunk.Font;
     hyphenation = chunk.GetHyphenation();
 }
Пример #9
0
 /// <summary>
 /// sets the hyphenation engine to this Chunk.
 /// </summary>
 /// <param name="hyphenation">the hyphenation engine</param>
 /// <returns>this Chunk</returns>
 public Chunk SetHyphenation(IHyphenationEvent hyphenation) {
     return SetAttribute(HYPHENATION, hyphenation);
 }
Пример #10
0
 /// <summary>
 /// sets the hyphenation engine to this Chunk.
 /// </summary>
 /// <param name="hyphenation">the hyphenation engine</param>
 /// <returns>this Chunk</returns>
 virtual public Chunk SetHyphenation(IHyphenationEvent hyphenation)
 {
     return(SetAttribute(HYPHENATION, hyphenation));
 }
Пример #11
0
        /**
         * Splits this <CODE>PdfChunk</CODE> if it's too long for the given width.
         * <P>
         * Returns <VAR>null</VAR> if the <CODE>PdfChunk</CODE> wasn't truncated.
         *
         * @param		width		a given width
         * @return		the <CODE>PdfChunk</CODE> that doesn't fit into the width.
         */

        internal PdfChunk split(float width)
        {
            newlineSplit = false;
            if (image != null)
            {
                if (image.ScaledWidth > width)
                {
                    PdfChunk pc = new PdfChunk(Chunk.OBJECT_REPLACEMENT_CHARACTER, this);
                    value      = "";
                    attributes = new Hashmap();
                    image      = null;
                    font       = new PdfFont(PdfFont.HELVETICA, 12);
                    return(pc);
                }
                else
                {
                    return(null);
                }
            }
            IHyphenationEvent hyphenationEvent = (IHyphenationEvent)noStroke[Chunk.HYPHENATION];
            int   currentPosition = 0;
            int   splitPosition   = -1;
            float currentWidth    = 0;

            // loop over all the characters of a string
            // or until the totalWidth is reached
            int      lastSpace      = -1;
            float    lastSpaceWidth = 0;
            int      length         = value.Length;
            char     character      = (char)0;
            BaseFont ft             = font.Font;

            if (ft.FontType == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ')
            {
                while (currentPosition < length)
                {
                    // the width of every character is added to the currentWidth
                    char cidChar = value[currentPosition];
                    character = ft.getUnicodeEquivalent(cidChar);
                    // if a newLine or carriageReturn is encountered
                    if (character == '\n')
                    {
                        newlineSplit = true;
                        string returnValue = value.Substring(currentPosition + 1);
                        value = value.Substring(0, currentPosition);
                        if (value.Length < 1)
                        {
                            value = "\u0001";
                        }
                        PdfChunk pc = new PdfChunk(returnValue, this);
                        return(pc);
                    }
                    currentWidth += font.width(cidChar);
                    if (character == ' ')
                    {
                        lastSpace      = currentPosition + 1;
                        lastSpaceWidth = currentWidth;
                    }
                    if (currentWidth > width)
                    {
                        break;
                    }
                    // if a split-character is encountered, the splitPosition is altered
                    if (splitCharacter.isSplitCharacter(character))
                    {
                        splitPosition = currentPosition + 1;
                    }
                    currentPosition++;
                }
            }
            else
            {
                while (currentPosition < length)
                {
                    // the width of every character is added to the currentWidth
                    character = value[currentPosition];
                    // if a newLine or carriageReturn is encountered
                    if (character == '\r' || character == '\n')
                    {
                        newlineSplit = true;
                        int inc = 1;
                        if (character == '\r' && currentPosition + 1 < length && value[currentPosition + 1] == '\n')
                        {
                            inc = 2;
                        }
                        string returnValue = value.Substring(currentPosition + inc);
                        value = value.Substring(0, currentPosition);
                        if (value.Length < 1)
                        {
                            value = " ";
                        }
                        PdfChunk pc = new PdfChunk(returnValue, this);
                        return(pc);
                    }
                    currentWidth += font.width(character);
                    if (character == ' ')
                    {
                        lastSpace      = currentPosition + 1;
                        lastSpaceWidth = currentWidth;
                    }
                    if (currentWidth > width)
                    {
                        break;
                    }
                    // if a split-character is encountered, the splitPosition is altered
                    if (splitCharacter.isSplitCharacter(character))
                    {
                        splitPosition = currentPosition + 1;
                    }
                    currentPosition++;
                }
            }

            // if all the characters fit in the total width, null is returned (there is no overflow)
            if (currentPosition == length)
            {
                return(null);
            }
            // otherwise, the string has to be truncated
            if (splitPosition < 0)
            {
                string returnValue = value;
                value = "";
                PdfChunk pc = new PdfChunk(returnValue, this);
                return(pc);
            }
            if (lastSpace > splitPosition && splitCharacter.isSplitCharacter(' '))
            {
                splitPosition = lastSpace;
            }
            if (hyphenationEvent != null && lastSpace < currentPosition)
            {
                int wordIdx = getWord(value, lastSpace);
                if (wordIdx > lastSpace)
                {
                    string pre  = hyphenationEvent.getHyphenatedWordPre(value.Substring(lastSpace, wordIdx - lastSpace), font.Font, font.Size, width - lastSpaceWidth);
                    string post = hyphenationEvent.HyphenatedWordPost;
                    if (post.Length > 0)
                    {
                        string returnValue = post + value.Substring(wordIdx);
                        value = trim(value.Substring(0, lastSpace) + pre);
                        PdfChunk pc = new PdfChunk(returnValue, this);
                        return(pc);
                    }
                }
            }
            string retVal = value.Substring(splitPosition);

            value = trim(value.Substring(0, splitPosition));
            PdfChunk tmp = new PdfChunk(retVal, this);

            return(tmp);
        }
Пример #12
0
        public PdfLine ProcessLine(float leftX, float width, int alignment, int runDirection, int arabicOptions)
        {
            ArabicOptions = arabicOptions;
            Save();
            bool isRtl = (runDirection == PdfWriter.RUN_DIRECTION_RTL);

            if (CurrentChar >= TotalTextLength)
            {
                bool hasText = GetParagraph(runDirection);
                if (!hasText)
                {
                    return(null);
                }
                if (TotalTextLength == 0)
                {
                    ArrayList ar  = new ArrayList();
                    PdfChunk  ckx = new PdfChunk("", DetailChunks[0]);
                    ar.Add(ckx);
                    return(new PdfLine(0, 0, 0, alignment, true, ar, isRtl));
                }
            }
            float originalWidth = width;
            int   lastSplit     = -1;

            if (CurrentChar != 0)
            {
                CurrentChar = TrimLeftEx(CurrentChar, TotalTextLength - 1);
            }
            int      oldCurrentChar = CurrentChar;
            int      uniC           = 0;
            PdfChunk ck             = null;
            float    charWidth      = 0;
            PdfChunk lastValidChunk = null;
            bool     splitChar      = false;
            bool     surrogate      = false;

            for (; CurrentChar < TotalTextLength; ++CurrentChar)
            {
                ck        = DetailChunks[CurrentChar];
                surrogate = Utilities.IsSurrogatePair(Text, CurrentChar);
                if (surrogate)
                {
                    uniC = ck.GetUnicodeEquivalent(Utilities.ConvertToUtf32(Text, CurrentChar));
                }
                else
                {
                    uniC = ck.GetUnicodeEquivalent(Text[CurrentChar]);
                }
                if (PdfChunk.NoPrint(uniC))
                {
                    continue;
                }
                if (surrogate)
                {
                    charWidth = ck.GetCharWidth(uniC);
                }
                else
                {
                    charWidth = ck.GetCharWidth(Text[CurrentChar]);
                }
                splitChar = ck.IsExtSplitCharacter(oldCurrentChar, CurrentChar, TotalTextLength, Text, DetailChunks);
                if (splitChar && char.IsWhiteSpace((char)uniC))
                {
                    lastSplit = CurrentChar;
                }
                if (width - charWidth < 0)
                {
                    break;
                }
                if (splitChar)
                {
                    lastSplit = CurrentChar;
                }
                width         -= charWidth;
                lastValidChunk = ck;
                if (surrogate)
                {
                    ++CurrentChar;
                }
                if (ck.IsTab())
                {
                    object[] tab         = (object[])ck.GetAttribute(Chunk.TAB);
                    float    tabPosition = (float)tab[1];
                    bool     newLine     = (bool)tab[2];
                    if (newLine && tabPosition < originalWidth - width)
                    {
                        return(new PdfLine(0, originalWidth, width, alignment, true, CreateArrayOfPdfChunks(oldCurrentChar, CurrentChar - 1), isRtl));
                    }
                    DetailChunks[CurrentChar].AdjustLeft(leftX);
                    width = originalWidth - tabPosition;
                }
            }
            if (lastValidChunk == null)
            {
                // not even a single char fit; must output the first char
                ++CurrentChar;
                if (surrogate)
                {
                    ++CurrentChar;
                }
                return(new PdfLine(0, originalWidth, 0, alignment, false, CreateArrayOfPdfChunks(CurrentChar - 1, CurrentChar - 1), isRtl));
            }
            if (CurrentChar >= TotalTextLength)
            {
                // there was more line than text
                return(new PdfLine(0, originalWidth, width, alignment, true, CreateArrayOfPdfChunks(oldCurrentChar, TotalTextLength - 1), isRtl));
            }
            int newCurrentChar = TrimRightEx(oldCurrentChar, CurrentChar - 1);

            if (newCurrentChar < oldCurrentChar)
            {
                // only WS
                return(new PdfLine(0, originalWidth, width, alignment, false, CreateArrayOfPdfChunks(oldCurrentChar, CurrentChar - 1), isRtl));
            }
            if (newCurrentChar == CurrentChar - 1)
            { // middle of word
                IHyphenationEvent he = (IHyphenationEvent)lastValidChunk.GetAttribute(Chunk.HYPHENATION);
                if (he != null)
                {
                    int[] word = GetWord(oldCurrentChar, newCurrentChar);
                    if (word != null)
                    {
                        float  testWidth = width + GetWidth(word[0], CurrentChar - 1);
                        string pre       = he.GetHyphenatedWordPre(new string(Text, word[0], word[1] - word[0]), lastValidChunk.Font.Font, lastValidChunk.Font.Size, testWidth);
                        string post      = he.HyphenatedWordPost;
                        if (pre.Length > 0)
                        {
                            PdfChunk extra = new PdfChunk(pre, lastValidChunk);
                            CurrentChar = word[1] - post.Length;
                            return(new PdfLine(0, originalWidth, testWidth - lastValidChunk.Font.Width(pre), alignment, false, CreateArrayOfPdfChunks(oldCurrentChar, word[0] - 1, extra), isRtl));
                        }
                    }
                }
            }
            if (lastSplit == -1 || lastSplit >= newCurrentChar)
            {
                // no split point or split point ahead of end
                return(new PdfLine(0, originalWidth, width + GetWidth(newCurrentChar + 1, CurrentChar - 1), alignment, false, CreateArrayOfPdfChunks(oldCurrentChar, newCurrentChar), isRtl));
            }
            // standard split
            CurrentChar    = lastSplit + 1;
            newCurrentChar = TrimRightEx(oldCurrentChar, lastSplit);
            if (newCurrentChar < oldCurrentChar)
            {
                // only WS again
                newCurrentChar = CurrentChar - 1;
            }
            return(new PdfLine(0, originalWidth, originalWidth - GetWidth(oldCurrentChar, newCurrentChar), alignment, false, CreateArrayOfPdfChunks(oldCurrentChar, newCurrentChar), isRtl));
        }