예제 #1
0
 public float[] DrawOn(Page page, bool draw)
 {
     this.x_text = x;
     this.y_text = y + font.GetAscent();
     foreach (Paragraph paragraph in paragraphs)
     {
         int           numberOfTextLines = paragraph.list.Count;
         StringBuilder buf = new StringBuilder();
         for (int i = 0; i < numberOfTextLines; i++)
         {
             TextLine textLine = paragraph.list[i];
             buf.Append(textLine.GetText());
         }
         for (int i = 0; i < numberOfTextLines; i++)
         {
             TextLine textLine = paragraph.list[i];
             if (i == 0)
             {
                 beginParagraphPoints.Add(new float[] { x_text, y_text });
             }
             textLine.SetAltDescription((i == 0) ? buf.ToString() : Single.space);
             textLine.SetActualText((i == 0) ? buf.ToString() : Single.space);
             float[] point = DrawTextLine(page, x_text, y_text, textLine, draw);
             if (i == (numberOfTextLines - 1))
             {
                 endParagraphPoints.Add(new float[] { point[0], point[1] });
             }
             x_text = point[0] + spaceBetweenTextLines;
             y_text = point[1];
         }
         x_text  = x;
         y_text += paragraphLeading;
     }
     return(new float[] { x_text, y_text + font.GetDescent() });
 }
예제 #2
0
        private TextLine DrawLineOnPage(TextLine textLine, Page page)
        {
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();

            String[] tokens     = Regex.Split(textLine.GetText(), @"\s+");
            bool     testForFit = true;

            for (int i = 0; i < tokens.Length; i++)
            {
                String token = tokens[i] + Single.space;
                if (testForFit && textLine.GetStringWidth((sb1.ToString() + token).Trim()) < this.w)
                {
                    sb1.Append(token);
                }
                else
                {
                    if (testForFit)
                    {
                        testForFit = false;
                    }
                    sb2.Append(token);
                }
            }
            textLine.SetText(sb1.ToString().Trim());
            if (page != null)
            {
                textLine.DrawOn(page);
            }

            textLine.SetText(sb2.ToString().Trim());
            return(textLine);
        }
예제 #3
0
        public float[] DrawTextLine(
            Page page, float x_text, float y_text, TextLine textLine, bool draw)
        {
            Font font         = textLine.GetFont();
            Font fallbackFont = textLine.GetFallbackFont();
            int  color        = textLine.GetColor();

            StringBuilder buf = new StringBuilder();

            String[] tokens           = Regex.Split(textLine.GetText(), "\\s+");
            bool     firstTextSegment = true;

            for (int i = 0; i < tokens.Length; i++)
            {
                String token = (i == 0) ? tokens[i] : (" " + tokens[i]);
                if (font.StringWidth(fallbackFont, token) < (this.w - (x_text - x)))
                {
                    buf.Append(token);
                    x_text += font.StringWidth(fallbackFont, token);
                }
                else
                {
                    if (draw)
                    {
                        new TextLine(font, buf.ToString())
                        .SetFallbackFont(textLine.GetFallbackFont())
                        .SetLocation(x_text - font.StringWidth(fallbackFont, buf.ToString()), y_text)
                        .SetColor(color)
                        .SetUnderline(textLine.GetUnderline())
                        .SetStrikeout(textLine.GetStrikeout())
                        .SetLanguage(textLine.GetLanguage())
                        .SetAltDescription(firstTextSegment ? textLine.GetAltDescription() : Single.space)
                        .SetActualText(firstTextSegment ? textLine.GetActualText() : Single.space)
                        .DrawOn(page);
                        firstTextSegment = false;
                    }
                    x_text     = x + font.StringWidth(fallbackFont, tokens[i]);
                    y_text    += leading;
                    buf.Length = 0;
                    buf.Append(tokens[i]);
                }
            }
            if (draw)
            {
                new TextLine(font, buf.ToString())
                .SetFallbackFont(textLine.GetFallbackFont())
                .SetLocation(x_text - font.StringWidth(fallbackFont, buf.ToString()), y_text)
                .SetColor(color)
                .SetUnderline(textLine.GetUnderline())
                .SetStrikeout(textLine.GetStrikeout())
                .SetLanguage(textLine.GetLanguage())
                .SetAltDescription(firstTextSegment ? textLine.GetAltDescription() : Single.space)
                .SetActualText(firstTextSegment ? textLine.GetActualText() : Single.space)
                .DrawOn(page);
                firstTextSegment = false;
            }

            return(new float[] { x_text, y_text });
        }
