Tk\_AllocFontFromObj(3) Tk Library Procedures Tk\_AllocFontFromObj(3)

```

```

# [NAME](#NAME)

Tk\_AllocFontFromObj, Tk\_GetFont, Tk\_GetFontFromObj, Tk\_NameOfFont, Tk\_FontGetDescription, Tk\_FreeFontFromObj, Tk\_FreeFont - maintain database of fonts

# [SYNOPSIS](#SYNOPSIS)

```
#include <tk.h>

Tk_Font
Tk_AllocFontFromObj(interp, tkwin, objPtr)

Tk_Font
Tk_GetFont(interp, tkwin, string)

Tk_Font
Tk_GetFontFromObj(tkwin, objPtr)

Tcl_Obj *
Tk_FontGetDescription(tkfont)

const char *
Tk_NameOfFont(tkfont)

Tk_Font
Tk_FreeFontFromObj(tkwin, objPtr)

Tk_FreeFont(tkfont)
```

# [ARGUMENTS](#ARGUMENTS)

Tcl\_Interp *\*interp* (in)

Interpreter to use for error reporting. If **NULL**, then no error messages are left after errors.

Tk\_Window *tkwin* (in)

Token for window in which font will be used.

Tcl\_Obj *\*objPtr* (in/out)

Gives name or description of font. See documentation for the **font** command for details on acceptable formats. Internal rep will be modified to cache corresponding Tk\_Font.

const char *\*string* (in)

Same as *objPtr* except description of font is passed as a string and resulting Tk\_Font is not cached.

Tk\_Font *tkfont* (in)

Opaque font token.

```
    
```

# [DESCRIPTION](#DESCRIPTION)

**Tk\_AllocFontFromObj** finds the font indicated by *objPtr* and returns a token that represents the font. The return value can be used in subsequent calls to procedures such as **Tk\_GetFontMetrics**, **Tk\_MeasureChars**, and **Tk\_FreeFont**. The Tk\_Font token will remain valid until **Tk\_FreeFontFromObj** or **Tk\_FreeFont** is called to release it. *ObjPtr* can contain either a symbolic name or a font description; see the documentation for the **font** command for a description of the valid formats. If **Tk\_AllocFontFromObj** is unsuccessful (because, for example, *objPtr* did not contain a valid font specification) then it returns **NULL** and leaves an error message in *interp*'s result if *interp* is not **NULL**. **Tk\_AllocFontFromObj** caches information about the return value in *objPtr*, which speeds up future calls to procedures such as **Tk\_AllocFontFromObj** and **Tk\_GetFontFromObj**.

**Tk\_GetFont** is identical to **Tk\_AllocFontFromObj** except that the description of the font is specified with a string instead of an object. This prevents **Tk\_GetFont** from caching the matching Tk\_Font, so **Tk\_GetFont** is less efficient than **Tk\_AllocFontFromObj**.

**Tk\_GetFontFromObj** returns the token for an existing font, given the window and description used to create the font. **Tk\_GetFontFromObj** does not actually create the font; the font must already have been created with a previous call to **Tk\_AllocFontFromObj** or **Tk\_GetFont**. The return value is cached in *objPtr*, which speeds up future calls to **Tk\_GetFontFromObj** with the same *objPtr* and *tkwin*.

**Tk\_AllocFontFromObj** and **Tk\_GetFont** maintain a database of all fonts they have allocated. If the same font is requested multiple times (e.g. by different windows or for different purposes), then a single Tk\_Font will be shared for all uses. The underlying resources will be freed automatically when no-one is using the font anymore.

The procedure **Tk\_FontGetDescription** returns information about the font description as a Tcl list. One possible result is “{{DejaVu Sans} -16 bold underline}”.

The procedure **Tk\_NameOfFont** is roughly the inverse of **Tk\_GetFont**. Given a *tkfont* that was created by **Tk\_GetFont** (or **Tk\_AllocFontFromObj**), the return value is the *string* argument that was passed to **Tk\_GetFont** to create the font. The string returned by **Tk\_NameOfFont** is only guaranteed to persist until the *tkfont* is deleted. The caller must not modify this string.

When a font is no longer needed, **Tk\_FreeFontFromObj** or **Tk\_FreeFont** should be called to release it. For **Tk\_FreeFontFromObj** the font to release is specified with the same information used to create it; for **Tk\_FreeFont** the font to release is specified with its Tk\_Font token. There should be exactly one call to **Tk\_FreeFontFromObj** or **Tk\_FreeFont** for each call to **Tk\_AllocFontFromObj** or **Tk\_GetFont**.

# [SEE ALSO](#SEE_ALSO)

[Tk\_FontId(3)](/man/os/tk/Tk_FontId.3)

# [KEYWORDS](#KEYWORDS)

font

8.1 Tk

---

Document: Tk_FontGetDescription(3) from tk 9.1.0-r0 (Chainguard OS)
Source: https://downloads.sourceforge.net/sourceforge/tcl/tk9.1.0-src.tar.gz
