void draw_graph (HPDF_Page page) { char buf[50]; int i; /* Draw 16 X 15 cells */ /* Draw vertical lines. */ HPDF_Page_SetLineWidth (page, 0.5); for (i = 0; i <= 17; i++) { int x = i * CELL_WIDTH + 40; HPDF_Page_MoveTo (page, x, PAGE_HEIGHT - 60); HPDF_Page_LineTo (page, x, 40); HPDF_Page_Stroke (page); if (i > 0 && i <= 16) { HPDF_Page_BeginText (page); HPDF_Page_MoveTextPos (page, x + 5, PAGE_HEIGHT - 75); #ifdef __WIN32__ _snprintf(buf, 5, "%X", i - 1); #else snprintf(buf, 5, "%X", i - 1); #endif HPDF_Page_ShowText (page, buf); HPDF_Page_EndText (page); } } /* Draw horizontal lines. */ for (i = 0; i <= 15; i++) { int y = i * CELL_HEIGHT + 40; HPDF_Page_MoveTo (page, 40, y); HPDF_Page_LineTo (page, PAGE_WIDTH - 40, y); HPDF_Page_Stroke (page); if (i < 14) { HPDF_Page_BeginText (page); HPDF_Page_MoveTextPos (page, 45, y + 5); #ifdef __WIN32__ _snprintf(buf, 5, "%X", 15 - i); #else snprintf(buf, 5, "%X", 15 - i); #endif HPDF_Page_ShowText (page, buf); HPDF_Page_EndText (page); } } }
bool pdf_plot_polygon(const int *p, unsigned int n, const plot_style_t *style) { unsigned int i; #ifdef PDF_DEBUG int pmaxx = p[0], pmaxy = p[1]; int pminx = p[0], pminy = p[1]; NSLOG(netsurf, INFO, "."); #endif if (n == 0) return true; apply_clip_and_mode(false, style->fill_colour, NS_TRANSPARENT, 0., DashPattern_eNone); HPDF_Page_MoveTo(pdf_page, p[0], page_height - p[1]); for (i = 1 ; i<n ; i++) { HPDF_Page_LineTo(pdf_page, p[i*2], page_height - p[i*2+1]); #ifdef PDF_DEBUG pmaxx = max(pmaxx, p[i*2]); pmaxy = max(pmaxy, p[i*2+1]); pminx = min(pminx, p[i*2]); pminy = min(pminy, p[i*2+1]); #endif } #ifdef PDF_DEBUG NSLOG(netsurf, INFO, "%d %d %d %d %f", pminx, pminy, pmaxx, pmaxy, page_height - pminy); #endif HPDF_Page_Fill(pdf_page); return true; }
bool pdf_plot_line(int x0, int y0, int x1, int y1, const plot_style_t *pstyle) { DashPattern_e dash; switch (pstyle->stroke_type) { case PLOT_OP_TYPE_DOT: dash = DashPattern_eDotted; break; case PLOT_OP_TYPE_DASH: dash = DashPattern_eDash; break; default: dash = DashPattern_eNone; break; } apply_clip_and_mode(false, NS_TRANSPARENT, pstyle->stroke_colour, plot_style_int_to_fixed(pstyle->stroke_width), dash); HPDF_Page_MoveTo(pdf_page, x0, page_height - y0); HPDF_Page_LineTo(pdf_page, x1, page_height - y1); HPDF_Page_Stroke(pdf_page); return true; }
/* * Class: org_libharu_PdfPage * Method: moveTo * Signature: (FF)V */ JNIEXPORT void JNICALL Java_org_libharu_PdfPage_moveTo(JNIEnv *env, jobject obj, jfloat x, jfloat y) { jint page; /* Get mHPDFPagePointer */ page = (*env)->GetIntField(env, obj, mHPDFPagePointer); HPDF_Page_MoveTo((HPDF_Page) page, (HPDF_REAL) x, (HPDF_REAL) y); }
JNIEXPORT void JNICALL Java_org_libharu_Page_moveTo (JNIEnv *env, jobject obj, jfloat x, jfloat y) { haru_setup_error_handler(env, __func__); HPDF_Page page = get_HPDF_Page(env, obj); HPDF_Page_MoveTo(page, x, y); haru_clear_error_handler(); }
void WPdfImage::drawLine(double x1, double y1, double x2, double y2) { if (painter()->pen().style() != PenStyle::None) { HPDF_Page_MoveTo(page_, x1, y1); HPDF_Page_LineTo(page_, x2, y2); HPDF_Page_Stroke(page_); } }
void hpdf_doc::polygon(HPDF_Point pt[], int count) { if (count <= 0) return; HPDF_Page_MoveTo(h_current_page, pt[0].x, pt[0].y); for (int i = 1; i < count; i++) { HPDF_Page_LineTo(h_current_page, pt[i].x, pt[i].y); } HPDF_Page_LineTo(h_current_page, pt[0].x, pt[0].y); HPDF_Page_Fill(h_current_page); }
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); }
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
bool pdf_plot_path(const float *p, unsigned int n, colour fill, float width, colour c, const float transform[6]) { unsigned int i; bool empty_path; #ifdef PDF_DEBUG NSLOG(netsurf, INFO, "."); #endif if (n == 0) return true; if (c == NS_TRANSPARENT && fill == NS_TRANSPARENT) return true; if (p[0] != PLOTTER_PATH_MOVE) return false; apply_clip_and_mode(false, fill, c, width, DashPattern_eNone); empty_path = true; for (i = 0 ; i < n ; ) { if (p[i] == PLOTTER_PATH_MOVE) { HPDF_Page_MoveTo(pdf_page, transform_x(transform, p[i+1], p[i+2]), transform_y(transform, p[i+1], p[i+2])); i+= 3; } else if (p[i] == PLOTTER_PATH_CLOSE) { if (!empty_path) HPDF_Page_ClosePath(pdf_page); i++; } else if (p[i] == PLOTTER_PATH_LINE) { HPDF_Page_LineTo(pdf_page, transform_x(transform, p[i+1], p[i+2]), transform_y(transform, p[i+1], p[i+2])); i+=3; empty_path = false; } else if (p[i] == PLOTTER_PATH_BEZIER) { HPDF_Page_CurveTo(pdf_page, transform_x(transform, p[i+1], p[i+2]), transform_y(transform, p[i+1], p[i+2]), transform_x(transform, p[i+3], p[i+4]), transform_y(transform, p[i+3], p[i+4]), transform_x(transform, p[i+5], p[i+6]), transform_y(transform, p[i+5], p[i+6])); i += 7; empty_path = false; } else { NSLOG(netsurf, INFO, "bad path command %f", p[i]); return false; } } if (empty_path) { HPDF_Page_EndPath(pdf_page); return true; } if (fill != NS_TRANSPARENT) { if (c != NS_TRANSPARENT) HPDF_Page_FillStroke(pdf_page); else HPDF_Page_Fill(pdf_page); } else HPDF_Page_Stroke(pdf_page); return true; }
void WPdfImage::drawPlainPath(const WPainterPath& path) { if (path.isEmpty()) return; const std::vector<WPainterPath::Segment>& segments = path.segments(); if (segments.size() > 0 && segments[0].type() != SegmentType::MoveTo) HPDF_Page_MoveTo(page_, 0, 0); for (unsigned i = 0; i < segments.size(); ++i) { const WPainterPath::Segment s = segments[i]; switch (s.type()) { case SegmentType::MoveTo: HPDF_Page_MoveTo(page_, s.x(), s.y()); break; case SegmentType::LineTo: HPDF_Page_LineTo(page_, s.x(), s.y()); break; case SegmentType::CubicC1: { const double x1 = s.x(); const double y1 = s.y(); const double x2 = segments[i+1].x(); const double y2 = segments[i+1].y(); const double x3 = segments[i+2].x(); const double y3 = segments[i+2].y(); HPDF_Page_CurveTo(page_, x1, y1, x2, y2, x3, y3); i += 2; break; } case SegmentType::CubicC2: case SegmentType::CubicEnd: assert(false); case SegmentType::ArcC: { const double x = s.x(); const double y = s.y(); const double radius = segments[i+1].x(); double ang1 = segments[i+2].x(); double ang2 = ang1 + segments[i+2].y(); HPDF_Page_Arc(page_, x, y, radius, ang1 + 90, ang2 + 90); i += 2; break; } case SegmentType::ArcR: case SegmentType::ArcAngleSweep: assert(false); case SegmentType::QuadC: { const double x1 = s.x(); const double y1 = s.y(); const double x2 = segments[i+1].x(); const double y2 = segments[i+1].y(); // or CurveTo3 ?? Are these any different ?? HPDF_Page_CurveTo2(page_, x1, y1, x2, y2); i += 1; break; } case SegmentType::QuadEnd: assert(false); } } }
void hpdf_doc::add_text(et_datachunk &dc) { et_type datatype = dc.type; wstring &out_string = dc.w_string; char *line = new char[4096]; memset(line, 0, 4096); _locale_t loceng; size_t size = 0; loceng = _create_locale(LC_ALL, "en-US"); int len = out_string.length(); // count of space int n_TX = (et_cp.TX > 0) ? et_cp.TX : et_cp.T, n_TY = (et_cp.TY > 0) ? et_cp.TY : et_cp.T; HPDF_REAL f_xpos = dc.rect.left, f_ypos = dc.rect.bottom; HPDF_REAL f_advance = 0.0; HPDF_REAL f_width = MMTEXT2PTX(et_cp.W * n_TX / 2); HPDF_REAL f_gap = MMTEXT2PTX(et_cp.X/2); HPDF_REAL f_space = MMTEXT2PTY((et_cp.Z * n_TY) + et_cp.L); HPDF_Page_BeginText(h_current_page); select_datatype_font(datatype); f_space = HPDF_Page_GetCurrentFontSize(h_current_page); if (f_space > f_linespace) f_linespace = f_space; switch (datatype) { case ET_LATAN: /* size = _wcstombs_l(line, out_string.c_str(), 4096, loceng); if (size == 0) goto END_PROC; HPDF_Page_TextOut(h_current_page, f_xpos, f_ypos - f_linespace, line); f_advance = HPDF_Page_TextWidth(h_current_page, line); */ if (et_cp.CorE == 'C') f_width = f_width * 2; text_out_eng(f_xpos, f_ypos, out_string, f_advance, f_width, f_gap, f_space, loceng); break; case ET_SPACE: f_advance += ((f_width + f_gap) * len); break; case ET_CJK: case ET_CJKFORM: case ET_BOXDRAW: if (et_cp.VorH == 'H' || datatype != ET_CJK) horizontal(f_xpos, f_ypos); else vertical(f_xpos, f_ypos); if (datatype == ET_BOXDRAW) resize_font_boxdraw(); /* size = wchar_to_utf8(out_string.c_str(), out_string.length(), line, 4096, NULL); if (size == 0) goto END_PROC; HPDF_Page_TextOut(h_current_page, f_xpos, f_ypos - f_linespace, line); //if (datatype == ET_BOXDRAW) // f_advance += (len * ((f_width + f_gap) * 2)); //else f_advance += HPDF_Page_TextWidth(h_current_page, line); //(len * ((f_width + f_gap) * 2)); */ text_out_cjk(f_xpos, f_ypos, out_string, f_advance, f_width, f_gap, f_space); break; } HPDF_Page_EndText(h_current_page); if (et_cp.U > 0) { HPDF_Page_SetLineWidth(h_current_page, 0.5); HPDF_Page_MoveTo(h_current_page, f_xpos, f_ypos - f_linespace); HPDF_Page_LineTo(h_current_page, f_xpos, f_ypos); HPDF_Page_Stroke(h_current_page); } f_xpos += f_advance; delete [] line; // free buffer _free_locale(loceng); }
int write_pdf(int argc, char ** argv, FILE * pipe_in, FILE * pipe_out, FILE * pipe_err) { char * filename = argc > 1 ? argv[1] : "output.pdf"; if (argc == 1) { fprintf(stderr, "no filename specified, using '%s'\n", filename); } float b[3][2] = { {FLT_MAX, -FLT_MAX}, {FLT_MAX, -FLT_MAX}, {FLT_MAX, -FLT_MAX} }; HPDF_Doc pdf; pdf = HPDF_New(NULL, NULL); if (!pdf) { fprintf(stderr, "ERROR: cannot create pdf object.\n"); return 1; } HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL); HPDF_SetPageMode (pdf, HPDF_PAGE_MODE_USE_OUTLINE); int num_shapes = 0; struct Shape ** shapes = NULL; HPDF_Page page; page = HPDF_AddPage (pdf); HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_LETTER, HPDF_PAGE_PORTRAIT); long i = 0; struct Shape * shape = NULL; while ((shape = read_shape(pipe_in))) { num_shapes++; shapes = (struct Shape **)realloc(shapes, sizeof(struct Shape*)*num_shapes); if (shapes == NULL) { fprintf(stderr, "realloc for shapes failed :(\n"); exit(0); } shapes[num_shapes-1] = shape; long j; for (j = 0 ; j < shape->num_vertexs ; j++) { float * v = get_vertex(shape, 0, j); if (v[0] < b[0][0]) b[0][0] = v[0]; if (v[0] > b[0][1]) b[0][1] = v[0]; if (v[1] < b[1][0]) b[1][0] = v[1]; if (v[1] > b[1][1]) b[1][1] = v[1]; } } float dx = b[0][1] - b[0][0]; float dy = b[1][1] - b[1][0]; float ratio = dy / dx; HPDF_Page_SetWidth(page, 2000); HPDF_Page_SetHeight(page, 2000.0 * ratio); for (i = 0 ; i < num_shapes ; i++) { shape = shapes[i]; //if (shape->vertex_arrays[0].num_dimensions < 2) { fprintf(stderr, "\n"); continue; } long j; for (j = 0 ; j < shape->num_vertexs ; j++) { float * v = get_vertex(shape, 0, j); if (j == 0) HPDF_Page_MoveTo(page, (v[0] - b[0][0]) / dx * 2000.0, (v[1] - b[1][0]) / dy * (2000.0 * ratio)); else HPDF_Page_LineTo(page, (float)(v[0] - b[0][0]) / dx * 2000.0, (float)(v[1] - b[1][0]) / dy * (2000.0 * ratio)); } if (shape->gl_type == GL_LINE_LOOP) { float * v = get_vertex(shape, 0, 0); HPDF_Page_LineTo(page, (float)(v[0] - b[0][0]) / dx * 2000.0, (float)(v[1] - b[1][0]) / dy * (2000.0 * ratio)); } HPDF_Page_Stroke(page); free_shape(shape); } free(shapes); HPDF_SaveToFile (pdf, filename); HPDF_Free(pdf); }