Example #1
0
        /// <summary>Output Text at a certain x and y coordinate.</summary>
        /// <remarks>Output Text at a certain x and y coordinate. Clipped or opaque text isn't supported as of yet.</remarks>
        /// <param name="x">x-coordinate</param>
        /// <param name="y">y-coordinate</param>
        /// <param name="flag">flag indicating clipped or opaque</param>
        /// <param name="x1">x1-coordinate of the rectangle if clipped or opaque</param>
        /// <param name="y1">y1-coordinate of the rectangle if clipped or opaque</param>
        /// <param name="x2">x2-coordinate of the rectangle if clipped or opaque</param>
        /// <param name="y2">y1-coordinate of the rectangle if clipped or opaque</param>
        /// <param name="text">text to output</param>
        /// <exception cref="System.IO.IOException"/>
        public virtual void OutputText(int x, int y, int flag, int x1, int y1, int x2, int y2, String text)
        {
            MetaFont    font     = state.GetCurrentFont();
            float       refX     = state.TransformX(x);
            float       refY     = state.TransformY(y);
            float       angle    = state.TransformAngle(font.GetAngle());
            float       sin      = (float)Math.Sin(angle);
            float       cos      = (float)Math.Cos(angle);
            float       fontSize = font.GetFontSize(state);
            FontProgram fp       = font.GetFont();
            int         align    = state.GetTextAlign();
            // NOTE, MetaFont always creates with CP1252 encoding.
            int normalizedWidth = 0;

            byte[] bytes = font.encoding.ConvertToBytes(text);
            foreach (byte b in bytes)
            {
                normalizedWidth += fp.GetWidth(0xff & b);
            }
            float textWidth = fontSize / FontProgram.UNITS_NORMALIZATION * normalizedWidth;
            float tx        = 0;
            float ty        = 0;
            float descender = fp.GetFontMetrics().GetTypoDescender();
            float ury       = fp.GetFontMetrics().GetBbox()[3];

            cb.SaveState();
            cb.ConcatMatrix(cos, sin, -sin, cos, refX, refY);
            if ((align & MetaState.TA_CENTER) == MetaState.TA_CENTER)
            {
                tx = -textWidth / 2;
            }
            else
            {
                if ((align & MetaState.TA_RIGHT) == MetaState.TA_RIGHT)
                {
                    tx = -textWidth;
                }
            }
            if ((align & MetaState.TA_BASELINE) == MetaState.TA_BASELINE)
            {
                ty = 0;
            }
            else
            {
                if ((align & MetaState.TA_BOTTOM) == MetaState.TA_BOTTOM)
                {
                    ty = -descender;
                }
                else
                {
                    ty = -ury;
                }
            }
            Color textColor;

            if (state.GetBackgroundMode() == MetaState.OPAQUE)
            {
                textColor = state.GetCurrentBackgroundColor();
                cb.SetFillColor(textColor);
                cb.Rectangle(tx, ty + descender, textWidth, ury - descender);
                cb.Fill();
            }
            textColor = state.GetCurrentTextColor();
            cb.SetFillColor(textColor);
            cb.BeginText();
            cb.SetFontAndSize(PdfFontFactory.CreateFont(state.GetCurrentFont().GetFont(), PdfEncodings.CP1252, true),
                              fontSize);
            cb.SetTextMatrix(tx, ty);
            cb.ShowText(text);
            cb.EndText();
            if (font.IsUnderline())
            {
                cb.Rectangle(tx, ty - fontSize / 4, textWidth, fontSize / 15);
                cb.Fill();
            }
            if (font.IsStrikeout())
            {
                cb.Rectangle(tx, ty + fontSize / 3, textWidth, fontSize / 15);
                cb.Fill();
            }
            cb.RestoreState();
        }