예제 #1
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();
        }
예제 #2
0
        public virtual void Texts(Xwt.Drawing.Context ctx, double x, double y)
        {
            ctx.Save ();

            ctx.Translate (x, y);

            ctx.SetColor (Colors.Black);

            var col1 = new Rectangle ();
            var col2 = new Rectangle ();

            var text = new TextLayout ();
            text.Font = this.Font.WithSize (24);
            Console.WriteLine (text.Font.Size);

            // first text
            text.Text = "Lorem ipsum dolor sit amet,";
            var size1 = text.GetSize ();
            col1.Width = size1.Width;
            col1.Height += size1.Height + 10;
            ctx.DrawTextLayout (text, 0, 0);

            // proofing width; test should align with text above
            ctx.SetColor (Colors.DarkMagenta);
            text.Text = "consetetur sadipscing elitr, sed diam nonumy";
            text.Width = col1.Width;
            var size2 = text.GetSize ();

            ctx.DrawTextLayout (text, 0, col1.Bottom);
            col1.Height += size2.Height + 10;

            ctx.SetColor (Colors.Black);

            // proofing scale, on second col
            ctx.Save ();
            ctx.SetColor (Colors.Red);
            col2.Left = col1.Right + 10;

            text.Text = "eirmod tempor invidunt ut.";

            var scale = 1.2;
            text.Width = text.Width / scale;
            var size3 = text.GetSize ();
            col2.Height = size3.Height * scale;
            col2.Width = size3.Width * scale + 5;
            ctx.Scale (scale, scale);
            ctx.DrawTextLayout (text, col2.Left / scale, col2.Top / scale);
            ctx.Restore ();

            // proofing heigth, on second col
            ctx.Save ();
            ctx.SetColor (Colors.DarkCyan);
            text.Text = "Praesent ac lacus nec dolor pulvinar feugiat a id elit.";
            var size4 = text.GetSize ();
            text.Height = size4.Height / 2;
            text.Trimming=TextTrimming.WordElipsis;
            ctx.DrawTextLayout (text, col2.Left, col2.Bottom + 5);

            ctx.SetLineWidth (1);
            ctx.SetColor (Colors.Blue);
            ctx.Rectangle (new Rectangle (col2.Left, col2.Bottom + 5, text.Width, text.Height));
            ctx.Stroke();
            ctx.Restore ();

            // drawing col line
            ctx.SetLineWidth (1);

            ctx.SetColor (Colors.Black.WithAlpha (.5));
            ctx.MoveTo (col1.Right + 5, col1.Top);
            ctx.LineTo (col1.Right + 5, col1.Bottom);
            ctx.Stroke ();
            ctx.MoveTo (col2.Right + 5, col2.Top);
            ctx.LineTo (col2.Right + 5, col2.Bottom);
            ctx.Stroke ();
            ctx.SetColor (Colors.Black);

            // proofing rotate, and printing size to see the values
            ctx.Save ();

            text.Font = this.Font.WithSize (10);
            text.Text = string.Format ("Size 1 {0}\r\nSize 2 {1}\r\nSize 3 {2} Scale {3}",
                                       size1, size2, size3, scale);
            text.Width = -1; // this clears textsize
            text.Height = -1;
            ctx.Rotate (5);
            // maybe someone knows a formula with angle and textsize to calculyte ty
            var ty = 30;
            ctx.DrawTextLayout (text, ty, col1.Bottom + 10);

            ctx.Restore ();

            // scale example here:

            ctx.Restore ();

            TextLayout tl0 = new TextLayout (this);

            tl0.Font = this.Font.WithSize (10);
            tl0.Text = "This text contains attributes.";
            tl0.SetUnderline ( 0, "This".Length);
            tl0.SetForeground (new Color (0, 1.0, 1.0), "This ".Length, "text".Length);
            tl0.SetBackground (new Color (0, 0, 0), "This ".Length, "text".Length);
            tl0.SetFontWeight (FontWeight.Bold, "This text ".Length, "contains".Length);
            tl0.SetFontStyle (FontStyle.Italic, "This text ".Length, "contains".Length);
            tl0.SetStrikethrough ("This text contains ".Length, "attributes".Length);

            ctx.SetColor(Colors.DarkGreen);
            ctx.DrawTextLayout (tl0, col2.Left, col2.Bottom + 100);

            // Text boces

            y = 180;

            // Without wrapping

            TextLayout tl = new TextLayout (this);
            tl.Text = "Stright text";
            DrawText (ctx, tl, ref y);

            // With wrapping

            tl = new TextLayout (this);
            tl.Text = "The quick brown fox jumps over the lazy dog";
            tl.Width = 100;
            DrawText (ctx, tl, ref y);

            // With blank lines

            tl = new TextLayout (this);
            tl.Text = "\nEmpty line above\nLine break above\n\nEmpty line above\n\n\nTwo empty lines above\nEmpty line below\n";
            tl.Width = 200;
            DrawText (ctx, tl, ref y);
        }
