TextCenteredBounds

>Psychtoolbox>PsychOneliners

bounds=TextCenteredBounds(window, string [, yPositionIsBaseline=0])

Returns the smallest enclosing rect for the drawn text, relative to the
current location. This bound is based on the actual pixels drawn, so it
incorporates effects of text smoothing, etc. All text is drawn on the
same baseline, horizontally centered by using the x offset
-Screen(w,’TextWidth’,string)/2. “text” may be a cell array or matrix of
1 or more strings. The strings are drawn one on top of another, all
horizontally centered at the current position, before the bounds are
calculated. This returns the smallest box that will contain all the
strings. The prior contents of the scratch window are lost. Usually it
should be an offscreen window, so the user won’t see it. If you only
know your nominal text size and number of characters, you might do this

w=Screen(‘OpenOffscreenWindow’,[],[],[0 0 1.5*textSize*length(string) 2*textSize]);

The suggested window size in that call is generously large because there
aren’t any guarantees from the font makers about how big the text might
be for a specified point size.

Be warned that TextBounds and TextCenteredBounds are slow (taking many
seconds) if the window is large. They use the whole window, so if the
window is 1024x1204 they process a million pixels. The two slowest calls
are Screen ‘GetImage’ and FIND. Their processing time is proportional to
the number of pixels in the window.

Also see TextBounds and Screen ‘TextWidth’ and ‘DrawText’.

Path   Retrieve current version from GitHub | View changelog
Psychtoolbox/PsychOneliners/TextCenteredBounds.m