JNIEXPORT void JNICALL Java_com_draekko_libharu_PdfPage_setRGBStroke (JNIEnv *env, jobject obj, jfloat red, jfloat green, jfloat blue) { haru_setup_error_handler(env, __func__); HPDF_Page page = get_HPDF_Page(env, obj); HPDF_Page_SetRGBStroke(page, red, green, blue); haru_clear_error_handler(); }
/* * Class: org_libharu_PdfPage * Method: setRGBStroke * Signature: (FFF)V */ JNIEXPORT void JNICALL Java_org_libharu_PdfPage_setRGBStroke(JNIEnv *env, jobject obj, jfloat red, jfloat green, jfloat blue) { jint page; /* Get mHPDFPagePointer */ page = (*env)->GetIntField(env, obj, mHPDFPagePointer); HPDF_Page_SetRGBStroke((HPDF_Page) page, (HPDF_REAL) red, (HPDF_REAL) green, (HPDF_REAL) blue); }
/** * Checks if given stroke colour is already set in PDF gstate and if not, * update the gstate accordingly. * \param page PDF page where the update needs to happen. * \param col Wanted stroke colour. */ void pdfw_gs_strokecolour(HPDF_Page page, colour col) { if (col == pdfw_gs[pdfw_gs_level].strokeColour) return; pdfw_gs[pdfw_gs_level].strokeColour = col; if (RBYTE(col) == GBYTE(col) && GBYTE(col) == BBYTE(col)) HPDF_Page_SetGrayStroke(pdf_page, R(col)); else HPDF_Page_SetRGBStroke(pdf_page, R(col), G(col), B(col)); }
void show_stripe_pattern (HPDF_Page page, HPDF_REAL x, HPDF_REAL y) { HPDF_UINT iy = 0; while (iy < 50) { HPDF_Page_SetRGBStroke (page, 0.0, 0.0, 0.5); HPDF_Page_SetLineWidth (page, 1); HPDF_Page_MoveTo (page, x, y + iy); HPDF_Page_LineTo (page, x + HPDF_Page_TextWidth (page, "ABCabc123"), y + iy); HPDF_Page_Stroke (page); iy += 3; } HPDF_Page_SetLineWidth (page, 2.5); }
void draw_circles (HPDF_Page page, const char *description, HPDF_REAL x, HPDF_REAL y) { HPDF_Page_SetLineWidth (page, 1.0f); HPDF_Page_SetRGBStroke (page, 0.0f, 0.0f, 0.0f); HPDF_Page_SetRGBFill (page, 1.0f, 0.0f, 0.0f); HPDF_Page_Circle (page, x + 40, y + 40, 40); HPDF_Page_ClosePathFillStroke (page); HPDF_Page_SetRGBFill (page, 0.0f, 1.0f, 0.0f); HPDF_Page_Circle (page, x + 100, y + 40, 40); HPDF_Page_ClosePathFillStroke (page); HPDF_Page_SetRGBFill (page, 0.0f, 0.0f, 1.0f); HPDF_Page_Circle (page, x + 70, y + 74.64, 40); HPDF_Page_ClosePathFillStroke (page); HPDF_Page_SetRGBFill (page, 0.0f, 0.0f, 0.0f); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, x + 0.0f, y + 130.0f, description); HPDF_Page_EndText (page); }
int fileWriter(HPDF_Doc pdf, HPDF_Outline root, HPDF_Font font, const std::string curFile, const std::vector< std::string> vec) { auto func_footbar = [](std::string fileName)->std::string { std::ostringstream buffer; buffer << " File:" << fileName.c_str() << " Autor:XCL "; return buffer.str(); }; auto func_pageOutline = [&](HPDF_Page page, std::string curFile) { HPDF_Outline outline; HPDF_Destination dst; outline = HPDF_CreateOutline(pdf, root, curFile.c_str(), NULL); dst = HPDF_Page_CreateDestination(page); HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page), 1);// 1 = 默认百分比100 HPDF_Outline_SetDestination(outline, dst); }; auto func_drawRow = [](HPDF_Page page,std::string rid,std::string row,int y ) { HPDF_Page_BeginText(page); HPDF_Page_MoveTextPos(page, config::ROWID_WIDTH, y - 2); HPDF_Page_ShowText(page, rid.c_str()); HPDF_Page_ShowText(page, row.c_str()); HPDF_Page_EndText(page); }; HPDF_Page page = nullptr; HPDF_REAL width,height; HPDF_UINT y = 0; // x =0, int currID = 0, pageID = 0, currRowCount; std::string ss, rid; for (int i = 0; i < vec.size(); i++) { if (i == 0 || currRowCount > config::PAGE_ROWNUM) { ////////////////////////////////////////////// page = HPDF_AddPage(pdf); width = HPDF_Page_GetWidth(page); height = HPDF_Page_GetHeight(page); // x = 0; y = height - config::ROW_HEIGHT; func_pageOutline(page, curFile); HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT); { HPDF_RGBColor c = HPDF_Page_GetRGBFill(page); HPDF_Page_SetRGBStroke(page, 0.0, 0.5, 0); HPDF_Page_SetFontAndSize(page, font, config::FOOTBAR_FONT_SIZE); HPDF_Page_SetLineWidth(page, 0.5); HPDF_Page_MoveTo(page, 0, 13); HPDF_Page_LineTo(page, width, 16); HPDF_Page_Stroke(page); HPDF_Page_SetRGBFill(page, 0.0, 0, 0.5); HPDF_Page_BeginText(page); HPDF_Page_MoveTextPos(page, 40, config::FOOTBAR_FONT_SIZE - 5); HPDF_Page_ShowText(page, func_footbar(curFile).c_str()); HPDF_Page_EndText(page); HPDF_Page_SetRGBFill(page, c.r, c.g, c.b); }; y = height - config::ROW_HEIGHT * 2; HPDF_Page_SetFontAndSize(page, font, config::PAGE_FONT_SIZE); pageID++; currRowCount = 1; ////////////////////////////////////////////// } /////////////////////////////////////////// ss = vec[i]; HPDF_REAL wrid = HPDF_Page_TextWidth(page, rid.c_str()); NEWROW: HPDF_REAL wss = HPDF_Page_TextWidth(page, ss.c_str()); rid = util::rowidWidth(currRowCount ); if (wrid + wss > width) { //简单处理:超过页宽,直接分成两行.足以应付大部份情况. int center = ss.length() / 2; func_drawRow(page, rid,ss.substr(0, center),y); y -= config::ROW_HEIGHT; currRowCount++; ss = ss.substr(center); goto NEWROW; } else { func_drawRow(page, rid, ss,y); y -= config::ROW_HEIGHT; currRowCount++; } ////////////////////////////////////////// } return 0; } //end func
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 generate_pdf (const char *outname, const char *tmpdirname, int num_fonts, int num_input_files, const JBDATA * data, const struct mapping *maps, int debug_draw_borders) { int i, j; /* Create the pdf document */ l_int32 ncomp = numaGetCount (data->naclass); HPDF_Doc pdf = HPDF_New (pdf_error_handler, NULL); HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL); HPDF_Font *fonts = malloc_guarded (num_fonts * (sizeof (HPDF_Font))); int dirlen = strlen (tmpdirname); /* Load the fonts */ for (i = 0; i < num_fonts; i++) { /* 1 for '/', 8 for %08d, 4 for '.ttf' */ int fontlen = dirlen + 1 + 8 + 4; char *font_tfname = malloc_guarded (fontlen + 1); sprintf (font_tfname, "%s/%08d.ttf", tmpdirname, i); const char *font_name = HPDF_LoadTTFontFromFile (pdf, font_tfname, HPDF_TRUE); fonts[i] = HPDF_GetFont (pdf, font_name, "KOI8-R"); free (font_tfname); } for (j = 0; j < num_input_files; j++) { /* Add page to document */ HPDF_Page pg = HPDF_AddPage (pdf); HPDF_Page_SetWidth (pg, data->w); HPDF_Page_SetHeight (pg, data->h); /* TODO: Boost efficiency by making startcomp go to the next page on the next iteration */ int start_comp = 0; for (i = start_comp; i < ncomp; i++) { l_int32 ipage; l_int32 iclass; l_int32 x; l_int32 y; numaGetIValue (data->napage, i, &ipage); if (ipage != j) continue; numaGetIValue (data->naclass, i, &iclass); ptaGetIPt (data->ptaul, i, &x, &y); /*double left = x; double top = data->h - y; double right = x + data->latticew; double bottom = data->h - (y + data->latticeh); */ char text[2]; text[0] = maps[iclass].code_point; text[1] = '\0'; HPDF_Font font = fonts[maps[iclass].font_num]; HPDF_Page_BeginText (pg); double fontsize = 100; if (fontsize > 300) { error_quit ("This is a known bug.\n" "libharu can't handle fontsizes bigger than 300, which is what is being requested here.\n" "Please report this bug, and the file that produced this error"); } HPDF_Page_SetFontAndSize (pg, font, 100); HPDF_Page_MoveTextPos (pg, x, (data->h - y) - data->latticeh); HPDF_Page_ShowText (pg, text); HPDF_Page_EndText (pg); if (debug_draw_borders) { HPDF_Page_SetRGBStroke (pg, 1, 0, 0); /* In this, x, y is the LOWER LEFT, not UPPER LEFT */ HPDF_Page_Rectangle (pg, x, (data->h - y) - data->latticeh, data->latticew, data->latticeh); HPDF_Page_Stroke (pg); } } } /* Output */ HPDF_SaveToFile (pdf, outname); /* Cleanup */ HPDF_Free (pdf); free (fonts); }