> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-framework/modularity/image-manipulation/reference/built-in-functions/imagemeasuretext.md).

# ImageMeasureText

## Syntax

```
ImageMeasureText( name, str [, attributeCollection] )
```

Or as a member:

```
someImage.measureText( str [, attributeCollection] )
```

## Arguments

| Name                | Type   | Required | Description                                                                                        |
| ------------------- | ------ | -------- | -------------------------------------------------------------------------------------------------- |
| name                | any    | Yes      | The image or the name of a variable referencing an image. Its rendering settings are used.         |
| str                 | string | Yes      | The single line of text to measure. An empty string has width 0.                                   |
| attributeCollection | struct | No       | The same font struct `ImageDrawText` takes: `font`, `size`, `style`, `underline`, `strikeThrough`. |

## Returns

A struct, all values in pixels:

| Key     | Description                                                |
| ------- | ---------------------------------------------------------- |
| width   | Advance width of the string.                               |
| height  | `ascent + descent`.                                        |
| ascent  | Distance from the baseline up to the top of the font.      |
| descent | Distance from the baseline down to the bottom of the font. |
| leading | Extra space the font recommends between lines.             |

## Description

Measures text without drawing it. It uses the very same font resolution as `ImageDrawText`, so the numbers match what gets rendered. An invalid `style` throws, as it does for `ImageDrawText`. The text is treated as one line; split on newlines yourself.

The `y` you pass to `drawText` is the baseline, so the text occupies `y - ascent` to `y + descent`. The distance between baselines of consecutive lines is `height + leading`.

## Example

```boxlang
img   = ImageNew( "", 1200, 630, "rgb", "black" );
attrs = { font: "SansSerif", size: 64, style: "bold" };

m = img.measureText( "Hello World", attrs );
// m.width, m.height, m.ascent, m.descent, m.leading

// center horizontally
img.setDrawingColor( "white" );
img.drawText( "Hello World", ( img.getWidth() - m.width ) / 2, 300, attrs );
```

### Word wrap and shrink to fit

Wrapping is left to the caller on purpose (breaking rules, ellipsis, hyphenation and max lines are policy), but it is a few lines on top of `measureText`:

```boxlang
function wrapLines( img, text, attrs, maxWidth ) {
    var lines = [];
    var line  = "";
    for ( var word in text.listToArray( " " ) ) {
        var candidate = line.len() ? line & " " & word : word;
        if ( line.len() && img.measureText( candidate, attrs ).width > maxWidth ) {
            lines.append( line );
            line = word;
        } else {
            line = candidate;
        }
    }
    if ( line.len() ) lines.append( line );
    return lines;
}

attrs = { font: "SansSerif", size: 72, style: "bold" };
lines = wrapLines( img, title, attrs, 1000 );
while ( lines.len() > 3 && attrs.size > 24 ) {
    attrs.size -= 4;
    lines = wrapLines( img, title, attrs, 1000 );
}
```

## Related BIFs

* ImageDrawText

## Notes

* Measurement uses the image's graphics settings (such as anti-aliasing), the same as drawing.
* Results are fractional; round up when sizing boxes.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/modularity/image-manipulation/reference/built-in-functions/imagemeasuretext.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