예제 #3
0
 void DrawGradientButton(Xwt.Drawing.Context G, GradientButton B)
 {
     DrawingPath P = new DrawingPath();
     P.Rectangle(new Xwt.Rectangle(B.Position, B.Size));
     LinearGradient gr;
     G.AppendPath(P);
     Pattern pat = gr = new LinearGradient(B.Position.X, B.Position.Y, B.Position.X + B.Size.Width, B.Position.Y + B.Size.Height);
     gr.AddColorStop(0, B.Color.BlendWith(Colors.White, 0.8));
     gr.AddColorStop(0.5, B.Color);
     gr.AddColorStop(1, B.Color.BlendWith(Colors.White, 0.8));
     G.Pattern = pat;
     G.Fill();
     G.AppendPath(P);
     G.SetColor(Xwt.Drawing.Colors.Black);
     G.SetLineWidth(1);
     G.Stroke();
     TextLayout L = new TextLayout()
     {
         Font = B.Font,
         Text = B.Text
     };
     Size TextSize = new Size(0.6 * L.Font.Size * L.Text.Count(), L.Font.Size);
     G.DrawTextLayout(L, new Xwt.Point(B.Position.X + B.Size.Width / 2 - TextSize.Width / 2, B.Position.Y + B.Size.Height / 2 - TextSize.Height / 2));
 }
