drawImage method

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]) {
  var o = 0;
  assert(() {
    if (_page.pdfDocument.settings.verbose) {
      o = _buf.offset;
      _buf.putString(' ' * (_indent));
    }
    return true;
  }());

  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(_page, _buf);
      break;
    case PdfImageOrientation.topRight:
      PdfNumList(<double>[-w, 0, 0, h, w + x, y]).output(_page, _buf);
      break;
    case PdfImageOrientation.bottomRight:
      PdfNumList(<double>[-w, 0, 0, -h, w + x, h + y]).output(_page, _buf);
      break;
    case PdfImageOrientation.bottomLeft:
      PdfNumList(<double>[w, 0, 0, -h, x, h + y]).output(_page, _buf);
      break;
    case PdfImageOrientation.leftTop:
      PdfNumList(<double>[0, -h, -w, 0, w + x, h + y]).output(_page, _buf);
      break;
    case PdfImageOrientation.rightTop:
      PdfNumList(<double>[0, -h, w, 0, x, h + y]).output(_page, _buf);
      break;
    case PdfImageOrientation.rightBottom:
      PdfNumList(<double>[0, h, w, 0, x, y]).output(_page, _buf);
      break;
    case PdfImageOrientation.leftBottom:
      PdfNumList(<double>[0, h, -w, 0, w + x, y]).output(_page, _buf);
      break;
  }

  _buf.putString(' cm ${img.name} Do Q ');
  _page.altered = true;

  assert(() {
    if (_page.pdfDocument.settings.verbose) {
      _buf.putString(' ' * math.max(0, _commentIndent - _buf.offset + o));
      _buf.putComment('drawImage(${img.ref()}, x: $x, y: $y, w: $w, h: $h)');
    }
    return true;
  }());
}