void PDFWriter::DrawChar(uint16 unicode, const char* utf8, int16 size) { // try to convert from utf8 to MacRoman encoding schema... int32 srcLen = size; int32 destLen = 1; char dest[3] = "\0\0"; int32 state = 0; bool embed = true; font_encoding encoding = macroman_encoding; char fontName[B_FONT_FAMILY_LENGTH+B_FONT_STYLE_LENGTH+1]; if (convert_from_utf8(B_MAC_ROMAN_CONVERSION, utf8, &srcLen, dest, &destLen, &state, 0) != B_OK || dest[0] == 0) { // could not convert to MacRoman font_encoding fenc; uint16 index = 0; uint8 enc; GetFontName(&fState->beFont, fontName); embed = EmbedFont(fontName); REPORT(kDebug, -1, "find_encoding unicode %d\n", (int)unicode); if (find_encoding(unicode, enc, index)) { // is code point in the Adobe Glyph List? // Note if rendering the glyphs only would be desired, we could // always use the second method below (MakeUserDefinedEncoding), // but extracting text from the generated PDF would be almost // impossible (OCR!) REPORT(kDebug, -1, "encoding for %x -> %d %d", unicode, (int)enc, (int)index); // use one of the user pre-defined encodings if (fState->beFont.FileFormat() == B_TRUETYPE_WINDOWS) { encoding = font_encoding(enc + tt_encoding0); } else { encoding = font_encoding(enc + t1_encoding0); } *dest = index; } else if (embed) { // if the font is embedded, create a user defined encoding at runtime uint8 index; MakeUserDefinedEncoding(unicode, enc, index); *dest = index; encoding = font_encoding(user_defined_encoding_start + enc); } else if (find_in_cid_tables(unicode, fenc, index, fFontSearchOrder)) { // font is not embedded use one of the CJK fonts for substitution REPORT(kDebug, -1, "cid table %d index = %d", (int)fenc, (int)index); dest[0] = unicode / 256; dest[1] = unicode % 256; destLen = 2; encoding = fenc; embed = false; } else { static bool found = false; REPORT(kDebug, -1, "encoding for %x not found!", (int)unicode); if (!found) { found = true; REPORT(kError, fPage, "Could not find an encoding for character " "with unicode %d! Message is not repeated for other unicode " "values.", (int)unicode); } *dest = 0; // paint a box (is 0 a box in MacRoman) or return; // simply skip character } } else { REPORT(kDebug, -1, "macroman srcLen=%d destLen=%d dest= %d %d!", srcLen, destLen, (int)dest[0], (int)dest[1]); } // Note we have to build the user defined encoding before it is used in // PDF_find_font! if (!MakesPDF()) return; int font; GetFontName(&fState->beFont, fontName, embed, encoding); font = FindFont(fontName, embed, encoding); if (font < 0) { REPORT(kWarning, fPage, "**** PDF_findfont(%s) failed, back to default " "font", fontName); font = PDF_findfont(fPdf, "Helvetica", "macroman", 0); } fState->font = font; uint16 face = fState->beFont.Face(); PDF_set_parameter(fPdf, "underline", (face & B_UNDERSCORE_FACE) != 0 ? "true" : "false"); PDF_set_parameter(fPdf, "strikeout", (face & B_STRIKEOUT_FACE) != 0 ? "true" : "false"); PDF_set_value(fPdf, "textrendering", (face & B_OUTLINED_FACE) != 0 ? 1 : 0); PDF_setfont(fPdf, fState->font, scale(fState->beFont.Size())); const float x = tx(fState->penX); const float y = ty(fState->penY); const float rotation = fState->beFont.Rotation(); const bool rotate = rotation != 0.0; if (rotate) { PDF_save(fPdf); PDF_translate(fPdf, x, y); PDF_rotate(fPdf, rotation); PDF_set_text_pos(fPdf, 0, 0); } else PDF_set_text_pos(fPdf, x, y); PDF_show2(fPdf, dest, destLen); if (rotate) { PDF_restore(fPdf); } }
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(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) { /* 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; }