Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2008 Adam Blokus <adamblokus@gmail.com>
  3.  * Copyright 2009 John Tytgat <joty@netsurf-browser.org>
  4.  *
  5.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  6.  *
  7.  * NetSurf is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; version 2 of the License.
  10.  *
  11.  * NetSurf is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19.  
  20.  /** \file
  21.  * Target independent PDF plotting using Haru Free PDF Library.
  22.  */
  23.  
  24. #include "utils/config.h"
  25. #ifdef WITH_PDF_EXPORT
  26.  
  27. #include <assert.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31. #include <hpdf.h>
  32.  
  33. #include "content/hlcache.h"
  34. #include "desktop/options.h"
  35. #include "desktop/plotters.h"
  36. #include "desktop/print.h"
  37. #include "desktop/printer.h"
  38. #include "desktop/save_pdf/pdf_plotters.h"
  39. #include "image/bitmap.h"
  40. #include "utils/log.h"
  41. #include "utils/utils.h"
  42. #include "utils/useragent.h"
  43.  
  44. #include "font_haru.h"
  45.  
  46. /* #define PDF_DEBUG */
  47. /* #define PDF_DEBUG_DUMPGRID */
  48.  
  49. static bool pdf_plot_rectangle(int x0, int y0, int x1, int y1, const plot_style_t *style);
  50. static bool pdf_plot_line(int x0, int y0, int x1, int y1, const plot_style_t *pstyle);
  51. static bool pdf_plot_polygon(const int *p, unsigned int n, const plot_style_t *style);
  52. static bool pdf_plot_clip(const struct rect *clip);
  53. static bool pdf_plot_text(int x, int y, const char *text, size_t length,
  54.                 const plot_font_style_t *fstyle);
  55. static bool pdf_plot_disc(int x, int y, int radius, const plot_style_t *style);
  56. static bool pdf_plot_arc(int x, int y, int radius, int angle1, int angle2,
  57.                 const plot_style_t *style);
  58. static bool pdf_plot_bitmap_tile(int x, int y, int width, int height,
  59.                 struct bitmap *bitmap, colour bg,
  60.                 bitmap_flags_t flags);
  61. static bool pdf_plot_path(const float *p, unsigned int n, colour fill, float width,
  62.                 colour c, const float transform[6]);
  63.  
  64. static HPDF_Image pdf_extract_image(struct bitmap *bitmap);
  65.  
  66. static void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no,
  67.                 void *user_data);
  68.  
  69. #ifdef PDF_DEBUG_DUMPGRID
  70. static void pdf_plot_grid(int x_dist,int y_dist,unsigned int colour);
  71. #endif
  72.  
  73. typedef enum {
  74.         DashPattern_eNone,
  75.         DashPattern_eDash,
  76.         DashPattern_eDotted
  77. } DashPattern_e;
  78.  
  79. /* Wrapper routines to minimize gstate updates in the produced PDF file.  */
  80. static void pdfw_gs_init(void);
  81. static void pdfw_gs_save(HPDF_Page page);
  82. static void pdfw_gs_restore(HPDF_Page page);
  83. static void pdfw_gs_fillcolour(HPDF_Page page, colour col);
  84. static void pdfw_gs_strokecolour(HPDF_Page page, colour col);
  85. static void pdfw_gs_linewidth(HPDF_Page page, float lineWidth);
  86. static void pdfw_gs_font(HPDF_Page page, HPDF_Font font, HPDF_REAL font_size);
  87. static void pdfw_gs_dash(HPDF_Page page, DashPattern_e dash);
  88.  
  89. /**
  90.  * Our PDF gstate mirror which we use to minimize gstate updates
  91.  * in the PDF file.
  92.  */
  93. typedef struct {
  94.         colour fillColour; /**< Current fill colour.  */
  95.         colour strokeColour; /**< Current stroke colour.  */
  96.         float lineWidth; /**< Current line width.  */
  97.         HPDF_Font font; /**< Current font.  */
  98.         HPDF_REAL font_size; /**< Current font size.  */
  99.         DashPattern_e dash; /**< Current dash state.  */
  100. } PDFW_GState;
  101.  
  102. static void apply_clip_and_mode(bool selectTextMode, colour fillCol,
  103.         colour strokeCol, float lineWidth, DashPattern_e dash);
  104.  
  105. #define PDFW_MAX_GSTATES 4
  106. static PDFW_GState pdfw_gs[PDFW_MAX_GSTATES];
  107. static unsigned int pdfw_gs_level;
  108.  
  109. static HPDF_Doc pdf_doc; /**< Current PDF document.  */
  110. static HPDF_Page pdf_page; /**< Current page.  */
  111.  
  112. /*PDF Page size*/
  113. static HPDF_REAL page_height, page_width;
  114.  
  115. static bool in_text_mode; /**< true if we're currently in text mode or not.  */
  116. static bool clip_update_needed; /**< true if pdf_plot_clip was invoked for
  117.         current page and not yet synced with PDF output.  */
  118. static int last_clip_x0, last_clip_y0, last_clip_x1, last_clip_y1;
  119.  
  120. static const struct print_settings *settings;
  121.  
  122. static const struct plotter_table pdf_plotters = {
  123.         .rectangle = pdf_plot_rectangle,
  124.         .line = pdf_plot_line,
  125.         .polygon = pdf_plot_polygon,
  126.         .clip = pdf_plot_clip,
  127.         .text = pdf_plot_text,
  128.         .disc = pdf_plot_disc,
  129.         .arc = pdf_plot_arc,
  130.         .bitmap = pdf_plot_bitmap_tile,
  131.         .path = pdf_plot_path,
  132.         .option_knockout = false,
  133. };
  134.  
  135. const struct printer pdf_printer = {
  136.         &pdf_plotters,
  137.         pdf_begin,
  138.         pdf_next_page,
  139.         pdf_end
  140. };
  141.  
  142. static char *owner_pass;
  143. static char *user_pass;
  144.  
  145. bool pdf_plot_rectangle(int x0, int y0, int x1, int y1, const plot_style_t *pstyle)
  146. {
  147.         DashPattern_e dash;
  148. #ifdef PDF_DEBUG
  149.         LOG(("%d %d %d %d %f %X", x0, y0, x1, y1, page_height - y0, pstyle->fill_colour));
  150. #endif
  151.  
  152.         if (pstyle->fill_type != PLOT_OP_TYPE_NONE) {
  153.  
  154.                 apply_clip_and_mode(false, pstyle->fill_colour, NS_TRANSPARENT, 0., DashPattern_eNone);
  155.  
  156.                 /* Normalize boundaries of the area - to prevent
  157.                    overflows.  It is needed only in a few functions,
  158.                    where integers are subtracted.  When the whole
  159.                    browser window is meant min and max int values are
  160.                    used what must be handled in paged output.
  161.                 */
  162.                 x0 = min(max(x0, 0), page_width);
  163.                 y0 = min(max(y0, 0), page_height);
  164.                 x1 = min(max(x1, 0), page_width);
  165.                 y1 = min(max(y1, 0), page_height);
  166.  
  167.                 HPDF_Page_Rectangle(pdf_page, x0, page_height - y1, x1 - x0, y1 - y0);
  168.                 HPDF_Page_Fill(pdf_page);
  169.  
  170.         }
  171.  
  172.         if (pstyle->stroke_type != PLOT_OP_TYPE_NONE) {
  173.  
  174.                 switch (pstyle->stroke_type) {
  175.                 case PLOT_OP_TYPE_DOT:
  176.                         dash = DashPattern_eDotted;
  177.                         break;
  178.  
  179.                 case PLOT_OP_TYPE_DASH:
  180.                         dash = DashPattern_eDash;
  181.                         break;
  182.  
  183.                 default:
  184.                         dash = DashPattern_eNone;
  185.                         break;
  186.  
  187.                 }
  188.  
  189.                 apply_clip_and_mode(false,
  190.                                     NS_TRANSPARENT,
  191.                                     pstyle->stroke_colour,
  192.                                     pstyle->stroke_width,
  193.                                     dash);
  194.  
  195.                 HPDF_Page_Rectangle(pdf_page, x0, page_height - y0, x1 - x0, -(y1 - y0));
  196.                 HPDF_Page_Stroke(pdf_page);
  197.         }
  198.  
  199.         return true;
  200. }
  201.  
  202. bool pdf_plot_line(int x0, int y0, int x1, int y1, const plot_style_t *pstyle)
  203. {
  204.         DashPattern_e dash;
  205.  
  206.         switch (pstyle->stroke_type) {
  207.         case PLOT_OP_TYPE_DOT:
  208.                 dash = DashPattern_eDotted;
  209.                 break;
  210.  
  211.         case PLOT_OP_TYPE_DASH:
  212.                 dash = DashPattern_eDash;
  213.                 break;
  214.  
  215.         default:
  216.                 dash = DashPattern_eNone;
  217.                 break;
  218.  
  219.         }
  220.  
  221.         apply_clip_and_mode(false,
  222.                             NS_TRANSPARENT,
  223.                             pstyle->stroke_colour,
  224.                             pstyle->stroke_width,
  225.                             dash);
  226.  
  227.         HPDF_Page_MoveTo(pdf_page, x0, page_height - y0);
  228.         HPDF_Page_LineTo(pdf_page, x1, page_height - y1);
  229.         HPDF_Page_Stroke(pdf_page);
  230.  
  231.         return true;
  232. }
  233.  
  234. bool pdf_plot_polygon(const int *p, unsigned int n, const plot_style_t *style)
  235. {
  236.         unsigned int i;
  237. #ifdef PDF_DEBUG
  238.         int pmaxx = p[0], pmaxy = p[1];
  239.         int pminx = p[0], pminy = p[1];
  240.         LOG(("."));
  241. #endif
  242.         if (n == 0)
  243.                 return true;
  244.  
  245.         apply_clip_and_mode(false, style->fill_colour, NS_TRANSPARENT, 0., DashPattern_eNone);
  246.  
  247.         HPDF_Page_MoveTo(pdf_page, p[0], page_height - p[1]);
  248.         for (i = 1 ; i<n ; i++) {
  249.                 HPDF_Page_LineTo(pdf_page, p[i*2], page_height - p[i*2+1]);
  250. #ifdef PDF_DEBUG
  251.                 pmaxx = max(pmaxx, p[i*2]);
  252.                 pmaxy = max(pmaxy, p[i*2+1]);
  253.                 pminx = min(pminx, p[i*2]);
  254.                 pminy = min(pminy, p[i*2+1]);
  255. #endif
  256.         }
  257.  
  258. #ifdef PDF_DEBUG
  259.         LOG(("%d %d %d %d %f", pminx, pminy, pmaxx, pmaxy, page_height - pminy));
  260. #endif
  261.  
  262.         HPDF_Page_Fill(pdf_page);
  263.  
  264.         return true;
  265. }
  266.  
  267.  
  268. /**here the clip is only queried */
  269. bool pdf_plot_clip(const struct rect *clip)
  270. {
  271. #ifdef PDF_DEBUG
  272.         LOG(("%d %d %d %d", clip->x0, clip->y0, clip->x1, clip->y1));
  273. #endif
  274.  
  275.         /*Normalize cllipping area - to prevent overflows.
  276.           See comment in pdf_plot_fill.
  277.         */
  278.         last_clip_x0 = min(max(clip->x0, 0), page_width);
  279.         last_clip_y0 = min(max(clip->y0, 0), page_height);
  280.         last_clip_x1 = min(max(clip->x1, 0), page_width);
  281.         last_clip_y1 = min(max(clip->y1, 0), page_height);
  282.  
  283.         clip_update_needed = true;
  284.  
  285.         return true;
  286. }
  287.  
  288. bool pdf_plot_text(int x, int y, const char *text, size_t length,
  289.                 const plot_font_style_t *fstyle)
  290. {
  291. #ifdef PDF_DEBUG
  292.         LOG((". %d %d %.*s", x, y, (int)length, text));
  293. #endif
  294.         char *word;
  295.         HPDF_Font pdf_font;
  296.         HPDF_REAL size;
  297.  
  298.         if (length == 0)
  299.                 return true;
  300.  
  301.         apply_clip_and_mode(true, fstyle->foreground, NS_TRANSPARENT, 0.,
  302.                         DashPattern_eNone);
  303.  
  304.         haru_nsfont_apply_style(fstyle, pdf_doc, pdf_page, &pdf_font, &size);
  305.         pdfw_gs_font(pdf_page, pdf_font, size);
  306.  
  307.         /* FIXME: UTF-8 to current font encoding needs to done.  Or the font
  308.          * encoding needs to be UTF-8 or other Unicode encoding.  */
  309.         word = (char *)malloc( sizeof(char) * (length+1) );
  310.         if (word == NULL)
  311.                 return false;
  312.         memcpy(word, text, length);
  313.         word[length] = '\0';
  314.  
  315.         HPDF_Page_TextOut (pdf_page, x, page_height - y, word);
  316.  
  317.         free(word);
  318.  
  319.         return true;
  320. }
  321.  
  322. bool pdf_plot_disc(int x, int y, int radius, const plot_style_t *style)
  323. {
  324. #ifdef PDF_DEBUG
  325.         LOG(("."));
  326. #endif
  327.         if (style->fill_type != PLOT_OP_TYPE_NONE) {
  328.                 apply_clip_and_mode(false,
  329.                                     style->fill_colour,
  330.                                     NS_TRANSPARENT,
  331.                                     1., DashPattern_eNone);
  332.  
  333.                 HPDF_Page_Circle(pdf_page, x, page_height - y, radius);
  334.  
  335.                 HPDF_Page_Fill(pdf_page);
  336.         }
  337.  
  338.         if (style->stroke_type != PLOT_OP_TYPE_NONE) {
  339.                 /* FIXME: line width 1 is ok ? */
  340.                 apply_clip_and_mode(false,
  341.                                     NS_TRANSPARENT,
  342.                                     style->stroke_colour,
  343.                                     1., DashPattern_eNone);
  344.  
  345.                 HPDF_Page_Circle(pdf_page, x, page_height - y, radius);
  346.  
  347.                 HPDF_Page_Stroke(pdf_page);
  348.         }
  349.  
  350.         return true;
  351. }
  352.  
  353. bool pdf_plot_arc(int x, int y, int radius, int angle1, int angle2, const plot_style_t *style)
  354. {
  355. #ifdef PDF_DEBUG
  356.         LOG(("%d %d %d %d %d %X", x, y, radius, angle1, angle2, style->stroke_colour));
  357. #endif
  358.  
  359.         /* FIXME: line width 1 is ok ? */
  360.         apply_clip_and_mode(false, NS_TRANSPARENT, style->fill_colour, 1., DashPattern_eNone);
  361.  
  362.         /* Normalize angles */
  363.         angle1 %= 360;
  364.         angle2 %= 360;
  365.         if (angle1 > angle2)
  366.                 angle1 -= 360;
  367.  
  368.         HPDF_Page_Arc(pdf_page, x, page_height - y, radius, angle1, angle2);
  369.  
  370.         HPDF_Page_Stroke(pdf_page);
  371.         return true;
  372. }
  373.  
  374.  
  375. bool pdf_plot_bitmap_tile(int x, int y, int width, int height,
  376.                 struct bitmap *bitmap, colour bg,
  377.                 bitmap_flags_t flags)
  378. {
  379.         HPDF_Image image;
  380.         HPDF_REAL current_x, current_y ;
  381.         HPDF_REAL max_width, max_height;
  382.  
  383. #ifdef PDF_DEBUG
  384.         LOG(("%d %d %d %d %p 0x%x", x, y, width, height,
  385.              bitmap, bg));
  386. #endif
  387.         if (width == 0 || height == 0)
  388.                 return true;
  389.  
  390.         apply_clip_and_mode(false, NS_TRANSPARENT, NS_TRANSPARENT, 0., DashPattern_eNone);
  391.  
  392.         image = pdf_extract_image(bitmap);
  393.         if (!image)
  394.                 return false;
  395.  
  396.         /*The position of the next tile*/
  397.         max_width =  (flags & BITMAPF_REPEAT_X) ? page_width : width;
  398.         max_height = (flags & BITMAPF_REPEAT_Y) ? page_height : height;
  399.  
  400.         for (current_y = 0; current_y < max_height; current_y += height)
  401.                 for (current_x = 0; current_x < max_width; current_x += width)
  402.                         HPDF_Page_DrawImage(pdf_page, image,
  403.                                         current_x + x,
  404.                                         page_height - current_y - y - height,
  405.                                         width, height);
  406.  
  407.         return true;
  408. }
  409.  
  410. HPDF_Image pdf_extract_image(struct bitmap *bitmap)
  411. {
  412.         HPDF_Image image = NULL;
  413.         hlcache_handle *content = NULL;
  414.  
  415.         /* TODO - get content from bitmap pointer */
  416.  
  417.         if (content) {
  418.                 const char *source_data;
  419.                 unsigned long source_size;
  420.  
  421.                 /*Not sure if I don't have to check if downloading has been
  422.                 finished.
  423.                 Other way - lock pdf plotting while fetching a website
  424.                 */
  425.                 source_data = content_get_source_data(content, &source_size);
  426.  
  427.                 switch(content_get_type(content)){
  428.                 /*Handle "embeddable" types of images*/
  429.                 case CONTENT_JPEG:
  430.                         image = HPDF_LoadJpegImageFromMem(pdf_doc,
  431.                                         (const HPDF_BYTE *) source_data,
  432.                                         source_size);
  433.                         break;
  434.  
  435.                 /*Disabled until HARU PNG support will be more stable.
  436.  
  437.                 case CONTENT_PNG:
  438.                         image = HPDF_LoadPngImageFromMem(pdf_doc,
  439.                                         (const HPDF_BYTE *)content->source_data,
  440.                                         content->total_size);
  441.                         break;*/
  442.                 default:
  443.                         break;
  444.                 }
  445.         }
  446.  
  447.         if (!image) {
  448.                 HPDF_Image smask;
  449.                 unsigned char *img_buffer, *rgb_buffer, *alpha_buffer;
  450.                 int img_width, img_height, img_rowstride;
  451.                 int i, j;
  452.  
  453.                 /*Handle pixmaps*/
  454.                 img_buffer = bitmap_get_buffer(bitmap);
  455.                 img_width = bitmap_get_width(bitmap);
  456.                 img_height = bitmap_get_height(bitmap);
  457.                 img_rowstride = bitmap_get_rowstride(bitmap);
  458.  
  459.                 rgb_buffer = (unsigned char *)malloc(3 * img_width * img_height);
  460.                 alpha_buffer = (unsigned char *)malloc(img_width * img_height);
  461.                 if (rgb_buffer == NULL || alpha_buffer == NULL) {
  462.                         LOG(("Not enough memory to create RGB buffer"));
  463.                         free(rgb_buffer);
  464.                         free(alpha_buffer);
  465.                         return NULL;
  466.                 }
  467.  
  468.                 for (i = 0; i < img_height; i++)
  469.                         for (j = 0; j < img_width; j++) {
  470.                                 rgb_buffer[((i * img_width) + j) * 3] =
  471.                                   img_buffer[(i * img_rowstride) + (j * 4)];
  472.  
  473.                                 rgb_buffer[(((i * img_width) + j) * 3) + 1] =
  474.                                   img_buffer[(i * img_rowstride) + (j * 4) + 1];
  475.  
  476.                                 rgb_buffer[(((i * img_width) + j) * 3) + 2] =
  477.                                   img_buffer[(i * img_rowstride) + (j * 4) + 2];
  478.  
  479.                                 alpha_buffer[(i * img_width)+j] =
  480.                                   img_buffer[(i * img_rowstride) + (j * 4) + 3];
  481.                         }
  482.  
  483.                 smask = HPDF_LoadRawImageFromMem(pdf_doc, alpha_buffer,
  484.                                 img_width, img_height,
  485.                                 HPDF_CS_DEVICE_GRAY, 8);
  486.  
  487.                 image = HPDF_LoadRawImageFromMem(pdf_doc, rgb_buffer,
  488.                                 img_width, img_height,
  489.                                 HPDF_CS_DEVICE_RGB, 8);
  490.  
  491.                 if (HPDF_Image_AddSMask(image, smask) != HPDF_OK)
  492.                         image = NULL;
  493.  
  494.                 free(rgb_buffer);
  495.                 free(alpha_buffer);
  496.         }
  497.  
  498.         return image;
  499. }
  500.  
  501. /**
  502.  * Enter/leave text mode and update PDF gstate for its clip, fill & stroke
  503.  * colour, line width and dash pattern parameters.
  504.  * \param selectTextMode true if text mode needs to be entered if required;
  505.  * false otherwise.
  506.  * \param fillCol Desired fill colour, use NS_TRANSPARENT if no update is
  507.  * required.
  508.  * \param strokeCol Desired stroke colour, use NS_TRANSPARENT if no update is
  509.  * required.
  510.  * \param lineWidth Desired line width. Only taken into account when strokeCol
  511.  * is different from NS_TRANSPARENT.
  512.  * \param dash Desired dash pattern. Only taken into account when strokeCol
  513.  * is different from NS_TRANSPARENT.
  514.  */
  515. static void apply_clip_and_mode(bool selectTextMode, colour fillCol,
  516.                 colour strokeCol, float lineWidth, DashPattern_e dash)
  517. {
  518.         /* Leave text mode when
  519.          *  1) we're not setting text anymore
  520.          *  2) or we need to update the current clippath
  521.          *  3) or we need to update any fill/stroke colour, linewidth or dash.
  522.          * Note: the test on stroke parameters (stroke colour, line width and
  523.          * dash) is commented out as if these need updating we want to be
  524.          * outside the text mode anyway (i.e. selectTextMode is false).
  525.          */
  526.         if (in_text_mode && (!selectTextMode || clip_update_needed
  527.                 || (fillCol != NS_TRANSPARENT
  528.                         && fillCol != pdfw_gs[pdfw_gs_level].fillColour)
  529.                 /* || (strokeCol != NS_TRANSPARENT
  530.                         && (strokeCol != pdfw_gs[pdfw_gs_level].strokeColour
  531.                                 || lineWidth != pdfw_gs[pdfw_gs_level].lineWidth
  532.                                 || dash != pdfw_gs[pdfw_gs_level].dash)) */)) {
  533.                 HPDF_Page_EndText(pdf_page);
  534.                 in_text_mode = false;
  535.         }
  536.  
  537.         if (clip_update_needed)
  538.                 pdfw_gs_restore(pdf_page);
  539.  
  540.         /* Update fill/stroke colour, linewidth and dash when needed.  */
  541.         if (fillCol != NS_TRANSPARENT)
  542.                 pdfw_gs_fillcolour(pdf_page, fillCol);
  543.         if (strokeCol != NS_TRANSPARENT) {
  544.                 pdfw_gs_strokecolour(pdf_page, strokeCol);
  545.                 pdfw_gs_linewidth(pdf_page, lineWidth);
  546.                 pdfw_gs_dash(pdf_page, dash);
  547.         }
  548.  
  549.         if (clip_update_needed) {
  550.                 pdfw_gs_save(pdf_page);
  551.  
  552.                 HPDF_Page_Rectangle(pdf_page, last_clip_x0,
  553.                                 page_height - last_clip_y1,
  554.                                 last_clip_x1 - last_clip_x0,
  555.                                 last_clip_y1 - last_clip_y0);
  556.                 HPDF_Page_Clip(pdf_page);
  557.                 HPDF_Page_EndPath(pdf_page);
  558.  
  559.                 clip_update_needed = false;
  560.         }
  561.  
  562.         if (selectTextMode && !in_text_mode) {
  563.                 HPDF_Page_BeginText(pdf_page);
  564.                 in_text_mode = true;
  565.         }
  566. }
  567.  
  568. static inline float transform_x(const float transform[6], float x, float y)
  569. {
  570.         return transform[0] * x + transform[2] * y + transform[4];
  571. }
  572.  
  573. static inline float transform_y(const float transform[6], float x, float y)
  574. {
  575.         return page_height
  576.                 - (transform[1] * x + transform[3] * y + transform[5]);
  577. }
  578.  
  579. bool pdf_plot_path(const float *p, unsigned int n, colour fill, float width,
  580.                 colour c, const float transform[6])
  581. {
  582.         unsigned int i;
  583.         bool empty_path;
  584.  
  585. #ifdef PDF_DEBUG
  586.         LOG(("."));
  587. #endif
  588.  
  589.         if (n == 0)
  590.                 return true;
  591.  
  592.         if (c == NS_TRANSPARENT && fill == NS_TRANSPARENT)
  593.                 return true;
  594.  
  595.         if (p[0] != PLOTTER_PATH_MOVE)
  596.                 return false;
  597.  
  598.         apply_clip_and_mode(false, fill, c, width, DashPattern_eNone);
  599.  
  600.         empty_path = true;
  601.         for (i = 0 ; i < n ; ) {
  602.                 if (p[i] == PLOTTER_PATH_MOVE) {
  603.                         HPDF_Page_MoveTo(pdf_page,
  604.                                         transform_x(transform, p[i+1], p[i+2]),
  605.                                         transform_y(transform, p[i+1], p[i+2]));
  606.                         i+= 3;
  607.                 } else if (p[i] == PLOTTER_PATH_CLOSE) {
  608.                         if (!empty_path)
  609.                                 HPDF_Page_ClosePath(pdf_page);
  610.                         i++;
  611.                 } else if (p[i] == PLOTTER_PATH_LINE) {
  612.                         HPDF_Page_LineTo(pdf_page,
  613.                                         transform_x(transform, p[i+1], p[i+2]),
  614.                                         transform_y(transform, p[i+1], p[i+2]));
  615.                         i+=3;
  616.                         empty_path = false;
  617.                 } else if (p[i] == PLOTTER_PATH_BEZIER) {
  618.                         HPDF_Page_CurveTo(pdf_page,
  619.                                         transform_x(transform, p[i+1], p[i+2]),
  620.                                         transform_y(transform, p[i+1], p[i+2]),
  621.                                         transform_x(transform, p[i+3], p[i+4]),
  622.                                         transform_y(transform, p[i+3], p[i+4]),
  623.                                         transform_x(transform, p[i+5], p[i+6]),
  624.                                         transform_y(transform, p[i+5], p[i+6]));
  625.                         i += 7;
  626.                         empty_path = false;
  627.                 } else {
  628.                         LOG(("bad path command %f", p[i]));
  629.                         return false;
  630.                 }
  631.         }
  632.  
  633.         if (empty_path) {
  634.                 HPDF_Page_EndPath(pdf_page);
  635.                 return true;
  636.         }
  637.  
  638.         if (fill != NS_TRANSPARENT) {
  639.                 if (c != NS_TRANSPARENT)
  640.                         HPDF_Page_FillStroke(pdf_page);
  641.                 else
  642.                         HPDF_Page_Fill(pdf_page);
  643.         }
  644.         else
  645.                 HPDF_Page_Stroke(pdf_page);
  646.  
  647.         return true;
  648. }
  649.  
  650. /**
  651.  * Begin pdf plotting - initialize a new document
  652.  * \param path Output file path
  653.  * \param pg_width page width
  654.  * \param pg_height page height
  655.  */
  656. bool pdf_begin(struct print_settings *print_settings)
  657. {
  658.         pdfw_gs_init();
  659.  
  660.         if (pdf_doc != NULL)
  661.                 HPDF_Free(pdf_doc);
  662.         pdf_doc = HPDF_New(error_handler, NULL);
  663.         if (!pdf_doc) {
  664.                 LOG(("Error creating pdf_doc"));
  665.                 return false;
  666.         }
  667.  
  668.         settings = print_settings;
  669.  
  670.         page_width = settings->page_width -
  671.                         FIXTOFLT(FSUB(settings->margins[MARGINLEFT],
  672.                         settings->margins[MARGINRIGHT]));
  673.  
  674.         page_height = settings->page_height -
  675.                         FIXTOFLT(settings->margins[MARGINTOP]);
  676.  
  677.  
  678. #ifndef PDF_DEBUG
  679.         if (option_enable_PDF_compression)
  680.                 HPDF_SetCompressionMode(pdf_doc, HPDF_COMP_ALL); /*Compression on*/
  681. #endif
  682.         HPDF_SetInfoAttr(pdf_doc, HPDF_INFO_CREATOR, user_agent_string());
  683.  
  684.         pdf_page = NULL;
  685.  
  686. #ifdef PDF_DEBUG
  687.         LOG(("pdf_begin finishes"));
  688. #endif
  689.         return true;
  690. }
  691.  
  692.  
  693. bool pdf_next_page(void)
  694. {
  695. #ifdef PDF_DEBUG
  696.         LOG(("pdf_next_page begins"));
  697. #endif
  698.         clip_update_needed = false;
  699.         if (pdf_page != NULL) {
  700.                 apply_clip_and_mode(false, NS_TRANSPARENT, NS_TRANSPARENT, 0.,
  701.                                 DashPattern_eNone);
  702.                 pdfw_gs_restore(pdf_page);
  703.         }
  704.  
  705. #ifdef PDF_DEBUG_DUMPGRID
  706.         if (pdf_page != NULL) {
  707.                 pdf_plot_grid(10, 10, 0xCCCCCC);
  708.                 pdf_plot_grid(100, 100, 0xCCCCFF);
  709.         }
  710. #endif
  711.         pdf_page = HPDF_AddPage(pdf_doc);
  712.         if (pdf_page == NULL)
  713.                 return false;
  714.  
  715.         HPDF_Page_SetWidth (pdf_page, settings->page_width);
  716.         HPDF_Page_SetHeight(pdf_page, settings->page_height);
  717.  
  718.         HPDF_Page_Concat(pdf_page, 1, 0, 0, 1,
  719.                         FIXTOFLT(settings->margins[MARGINLEFT]), 0);
  720.  
  721.         pdfw_gs_save(pdf_page);
  722.  
  723. #ifdef PDF_DEBUG
  724.         LOG(("%f %f", page_width, page_height));
  725. #endif
  726.  
  727.         return true;
  728. }
  729.  
  730.  
  731. void pdf_end(void)
  732. {
  733. #ifdef PDF_DEBUG
  734.         LOG(("pdf_end begins"));
  735. #endif
  736.         clip_update_needed = false;
  737.         if (pdf_page != NULL) {
  738.                 apply_clip_and_mode(false, NS_TRANSPARENT, NS_TRANSPARENT, 0.,
  739.                                 DashPattern_eNone);
  740.                 pdfw_gs_restore(pdf_page);
  741.         }
  742.  
  743. #ifdef PDF_DEBUG_DUMPGRID
  744.         if (pdf_page != NULL) {
  745.                 pdf_plot_grid(10, 10, 0xCCCCCC);
  746.                 pdf_plot_grid(100, 100, 0xCCCCFF);
  747.         }
  748. #endif
  749.  
  750.         assert(settings->output != NULL);
  751.  
  752.         /*Encryption on*/
  753.         if (option_enable_PDF_password)
  754.                 PDF_Password(&owner_pass, &user_pass,
  755.                                 (void *)settings->output);
  756.         else
  757.                 save_pdf(settings->output);
  758. #ifdef PDF_DEBUG
  759.         LOG(("pdf_end finishes"));
  760. #endif
  761. }
  762.  
  763. /** saves the pdf optionally encrypting it before*/
  764. void save_pdf(const char *path)
  765. {
  766.         bool success = false;
  767.  
  768.         if (option_enable_PDF_password && owner_pass != NULL ) {
  769.                 HPDF_SetPassword(pdf_doc, owner_pass, user_pass);
  770.                 HPDF_SetEncryptionMode(pdf_doc, HPDF_ENCRYPT_R3, 16);
  771.                 free(owner_pass);
  772.                 free(user_pass);
  773.         }
  774.  
  775.         if (path != NULL) {
  776.                 if (HPDF_SaveToFile(pdf_doc, path) != HPDF_OK)
  777.                         remove(path);
  778.                 else
  779.                         success = true;
  780.         }
  781.  
  782.         if (!success)
  783.                 warn_user("Unable to save PDF file.", 0);
  784.  
  785.         HPDF_Free(pdf_doc);
  786.         pdf_doc = NULL;
  787. }
  788.  
  789.  
  790. /**
  791.  * Haru error handler
  792.  * for debugging purposes - it immediately exits the program on the first error,
  793.  * as it would otherwise flood the user with all resulting complications,
  794.  * covering the most important error source.
  795. */
  796. static void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no,
  797.                 void *user_data)
  798. {
  799.         LOG(("ERROR:\n\terror_no=%x\n\tdetail_no=%d\n", (HPDF_UINT)error_no,
  800.                         (HPDF_UINT)detail_no));
  801. #ifdef PDF_DEBUG
  802.         exit(1);
  803. #endif
  804. }
  805.  
  806. /**
  807.  * This function plots a grid - used for debug purposes to check if all
  808.  * elements' final coordinates are correct.
  809. */
  810. #ifdef PDF_DEBUG_DUMPGRID
  811. void pdf_plot_grid(int x_dist, int y_dist, unsigned int colour)
  812. {
  813.         for (int i = x_dist ; i < page_width ; i += x_dist)
  814.                 pdf_plot_line(i, 0, i, page_height, 1, colour, false, false);
  815.  
  816.         for (int i = y_dist ; i < page_height ; i += x_dist)
  817.                 pdf_plot_line(0, i, page_width, i, 1, colour, false, false);
  818. }
  819. #endif
  820.  
  821. /**
  822.  * Initialize the gstate wrapper code.
  823.  */
  824. void pdfw_gs_init()
  825. {
  826.         pdfw_gs_level = 0;
  827.         pdfw_gs[0].fillColour = 0x00000000; /* Default PDF fill colour is black.  */
  828.         pdfw_gs[0].strokeColour = 0x00000000; /* Default PDF stroke colour is black.  */
  829.         pdfw_gs[0].lineWidth = 1.0; /* Default PDF line width is 1.  */
  830.         pdfw_gs[0].font = NULL;
  831.         pdfw_gs[0].font_size = 0.;
  832.         pdfw_gs[0].dash = DashPattern_eNone; /* Default dash state is a solid line.  */
  833. }
  834.  
  835. /**
  836.  * Increase gstate level.
  837.  * \param page  PDF page where the update needs to happen.
  838.  */
  839. void pdfw_gs_save(HPDF_Page page)
  840. {
  841.         if (pdfw_gs_level == PDFW_MAX_GSTATES)
  842.                 abort();
  843.         pdfw_gs[pdfw_gs_level + 1] = pdfw_gs[pdfw_gs_level];
  844.         ++pdfw_gs_level;
  845.         HPDF_Page_GSave(page);
  846. }
  847.  
  848. /**
  849.  * Decrease gstate level and restore the gstate to its value at last save
  850.  * operation.
  851.  * \param page  PDF page where the update needs to happen.
  852.  */
  853. void pdfw_gs_restore(HPDF_Page page)
  854. {
  855.         if (pdfw_gs_level == 0)
  856.                 abort();
  857.         --pdfw_gs_level;
  858.         HPDF_Page_GRestore(page);
  859. }
  860.  
  861. #define RBYTE(x) (((x) & 0x0000FF) >>  0)
  862. #define GBYTE(x) (((x) & 0x00FF00) >>  8)
  863. #define BBYTE(x) (((x) & 0xFF0000) >> 16)
  864. #define R(x) (RBYTE(x) / 255.)
  865. #define G(x) (GBYTE(x) / 255.)
  866. #define B(x) (BBYTE(x) / 255.)
  867.  
  868. /**
  869.  * Checks if given fill colour is already set in PDF gstate and if not,
  870.  * update the gstate accordingly.
  871.  * \param page  PDF page where the update needs to happen.
  872.  * \param col   Wanted fill colour.
  873.  */
  874. void pdfw_gs_fillcolour(HPDF_Page page, colour col)
  875. {
  876.         if (col == pdfw_gs[pdfw_gs_level].fillColour)
  877.                 return;
  878.         pdfw_gs[pdfw_gs_level].fillColour = col;
  879.         if (RBYTE(col) == GBYTE(col) && GBYTE(col) == BBYTE(col))
  880.                 HPDF_Page_SetGrayFill(pdf_page, R(col));
  881.         else
  882.                 HPDF_Page_SetRGBFill(pdf_page, R(col), G(col), B(col));
  883. }
  884.  
  885. /**
  886.  * Checks if given stroke colour is already set in PDF gstate and if not,
  887.  * update the gstate accordingly.
  888.  * \param page  PDF page where the update needs to happen.
  889.  * \param col   Wanted stroke colour.
  890.  */
  891. void pdfw_gs_strokecolour(HPDF_Page page, colour col)
  892. {
  893.         if (col == pdfw_gs[pdfw_gs_level].strokeColour)
  894.                 return;
  895.         pdfw_gs[pdfw_gs_level].strokeColour = col;
  896.         if (RBYTE(col) == GBYTE(col) && GBYTE(col) == BBYTE(col))
  897.                 HPDF_Page_SetGrayStroke(pdf_page, R(col));
  898.         else
  899.                 HPDF_Page_SetRGBStroke(pdf_page, R(col), G(col), B(col));
  900. }
  901.  
  902. /**
  903.  * Checks if given line width is already set in PDF gstate and if not, update
  904.  * the gstate accordingly.
  905.  * \param page          PDF page where the update needs to happen.
  906.  * \param lineWidth     Wanted line width.
  907.  */
  908. void pdfw_gs_linewidth(HPDF_Page page, float lineWidth)
  909. {
  910.         if (lineWidth == pdfw_gs[pdfw_gs_level].lineWidth)
  911.                 return;
  912.         pdfw_gs[pdfw_gs_level].lineWidth = lineWidth;
  913.         HPDF_Page_SetLineWidth(page, lineWidth);
  914. }
  915.  
  916. /**
  917.  * Checks if given font and font size is already set in PDF gstate and if not,
  918.  * update the gstate accordingly.
  919.  * \param page          PDF page where the update needs to happen.
  920.  * \param font          Wanted PDF font.
  921.  * \param font_size     Wanted PDF font size.
  922.  */
  923. void pdfw_gs_font(HPDF_Page page, HPDF_Font font, HPDF_REAL font_size)
  924. {
  925.         if (font == pdfw_gs[pdfw_gs_level].font
  926.                 && font_size == pdfw_gs[pdfw_gs_level].font_size)
  927.                 return;
  928.         pdfw_gs[pdfw_gs_level].font = font;
  929.         pdfw_gs[pdfw_gs_level].font_size = font_size;
  930.         HPDF_Page_SetFontAndSize(page, font, font_size);
  931. }
  932.  
  933. /**
  934.  * Checks if given dash pattern is already set in PDF gstate and if not,
  935.  * update the gstate accordingly.
  936.  * \param page  PDF page where the update needs to happen.
  937.  * \param dash  Wanted dash pattern.
  938.  */
  939. void pdfw_gs_dash(HPDF_Page page, DashPattern_e dash)
  940. {
  941.         if (dash == pdfw_gs[pdfw_gs_level].dash)
  942.                 return;
  943.         pdfw_gs[pdfw_gs_level].dash = dash;
  944.         switch (dash) {
  945.                 case DashPattern_eNone: {
  946.                         HPDF_Page_SetDash(page, NULL, 0, 0);
  947.                         break;
  948.                 }
  949.                 case DashPattern_eDash: {
  950.                         const HPDF_UINT16 dash_ptn[] = {3};
  951.                         HPDF_Page_SetDash(page, dash_ptn, 1, 1);
  952.                         break;
  953.                 }
  954.                 case DashPattern_eDotted: {
  955.                         const HPDF_UINT16 dash_ptn[] = {1};
  956.                         HPDF_Page_SetDash(page, dash_ptn, 1, 1);
  957.                         break;
  958.                 }
  959.         }
  960. }
  961.  
  962. #endif /* WITH_PDF_EXPORT */
  963.  
  964.