]> git.ipfire.org Git - thirdparty/cups-filters.git/commitdiff
libcupsfilters: Code clean-up for code of testpdf1 and testpdf2
authorTill Kamppeter <till.kamppeter@gmail.com>
Tue, 11 Oct 2022 23:02:42 +0000 (01:02 +0200)
committerTill Kamppeter <till.kamppeter@gmail.com>
Tue, 11 Oct 2022 23:02:42 +0000 (01:02 +0200)
Cleaned up the code following the coding style rules in the
DEVELOPING.md file of the CUPS source code.

This improves the readability of the code a lot, especially as missing
spaces got inserted in comma-separated lists ("xxx,yyy,zzz" -> "xxx,
yyy, zzz") and around operators ("x=a*(b+c)%4" -> "x = a * (b + c) %
4"), what got nearly completely missed out by several contributors.

Also we get rid of the mix of many different coding styles which came
together from the many code contributions received during more than a
decade, even before the start of the cups-filters project.

In addition, one can now supply a fon file as argument to testpdf2, so
that this program embeds the supplied font instead of a standard one.

cupsfilters/testpdf1.c
cupsfilters/testpdf2.c

index 63ba1464522982e342125a35e47ee879c6dfb69a..6e78685f9f194dad40a8b6dcd0eda3dc4e738e73 100644 (file)
@@ -2,7 +2,8 @@
 #include "debug-internal.h"
 #include <string.h>
 
-int main()
+int
+main()
 {
   cf_pdf_out_t *pdf;
 
@@ -12,41 +13,45 @@ int main()
   cfPDFOutBeginPDF(pdf);
 
   // bad font
-  int font_obj=cfPDFOutAddXRef(pdf);
-  cfPDFOutPrintF(pdf,"%d 0 obj\n"
-                    "<</Type/Font\n"
-                    "  /Subtype /Type1\n" // /TrueType,/Type3
-                    "  /BaseFont /%s\n"
-                    ">>\n"
-                    "endobj\n"
-                    ,font_obj,"Courier");
+  int font_obj = cfPDFOutAddXRef(pdf);
+  cfPDFOutPrintF(pdf,
+                "%d 0 obj\n"
+                "<</Type/Font\n"
+                "  /Subtype /Type1\n" // /TrueType,/Type3
+                "  /BaseFont /%s\n"
+                ">>\n"
+                "endobj\n",
+                font_obj, "Courier");
   // test
-  const int PageWidth=595,PageLength=842;
-  int cobj=cfPDFOutAddXRef(pdf);
-  const char buf[]="BT /a 10 Tf (abc) Tj ET";
-  cfPDFOutPrintF(pdf,"%d 0 obj\n"
-                    "<</Length %ld\n"
-                    ">>\n"
-                    "stream\n"
-                    "%s\n"
-                    "endstream\n"
-                    "endobj\n"
-                    ,cobj,strlen(buf),buf);
+  const int PageWidth = 595, PageLength = 842;
+  int cobj = cfPDFOutAddXRef(pdf);
+  const char buf[] = "BT /a 10 Tf (abc) Tj ET";
+  cfPDFOutPrintF(pdf,
+                "%d 0 obj\n"
+                "<</Length %ld\n"
+                ">>\n"
+                "stream\n"
+                "%s\n"
+                "endstream\n"
+                "endobj\n",
+                cobj, strlen(buf), buf);
 
-  int obj=cfPDFOutAddXRef(pdf);
-  cfPDFOutPrintF(pdf,"%d 0 obj\n"
-                    "<</Type/Page\n"
-                    "  /Parent 1 0 R\n"
-                    "  /MediaBox [0 0 %d %d]\n"
-                    "  /Contents %d 0 R\n"
-                    "  /Resources << /Font << /a %d 0 R >> >>\n"
-                    ">>\n"
-                    "endobj\n"
-                    ,obj,PageWidth,PageLength,cobj,font_obj); // TODO: into pdf->
-  cfPDFOutAddPage(pdf,obj);
+  int obj = cfPDFOutAddXRef(pdf);
+  cfPDFOutPrintF(pdf,
+                "%d 0 obj\n"
+                "<</Type/Page\n"
+                "  /Parent 1 0 R\n"
+                "  /MediaBox [0 0 %d %d]\n"
+                "  /Contents %d 0 R\n"
+                "  /Resources << /Font << /a %d 0 R >> >>\n"
+                ">>\n"
+                "endobj\n",
+                obj, PageWidth, PageLength, cobj, font_obj);
+                                                     // TODO: into pdf->
+  cfPDFOutAddPage(pdf, obj);
   cfPDFOutFinishPDF(pdf);
 
   cfPDFOutFree(pdf);
 
-  return 0;
+  return (0);
 }
index b3ee72d410a1779c7275472f5765c8788b615202..02ea5d3fe9f77154246773a4d4c8c37f63cdf38e 100644 (file)
 
 #include <stdio.h>
 
-static inline void write_string(cf_pdf_out_t *pdf,_cf_fontembed_emb_params_t *emb,const char *str) // {{{
+static inline void
+write_string(cf_pdf_out_t *pdf,
+            _cf_fontembed_emb_params_t *emb,
+            const char *str) // {{{
 {
   DEBUG_assert(pdf);
   DEBUG_assert(emb);
   int iA;
 
-  if (emb->plan&_CF_FONTEMBED_EMB_A_MULTIBYTE) {
-    putc('<',stdout); 
-    for (iA=0;str[iA];iA++) {
-      const unsigned short gid=_cfFontEmbedEmbGet(emb,(unsigned char)str[iA]);
-      fprintf(stdout,"%04x",gid);
+  if (emb->plan & _CF_FONTEMBED_EMB_A_MULTIBYTE)
+  {
+    putc('<', stdout);
+    for (iA = 0; str[iA]; iA ++)
+    {
+      const unsigned short gid = _cfFontEmbedEmbGet(emb,
+                                                   (unsigned char)str[iA]);
+      fprintf(stdout, "%04x", gid);
     }
-    putc('>',stdout); 
-    pdf->filepos+=4*iA+2;
-  } else { 
-    for (iA=0;str[iA];iA++) {
-      _cfFontEmbedEmbGet(emb,(unsigned char)str[iA]);
+    putc('>', stdout);
+    pdf->filepos += 4 * iA + 2;
+  }
+  else
+  {
+    for (iA = 0; str[iA]; iA ++)
+    {
+      _cfFontEmbedEmbGet(emb, (unsigned char)str[iA]);
       // TODO: pdf: otf_from_pdf_default_encoding
     }
-    cfPDFOutputString(pdf,str,-1);
+    cfPDFOutputString(pdf, str, -1);
   }
 }
 // }}}
 
-int main()
+
+int
+main(int  argc,
+     char *argv[])
 {
   cf_pdf_out_t *pdf;
 
-  pdf=cfPDFOutNew();
+  pdf = cfPDFOutNew();
   DEBUG_assert(pdf);
 
   cfPDFOutBeginPDF(pdf);
 
   // font, pt.1 
-  const char *fn=TESTFONT;
-  _cf_fontembed_otf_file_t *otf=NULL;
-/*
-  if (argc==2) {
-    fn=argv[1];
-  }
-*/
-  otf=_cfFontEmbedOTFLoad(fn);
+  const char *fn = TESTFONT;
+  _cf_fontembed_otf_file_t *otf = NULL;
+
+  if (argc == 2)
+    fn = argv[1];
+
+  otf = _cfFontEmbedOTFLoad(fn);
   if (!otf)
   {
-    printf("Font %s was not loaded, exiting.\n", TESTFONT);
-    return 1;
+    printf("Font %s was not loaded, exiting.\n", fn);
+    return (1);
   }
   DEBUG_assert(otf);
-  _cf_fontembed_fontfile_t *ff=_cfFontEmbedFontFileOpenSFNT(otf);
-  _cf_fontembed_emb_params_t *emb=_cfFontEmbedEmbNew(ff,
-                          _CF_FONTEMBED_EMB_DEST_PDF16,
-                          _CF_FONTEMBED_EMB_C_FORCE_MULTIBYTE|
-                          _CF_FONTEMBED_EMB_C_TAKE_FONTFILE);
+  _cf_fontembed_fontfile_t *ff = _cfFontEmbedFontFileOpenSFNT(otf);
+  _cf_fontembed_emb_params_t *emb =
+    _cfFontEmbedEmbNew(ff,
+                      _CF_FONTEMBED_EMB_DEST_PDF16,
+                      _CF_FONTEMBED_EMB_C_FORCE_MULTIBYTE |
+                      _CF_FONTEMBED_EMB_C_TAKE_FONTFILE);
 
   // test
-  const int PageWidth=595,PageLength=842;
-  const int cobj=cfPDFOutAddXRef(pdf);
-  cfPDFOutPrintF(pdf,"%d 0 obj\n"
-                    "<</Length %d 0 R\n"
-                    ">>\n"
-                    "stream\n"
-                    ,cobj,cobj+1);
-  long streamlen=-pdf->filepos;
-  cfPDFOutPrintF(pdf,"BT /a 10 Tf ");
-  write_string(pdf,emb,"Test");
-  cfPDFOutPrintF(pdf," Tj ET");
-
-  streamlen+=pdf->filepos;
-  cfPDFOutPrintF(pdf,"\nendstream\n"
-                    "endobj\n");
-  const int clobj=cfPDFOutAddXRef(pdf);
-  DEBUG_assert(clobj==cobj+1);
-  cfPDFOutPrintF(pdf,"%d 0 obj\n"
-                    "%ld\n"
-                    "endobj\n"
-                    ,clobj,streamlen);
+  const int PageWidth = 595, PageLength = 842;
+  const int cobj = cfPDFOutAddXRef(pdf);
+  cfPDFOutPrintF(pdf,
+                "%d 0 obj\n"
+                "<</Length %d 0 R\n"
+                ">>\n"
+                "stream\n",
+                cobj, cobj + 1);
+  long streamlen = -pdf->filepos;
+  cfPDFOutPrintF(pdf, "BT /a 10 Tf ");
+  write_string(pdf, emb, "Test");
+  cfPDFOutPrintF(pdf, " Tj ET");
+
+  streamlen += pdf->filepos;
+  cfPDFOutPrintF(pdf,
+                "\nendstream\n"
+                "endobj\n");
+  const int clobj = cfPDFOutAddXRef(pdf);
+  DEBUG_assert(clobj == cobj + 1);
+  cfPDFOutPrintF(pdf,
+                "%d 0 obj\n"
+                "%ld\n"
+                "endobj\n",
+                clobj, streamlen);
 
   // font
-  int font_obj=cfPDFOutWriteFont(pdf,emb);
+  int font_obj = cfPDFOutWriteFont(pdf, emb);
   DEBUG_assert(font_obj);
 
-  int obj=cfPDFOutAddXRef(pdf);
-  cfPDFOutPrintF(pdf,"%d 0 obj\n"
-                    "<</Type/Page\n"
-                    "  /Parent 1 0 R\n"
-                    "  /MediaBox [0 0 %d %d]\n"
-                    "  /Contents %d 0 R\n"
-                    "  /Resources << /Font << /a %d 0 R >> >>\n"
-                    ">>\n"
-                    "endobj\n"
-                    ,obj,PageWidth,PageLength,cobj,font_obj); // TODO: into pdf->
-  cfPDFOutAddPage(pdf,obj);
+  int obj = cfPDFOutAddXRef(pdf);
+  cfPDFOutPrintF(pdf,
+                "%d 0 obj\n"
+                "<</Type/Page\n"
+                "  /Parent 1 0 R\n"
+                "  /MediaBox [0 0 %d %d]\n"
+                "  /Contents %d 0 R\n"
+                "  /Resources << /Font << /a %d 0 R >> >>\n"
+                ">>\n"
+                "endobj\n",
+                obj, PageWidth, PageLength, cobj, font_obj);
+                                                  // TODO: into pdf->
+  cfPDFOutAddPage(pdf, obj);
   cfPDFOutFinishPDF(pdf);
 
   cfPDFOutFree(pdf);
 
   _cfFontEmbedEmbClose(emb);
 
-  return 0;
+  return (0);
 }