public Size MeasureString(FontConfig font, string text) { XftFontExt fontExt = objectCache.GetXftFont(font); byte[] utf32Text = Encoding.UTF32.GetBytes(text); int width = 0; GCHandle utf32TextHandle = GCHandle.Alloc(utf32Text, GCHandleType.Pinned); try { IntPtr utf32TextPtr = utf32TextHandle.AddrOfPinnedObject(); foreach (var range in fontExt.GetRanges(utf32Text)) { LibXft.XftTextExtents32( display, range.Font, utf32TextPtr + range.Start, (range.End - range.Start) / 4, out var extents ); width += extents.xOff; } } finally { utf32TextHandle.Free(); } var fontInfo = Marshal.PtrToStructure <XftFont>(fontExt.MainFont); return(new Size(width, fontInfo.height)); }
public void DrawString(Color color, FontConfig font, int x, int y, string text) { x -= origin.X; y -= origin.Y; var xftColorPtr = Marshal.AllocHGlobal(Marshal.SizeOf <XftColor>()); try { XRenderColor xColor = new XRenderColor(color); LibXft.XftColorAllocValue( display, visual, colormap, ref xColor, xftColorPtr ); try { XftFontExt fontExt = objectCache.GetXftFont(font); var fontInfo = Marshal.PtrToStructure <XftFont>(fontExt.MainFont); int xOffset = DrawString(xftDraw, xftColorPtr, fontExt, x, y + fontInfo.ascent, text); if (font.IsUnderline) { int lineHeight = Convert.ToInt32(Math.Max(font.Size / 10, 1)); LibXRender.XRenderFillRectangle( display, PictOp.PictOpOver, pictureId, ref xColor, x, y + fontInfo.ascent + (fontInfo.descent - lineHeight) / 2, (uint)xOffset, (uint)lineHeight ); } if (font.IsStrikeout) { int lineHeight = Convert.ToInt32(Math.Max(font.Size / 20, 1)); LibXRender.XRenderFillRectangle ( display, PictOp.PictOpOver, pictureId, ref xColor, x, y + fontInfo.ascent - (2 * fontInfo.ascent + 3 * lineHeight) / 6, (uint)xOffset, (uint)lineHeight ); } } finally { LibXft.XftColorFree(display, visual, colormap, xftColorPtr); } } finally { Marshal.FreeHGlobal(xftColorPtr); } }
private int DrawString(IntPtr xftDraw, IntPtr xftColorPtr, XftFontExt fontExt, int x, int y, string text) { // todo: reuse buffer? byte[] utf32Text = Encoding.UTF32.GetBytes(text); GCHandle utf32TextHandle = GCHandle.Alloc(utf32Text, GCHandleType.Pinned); try { IntPtr utf32TextPtr = utf32TextHandle.AddrOfPinnedObject(); int xOffset = 0; foreach (var range in fontExt.GetRanges(utf32Text)) { xOffset += DrawStringRange(xftDraw, xftColorPtr, range.Font, x + xOffset, y, utf32TextPtr, range.Start, range.End); } return(xOffset); } finally { utf32TextHandle.Free(); } }
private XftFontExt CreateXftFont(FontConfig fontConfig) { return(XftFontExt.Create(fontConfig, display, screen)); }