Eps_Page * eps_page_new (const Eps_Document *document) { Eps_Page *page; SpectreStatus status; if (!document) return NULL; page = (Eps_Page *)malloc (sizeof (Eps_Page)); if (!page) return NULL; page->document = document->ps_doc; page->page = spectre_document_get_page(document->ps_doc, 0); status = spectre_page_status (page->page); if (status != SPECTRE_STATUS_SUCCESS) { printf ("[eps] %s\n", spectre_status_to_string (status)); free (page); return 0; } page->rc = spectre_render_context_new (); if (!page->rc) { free (page); return NULL; } spectre_render_context_set_rotation (page->rc, 0); spectre_render_context_set_scale (page->rc, 1.0, 1.0); return page; }
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; }
void eps_page_render (const Eps_Page *page, Evas_Object *o) { unsigned char *data; unsigned char *d; unsigned int *m = NULL; double hscale; double vscale; SpectreStatus status; int width; int height; int stride; int yy; if (!page || !o) return; spectre_page_render (page->page, page->rc, &data, &stride); status = spectre_page_status (page->page); if (status != SPECTRE_STATUS_SUCCESS) { printf ("[eps] %s\n", spectre_status_to_string (status)); return; } eps_page_scale_get (page, &hscale, &vscale); eps_page_size_get (page, &width, &height); width *= hscale; height *= vscale; evas_object_image_size_set (o, width, height); evas_object_image_fill_set (o, 0, 0, width, height); m = (unsigned int *)evas_object_image_data_get (o, 1); if (!m) { free (data); return; } if (stride == 4 * width) memcpy(m, data, height * stride); else { d = data; for (yy = 0; yy < height; d += stride, m += width, ++yy) { memcpy (m, d, width * 4); } } evas_object_image_data_update_add (o, 0, 0, width, height); evas_object_resize (o, width, height); free (data); }
void eps_page_render_slice (const Eps_Page *page, Evas_Object *o, int x, int y, int width, int height) { unsigned char *data; unsigned char *d; unsigned int *m = NULL; double hscale; double vscale; SpectreStatus status; int stride; int yy; if (!page || !o || (width <= 0) || (height <= 0)) return; spectre_page_render_slice (page->page, page->rc, x, y, width, height, &data, &stride); status = spectre_page_status (page->page); if (status != SPECTRE_STATUS_SUCCESS) { printf ("aie !!%d %d %d %d %d\n", status, x, y, width, height); printf ("[eps] %s\n", spectre_status_to_string (status)); return; } eps_page_scale_get (page, &hscale, &vscale); width *= hscale; height *= vscale; evas_object_image_size_set (o, width, height); evas_object_image_fill_set (o, 0, 0, width, height); m = (unsigned int *)evas_object_image_data_get (o, 1); if (!m) { free (data); return; } d = data + y * stride + x; for (yy = 0; yy < height; d += stride, m += width, ++yy) { memcpy (m, d, width * 4); } evas_object_image_data_update_add (o, 0, 0, width, height); evas_object_resize (o, width, height); free (data); }
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; }
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; }