Пример #1
0
void ORPrintRender::renderPage(ORODocument * pDocument, int pageNb, QPainter *painter, qreal xDpi, qreal yDpi, QSize margins, int printResolution)
{
  OROPage * p = pDocument->page(pageNb);

  if(((!p->backgroundImage().isNull()) && (p->backgroundOpacity() != 0)) ||
     ((!p->watermarkText().isEmpty()) && (p->watermarkOpacity() != 0)))
  {
    // Do some simple processing used by both Background and Watermark
    const int resolution = 100;
    bool doBgWm = false;
    int printMarginWidth = margins.width();
    int printMarginHeight = margins.height();

    QString pageSize = pDocument->pageOptions().getPageSize();
    int pageWidth = 0;
    int pageHeight = 0;
    if(pageSize == "Custom") {
      // if this is custom sized sheet of paper we will just use those values
      pageWidth = (int)(pDocument->pageOptions().getCustomWidth() * resolution);
      pageHeight = (int)(pDocument->pageOptions().getCustomHeight() * resolution);
    } else {
      // lookup the correct size information for the specified size paper
      PageSizeInfo pi = PageSizeInfo::getByName(pageSize);
      if(!pi.isNull())
      {
        pageWidth = (int)((pi.width() / 100.0) * resolution);
        pageHeight = (int)((pi.height() / 100.0) * resolution);
      }
    }
    if(!pDocument->pageOptions().isPortrait()) {
      int tmp = pageWidth;
      pageWidth = pageHeight;
      pageHeight = tmp;
    }
    if(pageWidth < 1 || pageHeight < 1) {
      // whoops we couldn't find it.... we will use the values from the painter
      // and add in the margins of the printer to get what should be the correct
      // size of the sheet of paper we are printing to.
      pageWidth = (int)(((painter->viewport().width() + printMarginWidth + printMarginWidth) / xDpi) * resolution);
      pageHeight = (int)(((painter->viewport().height() + printMarginHeight + printMarginHeight) / yDpi) * resolution);
    }

    QImage image = QImage(pageWidth, pageHeight, QImage::Format_RGB32);
    QPainter gPainter;
    if(gPainter.begin(&image))
      gPainter.fillRect(gPainter.viewport(), QColor(Qt::white));

    // Render Background
    if((!p->backgroundImage().isNull()) && (p->backgroundOpacity() != 0))
    {
      doBgWm = true;
      QPointF ps = p->backgroundPosition();
      QSizeF sz = p->backgroundSize();
      QRectF rc = QRectF(ps.x() * resolution, ps.y() * resolution, sz.width() * resolution, sz.height() * resolution);
      renderBackground(image, p->backgroundImage(), rc.toRect(),
        p->backgroundScale(), p->backgroundScaleMode(),
        p->backgroundAlign(), p->backgroundOpacity());
    }

    // Render Watermark
    if((!p->watermarkText().isEmpty()) && (p->watermarkOpacity() != 0))
    {
      doBgWm = true;
      renderWatermark(image, p->watermarkText(), p->watermarkFont(), p->watermarkOpacity(),
        ((pDocument->pageOptions().getMarginLeft() + pDocument->pageOptions().getMarginRight()) * resolution),
        ((pDocument->pageOptions().getMarginTop() + pDocument->pageOptions().getMarginBottom()) * resolution),
        pDocument->pageOptions().getMarginLeft() * resolution, pDocument->pageOptions().getMarginTop() * resolution);
    }

    if(doBgWm)
    {
      QRectF target(-printMarginWidth, -printMarginHeight, (painter->viewport().width() + printMarginWidth + printMarginWidth), (painter->viewport().height() + printMarginHeight + printMarginHeight));
      QRectF source(0, 0, image.width(), image.height());
      painter->drawImage(target, image, source);
    }
  }

  // Render Page Objects
  for(int i = 0; i < p->primitives(); i++)
  {
    OROPrimitive * prim = p->primitive(i);

    QPen pen(prim->pen());
    painter->save();
    painter->setPen(pen);
    painter->setBrush(prim->brush());

	QPointF ps = prim->position();
	if(prim->rotationAxis().isNull()) {
		painter->translate(ps.x() * xDpi, ps.y() * yDpi); 
		painter->rotate(prim->rotation()); // rotation around the origin of the primitive (not the center)
	}
	else { // rotation around the defined axis
		qreal xRot = prim->rotationAxis().x();
		qreal yRot = prim->rotationAxis().y();
		painter->translate(xRot * xDpi, yRot * yDpi); 
		painter->rotate(prim->rotation());
		painter->translate((ps.x() - xRot) * xDpi, (ps.y() - yRot) * yDpi); 
	}

    if(prim->type() == OROTextBox::TextBox)
    {
      OROTextBox * tb = (OROTextBox*)prim;
	  painter->setFont(tb->font());

      QSizeF sz = tb->size();
      QRectF rc = QRectF(0, 0, sz.width() * xDpi, sz.height() * yDpi);

      painter->drawText(rc, tb->flags(), tb->text());
    }
    else if(prim->type() == OROLine::Line)
    {
		OROLine * ln = (OROLine*)prim;
		QPointF s = ln->startPoint();
		QPointF e = ln->endPoint();
		pen.setWidthF((ln->weight() / 100) * printResolution);
		painter->setPen(pen);
		painter->drawLine(QLineF(0, 0, (e.x()-s.x()) * xDpi, (e.y()-s.y()) * yDpi));
    }
    else if(prim->type() == OROImage::Image)
    {
      OROImage * im = (OROImage*)prim;
      QSizeF sz = im->size();
      QRectF rc = QRectF(0, 0, sz.width() * xDpi, sz.height() * yDpi);

      QImage img = im->image();
      if(im->scaled())
        img = img.scaled(rc.size().toSize(), (Qt::AspectRatioMode)im->aspectRatioMode(), (Qt::TransformationMode)im->transformationMode());

      QRectF sr = QRectF(QPointF(0.0, 0.0), rc.size().boundedTo(img.size()));
      painter->drawImage(rc.topLeft(), img, sr);
    }
    else if(prim->type() == ORORect::Rect)
    {
      ORORect * re = (ORORect*)prim;

      QSizeF sz = re->size();
      QRectF rc = QRectF(0, 0, sz.width() * xDpi, sz.height() * yDpi);
      pen.setWidthF((re->weight() / 100) * printResolution);
	  painter->setPen(pen);
      painter->drawRect(rc);
    }
    else
    {
      qDebug("unrecognized primitive type");
    }

    painter->restore();

  }
}
Пример #2
0
void ORPrintRender::renderPage(ORODocument * pDocument, int pageNb, QPainter *painter, qreal xDpi, qreal yDpi, QSize margins, int printResolution)
{
  OROPage * p = pDocument->page(pageNb);

  if(((!p->backgroundImage().isNull()) && (p->backgroundOpacity() != 0)) ||
     ((!p->watermarkText().isEmpty()) && (p->watermarkOpacity() != 0)))
  {
    // Do some simple processing used by both Background and Watermark
    const int resolution = 100;
    bool doBgWm = false;
    int printMarginWidth  = margins.width()  < 0 ? 0 : margins.width();
    int printMarginHeight = margins.height() < 0 ? 0 : margins.height();

    QString pageSize = pDocument->pageOptions().getPageSize();
    int pageWidth = 0;
    int pageHeight = 0;
    if(pageSize == "Custom") {
      // if this is custom sized sheet of paper we will just use those values
      pageWidth = (int)(pDocument->pageOptions().getCustomWidth() * resolution);
      pageHeight = (int)(pDocument->pageOptions().getCustomHeight() * resolution);
    } else {
      // lookup the correct size information for the specified size paper
      PageSizeInfo pi = PageSizeInfo::getByName(pageSize);
      if(!pi.isNull())
      {
        pageWidth = (int)((pi.width() / 100.0) * resolution);
        pageHeight = (int)((pi.height() / 100.0) * resolution);
      }
    }
    if(!pDocument->pageOptions().isPortrait()) {
      int tmp = pageWidth;
      pageWidth = pageHeight;
      pageHeight = tmp;
    }
    if(pageWidth < 1 || pageHeight < 1) {
      // whoops we couldn't find it.... we will use the values from the painter
      // and add in the margins of the printer to get what should be the correct
      // size of the sheet of paper we are printing to.
      pageWidth = (int)(((painter->viewport().width() + printMarginWidth + printMarginWidth) / xDpi) * resolution);
      pageHeight = (int)(((painter->viewport().height() + printMarginHeight + printMarginHeight) / yDpi) * resolution);
    }

    QImage image = QImage(pageWidth, pageHeight, QImage::Format_RGB32);
    QPainter gPainter;
    if(gPainter.begin(&image))
      gPainter.fillRect(gPainter.viewport(), QColor(Qt::white));

    // Render Background
    if((!p->backgroundImage().isNull()) && (p->backgroundOpacity() != 0))
    {
      doBgWm = true;
      QPointF ps = p->backgroundPosition();
      QSizeF sz = p->backgroundSize();
      QRectF rc = QRectF(ps.x() * resolution, ps.y() * resolution, sz.width() * resolution, sz.height() * resolution);
      renderBackground(image, p->backgroundImage(), rc.toRect(),
        p->backgroundScale(), p->backgroundScaleMode(),
        p->backgroundAlign(), p->backgroundOpacity());
    }

    // Render Watermark
    if((!p->watermarkText().isEmpty()) && (p->watermarkOpacity() != 0))
    {
      doBgWm = true;
      renderWatermark(image, p->watermarkText(), p->watermarkFont(), p->watermarkOpacity(),
                      pDocument->pageOptions().getMarginLeft()   * resolution,
                      pDocument->pageOptions().getMarginRight()  * resolution,
                      pDocument->pageOptions().getMarginTop()    * resolution,
                      pDocument->pageOptions().getMarginBottom() * resolution);
    }

    if(doBgWm)
    {
      QRectF target(0, 0, pageWidth, pageHeight);
      painter->save();
      painter->scale(xDpi / resolution, yDpi / resolution);
      QRectF source(0, 0, image.width(), image.height());
      painter->drawImage(target, image, source);
      painter->restore();
    }
  }

  // Render Page Objects
  for(int i = 0; i < p->primitives(); i++)
  {
    OROPrimitive * prim = p->primitive(i);

    QPen pen(prim->pen());
    painter->save();
    painter->setPen(pen);
    painter->setBrush(prim->brush());

    QPointF ps = prim->position();
    if(prim->rotationAxis().isNull())
    {
      painter->translate(ps.x() * xDpi, ps.y() * yDpi);
      painter->rotate(prim->rotation()); // rotation around the origin of the primitive (not the center)
    }
    else
    { // rotation around the defined axis
      qreal xRot = prim->rotationAxis().x();
      qreal yRot = prim->rotationAxis().y();
      painter->translate(xRot * xDpi, yRot * yDpi);
      painter->rotate(prim->rotation());
      painter->translate((ps.x() - xRot) * xDpi, (ps.y() - yRot) * yDpi);
    }

    if(prim->type() == OROTextBox::TextBox)
    {
      OROTextBox * tb = (OROTextBox*)prim;

      QSizeF sz = tb->size();
      QRectF rc = QRectF(0, 0, sz.width() * xDpi, sz.height() * yDpi);

      prim->drawRect(rc, painter, printResolution);

      painter->setFont(tb->font());
      QString text = tb->text();
      QString url;

      if(tb->text().startsWith("<http"))
      {
          int endOfUrl = tb->text().indexOf('>');
          if(endOfUrl > 0)
          {
              url = tb->text().mid(1, endOfUrl-1);
              text = tb->text().mid(endOfUrl+1);
              if(text.isEmpty()) text = url;
          }
      }

      bool toPdf = painter->paintEngine()->type() == QPaintEngine::Pdf;
      if(toPdf && !url.isEmpty())
      {
          QTextDocument doc;
          QTextCursor cursor(&doc);
          QTextCharFormat format;
          format.setFont(tb->font());
          format.setFontPointSize(tb->font().pointSizeF()*printResolution/100.0);
          format.setAnchor(true);
          format.setAnchorHref(url);
          cursor.insertText(text, format);
          doc.drawContents(painter);
      }
      else
      {
          painter->drawText(rc, tb->flags(), text);
      }

    }
    else if(prim->type() == OROBarcode::Barcode)
    {
      OROBarcode * bc = (OROBarcode*)prim;

      QSizeF sz = bc->size();
      QRectF rc = QRectF(0, 0, sz.width() * xDpi, sz.height() * yDpi);


      if(painter->paintEngine()->type() == QPaintEngine::User)
      {
        // label paint engine: the barcode parameters are encoded in a text item
        QString encodedData = ReportPrinter::barcodePrefix() + QString("%1;%2;%3;%4").arg(bc->format()).arg(sz.height()).arg(bc->narrowBarWidth()).arg(bc->data());
        painter->drawText(rc, 0, encodedData);
      }
      else
      {
        if(bc->format() == "3of9")
          render3of9(painter, xDpi, rc, bc->data(), bc);
        else if(bc->format() == "3of9+")
          renderExtended3of9(painter, xDpi, rc, bc->data(), bc);
        else if(bc->format() == "128")
          renderCode128(painter, xDpi, rc, bc->data(), bc);
        else if(bc->format() == "i2of5")
          renderI2of5(painter, xDpi, rc, bc->data(), bc);
        else if(bc->format() == "ean13")
          renderCodeEAN13(painter, xDpi, rc, bc->data(), bc);
        else if(bc->format() == "ean8")
          renderCodeEAN8(painter, xDpi, rc, bc->data(), bc);
        else if(bc->format() == "upc-a")
          renderCodeUPCA(painter, xDpi, rc, bc->data(), bc);
        else if(bc->format() == "upc-e")
          renderCodeUPCE(painter, xDpi, rc, bc->data(), bc);
        else if(bc->format().contains("datamatrix"))
          renderCodeDatamatrix(painter, rc, bc->data(), bc);
        else if(bc->format().contains("PDF417"))
          renderPDF417(painter, xDpi, rc, bc->data(), bc);
        else if(bc->format().contains("QR"))
          renderQR(painter, xDpi, rc, bc->data(), bc);
        else
        {
          painter->drawText(rc, 0, "ERR: [" + bc->format() + "]" + bc->data());
        }
      }
    }
    else if(prim->type() == OROLine::Line)
    {
        OROLine * ln = (OROLine*)prim;
		QPointF s = ln->startPoint();
		QPointF e = ln->endPoint();
        pen.setWidthF((pen.widthF() / 100.0) * printResolution);
		painter->setPen(pen);
		painter->drawLine(QLineF(0, 0, (e.x()-s.x()) * xDpi, (e.y()-s.y()) * yDpi));
    }
    else if(prim->type() == OROImage::Image)
    {
      OROImage * im = (OROImage*)prim;
      QSizeF sz = im->size();
      QRectF rc = QRectF(0, 0, sz.width() * xDpi, sz.height() * yDpi);

      prim->drawRect(rc, painter, printResolution);

      QImage img = im->image();
      if(im->scaled())
        img = img.scaled(rc.size().toSize(), (Qt::AspectRatioMode)im->aspectRatioMode(), (Qt::TransformationMode)im->transformationMode());
      else
        img = img.scaled(QSize(img.width()*(int)xDpi/150, img.height()*(int)yDpi/150), Qt::KeepAspectRatio);

      QRectF sr = QRectF(QPointF(0.0, 0.0), rc.size().boundedTo(img.size()));
      painter->drawImage(rc.topLeft(), img, sr);
    }
    else if(prim->type() == ORORect::Rect)
    {
      QSizeF sz = ((OROTextBox*)prim)->size();
      QRectF rc = QRectF(0, 0, sz.width() * xDpi, sz.height() * yDpi);
      prim->drawRect(rc, painter, printResolution);
    }
    else
    {
      qDebug("unrecognized primitive type");
    }

    painter->restore();

  }
}