You are here: Lua Scripting Resources eGuide > Lua Scripting API Reference Guide > Platform Library > withGC

14.8 withGC

platform.withGC(function, ...)

Executes function(... , gc) within a non-painting graphics context and returns all return values from function(). It is used to support layout procedures that measure the width and height of strings outside of the paint context. It is a good practice to separate the layout from the paint routine to enhance the performance of the script. A layout may happen during on.resize() and when data is changing based on user interaction or timer expiration. The script should not assume that any state, like a font size, is preserved from one call of platform.withGC to the next call of platform.withGC.

This graphics context cannot be used to draw.

Listing 14.1: Example of Using withGC() to get the Pixel Length and Height of a String

function getHeightWidth(str, gc) 
      gc:setFont('serif', 'b', 12) -- Set the font
      width = gc:getStringWidth(str) -- Pixel length of str
      height = gc:getStringHeight(str) -- Pixel height of str
      return height, width
end

height, width = platform.withGC(getHeightWidth, 'Hello World')

Introduced in platform.apiLevel = '2.0'