예제 #4
0
        public float[] DrawOn(Page page)
        {
            float xText = x;
            float yText = y + font.ascent;

            while (paragraphs.Count > 0)
            {
                // The paragraphs are reversed so we can efficiently remove the first one:
                TextLine textLine = paragraphs[paragraphs.Count - 1];
                paragraphs.RemoveAt(paragraphs.Count - 1);
                textLine.SetLocation(xText, yText);
                beginParagraphPoints.Add(new float[] { xText, yText });
                while (true)
                {
                    textLine = DrawLineOnPage(textLine, page);
                    if (textLine.GetText().Equals(""))
                    {
                        break;
                    }
                    yText = textLine.Advance(leading);
                    if (yText + font.descent >= (y + h))
                    {
                        // The paragraphs are reversed so we can efficiently add new first paragraph:
                        paragraphs.Add(textLine);

                        if (page != null && drawBorder)
                        {
                            Box box = new Box();
                            box.SetLocation(x, y);
                            box.SetSize(w, h);
                            box.DrawOn(page);
                        }

                        return(new float[] { x + w, y + h });
                    }
                }
                xText  = x;
                yText += paragraphLeading;
            }

            if (page != null && drawBorder)
            {
                Box box = new Box();
                box.SetLocation(x, y);
                box.SetSize(w, h);
                box.DrawOn(page);
            }

            return(new float[] { x + w, y + h });
        }
예제 #5
0
        private void DrawNonJustifiedLine(
            Page page, List <TextLine> list, bool draw)
        {
            float run_length = 0f;

            for (int i = 0; i < list.Count; i++)
            {
                TextLine text = list[i];
                if (i < (list.Count - 1))
                {
                    text.str += " ";
                }
                run_length += text.font.StringWidth(text.GetFallbackFont(), text.str);
            }

            if (alignment == Align.CENTER)
            {
                if (rotate == 0)
                {
                    x1 = x + ((w - run_length) / 2);
                }
                else if (rotate == 90)
                {
                    y1 = y - ((w - run_length) / 2);
                }
                else if (rotate == 270)
                {
                    y1 = y + ((w - run_length) / 2);
                }
            }
            else if (alignment == Align.RIGHT)
            {
                if (rotate == 0)
                {
                    x1 = x + (w - run_length);
                }
                else if (rotate == 90)
                {
                    y1 = y - (w - run_length);
                }
                else if (rotate == 270)
                {
                    y1 = y + (w - run_length);
                }
            }

            for (int i = 0; i < list.Count; i++)
            {
                TextLine text = list[i];
                text.SetPosition(x1, y1);

                if (text.GetGoToAction() != null)
                {
                    page.AddAnnotation(new Annotation(
                                           null,                 // The URI
                                           text.GetGoToAction(), // The destination name
                                           x,
                                           page.height - (y - text.font.ascent),
                                           x + text.font.StringWidth(text.GetFallbackFont(), text.GetText()),
                                           page.height - (y - text.font.descent),
                                           null,
                                           null,
                                           null));
                }

                if (rotate == 0)
                {
                    text.SetTextDirection(0);
                    text.DrawOn(page, draw);
                    x1 += text.font.StringWidth(text.GetFallbackFont(), text.str);
                }
                else if (rotate == 90)
                {
                    text.SetTextDirection(90);
                    text.DrawOn(page, draw);
                    y1 -= text.font.StringWidth(text.GetFallbackFont(), text.str);
                }
                else if (rotate == 270)
                {
                    text.SetTextDirection(270);
                    text.DrawOn(page, draw);
                    y1 += text.font.StringWidth(text.GetFallbackFont(), text.str);
                }
            }
        }
