﻿# Calculate Rectangular Text Metrics

_Reviewed and revised November 2018_

**Updated axcall MIAMEX, MX\_GDICALC, MXGDI\_CALCRECT, handle, status, lft, top, rght, btm, memo, height, overflow \{,flags\}**

This operation calculates how much text will fit in a specified rectangle, based on the current printer context and font.

| **Parameter** | **Type** | **I/O** | **Description** |
|------|------|------|------|
| handle | B,4 | in | Device context handle (from MXGDI\_OPEN) |
| status | F6 | out | [MX\_GDICALC Status Codes](mx_gdicalcstatuscodes.md). |
| lft,top,<br>rght,btm | Num | in/out | Coordinates of the rectangle, in the units specified by _mapmode_ when the printer device context was established. Coordinates _rght _and _btm _are are updated only when flags parameter contains TRF\_CALCRECT option. _lft _and _top _are never updated. |
| memo | [String](string.md) | in/out | On input, specifies the text we want to output within the rectangle. On return, it contains the amount of the original text that actually fits in the rectangle. |
| height | Num | out | If _memo_ fits entirely within the rectangle, _height_ returns the rectangle height actually required (which may be less than the full rectangle height). Otherwise, it will return the input height of the rectangle. |
| overflow | [String](string.md) | out | Returns the trailing portion of _memo_ which does not fit within the rectangle. If it all fits, then a null string is returned here. Note that this operation supports a dynamic string (S,0). |
| [Flags](flags_mx148.md) | Num | in | Optional flags used when calculating the optimum rectangle based on various formatting options. |




**Comments**

The vertical spacing used for the calculation is the natural vertical spacing for the current font and printer device context. To make sure you get the same spacing when actually printing, use the GDI printing directive RESETLPP after SETFONT and before  TEXTRECTANGLE. The vertical spacing is not automatically reset by SETFONT.

If _memo_ must be wrapped to multiple lines to fit within the rectangle, standard word-wrapping logic will be used to avoid breaking lines mid-word. Embedded CRLFs are also respected.

You can use this operation iteratively (changing the font size incrementally each time) to determine the optimum font size for the specified _memo_ text to fit within the rectangle.

The test program [GDICALC in EXLIB:\[908,37\]](https://bitbucket.org/microsabio/exlib/src/master/908037/gdicalc.bp) is to illustrate this kind of logic.

Note that MXGDI\_CALCRECT assumes vertical spacing optimum for the font, i.e. as if a RESETLPP command had been executed after SETFONT. If you don't do this in the actual GDI print file, the spacing for the print  operation may not match that used for the calculations.