Пример #1
0
	QSize Document::GetPageSize (int index) const
	{
		auto page = spectre_document_get_page (SD_, index);
		const auto& result = GetSpectrePageSize (page);
		spectre_page_free (page);
		return result;
	}
Пример #2
0
static const char *
_spectre_render_page (const char *filename,
		      const char *page_label,
		      cairo_surface_t **surface_out)
{
    static const cairo_user_data_key_t key;

    SpectreDocument *document;
    SpectreStatus status;
    int width, height, stride;
    unsigned char *pixels;
    cairo_surface_t *surface;

    document = spectre_document_new ();
    spectre_document_load (document, filename);
    status = spectre_document_status (document);
    if (status) {
	spectre_document_free (document);
	return spectre_status_to_string (status);
    }

    if (page_label) {
	SpectrePage *page;
	SpectreRenderContext *rc;

	page = spectre_document_get_page_by_label (document, page_label);
	spectre_document_free (document);
	if (page == NULL)
	    return "page not found";

	spectre_page_get_size (page, &width, &height);
	rc = spectre_render_context_new ();
	spectre_render_context_set_page_size (rc, width, height);
	spectre_page_render (page, rc, &pixels, &stride);
	spectre_render_context_free (rc);
	status = spectre_page_status (page);
	spectre_page_free (page);
	if (status) {
	    free (pixels);
	    return spectre_status_to_string (status);
	}
    } else {
	spectre_document_get_page_size (document, &width, &height);
	spectre_document_render (document, &pixels, &stride);
	spectre_document_free (document);
    }

    surface = cairo_image_surface_create_for_data (pixels,
						   CAIRO_FORMAT_RGB24,
						   width, height,
						   stride);
    cairo_surface_set_user_data (surface, &key,
				 pixels, (cairo_destroy_func_t) free);
    *surface_out = surface;
    return NULL;
}
Пример #3
0
void
eps_page_delete (Eps_Page *page)
{
   if (!page)
     return;

   spectre_render_context_free (page->rc);
   spectre_page_free (page->page);
   free (page);
}
Пример #4
0
zathura_error_t
ps_page_clear(zathura_page_t* page, SpectrePage* spectre_page)
{
  if (page == NULL) {
    return ZATHURA_ERROR_INVALID_ARGUMENTS;
  }

  if (spectre_page != NULL) {
    spectre_page_free(spectre_page);
  }

  return ZATHURA_ERROR_OK;
}
Пример #5
0
void
eps_page_page_set (Eps_Page *page, int p)
{
   SpectrePage  *new_page;
   SpectreStatus status;

   if (!page)
     return;

   new_page = spectre_document_get_page(page->document, p);

   status = spectre_page_status (new_page);
   if (status != SPECTRE_STATUS_SUCCESS) {
      printf ("[eps] %s\n", spectre_status_to_string (status));
      return;
   }

   spectre_page_free (page->page);
   page->page = new_page;
}
Пример #6
0
	QImage Document::RenderPage (int index, double xRes, double yRes)
	{
		auto page = spectre_document_get_page (SD_, index);

		auto rc = spectre_render_context_new ();
		auto size = GetPageSize (index);
		spectre_render_context_set_scale (rc, xRes, yRes);
		size.rwidth () *= xRes;
		size.rheight () *= yRes;

		unsigned char *data = 0;
		int rowLength = 0;
		spectre_page_render (page, rc, &data, &rowLength);
		spectre_render_context_free (rc);
		spectre_page_free (page);

		const QImage& img = rowLength == size.width () * 4 ?
				QImage (data, size.width (), size.height (), QImage::Format_RGB32) :
				QImage (data, rowLength / 4, size.height (), QImage::Format_RGB32)
					.copy (0, 0, size.width (), size.height ());
		free (data);
		return img;
	}