예제 #6
0
        private void DrawLineOfText(
            Page page, List <TextLine> list, bool draw)
        {
            if (alignment == Align.JUSTIFY)
            {
                float sum_of_word_widths = 0f;
                for (int i = 0; i < list.Count; i++)
                {
                    TextLine text = list[i];
                    sum_of_word_widths += text.font.StringWidth(text.GetFallbackFont(), text.str);
                }
                float dx = (w - sum_of_word_widths) / (list.Count - 1);
                for (int i = 0; i < list.Count; i++)
                {
                    TextLine text = list[i];
                    text.SetPosition(x1, y1);

                    if (text.GetGoToAction() != null)
                    {
                        page.AddAnnotation(new Annotation(
                                               null,                 // The URI
                                               text.GetGoToAction(), // The destination name
                                               x,
                                               page.height - (y - text.font.ascent),
                                               x + text.font.StringWidth(text.GetFallbackFont(), text.GetText()),
                                               page.height - (y - text.font.descent),
                                               null,
                                               null,
                                               null));
                    }

                    if (rotate == 0)
                    {
                        text.SetTextDirection(0);
                        text.DrawOn(page, draw);
                        x1 += text.font.StringWidth(text.GetFallbackFont(), text.str) + dx;
                    }
                    else if (rotate == 90)
                    {
                        text.SetTextDirection(90);
                        text.DrawOn(page, draw);
                        y1 -= text.font.StringWidth(text.GetFallbackFont(), text.str) + dx;
                    }
                    else if (rotate == 270)
                    {
                        text.SetTextDirection(270);
                        text.DrawOn(page, draw);
                        y1 += text.font.StringWidth(text.GetFallbackFont(), text.str) + dx;
                    }
                }
            }
            else
            {
                DrawNonJustifiedLine(page, list, draw);
            }
        }
예제 #7
0
파일: Text.cs 프로젝트: lubota/spartacus
        public float[] DrawTextLine(
            Page page, float x_text, float y_text, TextLine textLine, bool draw)
        {
            Font font = textLine.GetFont();
            Font fallbackFont = textLine.GetFallbackFont();
            int color = textLine.GetColor();

            StringBuilder buf = new StringBuilder();
            String[] tokens = Regex.Split(textLine.GetText(), "\\s+");
            bool firstTextSegment = true;
            for (int i = 0; i < tokens.Length; i++) {
            String token = (i == 0) ? tokens[i] : (" " + tokens[i]);
            if (font.StringWidth(fallbackFont, token) < (this.w - (x_text - x))) {
                buf.Append(token);
                x_text += font.StringWidth(fallbackFont, token);
            }
            else {
                if (draw) {
                    new TextLine(font, buf.ToString())
                            .SetFallbackFont(textLine.GetFallbackFont())
                            .SetLocation(x_text - font.StringWidth(fallbackFont, buf.ToString()), y_text)
                            .SetColor(color)
                            .SetUnderline(textLine.GetUnderline())
                            .SetStrikeout(textLine.GetStrikeout())
                            .SetLanguage(textLine.GetLanguage())
                            .SetAltDescription(firstTextSegment ? textLine.GetAltDescription() : Single.space)
                            .SetActualText(firstTextSegment ? textLine.GetActualText() : Single.space)
                            .DrawOn(page);
                    firstTextSegment = false;
                }
                x_text = x + font.StringWidth(fallbackFont, tokens[i]);
                y_text += leading;
                buf.Length = 0;
                buf.Append(tokens[i]);
            }
            }
            if (draw) {
            new TextLine(font, buf.ToString())
                    .SetFallbackFont(textLine.GetFallbackFont())
                    .SetLocation(x_text - font.StringWidth(fallbackFont, buf.ToString()), y_text)
                    .SetColor(color)
                    .SetUnderline(textLine.GetUnderline())
                    .SetStrikeout(textLine.GetStrikeout())
                    .SetLanguage(textLine.GetLanguage())
                    .SetAltDescription(firstTextSegment ? textLine.GetAltDescription() : Single.space)
                    .SetActualText(firstTextSegment ? textLine.GetActualText() : Single.space)
                    .DrawOn(page);
            firstTextSegment = false;
            }

            return new float[] { x_text, y_text };
        }
