示例#1
0
        private void DrawString(PageText pt, Graphics g, RectangleF r)
        {
            StyleInfo si = pt.SI;
            string    s  = pt.Text;

            Font         drawFont   = null;
            StringFormat drawFormat = null;
            Brush        drawBrush  = null;

            try
            {
                // STYLE
                System.Drawing.FontStyle fs = 0;
                if (si.FontStyle == FontStyleEnum.Italic)
                {
                    fs |= System.Drawing.FontStyle.Italic;
                }

                switch (si.TextDecoration)
                {
                case TextDecorationEnum.Underline:
                    fs |= System.Drawing.FontStyle.Underline;
                    break;

                case TextDecorationEnum.LineThrough:
                    fs |= System.Drawing.FontStyle.Strikeout;
                    break;

                case TextDecorationEnum.Overline:
                case TextDecorationEnum.None:
                    break;
                }

                // WEIGHT
                switch (si.FontWeight)
                {
                case FontWeightEnum.Bold:
                case FontWeightEnum.Bolder:
                case FontWeightEnum.W500:
                case FontWeightEnum.W600:
                case FontWeightEnum.W700:
                case FontWeightEnum.W800:
                case FontWeightEnum.W900:
                    fs |= System.Drawing.FontStyle.Bold;
                    break;

                default:
                    break;
                }
                try
                {
                    drawFont = new Font(si.GetFontFamily(), si.FontSize, fs);   // si.FontSize already in points
                }
                catch (ArgumentException)
                {
                    drawFont = new Font("Arial", si.FontSize, fs);      // if this fails we'll let the error pass thru
                }
                // ALIGNMENT
                drawFormat = new StringFormat();
                switch (si.TextAlign)
                {
                case TextAlignEnum.Right:
                    drawFormat.Alignment = StringAlignment.Far;
                    break;

                case TextAlignEnum.Center:
                    drawFormat.Alignment = StringAlignment.Center;
                    break;

                case TextAlignEnum.Left:
                default:
                    drawFormat.Alignment = StringAlignment.Near;
                    break;
                }
                if (pt.SI.WritingMode == WritingModeEnum.tb_rl)
                {
                    drawFormat.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
                    drawFormat.FormatFlags |= StringFormatFlags.DirectionVertical;
                }
                switch (si.VerticalAlign)
                {
                case VerticalAlignEnum.Bottom:
                    drawFormat.LineAlignment = StringAlignment.Far;
                    break;

                case VerticalAlignEnum.Middle:
                    drawFormat.LineAlignment = StringAlignment.Center;
                    break;

                case VerticalAlignEnum.Top:
                default:
                    drawFormat.LineAlignment = StringAlignment.Near;
                    break;
                }
                // draw the background
                DrawBackground(g, r, si);

                // adjust drawing rectangle based on padding
                // http://www.fyireporting.com/forum/viewtopic.php?t=892
                //A.S.> convert pt to px if needed(when printing we need px, when draw preview - pt)
                RectangleF r2;
                if (g.PageUnit == GraphicsUnit.Pixel)
                {
                    r2 = new RectangleF(r.Left + (si.PaddingLeft * g.DpiX) / 72,
                                        r.Top + (si.PaddingTop * g.DpiX) / 72,
                                        r.Width - ((si.PaddingLeft + si.PaddingRight) * g.DpiX) / 72,
                                        r.Height - ((si.PaddingTop + si.PaddingBottom) * g.DpiX) / 72);
                }
                else
                {
                    // adjust drawing rectangle based on padding
                    r2 = new RectangleF(r.Left + si.PaddingLeft,
                                        r.Top + si.PaddingTop,
                                        r.Width - si.PaddingLeft - si.PaddingRight,
                                        r.Height - si.PaddingTop - si.PaddingBottom);
                }

                drawBrush = new SolidBrush(si.Color);
                if (si.TextAlign == TextAlignEnum.Justified)
                {
                    GraphicsExtended.DrawStringJustified(g, pt.Text, drawFont, drawBrush, r2);
                }
                else if (pt.NoClip)     // request not to clip text
                {
                    g.DrawString(pt.Text, drawFont, drawBrush, new PointF(r.Left, r.Top), drawFormat);
                }
                else
                {
                    g.DrawString(pt.Text, drawFont, drawBrush, r2, drawFormat);
                }
            }
            finally
            {
                if (drawFont != null)
                {
                    drawFont.Dispose();
                }
                if (drawFormat != null)
                {
                    drawFont.Dispose();
                }
                if (drawBrush != null)
                {
                    drawBrush.Dispose();
                }
            }
        }
