/* testpdf.c -- test program for the ClibPDF library A relatively comprehensive test program. * Copyright (C) 1998, 1999 FastIO Systems, All Rights Reserved. * For conditions of use, license, and distribution, see LICENSE.txt or LICENSE.pdf. For the multi-threaded version of ClibPDF (version 2.00 and later). NEXTSTEP/OPENSTEP cc -Wall -o testpdf testpdf.c -lcpdfpm cc -Wall -s -object -o testpdf testpdf.c -lcpdfpm BSDI/gcc gcc -Wall -o testpdf testpdf.c -L. -lcpdf -lm SunOS5.4/CC-4.0 cc -o testpdf testpdf.c -Xa -L. -lcpdf -lm HP-UX B.11.00 cc -O -Aa -Ae +Z +DA1.0 -DHPUX -I /usr/local/include -I. -L. -o testpdf testpdf.c -lcpdf -lm 1999-08-23 [io] for v2.00 - Generates two PDF files at the same time (interleaved). 1998-07-07 [io] */ /* This for testing generating more than one PDF file (sequentially) from one program. */ #define NREPS 1 /* set it to more than 1 for calling cpdf_open() and all again */ #include #include #include #include #include "cpdflib.h" /* ======= Month Name Arrays for use with cpdf_setMonthNames() ======= Use MacRomanEncoding table for character codes to define month names. See PDF Reference Manual version 1.3, Adobe Systems, Inc. http://partners.adobe.com/asn/developer/PDFS/TN/PDFSPEC.PDF Appendix C: Predefined Font Encodings. Here are some examples. */ char *monthNamesFrench[] = { "Jan", "F\216v", "Mar", "Avr", "Mai", "Jun", "Jul", "Ao\236", "Sep", "Oct", "Nov", "D\216c" }; char *monthNamesGerman[] = { "Jan", "Feb", "M\212r", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez" }; char *monthNamesItalian[] = { "Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic" }; char *monthNamesSpanish[] = { "Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic" }; char *monthNamesSwedish[] = { "Jan", "Feb", "Mar", "Apr", "Maj", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dic" }; char *myAnnotation = "testpdf.c -- FastIO Systems\n\ Generated by ClibPDF test program\n\ Fri Aug 27 18:39:18 PDT 1999\n"; /* Some plot data. In a real program, these come from external sources. */ /* --------------------------------------------------------------------------- */ int Nxfreq = 12; float xfreq[] = {0.05, 0.072547, 0.105262, 0.152729, 0.221600, 0.321529, 0.466520, 0.676894, 0.982134, 1.425020, 2.067622, 3.0}; float Resp[] = { 1.500, 4.375, 7.312, 8.938, 25.938, 36.125, 59.125, 67.062, 31.750, 2.500, 1.562, 3.375 }; float nullresp = 1.750; float respmax = 80.0; float xfreqmax = 4.0; float xfreqmin = 0.03; /* Fit parameter definition for Gaussian: y = A*exp( - (u/Sigma)^2) + D As a test program for the plot library, a curve fitting routine is not included. It is left as an excercise for the reader. */ float A = 70.49879; float D = 0.1953; float fopt = 0.63581; float Sigma = 0.37157; /* --------------------------------------------------------------------------- */ extern void do_Document(CPDFdoc *pdf, int docNumber); extern void do_FreqResponse(CPDFdoc *pdf); extern void do_Lissa(CPDFdoc *pdf); extern void generateContent(CPDFdoc *pdf, int docNum); /* --------------------------------------------------------------------------- */ static char pathbuf[1024]; /* pathname scratch buffer */ static unsigned char aimage[256]; int main(int argc, char *argv[]) { CPDFdoc *pdf1, *pdf2; CPDFdocLimits dL = {120, -1, -1, -1, 8000}; int length = 0; char *bufPDF; /* Creat documents, pdf1 and pdf2 at the same time. */ pdf1 = cpdf_open(0, &dL); pdf2 = cpdf_open(0, &dL); /* For each doc, do a little bit on page 1, and then do page 3, return to page 1. */ /* Page 3 will become page 2 of file at the end, because page 2 was never created. */ do_Document(pdf1, 1); do_Document(pdf2, 2); /* For each document, do a lot on page 1. */ generateContent(pdf1, 1); /* Most drawing/graphics here ! */ generateContent(pdf2, 2); /* Finish Doc #1 */ cpdf_finalizeAll(pdf1); /* PDF file is actually written here */ bufPDF = cpdf_getBufferForPDF(pdf1, &length); printf("PDF document #1 buffer length= %d\n", length); /* Or you can save generated PDF to file */ cpdf_savePDFmemoryStreamToFile(pdf1, "atest-1.pdf"); /* cpdf_launchPreview() must come before cpdf_close(). "pdf1" would be invalid otherwise. */ cpdf_launchPreview(pdf1); /* launch Acrobat/PDF viewer on the output file */ cpdf_close(pdf1); /* shut down the library resources */ /* Finish Doc #2 */ cpdf_finalizeAll(pdf2); /* PDF file is actually written here */ bufPDF = cpdf_getBufferForPDF(pdf2, &length); printf("PDF document #2 buffer length= %d\n", length); /* Or you can save generated PDF to file */ cpdf_savePDFmemoryStreamToFile(pdf2, "atest-2.pdf"); cpdf_launchPreview(pdf2); /* launch Acrobat/PDF viewer on the output file */ cpdf_close(pdf2); /* shut down the library resources */ return(0); } void do_Document(CPDFdoc *pdf, int docNumber) { int i; float p1=0.0, p2=0.0, p3=0.0, p4=0.0; float width = 0.0, height = 0.0, xscale = 2.0, yscale = 2.0; /* char ofilename[128]; */ CPDFviewerPrefs vP = {PM_OUTLINES, 0, 0, 0, 0, 0, 0, 0}; char *act_dict; CPDFannotAttrib attrib; CPDFoutlineEntry *currTopOL, *currSectOL, *currSubsectOL; cpdf_setFontMapFile(pdf, "fontmap.lst"); cpdf_enableCompression(pdf, YES); /* use Flate/Zlib compression */ if(docNumber == 1) cpdf_setMonthNames(pdf, monthNamesFrench); /* leave pdf2 in English */ /* sprintf(ofilename, "atest-%d.pdf", docNumber); */ /* cpdf_setOutputFilename(pdf, ofilename); */ cpdf_init(pdf); /* This will have outline (book marks) visible upon document open */ /* cpdf_setViewerPreferences(PM_OUTLINES, 0, 0, 0, 0, 0, 0, 0); */ /* OBSOLETE see above */ if(docNumber == 1) cpdf_setViewerPreferences(pdf, &vP); /* book mark visible in pdf #1 only */ cpdf_pageInit(pdf, 1, PORTRAIT, LETTER, LETTER); /* page orientation */ cpdf_setCreator(pdf, "ClibPDF Test Program: testpdf.c"); if(docNumber == 1) cpdf_rawSetAnnotation(pdf, 0, 0, 300, 100, "Title: ClibPDF Documnet #1", myAnnotation, NULL); else cpdf_rawSetAnnotation(pdf, 0, 0, 300, 100, "Title: ClibPDF Documnet #2", myAnnotation, NULL); /* OK, now test hyper-link annotations and action outlines */ /* The first outline entry should be added to NULL */ /* URL outline and action */ attrib.flags = 0; /* AF_NOROTATE | AF_PRINT , etc. */ attrib.border_array = "[5 5 3]"; /* 5 pt radius, 3 pt linewidth */ attrib.BS = NULL; attrib.r = 1.0; attrib.g = 0.0; attrib.b = 0.0; /* GoTo page 3 (forward reference), page 3 must be created, or the program will crash. */ cpdf_setLinkGoToPage(pdf, 6.3, 6.6, 7.8, 6.9, 3, "/XYZ null null null", &attrib); cpdf_setgray(pdf, 0.0); /* black */ cpdf_beginText(pdf, 0); cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", 12.0); cpdf_textAligned(pdf, 7.05, 6.75, 0.0, TEXTPOS_MM, "GoTo p.3 (really 2)"); cpdf_endText(pdf); act_dict = "/Type /Action /S /URI /URI (http://partners.adobe.com/asn/developer/PDFS/TN/PDFSPEC.PDF)"; currTopOL = cpdf_addOutlineAction(pdf, NULL, OL_SAME, OL_OPEN, act_dict, "PDF Reference v1.3 at Adobe"); attrib.r = 0.0; attrib.g = 1.0; attrib.b = 0.0; cpdf_setLinkAction(pdf, 6.3, 6.2, 7.8, 6.5, act_dict, &attrib); cpdf_beginText(pdf, 0); cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", 12.0); cpdf_textAligned(pdf, 7.05, 6.35, 0.0, TEXTPOS_MM, "PDF Spec @ Adobe"); cpdf_endText(pdf); /* GoToR (goto another PDF file) outline and action. "[1 0 R" part must be the actual page number. */ act_dict = "/Type /Action /S /GoToR /D [47 /XYZ null null null] /F (../cpdfman110.pdf)"; currTopOL = cpdf_addOutlineAction(pdf, currTopOL, OL_SAME, OL_OPEN, act_dict, "Manual page 48"); attrib.r = 0.0; attrib.g = 0.0; attrib.b = 1.0; cpdf_setLinkAction(pdf, 6.3, 5.8, 7.8, 6.1, act_dict, &attrib); cpdf_beginText(pdf, 0); cpdf_setFont(pdf, "Times-Roman", "MacRomanEncoding", 12.0); cpdf_textAligned(pdf, 7.05, 5.95, 0.0, TEXTPOS_MM, "Manual page 48"); cpdf_endText(pdf); /* These are standard goto page outlines */ currTopOL = cpdf_addOutlineEntry(pdf, currTopOL, OL_SAME, OL_OPEN, 1, "Top 1 - Document Fit", DEST_FIT, p1, p2, p3, p4); /* #### New page 3, can skip page 2 */ /* Page 3 will become page 2 of file at the end, because page 2 was never created. */ cpdf_pageInit(pdf, 3, PORTRAIT, LETTER, LETTER); /* page orientation */ cpdf_convertUpathToOS(pathbuf, "testimg.jpg"); cpdf_importImage(pdf, pathbuf, JPEG_IMG, 2.0, 2.0, 20.0, &width, &height, &xscale, &yscale, 1); if(docNumber == 1) cpdf_setPageTransition(pdf, TRANS_BOX, 1.0, 30.0, 0, 0); else cpdf_setPageTransition(pdf, TRANS_GLITTER, 1.0, 30.0, 0, 0); /* different transitions */ p1 = 0.0; p2 = 792.0; p3=0.0; currSectOL = cpdf_addOutlineEntry(pdf, currTopOL, OL_SUBENT, OL_CLOSED, 3, "Section 1 - Page 2", DEST_XYZ, p1, p2, p3, p4); /* try an in-line image of gray ramp here */ for(i=0; i<256; i++) aimage[i] = (unsigned char)i; cpdf_rawPlaceInLineImage(pdf, aimage, 256, 72.0, 72.0, 0.0, 72.0*6.5, 36.0, 256, 1, 8, CS_GRAY, 1); cpdf_rawRect(pdf, 72.0, 72.0, 72.0*6.5, 36.0); /* put a box around it */ cpdf_setgray(pdf, 0.0); cpdf_setlinewidth(pdf, 0.3); cpdf_stroke(pdf); p1 = 200.0; p2 = 100.0; p3=500.0, p4=500.0; currSectOL = cpdf_addOutlineEntry(pdf, currSectOL, OL_SAME, OL_CLOSED, 3, "Section 2 - Page 2 FitR", DEST_FITR, p1, p2, p3, p4); currSubsectOL = cpdf_addOutlineEntry(pdf, currSectOL, OL_SUBENT, OL_OPEN, 3, "Subsection 1 - Fit", DEST_FIT, p1, p2, p3, p4); p1 = 0.0; p2 = 600.0; p3=2.0; currSectOL = cpdf_addOutlineEntry(pdf, currSectOL, OL_SAME, OL_CLOSED, 1, "Section 3 - Page 1 Zoom=2.0", DEST_XYZ, p1, p2, p3, p4); p1 = 600.0; /* top */ currTopOL = cpdf_addOutlineEntry(pdf, currTopOL, OL_SAME, OL_OPEN, 3, "Top 2 - Page 2 FitH", DEST_FITH, p1, p2, p3, p4); /* ### Switch back to page 1, but we will get the default domain there. Put 10 red stars in a row. */ cpdf_setCurrentPage(pdf, 1); /* We are back to page 1 */ cpdf_setrgbcolor(pdf, 1.0, 0.3, 0.3); for(i=0; i<10; i++) cpdf_marker(pdf, 0.7*(float)i + 1.0, 10.6, 4, 16.0); } void generateContent(CPDFdoc *pdf, int docNum) { CPDFplotDomain *myDomain, *oldDom; float width=0.0, height=0.0, xscale=0.0, yscale=0.0; /* cpdf_fillDomainWithRGBcolor(pdf->defaultDomain, 0.90, 0.93, 0.92); */ /* had to disable above to make the domain transparent */ myDomain = cpdf_createPlotDomain( 1.0*inch, 1.0*inch, 6.5*inch, 9.0*inch, 0.0, 6.5, 0.0, 9.0, LINEAR, LINEAR, 0); oldDom = cpdf_setPlotDomain(pdf, myDomain); /* save oldDomain for later restore */ /* OK, do some real plotting with actual data in a semi-log domain */ do_FreqResponse(pdf); /* plot in the default domain of x = cos(3t); y = sin(5t); */ do_Lissa(pdf); cpdf_comments(pdf, "\n%% Text examples here.\n"); cpdf_setgrayFill(pdf, 0.0); /* Black */ cpdf_beginText(pdf, 0); /* cpdf_setFont(pdf, "AvantGarde-Book", "MacRomanEncoding", 18.0); */ /* cpdf_setFont(pdf, "CPDF-Monospace", "MacRomanEncoding", 18.0); */ /* cpdf_setFont(pdf, "Palatino-Bold", "MacRomanEncoding", 18.0); */ /* cpdf_setFont(pdf, "CPDF-SmallCap", "MacRomanEncoding", 18.0); */ cpdf_setFont(pdf, "UtopiaBold", NULL, 20.0); /* cpdf_setFont(pdf, "AA-UtopiaMedium", "MacRomanEncoding", 18.0); */ /* cpdf_rawText() addresses the location by raw point-based coordinate system. */ if(docNum == 1) cpdf_rawText(pdf, 110.0, 720.0, 0.0, "Test of ClibPDF library for C (Document #1)"); else cpdf_rawText(pdf, 110.0, 720.0, 0.0, "Test of ClibPDF library for C (Document #2)"); cpdf_endText(pdf); cpdf_setrgbcolorFill(pdf, 1.0, 0.0, 0.0); /* Red */ cpdf_beginText(pdf, 0); cpdf_setFont(pdf, "Times-Bold", "MacRomanEncoding", 14.0); /* cpdf_text() uses a flexible domain based coordinate system. See cpdf_setPlotDomain(). */ cpdf_text(pdf, 3.5, 1.2, 90.0, "Vertical text line"); cpdf_text(pdf, 3.7, 1.2, 30.0, "Oblique text line at 30 degrees"); cpdf_endText(pdf); /* Now try importing JPEG images */ /* use the pixel size of image as points */ width=0.0; height=0.0; xscale=0.0; yscale=0.0; cpdf_convertUpathToOS(pathbuf, "testimg.jpg"); cpdf_importImage(pdf, pathbuf, JPEG_IMG, 4.0, 6.0, 20.0, &width, &height, &xscale, &yscale, 1); cpdf_setrgbcolorFill(pdf, 0.0, 0.0, 1.0); /* Blue */ cpdf_beginText(pdf, 0); /* cpdf_setFont(pdf, "UtopiaMedium", NULL, 14.0); */ cpdf_setFont(pdf, "AA-UtopiaMedium", "MacRomanEncoding", 14.0); /* cpdf_setFont(pdf, "Helvetica", NULL, 14.0); */ cpdf_text(pdf, 0.2, 8.5, 0.0, "A line of UtopiaMedium font at 14 point using the default domain"); cpdf_text(pdf, 0.2, 8.2, 0.0, "defined in the inch based coordinate system."); cpdf_endText(pdf); cpdf_setgrayFill(pdf, 0.0); /* Black */ cpdf_beginText(pdf, 0); cpdf_setFont(pdf, "Courier", "MacRomanEncoding", 14.0); cpdf_text(pdf, 0.2, 7.9, 0.0, "Test of Courier font here."); cpdf_endText(pdf); /* Here we use the same image again, the image data from the previous call will be reused, and will not be duplicated in the PDF file. */ width = 0.0; height = 0.0; /* must reset these, as they were used above */ xscale = 0.75; yscale = 0.0; /* cpdf_convertUpathToOS(pathbuf, "../images/palette.tiff"); */ /* cpdf_importImage(pdf, pathbuf, TIFF_IMG, 4.0, 0.5, 0.0, &width, &height, &xscale, &yscale, 1); */ /* cpdf_importImage(pdf, "rgbplanar.tiff", TIFF_IMG, 4.0, 0.5, 0.0, &width, &height, &xscale, &yscale, 1); */ /* cpdf_importImage(pdf, "bk318r03.tiff", TIFF_IMG, 4.0, 0.5, 0.0, &width, &height, &xscale, &yscale, 1); */ cpdf_setPlotDomain(pdf, oldDom); /* restore previous plot domain */ cpdf_freePlotDomain(myDomain); /* deallocate the plot domain */ } void do_FreqResponse(CPDFdoc *pdf) { int i; int marktype = 0; /* 0=circle, 1=up triangle, 2=diamond, 3=square, 4=star */ float markersize = 9.0; float yp, xp, xbump; CPDFplotDomain *sfDomain, *oldDomain, *timeDomain; CPDFaxis *xAxis, *yAxis, *xxA2; time_t t; struct tm tm; struct tm tm2; /* Create a semi log plot domain (X-axis logarithmic, Y-axis linear) */ /* (CPDFplotDomain *)cpdf_createPlotDomain(float x, float y, float w, float h, float xL, float xH, float yL, float yH, int xtype, int ytype, int rsvd) */ sfDomain = cpdf_createPlotDomain( 1.6*inch, 5.5*inch, 4.5*inch, 3.0*inch, xfreqmin, xfreqmax, 0.0, respmax, LOGARITHMIC, LINEAR, 0); oldDomain = cpdf_setPlotDomain(pdf, sfDomain); /* save oldDomain for later restore */ cpdf_fillDomainWithGray(sfDomain, 1.0); /* white */ /* cpdf_fillDomainWithRGBcolor(sfDomain, 1.0, 0.9, 0.9); */ /* light pink */ /* Mesh lines for the domain -------------------------------------------------------------- */ /* void cpdf_setLinearMeshParams(CPDFplotDomain *aDomain, int xy, float mesh1ValMajor, float intervalMajor, float mesh1ValMinor, float intervalMinor) */ /* void cpdf_drawMeshForDomain(CPDFplotDomain *aDomain) */ /* cpdf_setLinearMeshParams(sfDomain, Y_MESH, 0.0, 20.0, 0.0, 10.0); */ /* not needed */ cpdf_drawMeshForDomain(sfDomain); cpdf_setgray(pdf, 0.0); /* X-Axis --------------------------------------------------------------------------------- */ /* CPDFaxis *cpdf_createAxis(float angle, float axislength, int typeflag, float valL, float valH); */ xAxis = cpdf_createAxis( 0.0, 4.5*inch, LOGARITHMIC, xfreqmin, xfreqmax); cpdf_setLogAxisNumberSelector(xAxis, LOGAXSEL_13); /* Nums 1 and 3s */ cpdf_attachAxisToDomain(xAxis, sfDomain, 0.0, -0.2*inch); cpdf_setAxisNumberFormat(xAxis, "%g", "Helvetica", 16.0); cpdf_setAxisLabel(xAxis, "Frequency [Hz]", "Times-Roman", "MacRomanEncoding", 20.0); cpdf_drawAxis(xAxis); /* A given existing axis can be attached to other similar domains, and reused. */ /* Here, attach it to the default domain for fun. */ /* cpdf_attachAxisToDomain(xAxis, defaultDomain, 0.0, 0.0); cpdf_drawAxis(xAxis); */ cpdf_freeAxis(xAxis); /* Y-Axis --------------------------------------------------------------------------------- */ yAxis = cpdf_createAxis(90.0, 3.0*inch, LINEAR, 0.0, respmax); cpdf_attachAxisToDomain(yAxis, sfDomain, -0.2*inch, 0.0); cpdf_setAxisNumberFormat(yAxis, "%2.f", "Helvetica", 16.0); /* void cpdf_setAxisTicNumLabelPosition(CPDFaxis *anAx, int ticPos, int numPos, int horizNum, int horizLab); */ /* cpdf_setAxisTicNumLabelPosition(yAxis, 0, 2, 1, 1); */ cpdf_setAxisLabel(yAxis, "Amplitude [mV]", "Times-Roman", "MacRomanEncoding", 20.0); cpdf_drawAxis(yAxis); cpdf_freeAxis(yAxis); /* Null Response line */ cpdf_comments(pdf, "\n%% spontaneous response level.\n"); cpdf_setlinewidth(pdf, 0.6); cpdf_setgrayStroke(pdf, 0.0); /* black */ cpdf_moveto(pdf, xfreqmin, nullresp); cpdf_lineto(pdf, xfreqmax, nullresp); cpdf_stroke(pdf); /* Do the tuning curve first */ cpdf_comments(pdf, "\n%% plot the curve.\n"); cpdf_setlinewidth(pdf, 2.0); cpdf_setrgbcolorStroke(pdf, 0.0, 0.0, 1.0); /* blue */ xbump = pow(xfreqmax / xfreqmin, 0.01); xp = xfreqmin; for(i=0; i<100; i++) { xp *= xbump; /* A Gaussian curve */ yp = A * exp( - ((xp - fopt) / Sigma) * ((xp - fopt) / Sigma)) + D; if(i==0) cpdf_moveto(pdf, xp, yp); else cpdf_lineto(pdf, xp, yp); } cpdf_stroke(pdf); /* Do markers after the curve is drawn, so that the markers are in front of the curve. */ cpdf_comments(pdf, "\n%% place data point markers.\n"); cpdf_setgrayStroke(pdf, 0.0); cpdf_setrgbcolorFill(pdf, 1.0, 1.0, 0.0); /* yellow inside */ for(i=0; i