Package text

import ""

Overview ▾

Package text provides text rendering helper functions and an abstraction to create input text windows.

The DrawText rendering function included takes a window, font information and text and creates an image with the text written on it. The image is then painted to the window provided, and the window is resized to "snugly" fit the text. (Note that DrawText has a subtle bug that will manifest itself with large font sizes. Please see the bugs section.)

The other useful part of this package is the Input window type. It is an abstraction over xwindow.Window that provides an input box like window. The Input type's methods can then be used to easily add and remove text from the input box in response to KeyPress events. (You must write the KeyPress event handler.)

Here's a minimal example for creating an input window and allowing the user to type into it:

input := text.NewInput(XUtilValue, RootId, 500, 0, font, 20.0,
	textColor, bgColor)
	func(X *xgbutil.XUtil, ev xevent.KeyPressEvent) {
		if keybind.KeyMatch(X, "BackSpace", ev.State, ev.Detail) {
		} else {
			input.Add(ev.State, ev.Detail)
	}).Connect(X, input.Id)

Since the Input type embeds an xwindow.Window, it can be thought of as a regular window with special methods for handling text display.

Note that a slightly more involved and working example can be found in text/examples/input/main.go.

func DrawText

func DrawText(win *xwindow.Window, font *truetype.Font, size float64,
    fontClr, bgClr render.Color, text string) error

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.

type Input

type Input struct {
    Text []rune
    // contains filtered or unexported fields

Input encapsulates the information needed to construct and maintain an input window. The only exposed information is the Text field, in case you need to inspect it. Input values should *only* be made with the NewInput constructor.

func NewInput

func NewInput(X *xgbutil.XUtil, parent xproto.Window, width int, padding int,
    font *truetype.Font, fontSize float64,
    fontColor, bgColor render.Color) *Input

NewInput constructs Input values. It needs an X connection, a parent window, the width of the input box, and theme information related for the font and background. Padding separating the text and the edges of the window may also be specified.

While NewInput returns an *Input, a Input value also has an xwindow.Window value embedded into it. Thus, an Input can also be treated as a normal window on which you can assign callbacks, close, destroy, etc.

As with all windows, the Input window should be destroyed when it is no longer in used.

func (*Input) Add

func (ti *Input) Add(mods uint16, kc xproto.Keycode)

Add will convert a (modifiers, keycode) tuple taken directly from a Key{Press,Release}Event to a single character string. Note that sometimes this conversion will fail. When it fails, a message will be logged and no text will be added.

Note that sometimes the conversion should fail (like when the Shift key is pressed), and other times it will fail because the xgbutil/keybind package provides inadequate support for keyboard encodings.

I suspect that languages other than English will completely fail here.

If a work-around is desperately needed, use AddLetter.

func (*Input) AddLetter

func (ti *Input) AddLetter(char rune)

AddLetter will add a single character to the input and re-render the input box. Note that the Add method is quite convenient and should be used when reading Key{Press,Release} events. If input is coming from somewhere else, or if Add is not working, resort to AddLetter.

func (*Input) Remove

func (ti *Input) Remove()

Remove will remove the last character in the input box and re-render. If there are no characters in the input, Remove has no effect.

func (*Input) Render

func (ti *Input) Render()

Render will redraw the background image, and write whatever text is in (*Input).Text to the image. No clean-up by the caller is necessary.

Render probably should not be called, unless you are manipulating (*Input).Text manually. Otherwise, it is preferrable to use the Add, Remove and Reset methods.

func (*Input) Reset

func (ti *Input) Reset()

Reset will clear the entire input box and re-render.

func (*Input) SetString

func (ti *Input) SetString(s string)

SetString will clear the input box and set it to the string provided.



Name      Synopsis
     input      Example input shows how to create a window that reads text typed by the user and displays it.