void WPdfImage::init() { HPDF_Page_GSave(page_); // for Wt -> HPDF HPDF_Page_Concat(page_, 1, 0, 0, -1, 0, height_.toPixels()); HPDF_Page_Concat(page_, 1, 0, 0, 1, x_, y_); HPDF_Page_GSave(page_); // for painter->combinedTransform() }
JNIEXPORT void JNICALL Java_com_draekko_libharu_PdfPage_concatPage (JNIEnv *env, jobject obj, jfloat a, jfloat b, jfloat c, jfloat d, jfloat x, jfloat y) { haru_setup_error_handler(env, __func__); HPDF_Page page = get_HPDF_Page(env, obj); HPDF_Page_Concat(page, a, b, c, d, x, y); haru_clear_error_handler(); }
/* * Class: org_libharu_PdfPage * Method: concat * Signature: (FFFFFF)V */ JNIEXPORT void JNICALL Java_org_libharu_PdfPage_concat(JNIEnv *env, jobject obj, jfloat a, jfloat b, jfloat c, jfloat d, jfloat x, jfloat y) { jint page; /* Get mHPDFPagePointer */ page = (*env)->GetIntField(env, obj, mHPDFPagePointer); HPDF_Page_Concat((HPDF_Page) page, (HPDF_REAL) a, (HPDF_REAL) b, (HPDF_REAL) c, (HPDF_REAL) d, (HPDF_REAL) x, (HPDF_REAL) y); }
void WPdfImage::drawImage(const WRectF& rect, const std::string& imgUrl, int imgWidth, int imgHeight, const WRectF& srect) { HPDF_Image img = 0; if (Uri::isDataUri(imgUrl)) { Uri::Uri uri = Uri::parseDataUri(imgUrl); if ("image/png" == uri.mimeType) img = HPDF_LoadPngImageFromMem(pdf_, (HPDF_BYTE*)uri.data.c_str(), uri.data.size()); else if ("image/jpeg" == uri.mimeType) img = HPDF_LoadJpegImageFromMem(pdf_, (HPDF_BYTE*)uri.data.c_str(), uri.data.size()); } else { std::string mimeType = Image::identifyImageFileMimeType(imgUrl); if ("image/png" == mimeType) img = HPDF_LoadPngImageFromFile2(pdf_, imgUrl.c_str()); else if ("image/jpeg" == mimeType) img = HPDF_LoadJpegImageFromFile(pdf_, imgUrl.c_str()); } if (!img) throw WException("WPdfImage::drawImage(): cannot load image: " + imgUrl); double x = rect.x(); double y = rect.y(); double width = rect.width(); double height = rect.height(); HPDF_Page_GSave(page_); if (srect.x() != 0 || srect.y() != 0 || srect.width() != imgWidth || srect.height() != imgHeight) { double scaleX = width / imgWidth; double scaleY = height / imgHeight; x -= srect.x() * scaleX; y -= srect.y() * scaleY; width *= scaleX; height *= scaleY; HPDF_Page_Rectangle(page_, rect.x(), rect.y(), rect.width(), rect.height()); HPDF_Page_Clip(page_); } HPDF_Page_Concat(page_, 1, 0, 0, -1, x, y + height); // revert upside-down HPDF_Page_DrawImage(page_, img, 0, 0, width, height); HPDF_Page_GRestore(page_); }
void WPdfRenderer::endPage(WPaintDevice *device) { delete painter_; painter_ = 0; delete device; #ifndef WT_TARGET_JAVA HPDF_Page_Concat (page_, dpi_/72.0f, 0, 0, dpi_/72.0f, 0, 0); #endif }
void WPdfImage::drawArc(const WRectF& rect, double startAngle, double spanAngle) { HPDF_Page_GSave(page_); HPDF_Page_Concat(page_, 1, 0, 0, rect.height() / rect.width(), rect.center().x(), rect.center().y()); double start = startAngle; double end = start + spanAngle; if (end < start) std::swap(start, end); HPDF_Page_Arc(page_, 0, 0, rect.width()/1, start + 90, end + 90); paintPath(); HPDF_Page_GRestore(page_); }
WPaintDevice *WPdfRenderer::startPage(int page) { if (page > 0) page_ = createPage(page); #ifndef WT_TARGET_JAVA HPDF_Page_Concat (page_, 72.0f/dpi_, 0, 0, 72.0f/dpi_, 0, 0); #endif WPdfImage *device = new WPdfImage(pdf_, page_, 0, 0, pageWidth(page), pageHeight(page)); #ifdef WT_TARGET_JAVA WTransform deviceTransform; deviceTransform.scale(72.0f/dpi_, 72.0f/dpi_); device->setDeviceTransform(deviceTransform); #endif //WT_TARGET_JAVA for (unsigned i = 0; i < fontCollections_.size(); ++i) device->addFontCollection(fontCollections_[i].directory, fontCollections_[i].recursive); return device; }
bool pdf_next_page(void) { #ifdef PDF_DEBUG NSLOG(netsurf, INFO, "pdf_next_page begins"); #endif clip_update_needed = false; if (pdf_page != NULL) { apply_clip_and_mode(false, NS_TRANSPARENT, NS_TRANSPARENT, 0., DashPattern_eNone); pdfw_gs_restore(pdf_page); } #ifdef PDF_DEBUG_DUMPGRID if (pdf_page != NULL) { pdf_plot_grid(10, 10, 0xCCCCCC); pdf_plot_grid(100, 100, 0xCCCCFF); } #endif pdf_page = HPDF_AddPage(pdf_doc); if (pdf_page == NULL) return false; HPDF_Page_SetWidth (pdf_page, settings->page_width); HPDF_Page_SetHeight(pdf_page, settings->page_height); HPDF_Page_Concat(pdf_page, 1, 0, 0, 1, FIXTOFLT(settings->margins[MARGINLEFT]), 0); pdfw_gs_save(pdf_page); #ifdef PDF_DEBUG NSLOG(netsurf, INFO, "%f %f", page_width, page_height); #endif return true; }
void WPdfImage::drawText(const WRectF& rect, WFlags<AlignmentFlag> flags, TextFlag textFlag, const WString& text, const WPointF *clipPoint) { // FIXME: textFlag if (clipPoint && painter() && !painter()->clipPath().isEmpty()) { if (!painter()->clipPathTransform().map(painter()->clipPath()) .isPointInPath(painter()->worldTransform().map(*clipPoint))) return; } if (trueTypeFont_ && !trueTypeFonts_->busy()) trueTypeFonts_->drawText(painter()->font(), rect, flags, text); else { HPDF_REAL left, top, right, bottom; HPDF_TextAlignment alignment = HPDF_TALIGN_LEFT; AlignmentFlag horizontalAlign = flags & AlignHorizontalMask; AlignmentFlag verticalAlign = flags & AlignVerticalMask; switch (horizontalAlign) { default: // should never happen case AlignmentFlag::Left: left = rect.left(); right = left + 10000; alignment = HPDF_TALIGN_LEFT; break; case AlignmentFlag::Right: right = rect.right(); left = right - 10000; alignment = HPDF_TALIGN_RIGHT; break; case AlignmentFlag::Center: { float center = rect.center().x(); left = center - 5000; right = center + 5000; alignment = HPDF_TALIGN_CENTER; break; } } switch (verticalAlign) { default: // fall-through ; should never happen case AlignmentFlag::Top: top = rect.top(); break; case AlignmentFlag::Middle: // FIXME: use font metrics to center middle of ascent ! top = rect.center().y() - 0.60 * fontSize_; break; case AlignmentFlag::Bottom: top = rect.bottom() - fontSize_; break; } bottom = top + fontSize_; if (trueTypeFonts_->busy()) setChanged(PainterChangeFlag::Font); HPDF_Page_GSave(page_); // Undo the global inversion HPDF_Page_Concat(page_, 1, 0, 0, -1, 0, bottom); HPDF_Page_BeginText(page_); // Need to fill text using pen color const WColor& penColor = painter()->pen().color(); HPDF_Page_SetRGBFill(page_, penColor.red() / 255., penColor.green() / 255., penColor.blue() / 255.); std::string s = trueTypeFont_ ? text.toUTF8() : text.narrow(); HPDF_Page_TextRect(page_, left, fontSize_, right, 0, s.c_str(), alignment, nullptr); HPDF_Page_EndText(page_); HPDF_Page_GRestore(page_); } }
void WPdfImage::drawImage(const WRectF& rect, const std::string& imgUrl, int imgWidth, int imgHeight, const WRectF& srect) { HPDF_Image img = nullptr; if (DataUri::isDataUri(imgUrl)) { #define HAVE_LOAD_FROM_MEM HPDF_MAJOR_VERSION > 2 || (HPDF_MAJOR_VERSION == 2 && (HPDF_MINOR_VERSION >= 2)) #if HAVE_LOAD_FROM_MEM DataUri uri(imgUrl); if ("image/png" == uri.mimeType) img = HPDF_LoadPngImageFromMem(pdf_, (HPDF_BYTE*)&uri.data[0], uri.data.size()); else if ("image/jpeg" == uri.mimeType) img = HPDF_LoadJpegImageFromMem(pdf_, (HPDF_BYTE*)&uri.data[0], uri.data.size()); #else LOG_ERROR("drawImage: data URI support requires libharu 2.2.0 or later"); #endif } else { std::string mimeType = ImageUtils::identifyMimeType(imgUrl); if ("image/png" == mimeType) img = HPDF_LoadPngImageFromFile2(pdf_, imgUrl.c_str()); else if ("image/jpeg" == mimeType) img = HPDF_LoadJpegImageFromFile(pdf_, imgUrl.c_str()); } if (!img) throw WException("WPdfImage::drawImage(): cannot load image: " + imgUrl); double x = rect.x(); double y = rect.y(); double width = rect.width(); double height = rect.height(); HPDF_Page_GSave(page_); if (srect.x() != 0 || srect.y() != 0 || srect.width() != imgWidth || srect.height() != imgHeight) { double scaleX = width / imgWidth; double scaleY = height / imgHeight; x -= srect.x() * scaleX; y -= srect.y() * scaleY; width *= scaleX; height *= scaleY; HPDF_Page_Rectangle(page_, rect.x(), rect.y(), rect.width(), rect.height()); HPDF_Page_Clip(page_); } HPDF_Page_Concat(page_, 1, 0, 0, -1, x, y + height); // revert upside-down HPDF_Page_DrawImage(page_, img, 0, 0, width, height); HPDF_Page_GRestore(page_); }
void WPdfImage::applyTransform(const WTransform& t) { HPDF_Page_Concat(page_, t.m11(), t.m12(), t.m21(), t.m22(), t.dx(), t.dy()); }
void WPdfImage::drawText(const WRectF& rect, WFlags<AlignmentFlag> flags, TextFlag textFlag, const WString& text) { // FIXME: textFlag if (trueTypeFont_ && !trueTypeFonts_->busy()) trueTypeFonts_->drawText(painter()->font(), rect, flags, text); else { HPDF_REAL left, top, right, bottom; HPDF_TextAlignment alignment; AlignmentFlag horizontalAlign = flags & AlignHorizontalMask; AlignmentFlag verticalAlign = flags & AlignVerticalMask; switch (horizontalAlign) { case AlignLeft: left = rect.left(); right = left + 1000; alignment = HPDF_TALIGN_LEFT; break; case AlignRight: right = rect.right(); left = right - 1000; alignment = HPDF_TALIGN_RIGHT; break; case AlignCenter: { float center = rect.center().x(); left = center - 500; right = center + 500; alignment = HPDF_TALIGN_CENTER; break; } default: break; } switch (verticalAlign) { case AlignTop: top = rect.top(); break; case AlignMiddle: // FIXME: use font metrics to center middle of ascent ! top = rect.center().y() - 0.60 * fontSize_; break; case AlignBottom: top = rect.bottom() - fontSize_; break; default: break; } bottom = top + fontSize_; if (trueTypeFonts_->busy()) setChanged(Font); HPDF_Page_GSave(page_); // Undo the global inversion HPDF_Page_Concat(page_, 1, 0, 0, -1, 0, bottom); HPDF_Page_BeginText(page_); // Need to fill text using pen color const WColor& penColor = painter()->pen().color(); HPDF_Page_SetRGBFill(page_, penColor.red() / 255., penColor.green() / 255., penColor.blue() / 255.); std::string s = trueTypeFont_ ? text.toUTF8() : text.narrow(); HPDF_Page_TextRect(page_, left, fontSize_, right, 0, s.c_str(), alignment, 0); HPDF_Page_EndText(page_); HPDF_Page_GRestore(page_); } }