https://wiki.inspired-lua.org/index.php?title=platform.withGC&feed=atom&action=historyplatform.withGC - Revision history2024-03-29T12:31:15ZRevision history for this page on the wikiMediaWiki 1.33.0https://wiki.inspired-lua.org/index.php?title=platform.withGC&diff=726&oldid=prevAdriweb: Created page with "platform.'''withGC''' is a function that is part of the platform library. Executes ''function(args)'' within a dummy graphics context and returns all retu..."2012-06-06T20:53:19Z<p>Created page with "platform.'''withGC''' is a function that is part of the <a href="/Category:platform" title="Category:platform">platform</a> library. Executes ''function(args)'' within a dummy graphics context and returns all retu..."</p>
<p><b>New page</b></p><div>platform.'''withGC''' is a function that is part of the [[:Category:platform|platform]] library.<br />
<br />
Executes ''function(args)'' within a dummy graphics context and returns all return values from ''function()''. <br /><br />
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. <br /><br />
This graphics context should not be used to draw graphics since it is not associated with a window. <br />
<br /><br />
<br />
{{Since|3.2}}<br />
<br />
== Syntax ==<br />
platform.'''withGC'''(func, funcArgs...) <br />
<br />
{| class="wikitable"<br />
|-<br />
! Parameter !! Type !! Description<br />
|-<br />
| <u><center>func</center></u> || function || name of function to call<br />
|-<br />
| <u><center>funcArgs</center></u> || (any) || The arguments of the function ''func''<br />
|-<br />
|}<br />
<br />
== Example ==<br />
Here is an example of using withGC() to get the pixel length and height of a string :<br />
<syntaxhighlight>function setFont(family, style, size, gc)<br />
return gc:setFont(family, style, size) -- Sets the font to use<br />
end<br />
function getHeightWidth(str, gc)<br />
width = gc:getStringWidth(str) -- Gets the pixel length of str<br />
height = gc:getStringHeight(str) -- Gets the pixel height of str<br />
return height, width<br />
end<br />
platform.withGC(setFont, 'serif', 'b', 12)<br />
height, width = platform.withGC(getHeightWidth, 'Hello World')</syntaxhighlight><br />
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. <br />
<br />
<br />
== See also ==<br />
*[[on.paint]](gc)<br />
<br />
<br />
[[Category:platform]]</div>Adriweb