Example #1
0
        private void Form1_Shown(object sender, EventArgs e)
        {
            Debug.WriteLine("Form1_Shown");

            Win32Surface = new Win32Surface(this.CreateGraphics().GetHdc());
            FontContext = new Context(Win32Surface);

            //CRITICAL: Format of Win32Surface and ImageSurface must be identical!

            ImageSurface = new ImageSurface(Format.Rgb24, ClientSize.Width, ClientSize.Height);
            BackContext = new Context(ImageSurface);

            //Clear Surface2
            BackContext.SetSourceColor(new Color(1,1,1));
            BackContext.Operator = Operator.Source;
            BackContext.Paint();
            BackContext.Operator = Operator.Over;

            var textFormat = DWriteCairo.CreateTextFormat(
                "Arial",
                FontWeight.Normal,
                FontStyle.Normal,
                FontStretch.Normal,
                32f);
            
            Debug.Assert(Math.Abs(textFormat.FontSize - 32f) < 0.0001);
            
            const string s = "Hello World";
            textLayout = DWriteCairo.CreateTextLayout(s, textFormat, 300, 40);

        }
 /// <summary>
 /// CreateTextLayout takes a string, format, and associated constraints
 /// and produces an object representing the fully analyzed
 /// and formatted result.
 /// </summary>
 /// <param name="text">The text to layout.</param>
 /// <param name="textFormat">The format to apply to the string.</param>
 /// <param name="maxWidth">Width of the layout box.</param>
 /// <param name="maxHeight">Height of the layout box.</param>
 /// <returns>
 /// The resultant object.
 /// </returns>
 public static TextLayout CreateTextLayout(string text, TextFormat textFormat, int maxWidth, int maxHeight)
 {
     var ptr = Factory.CreateTextLayout(text,
     text.Length, textFormat.Handle, maxWidth, maxHeight);
     var dWriteTextLayout = new TextLayout(ptr);
     return dWriteTextLayout;
 }
Example #3
0
        private void Form1_Shown(object sender, EventArgs e)
        {
            Debug.WriteLine("Form1_Shown");

            surface = new Win32Surface(this.CreateGraphics().GetHdc());
            context = new Context(surface);

            textFormat = DWriteCairo.CreateTextFormat(
                "Consolas",
                FontWeight.Normal,
                FontStyle.Normal,
                FontStretch.Normal,
                12);

            textFormat.TextAlignment = TextAlignment.Center;
            
            float left, top, width, height;

            // get actual size of the text
            var measureLayout = DWriteCairo.CreateTextLayout(s, textFormat, 4096, 4096);
            measureLayout.GetRect(out left, out top, out width, out height);
            measureLayout.Dispose();

            // build text context against the size and format
            textLayout = DWriteCairo.CreateTextLayout(s, textFormat, (int)Math.Ceiling(width), (int)Math.Ceiling(height));

            Debug.WriteLine("showing layout");
            Path path = DWriteCairo.RenderLayoutToCairoPath(context, textLayout);
            context.AppendPath(path);
            context.Fill();

            textLayout.GetRect(out left, out top, out width, out height);
            textRect = new System.Drawing.RectangleF(left, top, width, height);
            context.Rectangle(left, top, width, height);
            context.Stroke();

            context.GetTarget().Flush();
        }
 internal Path RenderToCairoPath(Context context, DirectWriteCairoTextRenderer render, TextLayout textLayout)
 {
     //Debug.WriteLine("Before `Draw`: Current point at <{0},{1}>", (float)context.CurrentPoint.X, (float)context.CurrentPoint.Y);
     Draw(context.Handle, render, (float)context.CurrentPoint.X, (float)context.CurrentPoint.Y);
     var result = context.CopyPath();
     context.NewPath();
     return result;
 }
 /// <summary>
 /// Show the layout
 /// </summary>
 /// <param name="context">a Cairo context</param>
 /// <param name="textLayout">the layout to show</param>
 public static Path RenderLayoutToCairoPath(Cairo.Context context, TextLayout textLayout)
 {
     return textLayout.RenderToCairoPath(context, Render, textLayout);
 }