示例#2
0
        private void DrawString(PageText pt, Xwt.Drawing.Context g, Xwt.Rectangle r)
        {
            StyleInfo si = pt.SI;
            string    s  = pt.Text;

            g.Save();
            layout      = new Xwt.Drawing.TextLayout(g);
            layout.Font = g.Font;


            float fontsize = (si.FontSize * 72 / 96);

            layout.Font.WithFamily(si.GetFontFamily().Name);
            layout.Font.WithPixelSize(fontsize * PixelsX(1));

            if (si.FontStyle == FontStyleEnum.Italic)
            {
                layout.Font.WithStyle(Xwt.Drawing.FontStyle.Italic);
            }

            switch (si.FontWeight)
            {
            case FontWeightEnum.Bold:
            case FontWeightEnum.Bolder:
            case FontWeightEnum.W500:
            case FontWeightEnum.W600:
            case FontWeightEnum.W700:
            case FontWeightEnum.W800:
            case FontWeightEnum.W900:
                layout.Font.WithWeight(Xwt.Drawing.FontWeight.Bold);
                break;
            }

            // TODO: Fix Alignment
            //switch (si.TextAlign)
            //{
            //    case TextAlignEnum.Right:
            //        layout.Alignment = Pango.Alignment.Right;
            //        break;
            //    case TextAlignEnum.Center:
            //        layout.Alignment = Pango.Alignment.Center;
            //        break;
            //    case TextAlignEnum.Left:
            //    default:
            //        layout.Alignment = Pango.Alignment.Left;
            //        break;
            //}

            // TODO: Fix with
            //layout.Width = Pango.Units.FromPixels((int)(r.Width - si.PaddingLeft - si.PaddingRight - 2));

            layout.Text = s;



            //Xwt.Rectangle logical;
            // Xwt.Rectangle ink;

            // TODO: Fix
            //layout.GetExtents(out ink, out logical);
            double height = 12; // logical.Height / Pango.Scale.PangoScale;

            double y = 0;

            switch (si.VerticalAlign)
            {
            case VerticalAlignEnum.Top:
                y = r.Y + si.PaddingTop;
                break;

            case VerticalAlignEnum.Middle:
                y = r.Y + (r.Height - height) / 2;
                break;

            case VerticalAlignEnum.Bottom:
                y = r.Y + (r.Height - height) - si.PaddingBottom;
                break;
            }

            // draw the background
            DrawBackground(g, r, si);

            Xwt.Rectangle box = new Xwt.Rectangle(
                r.X + si.PaddingLeft + 1,
                y,
                r.Width,
                r.Height);

            Xwt.Drawing.Color sicolor = XwtColor.SystemColorToXwtColor(si.Color);
            g.SetColor(sicolor);

            g.MoveTo(box.X, box.Y);
            g.DrawTextLayout(layout, box.X, box.Y);
            g.Restore();
        }
        private void DrawString(string s, Cairo.Context g, Cairo.Rectangle r)
        {
            StyleInfo si = new StyleInfo();

            g.Save();

            Pango.Layout layout;

            layout = Pango.CairoHelper.CreateLayout(g);

            float fontsize = (si.FontSize * 72 / 96);
            var   font     = Pango.FontDescription.FromString(string.Format("{0} {1}", si.GetFontFamily().Name,
                                                                            fontsize * PixelsX(1)));

            if (si.FontStyle == FontStyleEnum.Italic)
            {
                font.Style = Pango.Style.Italic;
            }

            switch (si.FontWeight)
            {
            case FontWeightEnum.Bold:
            case FontWeightEnum.Bolder:
            case FontWeightEnum.W500:
            case FontWeightEnum.W600:
            case FontWeightEnum.W700:
            case FontWeightEnum.W800:
            case FontWeightEnum.W900:
                font.Weight = Pango.Weight.Bold;
                break;
            }

            Pango.FontDescription oldfont = layout.FontDescription;
            layout.FontDescription = font;

            switch (si.TextAlign)
            {
            case TextAlignEnum.Right:
                layout.Alignment = Pango.Alignment.Right;
                break;

            case TextAlignEnum.Center:
                layout.Alignment = Pango.Alignment.Center;
                break;

            case TextAlignEnum.Left:
            default:
                layout.Alignment = Pango.Alignment.Left;
                break;
            }

            layout.Width = Pango.Units.FromPixels((int)(r.Width - si.PaddingLeft - si.PaddingRight - 2));
            //				layout.Width =  (int)Pango.Units.FromPixels((int)r.Width);

            layout.SetText(s);

            Pango.Rectangle logical;
            Pango.Rectangle ink;
            layout.GetExtents(out ink, out logical);
            double height = logical.Height / Pango.Scale.PangoScale;
            double y      = 0;

            switch (si.VerticalAlign)
            {
            case VerticalAlignEnum.Top:
                y = r.Y + si.PaddingTop;
                break;

            case VerticalAlignEnum.Middle:
                y = r.Y + (r.Height - height) / 2;
                break;

            case VerticalAlignEnum.Bottom:
                y = r.Y + (r.Height - height) - si.PaddingBottom;
                break;
            }

            Cairo.Rectangle box = new Cairo.Rectangle(
                r.X + si.PaddingLeft + 1,
                y,
                r.Width,
                r.Height);

            g.Color = si.Color.ToCairoColor();

            g.MoveTo(box.X, box.Y);

            Pango.CairoHelper.ShowLayout(g, layout);

            layout.FontDescription = oldfont;
            g.Restore();
        }
