anastasis-gtk

Demonstrator GUI for Anastasis
Log | Files | Refs | README | LICENSE

print.c (4253B)


      1 /*
      2      This file is part of anastasis-gtk.
      3      Copyright (C) 2021 Anastasis SARL
      4 
      5      Anastasis is free software; you can redistribute it and/or modify
      6      it under the terms of the GNU General Public License as published
      7      by the Free Software Foundation; either version 3, or (at your
      8      option) any later version.
      9 
     10      Anastasis is distributed in the hope that it will be useful, but
     11      WITHOUT ANY WARRANTY; without even the implied warranty of
     12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13      General Public License for more details.
     14 
     15      You should have received a copy of the GNU General Public License
     16      along with Anastasis; see the file COPYING.  If not, write to the
     17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18      Boston, MA 02110-1301, USA.
     19 */
     20 
     21 /**
     22  * @file src/anastasis/print.c
     23  * @brief Printing helper.
     24  * @author Christian Grothoff
     25  */
     26 
     27 #include <setjmp.h>
     28 #include <hpdf.h>
     29 #include <gnunet/gnunet_util_lib.h>
     30 #include "print.h"
     31 
     32 
     33 static void
     34 error_handler  (HPDF_STATUS error_no,
     35                 HPDF_STATUS detail_no,
     36                 void         *user_data)
     37 {
     38   jmp_buf *env = user_data;
     39 
     40   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     41               "error_no=%04X, detail_no=%u\n",
     42               (HPDF_UINT) error_no,
     43               (HPDF_UINT) detail_no);
     44   longjmp (*env,
     45            1);
     46 }
     47 
     48 
     49 int
     50 AG_print (json_t *user,
     51           const char *fname)
     52 {
     53   const char *page_title = "GNU Anastasis Identity Sheet";
     54   const char *type;
     55   json_t *jvalue;
     56   HPDF_Doc pdf;
     57   HPDF_Font font;
     58   HPDF_Page page;
     59   float tw;
     60   float fsize;
     61   jmp_buf env;
     62 
     63 
     64   pdf = HPDF_New (&error_handler,
     65                   &env);
     66   if (! pdf)
     67   {
     68     printf ("error: cannot create PdfDoc object\n");
     69     return 1;
     70   }
     71 
     72   if (setjmp (env))
     73   {
     74     HPDF_Free (pdf);
     75     return 1;
     76   }
     77 
     78   /* set compression mode */
     79   HPDF_SetCompressionMode (pdf,
     80                            HPDF_COMP_ALL);
     81   /* create default-font */
     82   font = HPDF_GetFont (pdf,
     83                        "Helvetica",
     84                        NULL);
     85   /* add a new page object. */
     86   page = HPDF_AddPage (pdf);
     87 
     88   /* print the lines of the page. */
     89   HPDF_Page_SetLineWidth (page, 1);
     90   HPDF_Page_Rectangle (page,
     91                        50, 50,
     92                        HPDF_Page_GetWidth (page) - 100,
     93                        HPDF_Page_GetHeight (page) - 110);
     94   HPDF_Page_Stroke (page);
     95 
     96   /* print the title of the page (with positioning center). */
     97   HPDF_Page_SetFontAndSize (page,
     98                             font,
     99                             24);
    100   tw = HPDF_Page_TextWidth (page,
    101                             page_title);
    102   HPDF_Page_BeginText (page);
    103   HPDF_Page_TextOut (page,
    104                      (HPDF_Page_GetWidth (page) - tw) / 2,
    105                      HPDF_Page_GetHeight (page) - 50,
    106                      page_title);
    107   HPDF_Page_EndText (page);
    108 
    109   HPDF_Page_BeginText (page);
    110   HPDF_Page_MoveTextPos (page, 60, HPDF_Page_GetHeight (page) - 60);
    111 
    112   /*
    113    * font size
    114    */
    115   fsize = 18;
    116   json_object_foreach (user, type, jvalue)
    117   {
    118     char *val;
    119     int len;
    120 
    121     /* print the description. */
    122     HPDF_Page_MoveTextPos (page, 0, -12);
    123     HPDF_Page_SetFontAndSize (page, font, 10);
    124     HPDF_Page_ShowText (page, type);
    125 
    126     /* set the position of the text. */
    127     HPDF_Page_MoveTextPos (page, 10, -5 - fsize);
    128     val = GNUNET_strdup (json_string_value (jvalue));
    129     do {
    130       /* set style and size of font. */
    131       HPDF_Page_SetFontAndSize (page, font, fsize);
    132       /* measure the number of characters which included in the page. */
    133       len = HPDF_Page_MeasureText (page,
    134                                    val,
    135                                    HPDF_Page_GetWidth (page) - 130,
    136                                    HPDF_FALSE,
    137                                    NULL);
    138       if ( (len < (ssize_t) strlen (val)) &&
    139            (fsize > 10) )
    140         fsize--;
    141       else
    142         break;
    143     } while (1);
    144     /* truncate the text. */
    145     val[len] = 0x00;
    146     HPDF_Page_ShowText (page, val);
    147     GNUNET_free (val);
    148     HPDF_Page_MoveTextPos (page, -10, -25 - fsize);
    149   }
    150 
    151   HPDF_Page_EndText (page);
    152 
    153   /* save the document to a file */
    154   HPDF_SaveToFile (pdf, fname);
    155 
    156   /* clean up */
    157   HPDF_Free (pdf);
    158 
    159   return 0;
    160 }