// DrawText is a convenience function that will create a new image, render // the provided text to it, paint the image to the provided window, and resize // the window to fit the text snugly. // // An error can occur when rendering the text to an image. func DrawText(win *xwindow.Window, font *truetype.Font, size float64, fontClr, bgClr render.Color, text string) error { // BUG(burntsushi): If `text` is zero-length, very bad things happen. if len(text) == 0 { text = " " } // Over estimate the extents. ew, eh := xgraphics.Extents(font, size, text) // Create an image using the over estimated extents. img := xgraphics.New(win.X, image.Rect(0, 0, ew, eh)) xgraphics.BlendBgColor(img, bgClr.ImageColor()) // Now draw the text, grab the (x, y) position advanced by the text, and // check for an error in rendering. _, _, err := img.Text(0, 0, fontClr.ImageColor(), size, font, text) if err != nil { return err } // Resize the window to the geometry determined by (x, y). win.Resize(ew, eh) // Now draw the image to the window and destroy it. img.XSurfaceSet(win.Id) // subimg := img.SubImage(image.Rect(0, 0, ew, eh)) img.XDraw() img.XPaint(win.Id) img.Destroy() return nil }
// DrawText is a convenience function that will create a new image, render // the provided text to it, paint the image to the provided window, and resize // the window to fit the text snugly. // // An error can occur when rendering the text to an image. func DrawText(win *xwindow.Window, font *truetype.Font, size float64, fontClr, bgClr color.RGBA, text string) error { // Over estimate the extents. ew, eh := xgraphics.TextMaxExtents(font, size, text) eh += misc.TextBreathe // <-- this is the bug // Create an image using the over estimated extents. img := xgraphics.New(win.X, image.Rect(0, 0, ew, eh)) xgraphics.BlendBgColor(img, bgClr) // Now draw the text, grab the (x, y) position advanced by the text, and // check for an error in rendering. x, y, err := img.Text(0, 0, fontClr, size, font, text) if err != nil { return err } // Resize the window to the geometry determined by (x, y). w, h := x, y+misc.TextBreathe // <-- also part of the bug win.Resize(w, h) // Now draw the image to the window and destroy it. img.XSurfaceSet(win.Id) subimg := img.SubImage(image.Rect(0, 0, w, h)) subimg.XDraw() subimg.XPaint(win.Id) img.Destroy() return nil }
// UpdateImage will repaint the active and inactive images by calling // CycleChoice.CycleImage. This is not called when the cycle prompt is shown; // rather the burden is on the user to make sure the prompt has the most up // to date image. func (ci *CycleItem) UpdateImage() { active, inactive := ci.choice.CycleImage(), ci.choice.CycleImage() xgraphics.Alpha(inactive, ci.cycle.theme.IconTransparency) xgraphics.BlendBgColor(active, ci.cycle.theme.BgColor.ImageColor()) xgraphics.BlendBgColor(inactive, ci.cycle.theme.BgColor.ImageColor()) active.XSurfaceSet(ci.active.Id) active.XDraw() active.XPaint(ci.active.Id) active.Destroy() inactive.XSurfaceSet(ci.inactive.Id) inactive.XDraw() inactive.XPaint(ci.inactive.Id) inactive.Destroy() }
func main() { X, err := xgbutil.NewConn() if err != nil { log.Fatal(err) } // Get the list of window ids managed by the window manager. clients, err := ewmh.ClientListGet(X) if err != nil { log.Fatal(err) } // For each client, try to find its icon. If we find one, blend it with // a nice background color and show it in its own window. // Otherwise, skip it. for _, wid := range clients { // FindIcon will find an icon closest to the size specified. // If one can't be found, the resulting image will be scaled // automatically. // To avoid scaling the icon, specify '0' for both the width and height. // In this case, the largest icon found will be returned. xicon, err := xgraphics.FindIcon(X, wid, iconWidth, iconHeight) if err != nil { log.Printf("Could not find icon for window %d.", wid) continue } // Get the name of this client. (It will be set as the icon window's // name.) name, err := ewmh.WmNameGet(X, wid) if err != nil { // not a fatal error log.Println(err) name = "" } // Blend a pink background color so its easy to see that alpha blending // works. xgraphics.BlendBgColor(xicon, color.RGBA{0xff, 0x0, 0xff, 0xff}) xicon.XShowExtra(name, false) } // All we really need to do is block, so a 'select{}' would be sufficient. // But running the event loop will emit errors if anything went wrong. xevent.Main(X) }