示例#4
0
        private void DrawString(PageText pt, Cairo.Context g, Cairo.Rectangle r)
        {
            StyleInfo si = pt.SI;
            string    s  = pt.Text;

            g.Save();

            layout = Pango.CairoHelper.CreateLayout(g);

//            Font drawFont = null;
//            StringFormat drawFormat = null;
//            Brush drawBrush = null;


            // STYLE
//                System.Drawing.FontStyle fs = 0;
//                if (si.FontStyle == FontStyleEnum.Italic)
//                    fs |= System.Drawing.FontStyle.Italic;
            //Pango fonts are scaled to 72dpi, Windows fonts uses 96dpi
            float fontsize = (si.FontSize * 72 / 96);
            var   font     = Pango.FontDescription.FromString(string.Format("{0} {1}", si.GetFontFamily().Name,
                                                                            fontsize * PixelsX(1)));

            if (si.FontStyle == FontStyleEnum.Italic)
            {
                font.Style = Pango.Style.Italic;
            }
//
//                switch (si.TextDecoration)
//                {
//                    case TextDecorationEnum.Underline:
//                        fs |= System.Drawing.FontStyle.Underline;
//                        break;
//                    case TextDecorationEnum.LineThrough:
//                        fs |= System.Drawing.FontStyle.Strikeout;
//                        break;
//                    case TextDecorationEnum.Overline:
//                    case TextDecorationEnum.None:
//                        break;
//                }


            // WEIGHT
//                switch (si.FontWeight)
//                {
//                    case FontWeightEnum.Bold:
//                    case FontWeightEnum.Bolder:
//                    case FontWeightEnum.W500:
//                    case FontWeightEnum.W600:
//                    case FontWeightEnum.W700:
//                    case FontWeightEnum.W800:
//                    case FontWeightEnum.W900:
//                        fs |= System.Drawing.FontStyle.Bold;
//                        break;
//                    default:
//                        break;
//                }
//                try
//                {
//                    drawFont = new Font(si.GetFontFamily(), si.FontSize, fs);   // si.FontSize already in points
//                }
//                catch (ArgumentException)
//                {
//                    drawFont = new Font("Arial", si.FontSize, fs);   // if this fails we'll let the error pass thru
//                }
            //font.AbsoluteSize = (int)(PixelsX (si.FontSize));

            switch (si.FontWeight)
            {
            case FontWeightEnum.Bold:
            case FontWeightEnum.Bolder:
            case FontWeightEnum.W500:
            case FontWeightEnum.W600:
            case FontWeightEnum.W700:
            case FontWeightEnum.W800:
            case FontWeightEnum.W900:
                font.Weight = Pango.Weight.Bold;
                break;
            }

            Pango.FontDescription oldfont = layout.FontDescription;
            layout.FontDescription = font;

            // ALIGNMENT
//                drawFormat = new StringFormat();
//                switch (si.TextAlign)
//                {
//                    case TextAlignEnum.Right:
//                        drawFormat.Alignment = StringAlignment.Far;
//                        break;
//                    case TextAlignEnum.Center:
//                        drawFormat.Alignment = StringAlignment.Center;
//                        break;
//                    case TextAlignEnum.Left:
//                    default:
//                        drawFormat.Alignment = StringAlignment.Near;
//                        break;
//                }

            switch (si.TextAlign)
            {
            case TextAlignEnum.Right:
                layout.Alignment = Pango.Alignment.Right;
                break;

            case TextAlignEnum.Center:
                layout.Alignment = Pango.Alignment.Center;
                break;

            case TextAlignEnum.Left:
            default:
                layout.Alignment = Pango.Alignment.Left;
                break;
            }

            layout.Width = Pango.Units.FromPixels((int)(r.Width - si.PaddingLeft - si.PaddingRight - 2));
//				layout.Width =  (int)Pango.Units.FromPixels((int)r.Width);

            layout.SetText(s);


//                if (pt.SI.WritingMode == WritingModeEnum.tb_rl)
//                {
//                    drawFormat.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
//                    drawFormat.FormatFlags |= StringFormatFlags.DirectionVertical;
//                }
//                switch (si.VerticalAlign)
//                {
//                    case VerticalAlignEnum.Bottom:
//                        drawFormat.LineAlignment = StringAlignment.Far;
//                        break;
//                    case VerticalAlignEnum.Middle:
//                        drawFormat.LineAlignment = StringAlignment.Center;
//                        break;
//                    case VerticalAlignEnum.Top:
//                    default:
//                        drawFormat.LineAlignment = StringAlignment.Near;
//                        break;
//                }
//
            Pango.Rectangle logical;
            Pango.Rectangle ink;
            layout.GetExtents(out ink, out logical);
            double height = logical.Height / Pango.Scale.PangoScale;
            double y      = 0;

            switch (si.VerticalAlign)
            {
            case VerticalAlignEnum.Top:
                y = r.Y + si.PaddingTop;
                break;

            case VerticalAlignEnum.Middle:
                y = r.Y + (r.Height - height) / 2;
                break;

            case VerticalAlignEnum.Bottom:
                y = r.Y + (r.Height - height) - si.PaddingBottom;
                break;
            }
            // draw the background
            DrawBackground(g, r, si);

            // adjust drawing rectangle based on padding
//                Cairo.Rectangle r2 = new Cairo.Rectangle(r.X + si.PaddingLeft,
//                                               r.Y + si.PaddingTop,
//                                               r.Width - si.PaddingLeft - si.PaddingRight,
//                                               r.Height - si.PaddingTop - si.PaddingBottom);
            Cairo.Rectangle box = new Cairo.Rectangle(
                r.X + si.PaddingLeft + 1,
                y,
                r.Width,
                r.Height);

            //drawBrush = new SolidBrush(si.Color);
            g.Color = si.Color.ToCairoColor();
//                if (pt.NoClip)   // request not to clip text
//                {
//                    g.DrawString(pt.Text, drawFont, drawBrush, new PointF(r.Left, r.Top), drawFormat);
//                    //HighlightString(g, pt, new RectangleF(r.Left, r.Top, float.MaxValue, float.MaxValue),drawFont, drawFormat);
//                }
//                else
//                {
//                    g.DrawString(pt.Text, drawFont, drawBrush, r2, drawFormat);
//                    //HighlightString(g, pt, r2, drawFont, drawFormat);
//                }

            g.MoveTo(box.X, box.Y);

            Pango.CairoHelper.ShowLayout(g, layout);

            layout.FontDescription = oldfont;
            g.Restore();
        }
