/* ** cg_load_fonts() ** Load all specified fonts. */ yerr_t cg_load_fonts(cg_t *carta) { int i; cg_font_t *font; ystr_t s, param; char *pt; param = ys_new(""); for (i = 0; i < yv_len(carta->fonts); ++i) { font = carta->fonts[i]; s = ys_new(font->metrics); if ((pt = strrchr(s, '/'))) { *pt = '\0'; PDF_set_parameter(carta->p, "SearchPath", s); ys_printf(¶m, "%s=%s", font->id, pt + 1); PDF_set_parameter(carta->p, "FontAFM", param); } else { ys_printf(¶m, "%s=%s", font->id, font->metrics); PDF_set_parameter(carta->p, "FontAFM", param); } ys_printf(&s, font->outline); if ((pt = strrchr(s, '/'))) { *pt = '\0'; PDF_set_parameter(carta->p, "SearchPath", s); ys_printf(¶m, "%s=%s", font->id, pt + 1); PDF_set_parameter(carta->p, "FontOutline", param); } else { ys_printf(¶m, "%s=%s", font->id, font->outline); PDF_set_parameter(carta->p, "FontOutline", param); } ys_del(&s); /* font->f = PDF_load_font(carta->p, font->id, 0, "host", "embedding=true"); */ font->f = PDF_load_font(carta->p, font->id, 0, "iso8859-15", "embedding=true"); } ys_del(¶m); return (YENOERR); }
int main(int argc, char *argv[]) { char buf[BUFLEN], *s; char *pdffilename = NULL; FILE *textfile = stdin; PDF *p; int opt; int font; char *fontname, *encoding; double fontsize; double x, y, width = a4_width, height = a4_height, margin = 20; char ff, nl; fontname = "Courier"; fontsize = 12.0; encoding = "host"; nl = '\n'; ff = '\f'; while ((opt = getopt(argc, argv, "e:f:h:m:o:s:w:")) != -1) switch (opt) { case 'e': encoding = optarg; break; case 'f': fontname = optarg; break; case 'h': height = atof(optarg); if (height < 0) { fprintf(stderr, "Error: bad page height %f!\n", height); usage(); } break; case 'm': margin = atof(optarg); if (margin < 0) { fprintf(stderr, "Error: bad margin %f!\n", margin); usage(); } break; case 'o': pdffilename = optarg; break; case 's': fontsize = atof(optarg); if (fontsize < 0) { fprintf(stderr, "Error: bad font size %f!\n", fontsize); usage(); } break; case 'w': width = atof(optarg); if (width < 0) { fprintf(stderr, "Error: bad page width %f!\n", width); usage(); } break; case '?': default: usage(); } if (!strcmp(encoding, "ebcdic")) { /* form feed is 0x0C in both ASCII and EBCDIC */ nl = 0x15; } if (pdffilename == NULL) usage(); if (optind < argc) { if ((textfile = fopen(argv[optind], READMODE)) == NULL) { fprintf(stderr, "Error: cannot open input file %s.\n",argv[optind]); exit(2); } } else textfile = stdin; p = PDF_new(); if (p == NULL) { fprintf(stderr, "Error: cannot open output file %s.\n", pdffilename); exit(1); } PDF_begin_document(p, pdffilename, 0, ""); PDF_set_info(p, "Title", "Converted text"); PDF_set_info(p, "Creator", "text2pdf"); x = margin; y = height - margin; while ((s = fgets(buf, BUFLEN, textfile)) != NULL) { if (s[0] == ff) { if (y == height - margin) PDF_begin_page_ext(p, width, height, ""); PDF_end_page_ext(p, ""); y = height - margin; continue; } if (s[0] != '\0' && s[strlen(s) - 1] == nl) s[strlen(s) - 1] = '\0'; /* remove newline character */ if (y < margin) { /* page break necessary? */ y = height - margin; PDF_end_page_ext(p, ""); } if (y == height - margin) { PDF_begin_page_ext(p, width, height, ""); font = PDF_load_font(p, fontname, 0, encoding, ""); PDF_setfont(p, font, fontsize); PDF_set_text_pos(p, x, y); y -= fontsize; } PDF_continue_text(p, s); y -= fontsize; } if (y != height - margin) PDF_end_page_ext(p, ""); PDF_end_document(p, ""); PDF_delete(p); exit(0); }
int main(void) { PDF *p; int manual, page; int font, row, col; const int maxrow = 2; const int maxcol = 2; char optlist[128]; int startpage = 1, endpage = 4; const float width = 500, height = 770; int pageno; const char *infile = "reference.pdf"; /* This is where font/image/PDF input files live. Adjust as necessary. */ char *searchpath = "../data"; /* create a new PDFlib object */ if ((p = PDF_new()) == (PDF *) 0) { printf("Couldn't create PDFlib object (out of memory)!\n"); return(2); } PDF_TRY(p) { /* open new PDF file */ if (PDF_open_file(p, "quickreference.pdf") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(2); } PDF_set_parameter(p, "SearchPath", searchpath); /* This line is required to avoid problems on Japanese systems */ PDF_set_parameter(p, "hypertextencoding", "host"); PDF_set_info(p, "Creator", "quickreference.c"); PDF_set_info(p, "Author", "Thomas Merz"); PDF_set_info(p, "Title", "mini imposition demo (C)"); manual = PDF_open_pdi(p, infile, "", 0); if (manual == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(2); } row = 0; col = 0; PDF_set_parameter(p, "topdown", "true"); for (pageno = startpage; pageno <= endpage; pageno++) { if (row == 0 && col == 0) { PDF_begin_page(p, width, height); font = PDF_load_font(p, "Helvetica-Bold", 0, "host", ""); PDF_setfont(p, font, 18); PDF_set_text_pos(p, 24, 24); PDF_show(p, "PDFlib Quick Reference"); } page = PDF_open_pdi_page(p, manual, pageno, ""); if (page == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(2); } sprintf(optlist, "scale %f", (float) 1/maxrow); PDF_fit_pdi_page(p, page, width/maxcol*col, (row + 1) * height/maxrow, optlist); PDF_close_pdi_page(p, page); col++; if (col == maxcol) { col = 0; row++; } if (row == maxrow) { row = 0; PDF_end_page(p); } } /* finish the last partial page */ if (row != 0 || col != 0) PDF_end_page(p); PDF_close(p); PDF_close_pdi(p, manual); } PDF_CATCH(p) { printf("PDFlib exception occurred in quickreference sample:\n"); printf("[%d] %s: %s\n", PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_delete(p); return 0; }
int main(void) { /* This is where the data files are. Adjust as necessary.*/ const char * searchpath = "../data"; const char *targetname = "x5target.pdf"; PDF *p; char optlist[1024]; int font, proxy; double linewidth=2; double width, height; /* create a new PDFlib object */ if ((p = PDF_new()) == (PDF *) 0) { printf("Couldn't create PDFlib object (out of memory)!\n"); return(2); } PDF_TRY(p) { /* This means we must check return values of load_font() etc. */ PDF_set_parameter(p, "errorpolicy", "return"); PDF_set_parameter(p, "SearchPath", searchpath); if (PDF_begin_document(p, "starter_pdfx5g.pdf", 0, "pdfx=PDF/X-5g") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_set_info(p, "Creator", "PDFlib starter sample"); PDF_set_info(p, "Title", "starter_pdfx5g"); /* Open the output intent profile */ if (PDF_load_iccprofile(p, "ISOcoated.icc", 0, "usage=outputintent") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); printf("Please install the ICC profile package from " "www.pdflib.com to run the PDF/X-5g starter sample.\n"); PDF_delete(p); return(2); } /* Font embedding is required for PDF/X */ font = PDF_load_font(p, "LuciduxSans-Oblique", 0, "winansi", "embedding"); if (font == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } /* Create a template which will serve as proxy. The referenced * page (the target) is attached to the proxy. * The template width and height will be determined automatically, * so we don't have to supply them. */ sprintf(optlist, "reference={filename=%s pagenumber=1}", targetname); proxy = PDF_begin_template_ext(p, 0, 0, optlist); if (proxy == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } width = PDF_info_image(p, proxy, "imagewidth", ""); height = PDF_info_image(p, proxy, "imageheight", ""); /* Draw a crossed-out rectangle to visualize the proxy. * Attention: if we use the exact corner points, one half of the * linewidth would end up outside the template, and therefore be * clipped. */ PDF_setlinewidth(p, linewidth); PDF_moveto(p, linewidth/2, linewidth/2); PDF_lineto(p, width-linewidth/2, linewidth/2); PDF_lineto(p, width-linewidth/2, height-linewidth/2); PDF_lineto(p, linewidth/2, height-linewidth/2); PDF_lineto(p, linewidth/2, linewidth/2); PDF_lineto(p, width-linewidth/2, height-linewidth/2); PDF_moveto(p, width-linewidth/2, linewidth/2); PDF_lineto(p, linewidth/2, height-linewidth/2); PDF_stroke(p); PDF_setfont(p, font, 24); sprintf(optlist, "fitmethod=auto position=center boxsize={%f %f}", width, height); PDF_fit_textline(p, "Proxy replaces target here", 0, 0, 0, optlist); PDF_end_template_ext(p, 0, 0); /* Create the page */ PDF_begin_page_ext(p, 595, 842, ""); PDF_setfont(p, font, 18); PDF_fit_textline(p, "PDF/X-5 starter sample with reference to an external page", 0, 50, 700, ""); /* Place the proxy on the page */ PDF_fit_image(p, proxy, 50, 50, "boxsize={500 500} fitmethod=meet"); PDF_end_page_ext(p, ""); PDF_end_document(p, ""); } PDF_CATCH(p) { printf("PDFlib exception occurred:\n"); printf("[%d] %s: %s\n", PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_delete(p); return 0; }
void pdf_addfont(gfxdevice_t*dev, gfxfont_t*font) { internal_t*i = (internal_t*)dev->internal; int num = font->num_glyphs<256-32?font->num_glyphs:256-32; if(type3) { int fontid = 0; if(!gfxfontlist_hasfont(i->fontlist, font)) { static int fontnr = 1; char fontname[32]; sprintf(fontname, "font%d", fontnr++); int l = strlen(fontname); char fontname2[64]; int t; for(t=0;t<l+1;t++) { fontname2[t*2+0] = fontname[t]; fontname2[t*2+1] = 0; } PDF_begin_font(i->p, fontname2, l*2, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, ""); for(t=0;t<num;t++) { gfxglyph_t*g = &font->glyphs[t]; gfxbbox_t bbox = gfxline_getbbox(g->line); char name[32]; sprintf(name, "chr%d", t+32); PDF_encoding_set_char(i->p, fontname, t+32, name, 0); PDF_begin_glyph(i->p, name, g->advance, bbox.xmin/64.0, bbox.ymin/64.0, bbox.xmax/64.0, bbox.ymax/64.0); if(mkline(g->line, i->p, 0, 0, 1.0/64.0, 1)) PDF_fill(i->p); PDF_end_glyph(i->p); } PDF_end_font(i->p); fontid = PDF_load_font(i->p, fontname2, l*2, fontname, ""); i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid); } } else if(ttf) { int fontid = 0; if(!gfxfontlist_hasfont(i->fontlist, font)) { char fontname[32],filename[32],fontname2[64]; static int fontnr = 1; sprintf(fontname, "font%d", fontnr); sprintf(filename, "font%d.ttf", fontnr); fontnr++; const char*old_id = font->id; font->id = fontname; int t; for(t=0;t<num;t++) { font->glyphs[t].unicode = 32+t; } font->max_unicode = 0; font->unicode2glyph = 0; gfxfont_save(font, filename); font->id=old_id; #ifdef RUN_TTX /* for testing the generated fonts: run everything through ttx (fonttools) */ char cmd[256]; sprintf(cmd, "mv %s.ttf test.ttf", fontname);system(cmd); system("rm -f test.ttx"); if(system("ttx test.ttf")&0xff00) exit(1); sprintf(cmd, "mv test.ttf %s.old.ttf", fontname, fontname);system(cmd); sprintf(cmd, "ttx test.ttx;mv test.ttf %s.ttf", fontname);system(cmd); sprintf(cmd, "rm -f test.ttx");system(cmd); #endif int l = strlen(fontname); for(t=0;t<l+1;t++) { fontname2[t*2+0] = fontname[t]; fontname2[t*2+1] = 0; } fontid = PDF_load_font(i->p, fontname2, l*2, "host", "embedding=true"); i->fontlist = gfxfontlist_addfont2(i->fontlist, font, (void*)(ptroff_t)fontid); unlink(filename); } } }
int main(void) { /* This is where the data files are. Adjust as necessary.*/ const char * searchpath = "../data"; PDF *p; const char * imagefile = "nesrin.jpg"; char optlist[1024]; int font, image, spot, icc; /* create a new PDFlib object */ if ((p = PDF_new()) == (PDF *) 0) { printf("Couldn't create PDFlib object (out of memory)!\n"); return(2); } PDF_TRY(p) { /* This means we must check return values of load_font() etc. */ PDF_set_parameter(p, "errorpolicy", "return"); PDF_set_parameter(p, "SearchPath", searchpath); if (PDF_begin_document(p, "starter_pdfx.pdf", 0, "pdfx=PDF/X-3:2002") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_set_info(p, "Creator", "PDFlib starter sample"); PDF_set_info(p, "Title", "starter_pdfx"); /* * You can use one of the Standard output intents (e.g. for SWOP * printing) which do not require an ICC profile: PDF_load_iccprofile(p, "CGATS TR 001", 0, "usage=outputintent"); * However, if you use ICC or Lab color you must load an ICC * profile as output intent: */ if (PDF_load_iccprofile(p, "ISOcoated.icc", 0, "usage=outputintent") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); printf("Please install the ICC profile package from " "www.pdflib.com to run the PDF/X starter sample.\n"); PDF_delete(p); return(2); } PDF_begin_page_ext(p, 595, 842, ""); /* Font embedding is required for PDF/X */ font = PDF_load_font(p, "LuciduxSans-Oblique", 0, "winansi", "embedding"); if (font == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_setfont(p, font, 24); spot = PDF_makespotcolor(p, "PANTONE 123 C", 0); PDF_setcolor(p, "fill", "spot", spot, 1.0, 0.0, 0.0); PDF_fit_textline(p, "PDF/X-3:2002 starter", 0, 50, 700, ""); /* The RGB image below needs an ICC profile; we use sRGB. */ icc = PDF_load_iccprofile(p, "sRGB", 0, ""); sprintf(optlist, "iccprofile=%d", icc); image = PDF_load_image(p, "auto", imagefile, 0, optlist); if (image == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_fit_image(p, image, (float) 0.0, (float) 0.0, "scale=0.5"); PDF_end_page_ext(p, ""); PDF_end_document(p, ""); } PDF_CATCH(p) { printf("PDFlib exception occurred:\n"); printf("[%d] %s: %s\n", PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_delete(p); return 0; }
int main(void) { /* This is where the data files are. Adjust as necessary. */ const char* searchpath = "../data"; const char* outfile = "starter_layer.pdf"; PDF * p; const char* rgb = "nesrin.jpg"; const char* gray = "nesrin_gray.jpg"; #define BUFLEN 1024 char buf[BUFLEN]; int font, imageRGB, imageGray, layerRGB, layerGray, layerEN, layerDE; /* create a new PDFlib object */ if ((p = PDF_new()) == (PDF *) 0) { printf("Couldn't create PDFlib object (out of memory)!\n"); return(2); } PDF_TRY(p) { PDF_set_parameter(p, "SearchPath", searchpath); /* This means we must check return values of load_font() etc. */ PDF_set_parameter(p, "errorpolicy", "return"); /* Open the document with the "Layers" navigation tab visible */ if (PDF_begin_document(p, outfile, 0, "openmode=layers") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_set_info(p, "Creator", "PDFlib starter sample"); PDF_set_info(p, "Title", "starter_layer"); /* Load the font */ font = PDF_load_font(p, "Helvetica", 0, "winansi", ""); if (font == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } /* Load the Grayscale image */ imageGray = PDF_load_image(p, "auto", gray, 0, ""); if (imageGray == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } /* Load the RGB image */ imageRGB = PDF_load_image(p, "auto", rgb, 0, ""); if (imageRGB == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } /* * Define all layers which will be used, and their relationships. * This should be done before the first page if the layers are * used on more than one page. */ /* Define the layer "RGB" */ layerRGB = PDF_define_layer(p, "RGB", 0, ""); /* Define the layer "Grayscale" which is hidden when opening the * document or printing it. */ layerGray = PDF_define_layer(p, "Grayscale", 0, "initialviewstate=false initialprintstate=false"); /* At most one of the "Grayscale" and "RGB" layers should be visible */ sprintf(buf, "group={%d %d}", layerGray, layerRGB); PDF_set_layer_dependency(p, "Radiobtn", buf); /* Define the layer "English" */ layerEN = PDF_define_layer(p, "English", 0, ""); /* Define the layer "German" which is hidden when opening the document * or printing it. */ layerDE = PDF_define_layer(p, "German", 0, "initialviewstate=false initialprintstate=false"); /* At most one of the "English" and "German" layers should be visible */ sprintf(buf, "group={%d %d}", layerEN, layerDE); PDF_set_layer_dependency(p, "Radiobtn", buf); /* Start page */ PDF_begin_page_ext(p, 0, 0, "width=a4.width height=a4.height"); /* Place the RGB image on the layer "RGB" */ PDF_begin_layer(p, layerRGB); PDF_fit_image(p, imageRGB, 100, 400, "boxsize={400 300} fitmethod=meet"); /* Place the Grayscale image on the layer "Grayscale" */ PDF_begin_layer(p, layerGray); PDF_fit_image(p, imageGray, 100, 400, "boxsize={400 300} fitmethod=meet"); /* Place an English image caption on the layer "English" */ PDF_begin_layer(p, layerEN); sprintf(buf, "font=%d fontsize=20", font); PDF_fit_textline(p, "This is the Nesrin image.", 0, 100, 370, buf); /* Place a German image caption on the layer "German". */ PDF_begin_layer(p, layerDE); sprintf(buf, "font=%d fontsize=20", font); PDF_fit_textline(p, "Das ist das Nesrin-Bild.", 0, 100, 370, buf); PDF_end_layer(p); PDF_end_page_ext(p, ""); PDF_end_document(p, ""); } PDF_CATCH(p) { printf("PDFlib exception occurred:\n"); printf("[%d] %s: %s\n", PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_delete(p); return 0; }
int main(void) { /* This is where the data files are. Adjust as necessary. */ const char* searchpath = "../data"; PDF * p; const char* imagefile = "nesrin.jpg"; char* optlist; int font, image; /* create a new PDFlib object */ if ((p = PDF_new()) == (PDF *) 0) { printf("Couldn't create PDFlib object (out of memory)!\n"); return(2); } PDF_TRY(p) { /* This means we must check return values of load_font() etc. */ PDF_set_parameter(p, "errorpolicy", "return"); PDF_set_parameter(p, "SearchPath", searchpath); if (PDF_begin_document(p, "starter_basic.pdf", 0, "") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_set_info(p, "Creator", "PDFlib starter sample"); PDF_set_info(p, "Title", "starter_basic"); /* We load the image before the first page, and use it * on all pages */ image = PDF_load_image(p, "auto", imagefile, 0, ""); if (image == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } /* Page 1 */ PDF_begin_page_ext(p, 595, 842, ""); font = PDF_load_font(p, "Helvetica-Bold", 0, "winansi", ""); if (font == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_setfont(p, font, 24); PDF_set_text_pos(p, 50, 700); PDF_show(p, "Hello world!"); PDF_fit_image(p, image, (float) 0.0, (float) 0.0, "scale=0.25"); PDF_end_page_ext(p, ""); /* Page 2 */ PDF_begin_page_ext(p, 595, 842, ""); /* red rectangle */ PDF_setcolor(p, "fill", "rgb", 1.0, 0.0, 0.0, 0.0); PDF_rect(p, 200, 200, 250, 150); PDF_fill(p); /* blue circle */ PDF_setcolor(p, "fill", "rgb", 0.0, 0.0, 1.0, 0.0); PDF_arc(p, 400, 600, 100, 0, 360); PDF_fill(p); /* thick gray line */ PDF_setcolor(p, "stroke", "gray", 0.5, 0.0, 0.0, 0.0); PDF_setlinewidth(p, 10); PDF_moveto(p, 100, 500); PDF_lineto(p, 300, 700); PDF_stroke(p); /* Using the same image handle means the data will be copied * to the PDF only once, which saves space. */ PDF_fit_image(p, image, 150.0, 25.0, "scale=0.25"); PDF_end_page_ext(p, ""); /* Page 3 */ PDF_begin_page_ext(p, 595, 842, ""); /* Fit the image to a box of predefined size (without distortion) */ optlist = "boxsize={400 400} position={center} fitmethod=meet"; PDF_fit_image(p, image, 100, 200, optlist); PDF_end_page_ext(p, ""); PDF_close_image(p, image); PDF_end_document(p, ""); } PDF_CATCH(p) { printf("PDFlib exception occurred:\n"); printf("[%d] %s: %s\n", PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_delete(p); return 0; }