Difference between revisions of "platform.gc"
(Created page with "Returns a dummy graphics context.<br /> It is typically used to measure pixel lengths and heights of strings when a normal graphics context is not available.<br...") |
|||
Line 6: | Line 6: | ||
This graphics context cannot be used to draw graphics since it is not tied to a window. It can however do that if this gc is used in a function called within [[on.paint]]. | This graphics context cannot be used to draw graphics since it is not tied to a window. It can however do that if this gc is used in a function called within [[on.paint]]. | ||
Here is an example of using the dummy graphics context to get the pixel length and height of a string. | Here is an example of using the dummy graphics context to get the pixel length and height of a string. | ||
− | <syntaxhighlight>local gc = platform.gc() | + | <syntaxhighlight>local gc = platform.gc() -- Get the dummy graphics context |
− | gc:begin() | + | gc:begin() -- Makes it the current graphics context |
− | local width = gc:getStringWidth(a_string) -- Gets the pixel length of a_string | + | local width = gc:getStringWidth(a_string) -- Gets the pixel length of a_string |
− | local height = gc:getStringHeight(a_string) | + | local height = gc:getStringHeight(a_string) -- Gets the pixel height of a_string |
− | |||
-- do things here -- | -- do things here -- | ||
− | gc:finish() | + | gc:finish() -- Restores previous graphics context</syntaxhighlight> |
It is important to use [[gc:begin]]() to set up the graphics context before using it in the getString routines and to call [[gc:finish]]() to relinquish it when finished with it. | It is important to use [[gc:begin]]() to set up the graphics context before using it in the getString routines and to call [[gc:finish]]() to relinquish it when finished with it. | ||
+ | |||
+ | |||
{{Since|3.0}} | {{Since|3.0}} | ||
− | [[Category: | + | [[Category:platform]] |
Revision as of 00:03, 25 May 2011
Returns a dummy graphics context.
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 cannot be used to draw graphics since it is not tied to a window. It can however do that if this gc is used in a function called within on.paint.
Here is an example of using the dummy graphics context to get the pixel length and height of a string.
local gc = platform.gc() -- Get the dummy graphics context
gc:begin() -- Makes it the current graphics context
local width = gc:getStringWidth(a_string) -- Gets the pixel length of a_string
local height = gc:getStringHeight(a_string) -- Gets the pixel height of a_string
-- do things here --
gc:finish() -- Restores previous graphics context
It is important to use gc:begin() to set up the graphics context before using it in the getString routines and to call gc:finish() to relinquish it when finished with it.
This has been introduced in TI-Nspire OS 3.0 (Changes).