示例#5
0
        private void DrawStringTBLR(PageText pt, Cairo.Context g, Cairo.Rectangle r)
        {
            StyleInfo si = pt.SI;
            string    s  = pt.Text;

            g.Save();

            layout = CairoHelper.CreateLayout(g);

            //Pango fonts are scaled to 72dpi, Windows fonts uses 96dpi
            float fontsize = (si.FontSize * 72 / 96);
            var   font     = FontDescription.FromString($"{si.GetFontFamily().Name} {fontsize * PixelsX(1)}");

            if (si.FontStyle == FontStyleEnum.Italic)
            {
                font.Style = Style.Italic;
            }

            switch (si.FontWeight)
            {
            case FontWeightEnum.Bold:
            case FontWeightEnum.Bolder:
            case FontWeightEnum.W500:
            case FontWeightEnum.W600:
            case FontWeightEnum.W700:
            case FontWeightEnum.W800:
            case FontWeightEnum.W900:
                font.Weight = Weight.Bold;
                break;
            }

            FontDescription oldfont = layout.FontDescription;

            layout.FontDescription = font;

            switch (si.TextAlign)
            {
            case TextAlignEnum.Right:
                layout.Alignment = Alignment.Right;
                break;

            case TextAlignEnum.Center:
                layout.Alignment = Alignment.Center;
                break;

            case TextAlignEnum.Left:
            default:
                layout.Alignment = Alignment.Left;
                break;
            }

            layout.Width = Units.FromPixels((int)(r.Height - si.PaddingTop - si.PaddingBottom - 2));

            layout.Wrap = WrapMode.WordChar;
            layout.SetText(s);

            Rectangle logical;
            Rectangle ink;

            layout.GetExtents(out ink, out logical);
            double height = logical.Height / Scale.PangoScale;
            double y      = 0;
            double x      = 0;

            switch (si.VerticalAlign)
            {
            case VerticalAlignEnum.Top:
                x = r.X + si.PaddingLeft;
                break;

            case VerticalAlignEnum.Middle:
                x = r.X + (r.Width - height) / 2;
                break;

            case VerticalAlignEnum.Bottom:
                x = r.X + (r.Width - height) + si.PaddingLeft;
                break;
            }

            // draw the background
            DrawBackground(g, r, si);

            Cairo.Rectangle box = new Cairo.Rectangle(
                x,
                r.Y + r.Height - si.PaddingBottom - 1,
                r.Height - si.PaddingBottom - si.PaddingTop,
                r.Width - si.PaddingLeft + si.PaddingRight);

            g.Color = si.Color.ToCairoColor();

            g.Rotate(270 * Math.PI / 180.0);
            CairoHelper.UpdateLayout(g, layout);

            g.MoveTo(-box.Y, box.X);
            CairoHelper.ShowLayout(g, layout);

            layout.FontDescription = oldfont;
            g.Restore();
        }
