public TextLayout( TextServiceDirectWrite service, string line, Graphics _graphics, Font _font) { try { int hr; text = line; lineInterop = new TextServiceLineDirectWriteInterop(); hr = lineInterop.Init(service.interop, line, line.Length); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } #if true uniscribeLine = service.uniscribeService.AnalyzeText(_graphics, _font, _font.Height, line); #else // TODO: support Windows.Data.Text for universal script segmentation support #endif } catch (Exception) { Dispose(); throw; } }
public static void DrawText(Graphics graphics, string text, Font font, Rectangle bounds, Color foreColor, Color backColor, TextFormatFlags flags) { TextServiceDirectWriteInterop interop = EnsureInterop(font); TextServiceLineDirectWriteInterop localLineInterop = lineInterop; lineInterop = null; try { if (localLineInterop == null) { localLineInterop = new TextServiceLineDirectWriteInterop(); } int hr = localLineInterop.Init(interop, text, text.Length); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } if ((flags & TextFormatFlags.HorizontalCenter) != 0) { Size size; hr = localLineInterop.GetExtent(graphics, out size); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } bounds.X += (bounds.Width - size.Width) / 2; bounds.Width += size.Width / 2; } localLineInterop.DrawTextWithRenderTarget(graphics, bounds, foreColor, backColor); } finally { if (localLineInterop != null) // always clear contained references { localLineInterop.Dispose(); } if (lineInterop != null) { lineInterop.Dispose(); } lineInterop = localLineInterop; } }
//public static Size MeasureText(string text, Font font); public static Size MeasureText(Graphics graphics, string text, Font font) { TextServiceDirectWriteInterop interop = EnsureInterop(font); TextServiceLineDirectWriteInterop localLineInterop = lineInterop; lineInterop = null; try { if (localLineInterop == null) { localLineInterop = new TextServiceLineDirectWriteInterop(); } int hr = localLineInterop.Init(interop, text, text.Length); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } Size extent; hr = localLineInterop.GetExtent(graphics, out extent); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } return extent; } finally { if (localLineInterop != null) // always clear contained references { localLineInterop.Dispose(); } if (lineInterop != null) { lineInterop.Dispose(); } lineInterop = localLineInterop; } }