예제 #4
0
        protected override void OnDraw(Xwt.Drawing.Context ctx, Rectangle dirtyRect)
        {
            var w = System.Diagnostics.Stopwatch.StartNew();
            base.OnDraw(ctx, dirtyRect);

            // char size
            var charSize = new TextLayout(this) { Text = "X", Font = font }.GetSize();
            double charWidth = CharWidth = charSize.Width;
            double charHeight = CharHeight = charSize.Height;

            // scroll bars
            updateScrollBars();

            // calculations
            int start = Math.Max(0, (int)(Source.ViewOffset.Y / charHeight));
            int end = Math.Min(Source.Count, (int)((Source.ViewOffset.Y + Size.Height) / charHeight));

            double lineNrWidth = ((int)Math.Log10(Source.Count) + 2) * CharWidth;
            TextXOffset = lineNrWidth - Source.ViewOffset.X;

            // selection
            Range selection = Source.Selection;
            bool hasSelection = !selection.IsEmpty;
            bool isRectSelection = selection.Type == SelectionType.Rectangular;

            // text
            for (int i = start; i < end; i++)
            {
                Line line = Source[i];
                string text = line.GetString();

                if (hasSelection)
                {
                    if (i >= selection.Start.Y && i <= selection.End.Y)
                    {
                        ctx.SetColor(Colors.LightSkyBlue);
                        if (isRectSelection)
                        {
                            ctx.Rectangle(selection.Start.X * charWidth + lineNrWidth - Source.ViewOffset.X,
                                (i * charHeight) - Source.ViewOffset.Y,
                                Math.Abs(selection.End.X - selection.Start.X) * charWidth,
                                charHeight);
                            ctx.Fill();
                        }
                        else
                        {
                            if (selection.Start.Y == selection.End.Y)
                            {
                                ctx.Rectangle(selection.Start.X * charWidth + TextXOffset, (i * charHeight) - Source.ViewOffset.Y, (selection.End.X - selection.Start.X) * charWidth + 2, charHeight);
                            }
                            else
                            {
                                if (i == selection.Start.Y)
                                    ctx.Rectangle(selection.Start.X * charWidth + TextXOffset, (i * charHeight) - Source.ViewOffset.Y, (Source[i].Count - selection.Start.X) * charWidth + 2, charHeight);
                                else if (i == selection.End.Y)
                                    ctx.Rectangle(TextXOffset, (i * charHeight) - Source.ViewOffset.Y, selection.End.X * charWidth + 2, charHeight);
                                else
                                    ctx.Rectangle(TextXOffset, (i * charHeight) - Source.ViewOffset.Y, Source[i].Count * charWidth + 2, charHeight);
                                ctx.Fill();
                            }
                        }
                    }
                }

                //ctx.SetColor(Colors.Black);
                //var layout = new TextLayout(this)
                //{
                //    Text = text,
                //    Font = font,
                //};
                //ctx.DrawTextLayout(layout, -Source.ViewOffset.X + lineNrWidth, (i * charHeight) - Source.ViewOffset.Y);
            }

            for (int ic = 0; ic < colors.Length; ic++)
            {
                ctx.SetColor(colors[ic]);
                CharpStyle style = (CharpStyle)ic;
                StringBuilder builder = new StringBuilder(1024);
                bool draw = false;

                for (int y = start; y < end; y++)
                {
                    Line line = Source[y];
                    for (int x = 0; x < line.Count; x++)
                    {
                        char c = line[x].Char;
                        if (c != ' ' && (line[x].Style & CharpStyle.ColorAll) == style)
                        {
                            if (c == '\t')
                                builder.Append(' ');
                            else
                            {
                                builder.Append(line[x].Char);
                                draw = true;
                            }
                        }
                        else
                            builder.Append(' ');
                    }
                    builder.AppendLine(" ");
                }

                TextLayout layout = new TextLayout(this)
                {
                    Text = builder.ToString(),
                    Font = font,
                };
                if (draw)
                    ctx.DrawTextLayout(layout, TextXOffset, 0);
            }

            // line left
            ctx.SetColor(Colors.LightGray);
            ctx.Rectangle(0, 0, lineNrWidth, Size.Height);
            ctx.Fill();

            // line numbers, markers
            for (int i = start; i < end; i++)
            {
                ctx.SetColor(Colors.Black);
                ctx.DrawTextLayout(new TextLayout(this) { Text = (i + 1).ToString(), Font = font }, 0, (i * charHeight) - Source.ViewOffset.Y);
                if ((Source[i].LineStyle & LineStyle.Unsaved) == LineStyle.Unsaved)
                {
                    ctx.SetColor(Colors.Red);
                    ctx.Rectangle(TextXOffset - charWidth / 2 - 1, (i * charHeight) - Source.ViewOffset.Y, 2, charHeight);
                    ctx.Fill();
                }
            }

            // caret
            Loc caret = Source.GetSanitizedLoc(Source.CaretPosition);
            ctx.Rectangle((int)(charWidth * caret.X - Source.ViewOffset.X + lineNrWidth), (int)(charHeight * caret.Y - Source.ViewOffset.Y), 2, charHeight);
            ctx.SetColor(Colors.Red);
            ctx.Fill();

            // stopwatch
            w.Stop();
            //Console.WriteLine("Drawing: " + w.Elapsed.TotalMilliseconds);
        }