예제 #1
0
        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>");
            }

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

            //Set currently used properties
            lastBaseLine = curBaseline;
            lastFontSize = curFontSize;
            lastFont     = curFont;
        }
예제 #2
0
        public void RenderText(TextRenderInfo renderInfo)
        {
            var block = new TextBlock();

            block.FontName = renderInfo.GetFont().PostscriptFontName;
            // Check if faux bold is used
            if ((renderInfo.GetTextRenderMode() == (int)TextRenderMode.FillThenStrokeText))
            {
                block.ForceBold = true;
            }

            var curBaseline = renderInfo.GetBaseline().GetStartPoint();
            var topRight    = renderInfo.GetAscentLine().GetEndPoint();
            var rect        = MakeRectangle(curBaseline, topRight);

            //block.FontSize = rect.Height;
            block.FontSize  = rect.Height * 1.33f;
            block.Rectangle = rect;

            block.Value = renderInfo.GetText();

            blocks.Add(block);
        }
예제 #3
0
        public Color?GetColor(TextRenderInfo text)
        {
            iText.Kernel.Colors.Color color;

            float alpha;

            switch (text.GetTextRenderMode())
            {
            case 0:     // Fill text
            case 4:     // Fill text and add to path for clipping
                color = text.GetFillColor();
                alpha = text.GetGraphicsState().GetFillOpacity();
                break;

            case 1:     //  Stroke text
            case 2:     //Fill, then stroke text
            case 5:     // Stroke text and add to path for clipping
            case 6:     // Fill, then stroke text and add to path for clipping
                color = text.GetStrokeColor();
                alpha = text.GetGraphicsState().GetStrokeOpacity();
                break;

            case 3:     // Invisible
                return(System.Drawing.Color.Transparent);

            case 7:     // Add text to padd for clipping
                return(System.Drawing.Color.Black);

            default: throw new ApplicationException("wrong render mode");
            }


//           var value = text.GetStrokeColor().GetColorValue();
//           if(value.Length==1&&value[0]==0)
//               value = text.GetFillColor().GetColorValue();
            return(GetColor(color, alpha));
        }
        /// Captures text using a simplified algorithm for inserting hard returns and spaces
        ///     @param   renderInfo  render info
        public virtual void RenderText(TextRenderInfo renderInfo)
        {
            // todo handle adding last line

            string text = renderInfo.GetText().Replace('\t', ' ');

            if (!Utils.FloatsNearlyEqual(renderInfo.GetBaseline().GetStartPoint().Get(Vector.I2), baseline, 0.01f) &&
                !Utils.FloatsNearlyEqual(renderInfo.GetAscentLine().GetStartPoint().Get(Vector.I2), ascent, 0.01f))
            {
                startOfNewline = true;
            }

            if (startOfNewline)
            {
                if (currLine != null)
                {
                    if (currChunk.ChunkStr.Length > 0)
                    {
                        StartNewWord();
                        startOfChunk = true;
                    }
                    currLine.RightMostPos     = bottomRight.Get(Vector.I1);
                    currLine.LineSpacingBelow =
                        currLine.Descent - renderInfo.GetAscentLine().GetStartPoint().Get(Vector.I2);
                }

                currLine = new LineMetrics();
                pageMetrics.AddLine(currLine);
                currLine.Ascent           = renderInfo.GetAscentLine().GetStartPoint().Get(Vector.I2);
                currLine.Descent          = renderInfo.GetDescentLine().GetStartPoint().Get(Vector.I2);
                currLine.Baseline         = renderInfo.GetBaseline().GetStartPoint().Get(Vector.I2);
                currLine.LeftMostPos      = renderInfo.GetBaseline().GetStartPoint().Get(Vector.I1);
                currLine.LineSpacingAbove = descent - currLine.Ascent;

                startOfNewline = false;
            }

            CharMetrics charMetrics;

            // Check if there is space char required between chunks
            if (GetResultantText().Length > 0 &&
                IsSpaceRequired(bottomRight.Get(Vector.I1), renderInfo.GetBaseline().GetStartPoint().Get(Vector.I1),
                                renderInfo.GetBaseline().GetStartPoint().Get(Vector.I2),
                                renderInfo.GetSingleSpaceWidth())) // todo switch order

            {
                charMetrics             = new CharMetrics(' ');
                charMetrics.BottomLeft  = new Vector3D(bottomLeft);
                charMetrics.TopLeft     = new Vector3D(topLeft);
                charMetrics.BottomRight = new Vector3D(renderInfo.GetBaseline().GetStartPoint());
                charMetrics.TopRight    = new Vector3D(renderInfo.GetAscentLine().GetStartPoint());
                this.charMetrices.Add(charMetrics);
                text = " " + text; // todo extra spacing? e.g. handle if it is 4x singlespace width
            }

            TextRenderInfo lastChar = null;
            int            charInd  = 0;

            foreach (TextRenderInfo charInfo in renderInfo.GetCharacterRenderInfos())
            {
                if (charInfo.GetText().Length == 0)
                {
                    continue;
                }

                if (lastChar != null && IsSpaceRequired(lastChar.GetBaseline().GetEndPoint().Get(Vector.I1),
                                                        charInfo.GetBaseline().GetStartPoint().Get(Vector.I1),
                                                        renderInfo.GetSingleSpaceWidth()))
                {
                    charMetrics             = new CharMetrics(' ');
                    charMetrics.FontSize    = charInfo.GetFontSize();
                    charMetrics.BottomLeft  = new Vector3D(lastChar.GetBaseline().GetEndPoint());
                    charMetrics.TopLeft     = new Vector3D(lastChar.GetAscentLine().GetEndPoint());
                    charMetrics.BottomRight = new Vector3D(charInfo.GetBaseline().GetStartPoint());
                    charMetrics.TopRight    = new Vector3D(charInfo.GetAscentLine().GetStartPoint());
                    charMetrices.Add(charMetrics);
                    text = text.Insert(charInd + 1, " ");
                }

                char c = charInfo.GetText()[0] == '\t' ? ' ' : charInfo.GetText()[0];
                charMetrics             = new CharMetrics(c);
                charMetrics.FontSize    = charInfo.GetFontSize();
                charMetrics.BottomLeft  = new Vector3D(charInfo.GetBaseline().GetStartPoint());
                charMetrics.TopLeft     = new Vector3D(charInfo.GetAscentLine().GetStartPoint());
                charMetrics.BottomRight = new Vector3D(charInfo.GetBaseline().GetEndPoint());
                charMetrics.TopRight    = new Vector3D(charInfo.GetAscentLine().GetEndPoint());
                charMetrices.Add(charMetrics);

                lastChar = charInfo;
                charInd++;
            }

            if (startOfChunk)
            {
                if (Utils.FloatsNearlyEqual(currLine.Baseline, baseline, 0.001f))
                {
                    float horSpacing = Math.Abs(
                        bottomRight.Get(Vector.I1) - renderInfo.GetBaseline().GetStartPoint().Get(Vector.I1));
                    if (horSpacing > spaceWidth)
                    {
                        Console.Write("");
                    }
                }

                bottomLeft           = renderInfo.GetBaseline().GetStartPoint();
                topLeft              = renderInfo.GetAscentLine().GetStartPoint();
                currChunk.BottomLeft = new Vector3D(bottomLeft);
                currChunk.TopLeft    = new Vector3D(topLeft);
                baseline             = renderInfo.GetBaseline().GetStartPoint().Get(Vector.I2);
                ascent     = renderInfo.GetAscentLine().GetStartPoint().Get(Vector.I2);
                descent    = renderInfo.GetDescentLine().GetStartPoint().Get(Vector.I2);
                spaceWidth = renderInfo.GetSingleSpaceWidth();

                startOfChunk = false;
            }

            bottomRight = renderInfo.GetBaseline().GetEndPoint();
            topRight    = renderInfo.GetAscentLine().GetEndPoint();

            string currFont = renderInfo.GetFont() != null?renderInfo.GetFont().ToString() : "";

            if (currChunk.FontFamily == null && currFont.Length > 0)
            {
                currChunk.FontFamily = currFont;
            }

            //Check if faux bold is used
            if ((renderInfo.GetTextRenderMode() == (int)TextRenderMode.FillThenStrokeText) || // todo include render mode for outsets etc?
                renderInfo.GetFont().GetFontProgram().GetFontNames().IsBold())
            {
                currChunk.Bold = true;
            }

            if (!(Utils.FloatsNearlyEqual(renderInfo.GetFont().GetFontProgram().GetFontMetrics().GetItalicAngle(), 0, 0.001f)) ||
                renderInfo.GetFont().GetFontProgram().GetFontNames().IsItalic())
            {
                currChunk.Italic = true;
            }

            if (currChunk.FillColor == null && renderInfo.GetFillColor() != null)
            {
                currChunk.FillColor = ProcessColorInfo(renderInfo.GetFillColor());
            }

            if (currChunk.StrokeColor == null && renderInfo.GetStrokeColor() != null)
            {
                currChunk.StrokeColor = ProcessColorInfo(renderInfo.GetStrokeColor());
            }

            currChunk.Append(text);
            result.Append(text); // todo include newlines
        }