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() }
/* * Class: org_libharu_PdfPage * Method: gSave * Signature: ()V */ JNIEXPORT void JNICALL Java_org_libharu_PdfPage_gSave(JNIEnv *env, jobject obj) { jint page; /* Get mHPDFPagePointer */ page = (*env)->GetIntField(env, obj, mHPDFPagePointer); HPDF_Page_GSave((HPDF_Page) page); }
WPdfImage::WPdfImage(const WLength& width, const WLength& height) : width_(width), height_(height), painter_(nullptr) { myPdf_ = true; pdf_ = HPDF_New(error_handler, this); if (!pdf_) throw WException("Could not create libharu document."); HPDF_SetCompressionMode(pdf_, HPDF_COMP_ALL); page_ = HPDF_AddPage(pdf_); font_ = nullptr; x_ = y_ = 0; HPDF_Page_SetWidth(page_, width_.toPixels()); HPDF_Page_SetHeight(page_, height_.toPixels()); HPDF_Page_GSave(page_); trueTypeFonts_ = new FontSupport(this, FontSupport::TrueTypeOnly); #if HPDF_VERSION_ID>=20300 HPDF_UseUTFEncodings(pdf_); #endif }
/** * Increase gstate level. * \param page PDF page where the update needs to happen. */ void pdfw_gs_save(HPDF_Page page) { if (pdfw_gs_level == PDFW_MAX_GSTATES) abort(); pdfw_gs[pdfw_gs_level + 1] = pdfw_gs[pdfw_gs_level]; ++pdfw_gs_level; HPDF_Page_GSave(page); }
JNIEXPORT void JNICALL Java_com_draekko_libharu_PdfPage_gSave (JNIEnv *env, jobject obj) { haru_setup_error_handler(env, __func__); HPDF_Page page = get_HPDF_Page(env, obj); HPDF_Page_GSave(page); haru_clear_error_handler(); }
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 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_); }
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::setChanged(WFlags<PainterChangeFlag> flags) { if (!flags.empty()) { HPDF_Page_GRestore(page_); HPDF_Page_GSave(page_); HPDF_ExtGState gstate; gstate = HPDF_CreateExtGState (pdf_); currentFont_ = WFont(); if (painter()->hasClipping()) { const WTransform& t = painter()->clipPathTransform(); if (!painter()->clipPath().isEmpty()) { applyTransform(t); drawPlainPath(painter()->clipPath()); HPDF_Page_Clip(page_); HPDF_Page_EndPath(page_); applyTransform(t.inverted()); } } applyTransform(painter()->combinedTransform()); const WPen& pen = painter()->pen(); if (pen.style() != PenStyle::None) { const WColor& color = pen.color(); HPDF_Page_SetRGBStroke(page_, color.red() / 255., color.green() / 255., color.blue() / 255.); HPDF_ExtGState_SetAlphaStroke (gstate, color.alpha()/255.); WLength w = painter()->normalizedPenWidth(pen.width(), false); HPDF_Page_SetLineWidth(page_, w.toPixels()); switch (pen.capStyle()) { case PenCapStyle::Flat: HPDF_Page_SetLineCap(page_, HPDF_BUTT_END); break; case PenCapStyle::Square: HPDF_Page_SetLineCap(page_, HPDF_PROJECTING_SCUARE_END); // scuary ! break; case PenCapStyle::Round: HPDF_Page_SetLineCap(page_, HPDF_ROUND_END); break; } switch (pen.joinStyle()) { case PenJoinStyle::Miter: HPDF_Page_SetLineJoin(page_, HPDF_MITER_JOIN); break; case PenJoinStyle::Bevel: HPDF_Page_SetLineJoin(page_, HPDF_BEVEL_JOIN); break; case PenJoinStyle::Round: HPDF_Page_SetLineJoin(page_, HPDF_ROUND_JOIN); break; } switch (pen.style()) { case PenStyle::None: break; case PenStyle::SolidLine: HPDF_Page_SetDash(page_, nullptr, 0, 0); break; case PenStyle::DashLine: { const HPDF_UINT16 dash_ptn[] = { 4, 2 }; HPDF_Page_SetDash(page_, dash_ptn, 2, 0); break; } case PenStyle::DotLine: { const HPDF_UINT16 dash_ptn[] = { 1, 2 }; HPDF_Page_SetDash(page_, dash_ptn, 2, 0); break; } case PenStyle::DashDotLine: { const HPDF_UINT16 dash_ptn[] = { 4, 2, 1, 2 }; HPDF_Page_SetDash(page_, dash_ptn, 4, 0); break; } case PenStyle::DashDotDotLine: { const HPDF_UINT16 dash_ptn[] = { 4, 2, 1, 2, 1, 2 }; HPDF_Page_SetDash(page_, dash_ptn, 6, 0); break; } } } const WBrush& brush = painter()->brush(); if (brush.style() != BrushStyle::None) { const WColor& color = painter()->brush().color(); HPDF_Page_SetRGBFill(page_, color.red() / 255., color.green() / 255., color.blue() / 255.); HPDF_ExtGState_SetAlphaFill (gstate, color.alpha()/255.); } HPDF_Page_SetExtGState (page_, gstate); const WFont& font = painter()->font(); if (font == currentFont_ && !trueTypeFonts_->busy()) return; /* * First, try a true type font. */ std::string ttfFont; if (trueTypeFonts_->busy()) { /* * We have a resolved true type font. */ ttfFont = trueTypeFonts_->drawingFontPath(); } else { FontSupport::FontMatch match = trueTypeFonts_->matchFont(font); if (match.matched()) ttfFont = match.fileName(); } LOG_DEBUG("font: " << ttfFont); if (font == currentFont_ && !ttfFont.empty() && currentTtfFont_ == ttfFont) return; currentFont_ = font; const char *font_name = nullptr; font_ = nullptr; if (!ttfFont.empty()) { bool fontOk = false; std::map<std::string, const char *>::const_iterator i = ttfFonts_.find(ttfFont); if (i != ttfFonts_.end()) { font_name = i->second; fontOk = true; } else if (ttfFont.length() > 4) { std::string suffix = Utils::lowerCase(ttfFont.substr(ttfFont.length() - 4)); if (suffix == ".ttf") { font_name = HPDF_LoadTTFontFromFile (pdf_, ttfFont.c_str(), HPDF_TRUE); } else if (suffix == ".ttc") { /* Oops, pango didn't tell us which font to load ... */ font_name = HPDF_LoadTTFontFromFile2(pdf_, ttfFont.c_str(), 0, HPDF_TRUE); } if (!font_name) HPDF_ResetError (pdf_); else { ttfFonts_[ttfFont] = font_name; fontOk = true; } } if (!fontOk) LOG_ERROR("cannot read font: '" << ttfFont << "': " "expecting a true type font (.ttf, .ttc)"); } if (!font_ && font_name) { font_ = HPDF_GetFont (pdf_, font_name, "UTF-8"); if (!font_) HPDF_ResetError (pdf_); else { trueTypeFont_ = true; currentTtfFont_ = ttfFont; } } if (!font_) { trueTypeFont_ = false; currentTtfFont_.clear(); std::string name = Pdf::toBase14Font(font); font_ = HPDF_GetFont(pdf_, name.c_str(), nullptr); } fontSize_ = font.sizeLength(12).toPixels(); HPDF_Page_SetFontAndSize (page_, font_, fontSize_); } }
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_); } }
int main (int argc, char **argv) { HPDF_Doc pdf; HPDF_Page page; char fname[256]; HPDF_Font hfont; HPDF_ExtGState gstate; const HPDF_REAL PAGE_WIDTH = 600; const HPDF_REAL PAGE_HEIGHT = 900; strcpy (fname, argv[0]); strcat (fname, ".pdf"); pdf = HPDF_New (error_handler, NULL); if (!pdf) { printf ("error: cannot create PdfDoc object\n"); return 1; } if (setjmp(env)) { HPDF_Free (pdf); return 1; } hfont = HPDF_GetFont (pdf, "Helvetica-Bold", NULL); /* add a new page object. */ page = HPDF_AddPage (pdf); HPDF_Page_SetFontAndSize (page, hfont, 10); HPDF_Page_SetHeight (page, PAGE_HEIGHT); HPDF_Page_SetWidth (page, PAGE_WIDTH); /* normal */ HPDF_Page_GSave (page); draw_circles (page, "normal", 40.0f, PAGE_HEIGHT - 170); HPDF_Page_GRestore (page); /* transparency (0.8) */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetAlphaFill (gstate, 0.8); HPDF_ExtGState_SetAlphaStroke (gstate, 0.8); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "alpha fill = 0.8", 230.0f, PAGE_HEIGHT - 170); HPDF_Page_GRestore (page); /* transparency (0.4) */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetAlphaFill (gstate, 0.4); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "alpha fill = 0.4", 420.0f, PAGE_HEIGHT - 170); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_MULTIPLY */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_MULTIPLY); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_MULTIPLY", 40.0f, PAGE_HEIGHT - 340); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_SCREEN */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_SCREEN); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_SCREEN", 230.0f, PAGE_HEIGHT - 340); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_OVERLAY */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_OVERLAY); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_OVERLAY", 420.0f, PAGE_HEIGHT - 340); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_DARKEN */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_DARKEN); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_DARKEN", 40.0f, PAGE_HEIGHT - 510); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_LIGHTEN */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_LIGHTEN); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_LIGHTEN", 230.0f, PAGE_HEIGHT - 510); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_COLOR_DODGE */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_COLOR_DODGE); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_COLOR_DODGE", 420.0f, PAGE_HEIGHT - 510); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_COLOR_BUM */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_COLOR_BUM); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_COLOR_BUM", 40.0f, PAGE_HEIGHT - 680); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_HARD_LIGHT */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_HARD_LIGHT); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_HARD_LIGHT", 230.0f, PAGE_HEIGHT - 680); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_SOFT_LIGHT */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_SOFT_LIGHT); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_SOFT_LIGHT", 420.0f, PAGE_HEIGHT - 680); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_DIFFERENCE */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_DIFFERENCE); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_DIFFERENCE", 40.0f, PAGE_HEIGHT - 850); HPDF_Page_GRestore (page); /* blend-mode=HPDF_BM_EXCLUSHON */ HPDF_Page_GSave (page); gstate = HPDF_CreateExtGState (pdf); HPDF_ExtGState_SetBlendMode (gstate, HPDF_BM_EXCLUSHON); HPDF_Page_SetExtGState (page, gstate); draw_circles (page, "HPDF_BM_EXCLUSHON", 230.0f, PAGE_HEIGHT - 850); HPDF_Page_GRestore (page); /* save the document to a file */ HPDF_SaveToFile (pdf, fname); /* clean up */ HPDF_Free (pdf); return 0; }
int main (int argc, char **argv) { const char *page_title = "Text Demo"; HPDF_Doc pdf; HPDF_Font font; HPDF_Page page; char fname[256]; const char* samp_text = "abcdefgABCDEFG123!#$%&+-@?"; const char* samp_text2 = "The quick brown fox jumps over the lazy dog."; float tw; float fsize; int i; int len; float angle1; float angle2; float rad1; float rad2; float ypos; strcpy (fname, argv[0]); strcat (fname, ".pdf"); pdf = HPDF_New (error_handler, NULL); if (!pdf) { printf ("error: cannot create PdfDoc object\n"); return 1; } if (setjmp(env)) { HPDF_Free (pdf); return 1; } /* set compression mode */ HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL); /* create default-font */ font = HPDF_GetFont (pdf, "Helvetica", NULL); /* add a new page object. */ page = HPDF_AddPage (pdf); /* draw grid to the page */ print_grid (pdf, page); /* print the lines of the page. HPDF_Page_SetLineWidth (page, 1); HPDF_Page_Rectangle (page, 50, 50, HPDF_Page_GetWidth(page) - 100, HPDF_Page_GetHeight (page) - 110); HPDF_Page_Stroke (page); */ /* print the title of the page (with positioning center). */ HPDF_Page_SetFontAndSize (page, font, 24); tw = HPDF_Page_TextWidth (page, page_title); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, (HPDF_Page_GetWidth(page) - tw) / 2, HPDF_Page_GetHeight (page) - 50, page_title); HPDF_Page_EndText (page); HPDF_Page_BeginText (page); HPDF_Page_MoveTextPos (page, 60, HPDF_Page_GetHeight(page) - 60); /* * font size */ fsize = 8; while (fsize < 60) { char buf[50]; int len; /* set style and size of font. */ HPDF_Page_SetFontAndSize(page, font, fsize); /* set the position of the text. */ HPDF_Page_MoveTextPos (page, 0, -5 - fsize); /* measure the number of characters which included in the page. */ strcpy(buf, samp_text); len = HPDF_Page_MeasureText (page, samp_text, HPDF_Page_GetWidth(page) - 120, HPDF_FALSE, NULL); /* truncate the text. */ buf[len] = 0x00; HPDF_Page_ShowText (page, buf); /* print the description. */ HPDF_Page_MoveTextPos (page, 0, -10); HPDF_Page_SetFontAndSize(page, font, 8); #ifdef __WIN32__ _snprintf(buf, 50, "Fontsize=%.0f", fsize); #else snprintf(buf, 50, "Fontsize=%.0f", fsize); #endif HPDF_Page_ShowText (page, buf); fsize *= 1.5; } /* * font color */ HPDF_Page_SetFontAndSize(page, font, 8); HPDF_Page_MoveTextPos (page, 0, -30); HPDF_Page_ShowText (page, "Font color"); HPDF_Page_SetFontAndSize (page, font, 18); HPDF_Page_MoveTextPos (page, 0, -20); len = strlen (samp_text); for (i = 0; i < len; i++) { char buf[2]; float r = (float)i / (float)len; float g = 1 - ((float)i / (float)len); buf[0] = samp_text[i]; buf[1] = 0x00; HPDF_Page_SetRGBFill (page, r, g, 0.0); HPDF_Page_ShowText (page, buf); } HPDF_Page_MoveTextPos (page, 0, -25); for (i = 0; i < len; i++) { char buf[2]; float r = (float)i / (float)len; float b = 1 - ((float)i / (float)len); buf[0] = samp_text[i]; buf[1] = 0x00; HPDF_Page_SetRGBFill (page, r, 0.0, b); HPDF_Page_ShowText (page, buf); } HPDF_Page_MoveTextPos (page, 0, -25); for (i = 0; i < len; i++) { char buf[2]; float b = (float)i / (float)len; float g = 1 - ((float)i / (float)len); buf[0] = samp_text[i]; buf[1] = 0x00; HPDF_Page_SetRGBFill (page, 0.0, g, b); HPDF_Page_ShowText (page, buf); } HPDF_Page_EndText (page); ypos = 450; /* * Font rendering mode */ HPDF_Page_SetFontAndSize(page, font, 32); HPDF_Page_SetRGBFill (page, 0.5, 0.5, 0.0); HPDF_Page_SetLineWidth (page, 1.5); /* PDF_FILL */ show_description (page, 60, ypos, "RenderingMode=PDF_FILL"); HPDF_Page_SetTextRenderingMode (page, HPDF_FILL); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, 60, ypos, "ABCabc123"); HPDF_Page_EndText (page); /* PDF_STROKE */ show_description (page, 60, ypos - 50, "RenderingMode=PDF_STROKE"); HPDF_Page_SetTextRenderingMode (page, HPDF_STROKE); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, 60, ypos - 50, "ABCabc123"); HPDF_Page_EndText (page); /* PDF_FILL_THEN_STROKE */ show_description (page, 60, ypos - 100, "RenderingMode=PDF_FILL_THEN_STROKE"); HPDF_Page_SetTextRenderingMode (page, HPDF_FILL_THEN_STROKE); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, 60, ypos - 100, "ABCabc123"); HPDF_Page_EndText (page); /* PDF_FILL_CLIPPING */ show_description (page, 60, ypos - 150, "RenderingMode=PDF_FILL_CLIPPING"); HPDF_Page_GSave (page); HPDF_Page_SetTextRenderingMode (page, HPDF_FILL_CLIPPING); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, 60, ypos - 150, "ABCabc123"); HPDF_Page_EndText (page); show_stripe_pattern (page, 60, ypos - 150); HPDF_Page_GRestore (page); /* PDF_STROKE_CLIPPING */ show_description (page, 60, ypos - 200, "RenderingMode=PDF_STROKE_CLIPPING"); HPDF_Page_GSave (page); HPDF_Page_SetTextRenderingMode (page, HPDF_STROKE_CLIPPING); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, 60, ypos - 200, "ABCabc123"); HPDF_Page_EndText (page); show_stripe_pattern (page, 60, ypos - 200); HPDF_Page_GRestore (page); /* PDF_FILL_STROKE_CLIPPING */ show_description (page, 60, ypos - 250, "RenderingMode=PDF_FILL_STROKE_CLIPPING"); HPDF_Page_GSave (page); HPDF_Page_SetTextRenderingMode (page, HPDF_FILL_STROKE_CLIPPING); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, 60, ypos - 250, "ABCabc123"); HPDF_Page_EndText (page); show_stripe_pattern (page, 60, ypos - 250); HPDF_Page_GRestore (page); /* Reset text attributes */ HPDF_Page_SetTextRenderingMode (page, HPDF_FILL); HPDF_Page_SetRGBFill (page, 0, 0, 0); HPDF_Page_SetFontAndSize(page, font, 30); /* * Rotating text */ angle1 = 30; /* A rotation of 30 degrees. */ rad1 = angle1 / 180 * 3.141592; /* Calcurate the radian value. */ show_description (page, 320, ypos - 60, "Rotating text"); HPDF_Page_BeginText (page); HPDF_Page_SetTextMatrix (page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), 330, ypos - 60); HPDF_Page_ShowText (page, "ABCabc123"); HPDF_Page_EndText (page); /* * Skewing text. */ show_description (page, 320, ypos - 120, "Skewing text"); HPDF_Page_BeginText (page); angle1 = 10; angle2 = 20; rad1 = angle1 / 180 * 3.141592; rad2 = angle2 / 180 * 3.141592; HPDF_Page_SetTextMatrix (page, 1, tan(rad1), tan(rad2), 1, 320, ypos - 120); HPDF_Page_ShowText (page, "ABCabc123"); HPDF_Page_EndText (page); /* * scaling text (X direction) */ show_description (page, 320, ypos - 175, "Scaling text (X direction)"); HPDF_Page_BeginText (page); HPDF_Page_SetTextMatrix (page, 1.5, 0, 0, 1, 320, ypos - 175); HPDF_Page_ShowText (page, "ABCabc12"); HPDF_Page_EndText (page); /* * scaling text (Y direction) */ show_description (page, 320, ypos - 250, "Scaling text (Y direction)"); HPDF_Page_BeginText (page); HPDF_Page_SetTextMatrix (page, 1, 0, 0, 2, 320, ypos - 250); HPDF_Page_ShowText (page, "ABCabc123"); HPDF_Page_EndText (page); /* * char spacing, word spacing */ show_description (page, 60, 140, "char-spacing 0"); show_description (page, 60, 100, "char-spacing 1.5"); show_description (page, 60, 60, "char-spacing 1.5, word-spacing 2.5"); HPDF_Page_SetFontAndSize (page, font, 20); HPDF_Page_SetRGBFill (page, 0.1, 0.3, 0.1); /* char-spacing 0 */ HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, 60, 140, samp_text2); HPDF_Page_EndText (page); /* char-spacing 1.5 */ HPDF_Page_SetCharSpace (page, 1.5); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, 60, 100, samp_text2); HPDF_Page_EndText (page); /* char-spacing 1.5, word-spacing 3.5 */ HPDF_Page_SetWordSpace (page, 2.5); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, 60, 60, samp_text2); HPDF_Page_EndText (page); /* save the document to a file */ HPDF_SaveToFile (pdf, fname); /* clean up */ HPDF_Free (pdf); return 0; }