ttfMeasureString function

bool ttfMeasureString(
  1. Pointer<TtfFont> font,
  2. String? text,
  3. int length,
  4. int maxWidth,
  5. Pointer<Int32> measuredWidth,
  6. Pointer<Uint32> measuredLength,
)

Calculate how much of a UTF-8 string will fit in a given width.

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

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

\param font the font to query. \param text text to calculate, in UTF-8 encoding. \param length the length of the text, in bytes, or 0 for null terminated text. \param max_width maximum width, in pixels, available for the string, or 0 for unbounded width. \param measured_width a pointer filled in with the width, in pixels, of the string that will fit, may be NULL. \param measured_length a pointer filled in with the length, in bytes, of the string that will fit, may be NULL. \returns true on success or false on failure; call SDL_GetError() for more information.

\threadsafety This function should be called on the thread that created the font.

\since This function is available since SDL_ttf 3.0.0.

extern SDL_DECLSPEC bool SDLCALL TTF_MeasureString(TTF_Font *font, const char *text, size_t length, int max_width, int *measured_width, size_t *measured_length)

Implementation

bool ttfMeasureString(
    Pointer<TtfFont> font,
    String? text,
    int length,
    int maxWidth,
    Pointer<Int32> measuredWidth,
    Pointer<Uint32> measuredLength) {
  final ttfMeasureStringLookupFunction = libSdl3Ttf.lookupFunction<
      Uint8 Function(
          Pointer<TtfFont> font,
          Pointer<Utf8> text,
          Uint32 length,
          Int32 maxWidth,
          Pointer<Int32> measuredWidth,
          Pointer<Uint32> measuredLength),
      int Function(
          Pointer<TtfFont> font,
          Pointer<Utf8> text,
          int length,
          int maxWidth,
          Pointer<Int32> measuredWidth,
          Pointer<Uint32> measuredLength)>('TTF_MeasureString');
  final textPointer = text != null ? text.toNativeUtf8() : nullptr;
  final result = ttfMeasureStringLookupFunction(
          font, textPointer, length, maxWidth, measuredWidth, measuredLength) ==
      1;
  calloc.free(textPointer);
  return result;
}