示例#6
0
        private void DrawStringHorizontal(PageText pt, Cairo.Context g, Cairo.Rectangle r)
        {
            StyleInfo si = pt.SI;
            string    s  = pt.Text;

            g.Save();

            layout = CairoHelper.CreateLayout(g);

            float fontsize = si.FontSize * 72f / 96f;
            var   font     = FontDescription.FromString(string.Format("{0} {1}", si.GetFontFamily().Name,
                                                                      fontsize * PixelsX(1)));

            if (si.FontStyle == FontStyleEnum.Italic)
            {
                font.Style = Style.Italic;
            }

            switch (si.FontWeight)
            {
            case FontWeightEnum.Bold:
            case FontWeightEnum.Bolder:
            case FontWeightEnum.W500:
            case FontWeightEnum.W600:
            case FontWeightEnum.W700:
            case FontWeightEnum.W800:
            case FontWeightEnum.W900:
                font.Weight = Weight.Bold;
                break;
            }

            FontDescription oldfont = layout.FontDescription;

            layout.FontDescription = font;

            switch (si.TextAlign)
            {
            case TextAlignEnum.Right:
                layout.Alignment = Alignment.Right;
                break;

            case TextAlignEnum.Center:
                layout.Alignment = Alignment.Center;
                break;

            case TextAlignEnum.Left:
            default:
                layout.Alignment = Alignment.Left;
                break;
            }

            layout.Width = Units.FromPixels((int)(r.Width - si.PaddingLeft - si.PaddingRight - 2));
            layout.Wrap  = WrapMode.WordChar;
            layout.SetText(s);

            Rectangle logical;
            Rectangle ink;

            layout.GetExtents(out ink, out logical);
            double height = logical.Height / Scale.PangoScale;
            double y      = 0;

            switch (si.VerticalAlign)
            {
            case VerticalAlignEnum.Top:
                y = r.Y + si.PaddingTop;
                break;

            case VerticalAlignEnum.Middle:
                y = r.Y + (r.Height - height) / 2;
                break;

            case VerticalAlignEnum.Bottom:
                y = r.Y + (r.Height - height) - si.PaddingBottom;
                break;
            }

            // draw the background
            DrawBackground(g, r, si);

            Cairo.Rectangle box = new Cairo.Rectangle(
                r.X + si.PaddingLeft + 1,
                y,
                r.Width,
                r.Height);

            g.Color = si.Color.ToCairoColor();

            g.MoveTo(box.X, box.Y);

            CairoHelper.ShowLayout(g, layout);

            layout.FontDescription = oldfont;
            g.Restore();
        }