Пример #7
0
bool PostScriptDocument::load(const QString& fileName) {

  p_filename = fileName;

  p_internal_document = spectre_document_new();
  spectre_document_load(p_internal_document, QFile::encodeName(p_filename));

  const SpectreStatus loadStatus = spectre_document_status(p_internal_document);
  if (loadStatus != SPECTRE_STATUS_SUCCESS) {
    kDebug() << "ERR:" << spectre_status_to_string(loadStatus);
    spectre_document_free(p_internal_document);
    clear();
    return FALSE;
  }

  int numPages = spectre_document_get_n_pages(p_internal_document);
  if (numPages > 0) {
    kDebug() << "Page Count: " << numPages;
  } else {
    kWarning() << "Unable to calculate number of pages.";
    numPages = 0;
  }

  int width, height;
  spectre_document_get_page_size(p_internal_document, &width, &height);
  if ((width > 0) && (height > 0)) {
    p_page_size = QSize(width, height);
    kDebug() << "Page Size: " << width << "x" << height << " (" << PaperSizeUtils::paperSizeToString(PaperSizeUtils::sizeToPaperSize(p_page_size)) << ")";
  } else {
    kWarning() << "Unable to calculate page size.";
  }

  SpectreOrientation orientation = spectre_document_get_orientation(p_internal_document);
  bool reversePage;
  p_orientation = spectreOrientationToOrientation(orientation, &reversePage);
  kDebug() << "Page Orientation: " << PaperSizeUtils::orientationToString(p_orientation);
  kDebug() << "Note: The page orientation may differ from orientation for each page.";

  /* Load the pages now */
  SpectrePage *page;
  SpectreOrientation pageOrientation;
  QPrinter::Orientation pageOrientation2;
  width = 0; height = 0;
  for (int i = 0; i < numPages; ++i) {

    pageOrientation = SPECTRE_ORIENTATION_PORTRAIT;
    page = spectre_document_get_page(p_internal_document, i);
    if (spectre_document_status(p_internal_document)) {
      kWarning() << "Error getting page " << i << spectre_status_to_string(spectre_document_status(p_internal_document));
    } else {
      spectre_page_get_size(page, &width, &height);
      pageOrientation = spectre_page_get_orientation(page);
    }
    spectre_page_free(page);

    pageOrientation2 = spectreOrientationToOrientation(pageOrientation, &reversePage);

    p_pages.append(PostScriptDocumentPage(QSize(width, height), pageOrientation2, reversePage));
    kDebug() << "Append page" << i+1 << "with Size (" << width << "," << height << ")," << PaperSizeUtils::orientationToString(pageOrientation2);

  }
  kDebug() << "Loaded" << p_pages.count() << "pages";

  p_is_valid = TRUE;
  return TRUE;

}
Пример #8
0
QImage* PostScriptDocument::renderPage(const int pageNum, const int dpiX, const int dpiY) {

  Q_UNUSED(dpiX);
  Q_UNUSED(dpiY);

  if ((pageNum < 0) || (pageNum >= p_pages.count())) return NULL;

  PostScriptDocumentPage page = p_pages[pageNum];
  /*int width = reqSize.width();
  int height = reqSize.height();
  double magnify = 1.0f;
  if (page.orientation() == QPrinter::Landscape) {
    magnify = qMax((double)height / (double)page.size().width(),
                   (double)width / (double)page.size().height());
  } else {
    magnify = qMax((double)width / (double)page.size().width(),
                   (double)height / (double)page.size().height());
  }*/

  SpectrePage *spage = spectre_document_get_page(p_internal_document, pageNum);

  SpectreRenderContext *renderContext = spectre_render_context_new();

  /*spectre_render_context_set_scale(renderContext, magnify, magnify);*/
  spectre_render_context_set_use_platform_fonts(renderContext, false);
  spectre_render_context_set_antialias_bits(renderContext, 4, 4);
  /* Do not use spectre_render_context_set_rotation makes some files not render correctly, e.g. bug210499.ps
   * so we basically do the rendering without any rotation and then rotate to the orientation as needed
   * spectre_render_context_set_rotation(m_renderContext, req.orientation);
   */

  unsigned char *data = NULL;
  int row_length = 0;

  spectre_page_render(spage, renderContext, &data, &row_length);

  if (spectre_page_status(spage) != SPECTRE_STATUS_SUCCESS) {
    kDebug() << "Failed to render page" << pageNum+1 << ". Spectre fail status:" << spectre_page_status(spage);
    return NULL;
  }

  int width = page.size().width();
  int height = page.size().height();

  kDebug() << "Size of page" << pageNum+1 << "to render: " << width << "x" << height;

  // Qt4 needs the missing alpha of QImage::Format_RGB32 to be 0xff
  if (data && data[3] != 0xff)
    for (int i = 3; i < row_length * height; i += 4)
      data[i] = 0xff;

  QImage image;

  if (row_length == width * 4) {
    image = QImage(data, width, height, QImage::Format_RGB32);
  } else {
    // In case this ends up beign very slow we can try with some memmove
    QImage aux(data, row_length / 4, height, QImage::Format_RGB32);
    image = QImage(aux.copy(0, 0, width, height));
  }

  /*if (page.reversePage()) {

    if (page.orientation() == QPrinter::Portrait) {
      QTransform m;
      m.rotate(180);
      image = image.transformed(m);
    } else if (page.orientation() == QPrinter::Landscape) {
      QTransform m;
      m.rotate(270);
      image = image.transformed(m);
    }

  } else {

    if (page.orientation() == QPrinter::Landscape) {
      QTransform m;
      m.rotate(90);
      image = image.transformed(m);
    }

  }*/

  QImage *result = new QImage(image.copy());

  if ((result->width() != width) || (result->height() != height)) {
    kWarning().nospace() << "Generated image does not match wanted size: "
                    << "[" << result->width() << "x" << result->height() << "] vs requested "
                    << "[" << width << "x" << height << "]";
    QImage aux = result->scaled(width, height);
    delete result;
    result = new QImage(aux);
  }

  spectre_page_free(spage);

  spectre_render_context_free(renderContext);

  return result;

}