"Fossies" - the Fresh Open Source Software Archive

Member "cups-filters-1.25.1/fontembed/test_pdf.c" (5 Jul 2019, 4474 Bytes) of package /linux/misc/cups-filters-1.25.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "test_pdf.c" see the Fossies "Dox" file reference documentation.

    1 #include "embed.h"
    2 #include "config.h"
    3 #include "sfnt.h"
    4 #include <assert.h>
    5 #include <stdio.h>
    6 #include <stdlib.h>
    7 
    8 static void example_outfn(const char *buf,int len,void *context) // {{{
    9 {
   10   FILE *f=(FILE *)context;
   11   if (fwrite(buf,1,len,f)!=len) {
   12     fprintf(stderr,"Short write: %m\n");
   13     assert(0);
   14     return;
   15   }
   16 }
   17 // }}}
   18 
   19 #define OBJ \
   20     xref[xrefpos++]=ftell(f); \
   21     fprintf(f,"%d 0 obj\n",xrefpos);
   22 
   23 #define ENDOBJ \
   24     fprintf(f,"endobj\n");
   25 
   26 #define STREAMDICT \
   27     OBJ; \
   28     fprintf(f,"<<\n" \
   29               "  /Length %d 0 R\n",xrefpos+1);
   30 
   31 #define STREAMDATA \
   32     fprintf(f,">>\n" \
   33               "stream\n"); \
   34   stream_len=-ftell(f);
   35 
   36 #define STREAM \
   37   STREAMDICT \
   38   STREAMDATA
   39 
   40 #define ENDSTREAM \
   41   stream_len+=ftell(f); \
   42   fprintf(f,"endstream\n" \
   43             "endobj\n"); \
   44   OBJ; \
   45   fprintf(f,"%d\n",stream_len); \
   46   ENDOBJ;
   47 
   48 static inline void write_string(FILE *f,EMB_PARAMS *emb,const char *str) // {{{
   49 {
   50   assert(f);
   51   assert(emb);
   52   int iA;
   53 
   54   if (emb->plan&EMB_A_MULTIBYTE) {
   55     putc('<',f);
   56     for (iA=0;str[iA];iA++) {
   57       const unsigned short gid=emb_get(emb,(unsigned char)str[iA]);
   58       fprintf(f,"%04x",gid);
   59     }
   60     putc('>',f);
   61   } else {
   62     putc('(',f);
   63     for (iA=0;str[iA];iA++) {
   64       emb_get(emb,(unsigned char)str[iA]);
   65     }
   66     fprintf(f,"%s",str); // TODO
   67     putc(')',f);
   68   }
   69 }
   70 // }}}
   71 
   72 int main(int argc,char **argv)
   73 {
   74   const char *fn=TESTFONT;
   75   if (argc==2) {
   76     fn=argv[1];
   77   }
   78   OTF_FILE *otf=otf_load(fn);
   79   assert(otf);
   80   FONTFILE *ff=fontfile_open_sfnt(otf);
   81   EMB_PARAMS *emb=emb_new(ff,
   82                           EMB_DEST_PDF16,
   83                           EMB_C_FORCE_MULTIBYTE|
   84                           EMB_C_TAKE_FONTFILE);
   85 
   86   FILE *f=fopen("test.pdf","w");
   87   assert(f);
   88   int xref[100],xrefpos=3;
   89   int stream_len;
   90 
   91   fprintf(f,"%%PDF-1.3\n");
   92   // content
   93   STREAM;
   94   fprintf(f,"BT\n" // content
   95             "  100 100 Td\n"
   96             "  /F1 10 Tf\n");
   97   write_string(f,emb,"Hallo");
   98   fprintf(f," Tj\n"
   99             "ET\n");
  100   ENDSTREAM;
  101 
  102   emb_get(emb,'a');
  103 
  104   // {{{ do font
  105   EMB_PDF_FONTDESCR *fdes=emb_pdf_fontdescr(emb);
  106   assert(fdes);
  107   EMB_PDF_FONTWIDTHS *fwid=emb_pdf_fontwidths(emb);
  108   assert(fwid);
  109 
  110   STREAMDICT;
  111   int ff_ref=xrefpos;
  112   if (emb_pdf_get_fontfile_subtype(emb)) {
  113     fprintf(f,"  /Subtype /%s\n",
  114               emb_pdf_get_fontfile_subtype(emb));
  115   }
  116   if (emb->outtype==EMB_FMT_T1) {
  117     fprintf(f,"  /Length1 ?\n"
  118               "  /Length2 ?\n"
  119               "  /Length3 ?\n");
  120   } else if (emb->outtype==EMB_FMT_TTF) {
  121     fprintf(f,"  /Length1 %d 0 R\n",xrefpos+2);
  122   }
  123   STREAMDATA;
  124   const int outlen=emb_embed(emb,example_outfn,f);
  125   ENDSTREAM;
  126   if (emb->outtype==EMB_FMT_TTF) {
  127     OBJ;
  128     fprintf(f,"%d\n",outlen);
  129     ENDOBJ;
  130   }
  131 
  132   OBJ;
  133   const int fd_ref=xrefpos;
  134   char *res=emb_pdf_simple_fontdescr(emb,fdes,ff_ref);
  135   assert(res);
  136   fputs(res,f);
  137   free(res);
  138   ENDOBJ;
  139 
  140   OBJ;
  141   int f_ref=xrefpos;
  142   res=emb_pdf_simple_font(emb,fdes,fwid,fd_ref);
  143   assert(res);
  144   fputs(res,f);
  145   free(res);
  146   ENDOBJ;
  147 
  148   if (emb->plan&EMB_A_MULTIBYTE) {
  149     OBJ;
  150     res=emb_pdf_simple_cidfont(emb,fdes->fontname,f_ref);
  151     f_ref=xrefpos;
  152     assert(res);
  153     fputs(res,f);
  154     free(res);
  155     ENDOBJ;
  156   }
  157 
  158   free(fdes);
  159   free(fwid);
  160   // }}}
  161 
  162   int iA;
  163 
  164   xref[2]=ftell(f);
  165   fprintf(f,"3 0 obj\n"
  166             "<</Type/Page\n"
  167             "  /Parent 2 0 R\n"
  168             "  /MediaBox [0 0 595 842]\n"
  169             "  /Contents 4 0 R\n"
  170             "  /Resources <<\n"
  171             "    /Font <<\n"
  172             "      /F1 %d 0 R\n"
  173             "    >>\n"
  174             "  >>\n"
  175             ">>\n"
  176             "endobj\n",
  177             f_ref);
  178   xref[1]=ftell(f);
  179   fprintf(f,"2 0 obj\n"
  180             "<</Type/Pages\n"
  181             "  /Count 1\n"
  182             "  /Kids [3 0 R]"
  183             ">>\n"
  184             "endobj\n");
  185   xref[0]=ftell(f);
  186   fprintf(f,"1 0 obj\n"
  187             "<</Type/Catalog\n"
  188             "  /Pages 2 0 R\n"
  189             ">>\n"
  190             "endobj\n");
  191   // {{{ pdf trailer
  192   int xref_start=ftell(f);
  193   fprintf(f,"xref\n"
  194             "0 %d\n"
  195             "%010d 65535 f \n",
  196             xrefpos+1,0);
  197   for (iA=0;iA<xrefpos;iA++) {
  198     fprintf(f,"%010d 00000 n \n",xref[iA]);
  199   }
  200   fprintf(f,"trailer\n"
  201           "<<\n"
  202           "  /Size %d\n"
  203           "  /Root 1 0 R\n"
  204           ">>\n"
  205           "startxref\n"
  206           "%d\n"
  207           "%%%%EOF\n",
  208           xrefpos+1,xref_start);
  209   // }}}
  210   fclose(f);
  211 
  212   emb_close(emb);
  213 
  214   return 0;
  215 }