Subversion Repositories Kolibri OS

Rev

Rev 3584 | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2008 Adam Blokus <adamblokus@gmail.com>
  3.  *
  4.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  5.  *
  6.  * NetSurf is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; version 2 of the License.
  9.  *
  10.  * NetSurf is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. /** \file
  20.  * Output-in-pages implementation
  21. */
  22.  
  23. #include "utils/config.h"
  24.  
  25. #include <assert.h>
  26. #include <string.h>
  27.  
  28. #include <dom/dom.h>
  29.  
  30. #include "content/content.h"
  31. #include "content/hlcache.h"
  32. #include "css/utils.h"
  33. #include "desktop/options.h"
  34. #include "desktop/print.h"
  35. #include "desktop/printer.h"
  36. #include "render/box.h"
  37. #include "utils/log.h"
  38. #include "utils/talloc.h"
  39. #include "utils/types.h"
  40.  
  41. /* Default print settings */
  42. #define DEFAULT_PAGE_WIDTH 595
  43. #define DEFAULT_PAGE_HEIGHT 840
  44. #define DEFAULT_COPIES 1
  45.  
  46. static hlcache_handle *print_init(hlcache_handle *, struct print_settings *);
  47. static bool print_apply_settings(hlcache_handle *, struct print_settings *);
  48.  
  49. static float page_content_width, page_content_height;
  50. static hlcache_handle *printed_content;
  51. static float done_height;
  52.  
  53. bool html_redraw_printing = false;
  54. int html_redraw_printing_border = 0;
  55. int html_redraw_printing_top_cropped = 0;
  56.  
  57. /**
  58.  * This function calls print setup, prints page after page until the whole
  59.  * content is printed calls cleaning up afterwise.
  60.  *
  61.  * \param content The content to be printed
  62.  * \param printer The printer interface for the printer to be used
  63.  * \param settings The settings for printing to use
  64.  * \return true if successful, false otherwise
  65.  */
  66. bool print_basic_run(hlcache_handle *content,
  67.                 const struct printer *printer,
  68.                 struct print_settings *settings)
  69. {
  70.         bool ret = true;
  71.  
  72.         assert(content != NULL && printer != NULL && settings != NULL);
  73.        
  74.         if (print_set_up(content, printer, settings, NULL))
  75.                 return false;
  76.  
  77.         while (ret && (done_height < content_get_height(printed_content)) ) {
  78.                 ret = print_draw_next_page(printer, settings);
  79.         }
  80.  
  81.         print_cleanup(content, printer, settings);
  82.        
  83.         return ret;
  84. }
  85.  
  86. /**
  87.  * This function prepares the content to be printed. The current browser content
  88.  * is duplicated and resized, printer initialization is called.
  89.  *
  90.  * \param content The content to be printed
  91.  * \param printer The printer interface for the printer to be used
  92.  * \param settings The settings for printing to use
  93.  * \param height updated to the height of the printed content
  94.  * \return true if successful, false otherwise
  95.  */
  96. bool print_set_up(hlcache_handle *content,
  97.                 const struct printer *printer, struct print_settings *settings,
  98.                 double *height)
  99. {
  100.         printed_content = print_init(content, settings);
  101.        
  102.         if (printed_content == NULL)
  103.                 return false;
  104.        
  105.         print_apply_settings(printed_content, settings);
  106.  
  107.         if (height)
  108.                 *height = content_get_height(printed_content);
  109.        
  110.         printer->print_begin(settings);
  111.  
  112.         done_height = 0;
  113.        
  114.         return true;   
  115. }
  116.  
  117. /**
  118.  * This function draws one page, beginning with the height offset of done_height
  119.  *
  120.  * \param printer The printer interface for the printer to be used
  121.  * \param settings The settings for printing to use
  122.  * \return true if successful, false otherwise
  123.  */
  124. bool print_draw_next_page(const struct printer *printer,
  125.                 struct print_settings *settings)
  126. {
  127.         struct rect clip;
  128.         struct content_redraw_data data;
  129.         struct redraw_context ctx = {
  130.                 .interactive = false,
  131.                 .background_images = !nsoption_bool(remove_backgrounds),
  132.                 .plot = printer->plotter
  133.         };
  134.  
  135.         html_redraw_printing_top_cropped = INT_MAX;
  136.  
  137.         clip.x0 = 0;
  138.         clip.y0 = 0;
  139.         clip.x1 = page_content_width * settings->scale;
  140.         clip.y1 = page_content_height  * settings->scale;
  141.  
  142.         data.x = 0;
  143.         data.y = -done_height;
  144.         data.width = 0;
  145.         data.height = 0;
  146.         data.background_colour = 0xFFFFFF;
  147.         data.scale = settings->scale;
  148.         data.repeat_x = false;
  149.         data.repeat_y = false;
  150.  
  151.         html_redraw_printing = true;
  152.         html_redraw_printing_border = clip.y1;
  153.  
  154.         printer->print_next_page();
  155.         if (!content_redraw(printed_content, &data, &clip, &ctx))
  156.                 return false;
  157.  
  158.         done_height += page_content_height -
  159.                         (html_redraw_printing_top_cropped != INT_MAX ?
  160.                         clip.y1 - html_redraw_printing_top_cropped : 0) /
  161.                         settings->scale;
  162.  
  163.         return true;
  164. }
  165.  
  166. /**
  167.  * The content passed to the function is duplicated with its boxes, font
  168.  * measuring functions are being set.
  169.  *
  170.  * \param content The content to be printed
  171.  * \param settings The settings for printing to use
  172.  * \return true if successful, false otherwise
  173.  */
  174. hlcache_handle *print_init(hlcache_handle *content,
  175.                 struct print_settings *settings)
  176. {
  177.         hlcache_handle* printed_content;
  178.        
  179.         hlcache_handle_clone(content, &printed_content);
  180.                        
  181.         return printed_content;
  182. }
  183.  
  184. /**
  185.  * The content is resized to fit page width.
  186.  *
  187.  * \param content The content to be printed
  188.  * \param settings The settings for printing to use
  189.  * \return true if successful, false otherwise
  190.  */
  191. bool print_apply_settings(hlcache_handle *content,
  192.                           struct print_settings *settings)
  193. {
  194.         if (settings == NULL)
  195.                 return false;
  196.        
  197.         /*Apply settings - adjust page size etc*/
  198.  
  199.         page_content_width = (settings->page_width -
  200.                         FIXTOFLT(FSUB(settings->margins[MARGINLEFT],
  201.                         settings->margins[MARGINRIGHT]))) / settings->scale;
  202.        
  203.         page_content_height = (settings->page_height -
  204.                         FIXTOFLT(FSUB(settings->margins[MARGINTOP],
  205.                         settings->margins[MARGINBOTTOM]))) / settings->scale;
  206.        
  207.         content_reformat(content, false, page_content_width, 0);
  208.  
  209.         LOG(("New layout applied.New height = %d ; New width = %d ",
  210.                         content_get_height(content),
  211.                         content_get_width(content)));
  212.        
  213.         return true;
  214. }
  215.  
  216. /**
  217.  * Memory allocated during printing is being freed here.
  218.  *
  219.  * \param content The original content
  220.  * \param printer The printer interface for the printer to be used
  221.  * \return true if successful, false otherwise
  222.  */
  223. bool print_cleanup(hlcache_handle *content, const struct printer *printer,
  224.                 struct print_settings *settings)
  225. {
  226.         printer->print_end();
  227.        
  228.         html_redraw_printing = false;
  229.        
  230.         if (printed_content) {
  231.                 hlcache_handle_release(printed_content);
  232.         }
  233.        
  234.         free((void *)settings->output);
  235.         free(settings);
  236.        
  237.         return true;
  238. }
  239.  
  240. /**
  241.  * Generates one of the predefined print settings sets.
  242.  *
  243.  * \param configuration the requested configuration
  244.  * \param filename the filename or NULL
  245.  * \param font handling functions
  246.  * \return print_settings in case if successful, NULL if unknown configuration \
  247.  *      or lack of memory.
  248.  */
  249. struct print_settings *print_make_settings(print_configuration configuration,
  250.                 const char *filename, const struct font_functions *font_func)
  251. {
  252.         struct print_settings *settings;
  253.         css_fixed length = 0;
  254.         css_unit unit = CSS_UNIT_MM;
  255.        
  256.         switch (configuration){
  257.                 case PRINT_DEFAULT:    
  258.                         settings = (struct print_settings*)
  259.                                         malloc(sizeof(struct print_settings));
  260.                         if (settings == NULL)
  261.                                 return NULL;
  262.                        
  263.                         settings->page_width  = DEFAULT_PAGE_WIDTH;
  264.                         settings->page_height = DEFAULT_PAGE_HEIGHT;
  265.                         settings->copies = DEFAULT_COPIES;
  266.  
  267.                         settings->scale = DEFAULT_EXPORT_SCALE;
  268.                        
  269.                         length = INTTOFIX(DEFAULT_MARGIN_LEFT_MM);
  270.                         settings->margins[MARGINLEFT] =
  271.                                         nscss_len2px(length, unit, NULL);
  272.                         length = INTTOFIX(DEFAULT_MARGIN_RIGHT_MM);
  273.                         settings->margins[MARGINRIGHT] =
  274.                                         nscss_len2px(length, unit, NULL);
  275.                         length = INTTOFIX(DEFAULT_MARGIN_TOP_MM);
  276.                         settings->margins[MARGINTOP] =
  277.                                         nscss_len2px(length, unit, NULL);
  278.                         length = INTTOFIX(DEFAULT_MARGIN_BOTTOM_MM);
  279.                         settings->margins[MARGINBOTTOM] =
  280.                                         nscss_len2px(length, unit, NULL);
  281.                         break;
  282.                 /* use settings from the Export options tab */
  283.                 case PRINT_OPTIONS:
  284.                         settings = (struct print_settings*)
  285.                                         malloc(sizeof(struct print_settings));
  286.                         if (settings == NULL)
  287.                                 return NULL;
  288.                        
  289.                         settings->page_width  = DEFAULT_PAGE_WIDTH;
  290.                         settings->page_height = DEFAULT_PAGE_HEIGHT;
  291.                         settings->copies = DEFAULT_COPIES;
  292.                        
  293.                         settings->scale = (float)nsoption_int(export_scale) / 100;
  294.                        
  295.                         length = INTTOFIX(nsoption_int(margin_left));
  296.                         settings->margins[MARGINLEFT] =
  297.                                         nscss_len2px(length, unit, NULL);
  298.                         length = INTTOFIX(nsoption_int(margin_right));
  299.                         settings->margins[MARGINRIGHT] =
  300.                                         nscss_len2px(length, unit, NULL);
  301.                         length = INTTOFIX(nsoption_int(margin_top));
  302.                         settings->margins[MARGINTOP] =
  303.                                         nscss_len2px(length, unit, NULL);
  304.                         length = INTTOFIX(nsoption_int(margin_bottom));
  305.                         settings->margins[MARGINBOTTOM] =
  306.                                         nscss_len2px(length, unit, NULL);
  307.                         break;
  308.                 default:
  309.                         return NULL;
  310.         }
  311.  
  312.         /* Set font functions */
  313.         settings->font_func = font_func;
  314.  
  315.         /* Output filename, or NULL if printing */
  316.         if (filename != NULL) {
  317.                 settings->output = strdup(filename);
  318.                 if (settings->output == NULL) {
  319.                         free(settings);
  320.                         return NULL;
  321.                 }
  322.         } else
  323.                 settings->output = NULL;
  324.  
  325.         return settings;       
  326. }
  327.  
  328.