/// <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;
 }
 /// <summary>
 /// Create a text format object used for text layout.
 /// </summary>
 /// <param name="fontFamilyName">Name of the font family</param>
 /// <param name="fontWeight">Font weight</param>
 /// <param name="fontStyle">Font style</param>
 /// <param name="fontStretch">Font stretch</param>
 /// <param name="fontSize">Logical size of the font in DIP units. A DIP ("device-independent pixel") equals 1/96 inch.</param>
 /// <param name="localeName">Locale name(optional)</param>
 /// TODO understand the meaning of Locale name
 /// <returns> newly created text format object </returns>
 public static TextFormat CreateTextFormat(
     string fontFamilyName,
     FontWeight fontWeight, FontStyle fontStyle, FontStretch fontStretch,
     float fontSize,
     string localeName = "en-us")
 {
     var ptr = Factory.CreateTextFormat(fontFamilyName, IntPtr.Zero,
         fontWeight,
         fontStyle, fontStretch, fontSize,
         localeName);
     var dWriteTextFormat = new TextFormat(ptr);
     return dWriteTextFormat;
 }
Exemple #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();
        }