예제 #8
0
        public TextLine DrawTextLine(
            Page page, float x_text, float y_text, TextLine textLine, bool draw)
        {
            TextLine textLine2    = null;
            Font     font         = textLine.GetFont();
            Font     fallbackFont = textLine.GetFallbackFont();
            int      color        = textLine.GetColor();

            StringBuilder buf = new StringBuilder();

            String[] tokens           = Regex.Split(textLine.GetText(), @"\s+");
            bool     firstTextSegment = true;

            for (int i = 0; i < tokens.Length; i++)
            {
                String token = (i == 0) ? tokens[i] : (Single.space + tokens[i]);
                if (font.StringWidth(fallbackFont, token) < (this.w - (x_text - x)))
                {
                    buf.Append(token);
                    x_text += font.StringWidth(fallbackFont, token);
                }
                else
                {
                    if (draw)
                    {
                        new TextLine(font, buf.ToString())
                        .SetFallbackFont(textLine.GetFallbackFont())
                        .SetLocation(x_text - font.StringWidth(fallbackFont, buf.ToString()),
                                     y_text + textLine.GetVerticalOffset())
                        .SetColor(color)
                        .SetUnderline(textLine.GetUnderline())
                        .SetStrikeout(textLine.GetStrikeout())
                        .SetLanguage(textLine.GetLanguage())
                        .SetAltDescription(firstTextSegment ? textLine.GetAltDescription() : Single.space)
                        .SetActualText(firstTextSegment ? textLine.GetActualText() : Single.space)
                        .DrawOn(page);
                        firstTextSegment = false;
                    }
                    x_text     = x + font.StringWidth(fallbackFont, tokens[i]);
                    y_text    += leading;
                    buf.Length = 0;
                    buf.Append(tokens[i]);

                    if (y_text + font.GetDescent() > (y + h))
                    {
                        i++;
                        while (i < tokens.Length)
                        {
                            buf.Append(Single.space);
                            buf.Append(tokens[i]);
                            i++;
                        }
                        textLine2 = new TextLine(font, buf.ToString());
                        textLine2.SetLocation(x, y_text);
                        return(textLine2);
                    }
                }
            }
            if (draw)
            {
                new TextLine(font, buf.ToString())
                .SetFallbackFont(textLine.GetFallbackFont())
                .SetLocation(x_text - font.StringWidth(fallbackFont, buf.ToString()),
                             y_text + textLine.GetVerticalOffset())
                .SetColor(color)
                .SetUnderline(textLine.GetUnderline())
                .SetStrikeout(textLine.GetStrikeout())
                .SetLanguage(textLine.GetLanguage())
                .SetAltDescription(firstTextSegment ? textLine.GetAltDescription() : Single.space)
                .SetActualText(firstTextSegment ? textLine.GetActualText() : Single.space)
                .DrawOn(page);
                firstTextSegment = false;
            }

            textLine2 = new TextLine(font, "");
            textLine2.SetLocation(x_text, y_text);
            return(textLine2);
        }
예제 #9
0
        public TextFrame DrawOn(Page page, bool draw)
        {
            this.x_text = x;
            this.y_text = y + font.GetAscent();

            Paragraph paragraph = null;

            for (int i = 0; i < paragraphs.Count; i++)
            {
                paragraph = paragraphs[i];

                StringBuilder buf = new StringBuilder();
                foreach (TextLine textLine in paragraph.list)
                {
                    buf.Append(textLine.GetText());
                    buf.Append(Single.space);
                }

                int numOfTextLines = paragraph.list.Count;
                for (int j = 0; j < numOfTextLines; j++)
                {
                    TextLine textLine = paragraph.list[j];
                    if (j == 0)
                    {
                        beginParagraphPoints.Add(new float[] { x_text, y_text });
                    }
                    textLine.SetAltDescription((i == 0) ? buf.ToString() : Single.space);
                    textLine.SetActualText((i == 0) ? buf.ToString() : Single.space);

                    TextLine textLine2 = DrawTextLine(page, x_text, y_text, textLine, draw);
                    if (!textLine2.GetText().Equals(""))
                    {
                        List <Paragraph> theRest    = new List <Paragraph>();
                        Paragraph        paragraph2 = new Paragraph(textLine2);
                        j++;
                        while (j < numOfTextLines)
                        {
                            paragraph2.Add(paragraph.list[j]);
                            j++;
                        }
                        theRest.Add(paragraph2);
                        i++;
                        while (i < paragraphs.Count)
                        {
                            theRest.Add(paragraphs[i]);
                            i++;
                        }
                        return(new TextFrame(theRest));
                    }

                    if (j == (numOfTextLines - 1))
                    {
                        endParagraphPoints.Add(new float[] { textLine2.x, textLine2.y });
                    }
                    x_text = textLine2.x;
                    if (textLine.GetTrailingSpace())
                    {
                        x_text += spaceBetweenTextLines;
                    }
                    y_text = textLine2.y;
                }
                x_text  = x;
                y_text += paragraphLeading;
            }

            TextFrame textFrame = new TextFrame(null);

            textFrame.SetLocation(x_text, y_text + font.GetDescent());
            return(textFrame);
        }