示例#7
0
        private void DrawString(PageText pt, Graphics g, RectangleF r)
        {
            StyleInfo si = pt.SI;
            string    s  = pt.Text;

            Font         drawFont   = null;
            StringFormat drawFormat = null;
            Brush        drawBrush  = null;

            try
            {
                // STYLE
                System.Drawing.FontStyle fs = 0;
                if (si.FontStyle == FontStyleEnum.Italic)
                {
                    fs |= System.Drawing.FontStyle.Italic;
                }

                switch (si.TextDecoration)
                {
                case TextDecorationEnum.Underline:
                    fs |= System.Drawing.FontStyle.Underline;
                    break;

                case TextDecorationEnum.LineThrough:
                    fs |= System.Drawing.FontStyle.Strikeout;
                    break;

                case TextDecorationEnum.Overline:
                case TextDecorationEnum.None:
                    break;
                }

                // WEIGHT
                switch (si.FontWeight)
                {
                case FontWeightEnum.Bold:
                case FontWeightEnum.Bolder:
                case FontWeightEnum.W500:
                case FontWeightEnum.W600:
                case FontWeightEnum.W700:
                case FontWeightEnum.W800:
                case FontWeightEnum.W900:
                    fs |= System.Drawing.FontStyle.Bold;
                    break;

                default:
                    break;
                }
                try
                {
                    drawFont = new Font(si.GetFontFamily(), si.FontSize, fs);   // si.FontSize already in points
                }
                catch (ArgumentException)
                {
                    drawFont = new Font("Arial", si.FontSize, fs);   // if this fails we'll let the error pass thru
                }
                // ALIGNMENT
                drawFormat = new StringFormat();
                switch (si.TextAlign)
                {
                case TextAlignEnum.Right:
                    drawFormat.Alignment = StringAlignment.Far;
                    break;

                case TextAlignEnum.Center:
                    drawFormat.Alignment = StringAlignment.Center;
                    break;

                case TextAlignEnum.Left:
                default:
                    drawFormat.Alignment = StringAlignment.Near;
                    break;
                }
                if (pt.SI.WritingMode == WritingModeEnum.tb_rl)
                {
                    drawFormat.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
                    drawFormat.FormatFlags |= StringFormatFlags.DirectionVertical;
                }
                switch (si.VerticalAlign)
                {
                case VerticalAlignEnum.Bottom:
                    drawFormat.LineAlignment = StringAlignment.Far;
                    break;

                case VerticalAlignEnum.Middle:
                    drawFormat.LineAlignment = StringAlignment.Center;
                    break;

                case VerticalAlignEnum.Top:
                default:
                    drawFormat.LineAlignment = StringAlignment.Near;
                    break;
                }
                // draw the background
                DrawBackground(g, r, si);

                // adjust drawing rectangle based on padding
                RectangleF r2 = new RectangleF(r.Left + si.PaddingLeft,
                                               r.Top + si.PaddingTop,
                                               r.Width - si.PaddingLeft - si.PaddingRight,
                                               r.Height - si.PaddingTop - si.PaddingBottom);

                drawBrush = new SolidBrush(si.Color);
                if (pt.NoClip)   // request not to clip text
                {
                    g.DrawString(pt.Text, drawFont, drawBrush, new PointF(r.Left, r.Top), drawFormat);
                    //HighlightString(g, pt, new RectangleF(r.Left, r.Top, float.MaxValue, float.MaxValue),drawFont, drawFormat);
                }
                else
                {
                    g.DrawString(pt.Text, drawFont, drawBrush, r2, drawFormat);
                    //HighlightString(g, pt, r2, drawFont, drawFormat);
                }
            }
            finally
            {
                if (drawFont != null)
                {
                    drawFont.Dispose();
                }
                if (drawFormat != null)
                {
                    drawFont.Dispose();
                }
                if (drawBrush != null)
                {
                    drawBrush.Dispose();
                }
            }
        }
