source: trunk/libdjvu/DjVuToPS.h @ 206

Last change on this file since 206 was 206, checked in by Eugene Romanenko, 14 years ago

DJVU plugin: djvulibre updated to version 3.5.19

File size: 18.7 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, either Version 2 of the license,
9//C- or (at your option) any later version. The license should have
10//C- accompanied the software or you may obtain a copy of the license
11//C- from the Free Software Foundation at http://www.fsf.org .
12//C-
13//C- This program is distributed in the hope that it will be useful,
14//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//C- GNU General Public License for more details.
17//C-
18//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19//C- Lizardtech Software.  Lizardtech Software has authorized us to
20//C- replace the original DjVu(r) Reference Library notice by the following
21//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22//C-
23//C-  ------------------------------------------------------------------
24//C- | DjVu (r) Reference Library (v. 3.5)
25//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26//C- | The DjVu Reference Library is protected by U.S. Pat. No.
27//C- | 6,058,214 and patents pending.
28//C- |
29//C- | This software is subject to, and may be distributed under, the
30//C- | GNU General Public License, either Version 2 of the license,
31//C- | or (at your option) any later version. The license should have
32//C- | accompanied the software or you may obtain a copy of the license
33//C- | from the Free Software Foundation at http://www.fsf.org .
34//C- |
35//C- | The computer code originally released by LizardTech under this
36//C- | license and unmodified by other parties is deemed "the LIZARDTECH
37//C- | ORIGINAL CODE."  Subject to any third party intellectual property
38//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39//C- | non-exclusive license to make, use, sell, or otherwise dispose of
40//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42//C- | General Public License.   This grant only confers the right to
43//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44//C- | the extent such infringement is reasonably necessary to enable
45//C- | recipient to make, have made, practice, sell, or otherwise dispose
46//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47//C- | any greater extent that may be necessary to utilize further
48//C- | modifications or combinations.
49//C- |
50//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54//C- +------------------------------------------------------------------
55//
56// $Id: DjVuToPS.h,v 1.15 2007/03/25 20:48:31 leonb Exp $
57// $Name: release_3_5_19 $
58
59#ifndef _DJVU_TO_PS_H_
60#define _DJVU_TO_PS_H_
61#ifdef HAVE_CONFIG_H
62#include "config.h"
63#endif
64#if NEED_GNUG_PRAGMAS
65# pragma interface
66#endif
67
68/** @name DjVuToPS.h
69    Files #"DjVuToPS.h"# and #"DjVuToPS.cpp"# implement code that can be
70    used to convert a \Ref{DjVuImage} or \Ref{DjVuDocument} to PostScript
71    format. The conversion is carried out by the \Ref{DjVuToPS} class.
72   
73    @memo PostScript file generator
74    @author Andrei Erofeev <eaf@geocities.com> \\
75            Florin Nicsa <Florin.Nicsa@insa-lyon.fr>
76    @version
77    #$Id: DjVuToPS.h,v 1.15 2007/03/25 20:48:31 leonb Exp $#
78*/
79//@{
80
81#include "DjVuGlobal.h"
82#include "GRect.h"
83#include "DjVuDocument.h"
84#include "DjVuText.h"
85
86#ifdef HAVE_NAMESPACES
87namespace DJVU {
88# ifdef NOT_DEFINED // Just to fool emacs c++ mode
89}
90#endif
91#endif
92
93/** DjVuImage to PostScript converter.
94    Use this class to print \Ref{DjVuImage}s and \Ref{DjVuDocument}s.
95    The behavior is customizable. See \Ref{DjVuToPS::Options} for the
96    description of available options.*/
97class DjVuToPS
98{
99public:
100  class DecodePort;
101
102  /** DjVuToPS options. Use this class to customize the way
103      in which DjVu to PS conversion will be done. You can adjust
104      the following things:
105          \begin{description}
106             \item[Format] ({\em EPS} or {\em PS}). Use the {\em EPS}
107                format if you plan to embed the output image into another
108                document. Print {\em PS} otherwise.
109             \item[Language level] ({\em 1} or {\em 2}). Any PostScript
110                printer or interpreter should understand PostScript Level 1
111                files. Unfortunately we cannot efficiently compress and encode
112                data when generating Level 1 files. PostScript Level 2 allows
113                to employ an RLE compression and ASCII85 encoding scheme,
114                which makes output files significantly smaller. Most of
115                the printers and word processors nowadays support PostScript
116                Level 2.
117             \item[Orientation] ({\em PORTRAIT} or {\em LANDSCAPE})
118             \item[Zoom factor] ({\em FIT_PAGE} or {\em ONE_TO_ONE}).
119                {\em ONE_TO_ONE} mode is useful, if you want the output to
120                be of the same size as the original image (before compression).
121                This requires that the #dpi# setting inside the \Ref{DjVuImage}
122                is correct. In most of the cases the {\em FIT_PAGE} zoom is
123                would be your best choice.
124             \item[Mode] ({\em COLOR}, {\em FORE}, {\em BACK}, or {\em BW})
125                Specifies how the \Ref{DjVuImage}s will be rendered (all layers,
126                foreground layer, background layer, and the mask respectively)
127             \item[Color] ({\em TRUE} or {\em FALSE}). Choosing {\em FALSE}
128                converts color images to gray scale.
129             \item[Gamma] Printer color correction.
130                This parameter ranges from #0.3# to #5.0#.
131             \item[sRGB] ({\em TRUE} or {\em FALSE}).  Choosing {\em TRUE}
132                enables accurate sRGB color calibration.  This option
133                only works with language level 2.  When this is set,
134                gamma correction is clamped to #2.2#.
135             \item[Number of copies] Specifies how many copies should be
136                printed. This does {\bf not} affect the size of the output file.
137          \end{description}
138  */
139  class Options
140  {
141  public:
142    /** Specifies the rendering mode */
143    enum Mode { COLOR, FORE, BACK, BW };
144    /** Selects the output format */
145    enum Format { PS, EPS };
146    /** Specifies the orientation of the output image */
147    enum Orientation { PORTRAIT, LANDSCAPE, AUTO };
148    /** Specifies the booklet mode */
149    enum BookletMode { OFF, RECTO, VERSO, RECTOVERSO };
150  private:
151    Format format;
152    int level;
153    Orientation orientation;
154    Mode mode;
155    int zoom;
156    bool color;
157    bool calibrate;
158    bool text;
159    double gamma;
160    int copies;
161    bool frame;
162    bool cropmarks;
163    BookletMode bookletmode;
164    int bookletmax;
165    int bookletalign;
166    int bookletfold;
167    int bookletxfold;
168  public:
169    /** Sets output image format to #PS# or #EPS# */
170    void set_format(Format format);
171    /** Sets PostScript level (#1#, #2#, or #3#) */
172    void set_level(int level);
173    /** Sets orientation (#LANDSCAPE# or #PORTRAIT#) */
174    void set_orientation(Orientation orientation);
175    /** Sets \Ref{DjVuImage} rendering mode (#COLOR#, #BW#, #FORE#, #BACK#) */
176    void set_mode(Mode mode);
177    /** Sets zoom factor. Zoom 0 means fit page. */
178    void set_zoom(int zoom);
179    /** Affects automatic conversion to GreyScale mode. */
180    void set_color(bool color);
181    /** Sets gamma correction factor. Ranges from #0.3# to #5.0#. */   
182    void set_gamma(double gamma);
183    /** Sets sRGB color calibration flag. */
184    void set_sRGB(bool calibrate);
185    /** Specifies the number of copies to be printed. */
186    void set_copies(int copies);
187    /** Specifies if a gray frame around the image should be printed. */
188    void set_frame(bool on);
189    /** Specifies if crop marks should be printed. */
190    void set_cropmarks(bool on);
191    /** Specifies if a shadow text should be printed. */
192    void set_text(bool on);
193    /** Specifies the bookletmode */
194    void set_bookletmode(BookletMode m);
195    /** Specifies the maximal number of pages in a booklet */
196    void set_bookletmax(int m);
197    /** Specifies an offset (points) between
198        booklet recto(s) and verso(s). */
199    void set_bookletalign(int m);
200    /** Specifies the margin (points) required to fold
201        the booklet (#fold# in points) and the margin
202        increase required for each sheet (#xfold# in millipoints). */
203    void set_bookletfold(int fold, int xfold=0);
204
205    /** Returns output image format (#PS# or #EPS#) */
206    Format get_format(void) const {
207      return format; }
208    /** Returns PostScript level (#1# or #2#) */
209    int get_level(void) const {
210      return level; }
211    /** Returns output image orientation (#PORTRAIT# or #LANDSCAPE#) */
212    Orientation get_orientation(void) const {
213      return orientation; }
214    /** Returns \Ref{DjVuImage} rendering mode
215        (#COLOR#, #FORE#, #BACK#, #BW#) */
216    Mode get_mode(void) const {
217      return mode; }
218    /** Returns output zoom factor (#FIT_PAGE# or #ONE_TO_ONE#) */
219    int get_zoom(void) const {
220      return zoom; }
221    /** Returns color printing flag. */
222    bool get_color(void) const {
223      return color; }
224    /** Returns sRGB color calibration flag. */
225    bool get_sRGB(void) const {
226      return calibrate; }
227    /** Returns printer gamma correction factor */
228    double get_gamma(void) const {
229      return ((calibrate) ? ((double)2.2) : gamma); }
230    /** Returns the number of copies, which will be printed by printer
231        This parameter does {\bf not} affect the size of output file */
232    int get_copies(void) const {
233      return copies; }
234    /** Returns #TRUE# if there will be a gray frame */
235    bool get_frame(void) const {
236      return frame; }
237    /** Returns #TRUE# if there will be a gray frame */
238    bool get_cropmarks(void) const {
239      return cropmarks; }
240    /** Returns #TRUE# if there will be a shadow text printed */
241    bool get_text(void) const {
242      return text; }
243    /** Returns the booklet mode */
244    BookletMode get_bookletmode(void) const {
245      return bookletmode; }
246    /** Returns the booklet max size */
247    int get_bookletmax(void) {
248      return bookletmax; }
249    /** Returns the booklet recto/verso offset */
250    int get_bookletalign(void) {
251      return bookletalign; }
252    /** Returns the folding margin for sheet number #n#. */
253    int get_bookletfold(int n=0) {
254      return bookletfold + (n*bookletxfold+500)/1000; }
255    /* Constructor */
256    Options(void);
257  };
258 
259  /** Describes current page processing stage. This is passed to
260      the #info_cb()# callback. See \Ref{set_info_cb}() for details. */
261  enum Stage { DECODING, PRINTING };
262 
263private:
264  void (*refresh_cb)(void*);
265  void  *refresh_cl_data;
266  void (*prn_progress_cb)(double, void*);
267  void  *prn_progress_cl_data;
268  void (*dec_progress_cb)(double, void*);
269  void  *dec_progress_cl_data;
270  void (*info_cb)(int,int,int,Stage,void*);
271  void  *info_cl_data;
272  unsigned char ramp[256];
273  GP<DecodePort> port;
274protected:
275  void store_doc_prolog(ByteStream&,int,int,GRect*);
276  void store_doc_setup(ByteStream&);
277  void store_doc_trailer(ByteStream&);
278  void store_page_setup(ByteStream&,int,const GRect&,int align=0);
279  void store_page_trailer(ByteStream&);
280  void make_gamma_ramp(GP<DjVuImage>);
281  void print_image_lev1(ByteStream&,GP<DjVuImage>,const GRect&);
282  void print_image_lev2(ByteStream&,GP<DjVuImage>,const GRect&);
283  void print_bg(ByteStream&,GP<DjVuImage>,const GRect&);
284  void print_fg_3layer(ByteStream&,GP<DjVuImage>,const GRect&,unsigned char*);
285  void print_fg_2layer(ByteStream&,GP<DjVuImage>,const GRect&,unsigned char*);
286  void print_fg(ByteStream&,GP<DjVuImage>,const GRect&);
287  void print_image(ByteStream&,GP<DjVuImage>,const GRect&,GP<DjVuTXT>);
288  void parse_range(GP<DjVuDocument>,GUTF8String,GList<int>&);
289  GP<DjVuImage> decode_page(GP<DjVuDocument>,int,int,int);
290  void process_single_page(ByteStream&,GP<DjVuDocument>,int,int,int,int);
291  void process_double_page(ByteStream&,GP<DjVuDocument>,void*,int,int);
292 
293public:
294  /** Options affecting the print result. Please refer to
295      \Ref{DjVuToPS::Options} for details. */
296  Options options;
297 
298  /** @name Callbacks */
299  //@{
300  /** Refresh callback is a function, which will be called fairly
301      often while the image (document) is being printed. It can
302      be used to refresh a GUI, if necessary.
303     
304      @param refresh_cb Callback function to be called periodically
305      @param refresh_cl_data Pointer passed to #refresh_cb()# */
306   void set_refresh_cb(void (*refresh_cb)(void*), void *refresh_cl_data);
307  /** Callback used to report the progress of printing.  The progress is a
308      double number from 0 to 1.  If an existing \Ref{DjVuImage} is printed,
309      this callback will be called at least twice: in the beginning and at the
310      end of printing.  If a \Ref{DjVuDocument} is being printed, this
311      callback will be used to report printing progress of every page. To
312      learn the number of the page being printed you can use
313      \Ref{set_info_cb}() function.  See \Ref{set_dec_progress_cb}() to find
314      out how to learn the decoding progress.
315     
316      @param cb Callback function to be called
317      @param data Pointer passed to #cb()#. */
318  void set_prn_progress_cb(void (*cb)(double, void*), void *data);
319  /** Callback used to report the progress of decoding.  The progress is a
320      double number from 0 to 1.  This callback is only used when printing a
321      \Ref{DjVuDocument} in a multithreaded environment. In all other cases it
322      will not be called.  Whenever you \Ref{print}() a page range from a
323      \Ref{DjVuDocument}, the #DjVuToPS# has to decode the mentioned pages
324      before writing them to the output \Ref{ByteStream}. This callback can be
325      helpful to find out the status of the decoding.  See
326      \Ref{set_prn_progress_cb}() to find out how to learn the printing
327      progress.  See \Ref{set_info_cb}() to learn how to find out the number
328      of the page being processed, the total number of pages and the number of
329      processed pages.
330         
331      @param cb Callback function to be called
332      @param data Pointer passed to #cb()#. */
333  void set_dec_progress_cb(void (*cb)(double, void*), void *data);
334  /** Callback used to report the current printing stage of a
335      \Ref{DjVuDocument}.  When printing a \Ref{DjVuDocument} ({\bf not} a
336      \Ref{DjVuImage}), the #DjVuToPS# class will decode and output every page
337      mentioned in the {\em page range}. Before decoding and outputing, it
338      will call this #info_cb()# callback in order to let you know about what
339      is going on. This can be quite useful in a GUI program to keep the user
340      informed.  This function is not called when you print a \Ref{DjVuImage}.
341
342      Description of the arguments passed to #info_cb#:
343      \begin{description}
344      \item[page_num] The number of the page being processed
345      \item[page_cnt] Counts how many pages have already been processed.
346      \item[tot_pages] Counts how many pages will be output enventually.
347      \item[stage] Describes the current processing stage
348                   (#DECODING# or #PRINTING#).
349      \end{description}
350      @param cb Callback function to be called
351      @param data Pointer, which will be passed to #cb()#. */
352  void set_info_cb(void (*cb)(int,int,int,Stage,void*), void *data);
353  //@}
354 
355  /** Prints the specified \Ref{DjVuImage} #dimg# into the
356      \Ref{ByteStream} #str#. The function will first scale
357      the image to fit the #img_rect#, then extract #prn_rect#
358      from the obtained bitmap and will output it in the
359      PostScript format. The function generates a legal PostScript
360      (or Encapsulated PostScript) file taking care of all comments
361      conforming to Document Structure Conventions v. 3.0.
362     
363      {\bf Warning:} The zoom factor specified in \Ref{Options} does
364      not affect the amount of data stored into the PostScript file.
365      It will be used by the PostScript code to additionally scale
366      the image. We cannot pre-scale it here, because we do not know
367      the future resolution of the printer. The #img_rect# and
368      #prn_rect# alone define how much data will be sent to printer.
369     
370      Using #img_rect# one can upsample or downsample the image prior
371      to sending it to the printer.
372     
373      @param str \Ref{ByteStream} where PostScript output will be sent
374      @param dimg \Ref{DjVuImage} to print
375      @param img_rect Rectangle to which the \Ref{DjVuImage} will be scaled.
376             Note that this parameters defines the amount of data
377             that will actually be sent to the printer. The PostScript
378             code can futher resize the image according to the
379             #zoom# parameter from the \Ref{Options} structure.
380      @param prn_rect Part of img_rect to send to printer.
381      @param override_dpi Optional parameter allowing you to override
382             dpi setting that would otherwise be extracted from #dimg# */
383   void print(ByteStream&, GP<DjVuImage> dimg,
384              const GRect &prn_rect, const GRect &img_rect,
385              int override_dpi=-1 );
386 
387  /** Outputs the specifies pages from the \Ref{DjVuDocument} into the
388      \Ref{ByteStream} in PostScript format.  The function will generate a
389      multipage PostScript document conforming to PS DSC 3.0 by storing into
390      it every page mentioned in the #page_range#.
391
392      If #page_range# is empty, all pages from the \Ref{DjVuDocument} #doc#
393      will be printed.  The #page_range# is a set of ranges separated by
394      commas. Every range has this form: {\em start_page}[-{\em
395      end_page}]. {\em end_page} is optional and can be less than the {\em
396      start_page}, in which case the pages will be printed in the reverse
397      order.
398
399      Examples:
400      \begin{itemize}
401      \item {\bf 1-10} - Will print pages 1 to 10.
402      \item {\bf 10-1} - Will print pages 1 to 10 in reverse order.
403      \item {\bf 1-10,12-20} - Will print pages 1 to 20 with page 11 skipped.
404      \end{itemize} */
405  void print(ByteStream&, GP<DjVuDocument> doc, GUTF8String page_range);
406  void print(ByteStream&, GP<DjVuDocument> doc);
407 
408 
409  /** Default constructor. Initializes the class. */
410  DjVuToPS(void);
411};
412
413
414//****************************************************************************
415//******************************** DjVuToPS **********************************
416//****************************************************************************
417
418//@}
419// ------------
420
421#ifdef HAVE_NAMESPACES
422}
423# ifndef NOT_USING_DJVU_NAMESPACE
424using namespace DJVU;
425# endif
426#endif
427#endif
Note: See TracBrowser for help on using the repository browser.