ttfMeasureText function

int ttfMeasureText(
  1. Pointer<TtfFont> font,
  2. String? text,
  3. int measureWidth,
  4. Pointer<Int32> extent,
  5. Pointer<Int32> count,
)

Calculate how much of a Latin1 string will fit in a given width.

This reports the number of characters that can be rendered before reaching measure_width.

This does not need to render the string to do this calculation.

You almost certainly want TTF_MeasureUTF8() unless you're sure you have a 1-byte Latin1 encoding. US ASCII characters will work with either function, but most other Unicode characters packed into a const char * will need UTF-8.

\param font the font to query. \param text text to calculate, in Latin1 encoding. \param measure_width maximum width, in pixels, available for the string. \param count on return, filled with number of characters that can be rendered. \param extent on return, filled with latest calculated width. \returns 0 if successful, -1 on error.

\since This function is available since SDL_ttf 2.0.18.

\sa TTF_MeasureText \sa TTF_MeasureUTF8 \sa TTF_MeasureUNICODE

extern DECLSPEC int SDLCALL TTF_MeasureText(TTF_Font *font, const char *text, int measure_width, int *extent, int *count)

Implementation

int ttfMeasureText(Pointer<TtfFont> font, String? text, int measureWidth,
    Pointer<Int32> extent, Pointer<Int32> count) {
  final ttfMeasureTextLookupFunction = libSdl2Ttf.lookupFunction<
      Int32 Function(Pointer<TtfFont> font, Pointer<Utf8> text,
          Int32 measureWidth, Pointer<Int32> extent, Pointer<Int32> count),
      int Function(Pointer<TtfFont> font, Pointer<Utf8> text, int measureWidth,
          Pointer<Int32> extent, Pointer<Int32> count)>('TTF_MeasureText');
  final textPointer = text != null ? text.toNativeUtf8() : nullptr;
  final result = ttfMeasureTextLookupFunction(
      font, textPointer, measureWidth, extent, count);
  calloc.free(textPointer);
  return result;
}