source: trunk/libdjvu/DjVuToPS.h @ 76

Last change on this file since 76 was 17, checked in by Eugene Romanenko, 16 years ago

update makefiles, remove absolute paths, update djvulibre to version 3.5.17

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