drawImage method Null safety

void drawImage(
  1. PdfImage img,
  2. double x,
  3. double y,
  4. [double? w,
  5. double? h]
)

Draws an image onto the page.

Implementation

void drawImage(PdfImage img, double x, double y, [double? w, double? h]) {
  w ??= img.width.toDouble();
  h ??= img.height.toDouble() * w / img.width.toDouble();

  // The image needs to be registered in the page resources
  _page.addXObject(img);

  // q w 0 0 h x y cm % the coordinate matrix
  _buf.putString('q ');
  switch (img.orientation) {
    case PdfImageOrientation.topLeft:
      PdfNumList(<double>[w, 0, 0, h, x, y]).output(_buf);
      break;
    case PdfImageOrientation.topRight:
      PdfNumList(<double>[-w, 0, 0, h, w + x, y]).output(_buf);
      break;
    case PdfImageOrientation.bottomRight:
      PdfNumList(<double>[-w, 0, 0, -h, w + x, h + y]).output(_buf);
      break;
    case PdfImageOrientation.bottomLeft:
      PdfNumList(<double>[w, 0, 0, -h, x, h + y]).output(_buf);
      break;
    case PdfImageOrientation.leftTop:
      PdfNumList(<double>[0, -h, -w, 0, w + x, h + y]).output(_buf);
      break;
    case PdfImageOrientation.rightTop:
      PdfNumList(<double>[0, -h, w, 0, x, h + y]).output(_buf);
      break;
    case PdfImageOrientation.rightBottom:
      PdfNumList(<double>[0, h, w, 0, x, y]).output(_buf);
      break;
    case PdfImageOrientation.leftBottom:
      PdfNumList(<double>[0, h, -w, 0, w + x, y]).output(_buf);
      break;
  }

  _buf.putString(' cm ${img.name} Do Q\n');
}