measureString method Null safety

Size measureString(
  1. String text,
  2. {Size? layoutArea,
  3. PdfStringFormat? format}
)

Measures a string by using this font.

//Create a new PDF document.
PdfDocument document = PdfDocument();
//Add a page to the document.
PdfPage page = document.pages.add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.graphics;
//Create a new PDF font instance.
PdfFont font = PdfStandardFont(PdfFontFamily.helvetica, 12);
String text = "Hello World!";
//Measure the text.
Size size = font.measureString(text);
//Draw string to PDF page.
graphics.drawString(text, font,
    brush: PdfBrushes.black,
    bounds: Rect.fromLTWH(0, 0, size.width, size.height));
//Saves the document.
List<int> bytes = document.save();
//Dispose the document.
document.dispose();

Implementation

Size measureString(String text, {Size? layoutArea, PdfStringFormat? format}) {
  layoutArea ??= const Size(0, 0);
  final _PdfStringLayouter layouter = _PdfStringLayouter();
  final _PdfStringLayoutResult result = layouter._layout(text, this, format,
      width: layoutArea.width, height: layoutArea.height);
  return result._size.size;
}