示例#8
0
        private string[] MeasureString(PageText pt, Graphics g, out float[] width)
        {
            StyleInfo si = pt.SI;
            string    s  = pt.Text;

            System.Drawing.Font drawFont   = null;
            StringFormat        drawFormat = null;
            SizeF ms;

            string[] sa = null;
            width = null;
            try
            {
                // STYLE
                System.Drawing.FontStyle fs = 0;
                if (si.FontStyle == FontStyleEnum.Italic)
                {
                    fs |= System.Drawing.FontStyle.Italic;
                }

                // WEIGHT
                switch (si.FontWeight)
                {
                case FontWeightEnum.Bold:
                case FontWeightEnum.Bolder:
                case FontWeightEnum.W500:
                case FontWeightEnum.W600:
                case FontWeightEnum.W700:
                case FontWeightEnum.W800:
                case FontWeightEnum.W900:
                    fs |= System.Drawing.FontStyle.Bold;
                    break;

                default:
                    break;
                }

                drawFont             = new System.Drawing.Font(StyleInfo.GetFontFamily(si.FontFamilyFull), si.FontSize, fs);
                drawFormat           = new StringFormat();
                drawFormat.Alignment = StringAlignment.Near;

                // Measure string
                //  pt.NoClip indicates that this was generated by PageTextHtml Build.  It has already word wrapped.
                if (pt.NoClip || pt.SI.WritingMode == WritingModeEnum.tb_rl)    // TODO: support multiple lines for vertical text
                {
                    ms       = MeasureString(s, g, drawFont, drawFormat);
                    width    = new float[1];
                    width[0] = Measurement.PointsFromPixels(ms.Width, g.DpiX);  // convert to points from pixels
                    sa       = new string[1];
                    sa[0]    = s;
                    return(sa);
                }

                // handle multiple lines;
                //  1) split the string into the forced line breaks (ie "\n and \r")
                //  2) foreach of the forced line breaks; break these into words and recombine
                s = s.Replace("\r\n", "\n");    // don't want this to result in double lines
                string[]      flines     = s.Split(lineBreak);
                List <string> lines      = new List <string>();
                List <float>  lineWidths = new List <float>();
                // remove the size reserved for left and right padding
                float ptWidth = pt.W - pt.SI.PaddingLeft - pt.SI.PaddingRight;
                if (ptWidth <= 0)
                {
                    ptWidth = 1;
                }
                foreach (string tfl in flines)
                {
                    string fl;
                    if (tfl.Length > 0 && tfl[tfl.Length - 1] == ' ')
                    {
                        fl = tfl.TrimEnd(' ');
                    }
                    else
                    {
                        fl = tfl;
                    }

                    // Check if entire string fits into a line
                    ms = MeasureString(fl, g, drawFont, drawFormat);
                    float tw = Measurement.PointsFromPixels(ms.Width, g.DpiX);
                    if (tw <= ptWidth)
                    {                       // line fits don't need to break it down further
                        lines.Add(fl);
                        lineWidths.Add(tw);
                        continue;
                    }

                    // Line too long; need to break into multiple lines
                    // 1) break line into parts; then build up again keeping track of word positions
                    string[]         parts = fl.Split(wordBreak); // this is the maximum split of lines
                    StringBuilder    sb    = new StringBuilder(fl.Length);
                    CharacterRange[] cra   = new CharacterRange[parts.Length];
                    for (int i = 0; i < parts.Length; i++)
                    {
                        int sc = sb.Length;        // starting character
                        sb.Append(parts[i]);       // endding character
                        if (i != parts.Length - 1) // last item doesn't need blank
                        {
                            sb.Append(" ");
                        }
                        int            ec = sb.Length;
                        CharacterRange cr = new CharacterRange(sc, ec - sc);
                        cra[i] = cr;            // add to character array
                    }

                    // 2) Measure the word locations within the line
                    string            wfl           = sb.ToString();
                    WordStartFinish[] wordLocations = MeasureString(wfl, g, drawFont, drawFormat, cra);
                    if (wordLocations == null)
                    {
                        continue;
                    }

                    // 3) Loop thru creating new lines as needed
                    int            startLoc = 0;
                    CharacterRange crs      = cra[startLoc];
                    CharacterRange cre      = cra[startLoc];
                    float          cwidth   = wordLocations[0].end;   // length of the first
                    float          bwidth   = wordLocations[0].start; // characters need a little extra on start
                    string         ts;
                    bool           bLine = true;
                    for (int i = 1; i < cra.Length; i++)
                    {
                        cwidth = wordLocations[i].end - wordLocations[startLoc].start + bwidth;
                        if (cwidth > ptWidth)
                        {       // time for a new line
                            cre = cra[i - 1];
                            ts  = wfl.Substring(crs.First, cre.First + cre.Length - crs.First);
                            lines.Add(ts);
                            lineWidths.Add(wordLocations[i - 1].end - wordLocations[startLoc].start + bwidth);

                            // Find the first non-blank character of the next line
                            while (i < cra.Length &&
                                   cra[i].Length == 1 &&
                                   fl[cra[i].First] == ' ')
                            {
                                i++;
                            }
                            if (i < cra.Length) // any lines left?
                            {                   // yes, continue on
                                startLoc = i;
                                crs      = cre = cra[startLoc];
                                cwidth   = wordLocations[i].end - wordLocations[startLoc].start + bwidth;
                            }
                            else  // no, we can stop
                            {
                                bLine = false;
                            }
                            //  bwidth = wordLocations[startLoc].start - wordLocations[startLoc - 1].end;
                        }
                        else
                        {
                            cre = cra[i];
                        }
                    }
                    if (bLine)
                    {
                        ts = fl.Substring(crs.First, cre.First + cre.Length - crs.First);
                        lines.Add(ts);
                        lineWidths.Add(cwidth);
                    }
                }
                // create the final array from the Lists
                string[] la = lines.ToArray();
                width = lineWidths.ToArray();
                return(la);
            }
            finally
            {
                if (drawFont != null)
                {
                    drawFont.Dispose();
                }
                if (drawFormat != null)
                {
                    drawFont.Dispose();
                }
            }
        }