platform.withGC

From Inspired-Lua Wiki
Jump to: navigation, search

platform.withGC is a function that is part of the platform library.

Executes function(args) within a dummy graphics context and returns all return values from function().
It is typically used to measure pixel lengths and heights of strings when a normal graphics context is not available. This may be the case when creating new text elements when the script app is initialized. A graphics context is available only during paint events, and that may be too late to create and size the containers for text fields.
This graphics context should not be used to draw graphics since it is not associated with a window. 

This has been introduced in TI-Nspire OS 3.2 (Changes).


Syntax[edit]

platform.withGC(func, funcArgs...)

Parameter Type Description
func
function name of function to call
funcArgs
(any) The arguments of the function func

Example[edit]

Here is an example of using withGC() to get the pixel length and height of a string :

function setFont(family, style, size, gc)
    return gc:setFont(family, style, size) -- Sets the font to use
end
function getHeightWidth(str, gc)
    width = gc:getStringWidth(str) -- Gets the pixel length of str
    height = gc:getStringHeight(str) -- Gets the pixel height of str
    return height, width
end
platform.withGC(setFont, 'serif', 'b', 12)
height, width = platform.withGC(getHeightWidth, 'Hello World')

Note: You could combine the two functions above into a single one in order to avoid calling withGC() twice, but that is not required since the dummy graphics context remembers its state.


See also[edit]