source: wkhtmltox/trunk/fuentes/src/pdf/pdfarguments.cc @ 51

Last change on this file since 51 was 51, checked in by mabarracus, 4 years ago

wip

File size: 13.6 KB
Line 
1// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2// vi:set ts=4 sts=4 sw=4 noet :
3//
4// Copyright 2010, 2011 wkhtmltopdf authors
5//
6// This file is part of wkhtmltopdf.
7//
8// wkhtmltopdf is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Lesser General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12//
13// wkhtmltopdf is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public License
19// along with wkhtmltopdf.  If not, see <http://www.gnu.org/licenses/>.
20
21#include "arghandler.inl"
22#include "pdfcommandlineparser.hh"
23#include <QFile>
24#include <qglobal.h>
25#include <wkhtmltox/pdfconverter.hh>
26
27/*!
28  \class ArgHandler
29  \brief Class responsible for handling an argument
30*/
31
32/*!
33  \var ArgHandler::longName
34  \brief The long name of the argument, e.g. "help" for "--help"
35*/
36
37/*!
38  \var ArgHandler::desc
39  \brief A descriptive text of the argument
40*/
41
42/*!
43  \var ArgHandler::shortSwitch
44  \brief Sort name, e.g. 'h' for '-h', if 0 there is no short name
45*/
46
47/*!
48  \var ArgHandler::argn
49  \brief The names of the arguments to the switch
50*/
51
52/*!
53  \var ArgHandler::display
54  \brief Indicate that the argument is not hidden
55*/
56
57/*!
58  \var ArgHandler::extended
59  \brief Indicate if the argument is an extended argument
60*/
61
62/*!
63  \var ArgHandler::qthack
64  \brief Indicate that the argument is only available with hacked qt
65*/
66
67/*!
68  \fn ArgHandler::operator()(const char ** args, CommandLineParserPrivate & parser)
69  Callend when the switch was specified
70  \param args The arguments to the switch, guarantied to have size of argn
71  \param settings The settings to store the information in
72*/
73
74
75/*!
76  \class CommandLineParserPrivate
77  Implementation details for CommandLineParser
78*/
79
80using namespace wkhtmltopdf::settings;
81
82struct UnitRealTM: public SomeSetterTM<UnitReal> {
83        static UnitReal strToT(const char * val, bool &ok) {
84                return strToUnitReal(val, &ok);
85        }
86        static QString TToStr(const UnitReal & u, bool & ok) {
87                return unitRealToStr(u, &ok);
88        }
89};
90/*!
91  Argument handler setting a real-number/unit combo variable
92 */
93typedef SomeSetter<UnitRealTM> UnitRealSetter;
94
95struct PageSizeTM: public SomeSetterTM<QPrinter::PageSize> {
96        static QPrinter::PageSize strToT(const char * val, bool &ok) {
97                return strToPageSize(val, &ok);
98        }
99        static QString TToStr(const QPrinter::PageSize & s, bool & ok) {
100                ok=true;
101                return pageSizeToStr(s);
102        }
103};
104/*!
105  Argument handler setting a page size variable
106 */
107typedef SomeSetter<PageSizeTM> PageSizeSetter;
108
109struct OrientationTM: public SomeSetterTM<QPrinter::Orientation> {
110        static QPrinter::Orientation strToT(const char * val, bool &ok) {
111                return strToOrientation(val, &ok);
112        }
113        static QString TToStr(const QPrinter::Orientation & o, bool & ok) {
114                ok=true;
115                return orientationToStr(o);
116        }
117};
118/*!
119  Argument handler setting a orientation variable
120 */
121typedef SomeSetter<OrientationTM> OrientationSetter;
122
123struct DefaultTocFunc {
124        bool operator()(const char **, CommandLineParserBase &, char *) {
125                QFile file;
126                file.open(stdout, QIODevice::WriteOnly | QIODevice::Text);
127                QTextStream stream(&file);
128                wkhtmltopdf::settings::TableOfContent toc;
129                wkhtmltopdf::dumpDefaultTOCStyleSheet(stream, toc);
130                exit(0);
131        }
132};
133
134
135/*!
136  Set the default header
137*/
138struct DefaultHeaderFunc {
139        bool operator()(const char **, CommandLineParserBase & p, char * page) {
140                reinterpret_cast<PdfObject*>(page)->header.left="[webpage]";
141                reinterpret_cast<PdfObject*>(page)->header.right="[page]/[topage]";
142                reinterpret_cast<PdfObject*>(page)->header.line=true;
143                static_cast<PdfCommandLineParser&>(p).globalSettings.margin.top = strToUnitReal("2cm");
144                return true;
145        }
146};
147
148/*!
149  Setup default book mode
150*/
151struct BookFunc {
152        bool operator()(const char **, CommandLineParserBase &) {
153                //p.settings.header.left="[section]";
154                //p.settings.header.right="[page]/[toPage]";
155                //p.settings.header.line=true;
156                //p.settings.outline = true;
157                //p.settings.printToc = true;
158                //p.settings.margin.top = Settings::strToUnitReal("2cm");
159                return true;
160        }
161};
162
163/*!
164  Construct the commandline parser adding all the arguments
165  \param s The settings to store values in
166*/
167PdfCommandLineParser::PdfCommandLineParser(PdfGlobal & s, QList<PdfObject> & ps):
168        readArgsFromStdin(false),
169        globalSettings(s),
170        pageSettings(ps) {
171        section("Global Options");
172        mode(global);
173
174        addDocArgs();
175
176        extended(false);
177        qthack(false);
178
179        addarg("quiet", 'q', "Be less verbose", new ConstSetter<bool>(s.quiet,true));
180
181        addarg("no-collate", 0, "Do not collate when printing multiple copies", new ConstSetter<bool>(s.collate, false));
182        addarg("collate", 0, "Collate when printing multiple copies", new ConstSetter<bool>(s.collate, true));
183
184        addarg("copies", 0, "Number of copies to print into the pdf file", new IntSetter(s.copies, "number"));
185        addarg("orientation",'O',"Set orientation to Landscape or Portrait", new OrientationSetter(s.orientation, "orientation"));
186        addarg("page-size",'s',"Set paper size to: A4, Letter, etc.", new PageSizeSetter(s.size.pageSize, "Size"));
187
188        addarg("grayscale",'g',"PDF will be generated in grayscale", new ConstSetter<QPrinter::ColorMode>(s.colorMode,QPrinter::GrayScale));
189
190        addarg("lowquality",'l',"Generates lower quality pdf/ps. Useful to shrink the result document space", new ConstSetter<QPrinter::PrinterMode>(s.resolution,QPrinter::ScreenResolution));
191        addarg("title", 0, "The title of the generated pdf file (The title of the first document is used if not specified)", new QStrSetter(s.documentTitle,"text"));
192
193        addarg("read-args-from-stdin", 0, "Read command line arguments from stdin", new ConstSetter<bool>(readArgsFromStdin, true) );
194
195        extended(true);
196        qthack(false);
197        addarg("margin-bottom",'B',"Set the page bottom margin", new UnitRealSetter(s.margin.bottom,"unitreal"));
198        addarg("margin-left",'L',"Set the page left margin", new UnitRealSetter(s.margin.left,"unitreal"));
199        addarg("margin-right",'R',"Set the page right margin", new UnitRealSetter(s.margin.right,"unitreal"));
200        addarg("margin-top",'T',"Set the page top margin", new UnitRealSetter(s.margin.top,"unitreal"));
201
202        addarg("dpi",'d',"Change the dpi explicitly (this has no effect on X11 based systems)", new IntSetter(s.dpi,"dpi"));
203        addarg("page-height", 0, "Page height", new UnitRealSetter(s.size.height,"unitreal"));
204        addarg("page-width", 0, "Page width", new UnitRealSetter(s.size.width,"unitreal"));
205
206        addGlobalLoadArgs(s.load);
207
208        extended(true);
209        qthack(true);
210
211        addarg("image-quality", 0, "When jpeg compressing images use this quality", new IntSetter(s.imageQuality,"integer"));
212        addarg("image-dpi", 0, "When embedding images scale them down to this dpi", new IntSetter(s.imageDPI, "integer"));
213        addarg("no-pdf-compression", 0 , "Do not use lossless compression on pdf objects", new ConstSetter<bool>(s.useCompression,false));
214
215#ifdef Q_OS_UNIX
216        addarg("use-xserver",0,"Use the X server (some plugins and other stuff might not work without X11)", new ConstSetter<bool>(s.useGraphics,true));
217#endif
218
219        section("Outline Options");
220        extended(true);
221        qthack(true);
222        addarg("outline",0,"Put an outline into the pdf", new ConstSetter<bool>(s.outline,true));
223        addarg("no-outline",0,"Do not put an outline into the pdf", new ConstSetter<bool>(s.outline,false));
224        addarg("outline-depth",0,"Set the depth of the outline", new IntSetter(s.outlineDepth,"level"));
225        addarg("dump-outline",0,"Dump the outline to a file",new QStrSetter(s.dumpOutline,"file"));
226        addarg("dump-default-toc-xsl",0,"Dump the default TOC xsl style sheet to stdout", new Caller<DefaultTocFunc>());
227
228        section("Page Options");
229        mode(page);
230        addarg("default-header",0,"Add a default header, with the name of the page to the left, and the page number to the right, this is short for: --header-left='[webpage]' --header-right='[page]/[toPage]' --top 2cm --header-line", new Caller<DefaultHeaderFunc>());
231
232        addarg("viewport-size", 0, "Set viewport size if you have custom scrollbars or css attribute overflow to emulate window size",new QStrSetter(s.viewportSize,""));
233        addWebArgs(od.web);
234        extended(true);
235        qthack(false);
236        addarg("no-background",0,"Do not print background", new ConstSetter<bool>(od.web.background, false));
237        addarg("background",0,"Do print background", new ConstSetter<bool>(od.web.background, true));
238
239        extended(true);
240        qthack(true);
241        addarg("include-in-outline", 0, "Include the page in the table of contents and outlines", new ConstSetter<bool>(od.includeInOutline, true));
242        addarg("exclude-from-outline", 0, "Do not include the page in the table of contents and outlines", new ConstSetter<bool>(od.includeInOutline, false));
243
244        addarg("disable-smart-shrinking", 0, "Disable the intelligent shrinking strategy used by WebKit that makes the pixel/dpi ratio none constant",new ConstSetter<bool>(od.web.enableIntelligentShrinking, false));
245        addarg("enable-smart-shrinking", 0, "Enable the intelligent shrinking strategy used by WebKit that makes the pixel/dpi ratio none constant",new ConstSetter<bool>(od.web.enableIntelligentShrinking, true));
246
247        extended(false);
248        qthack(true);
249        addarg("print-media-type",0,"Use print media-type instead of screen", new ConstSetter<bool>(od.web.printMediaType,true));
250        addarg("no-print-media-type",0,"Do not use print media-type instead of screen", new ConstSetter<bool>(od.web.printMediaType, false));
251
252        extended(true);
253        qthack(true);
254        addarg("enable-forms", 0, "Turn HTML form fields into pdf form fields", new ConstSetter<bool>(od.produceForms, true));
255        addarg("disable-forms", 0, "Do not turn HTML form fields into pdf form fields", new ConstSetter<bool>(od.produceForms, false));
256        addarg("disable-internal-links",0,"Do not make local links", new ConstSetter<bool>(od.useLocalLinks, false));
257        addarg("enable-internal-links",0,"Make local links", new ConstSetter<bool>(od.useLocalLinks, true));
258        addarg("disable-external-links",0,"Do not make links to remote web pages", new ConstSetter<bool>(od.useExternalLinks, false));
259        addarg("enable-external-links",0,"Make links to remote web pages", new ConstSetter<bool>(od.useExternalLinks, true));
260        addarg("resolve-relative-links", 0, "Resolve relative external links into absolute links", new ConstSetter<bool>(s.resolveRelativeLinks, true));
261        addarg("keep-relative-links", 0, "Keep relative external links as relative external links", new ConstSetter<bool>(s.resolveRelativeLinks, false));
262
263        addarg("enable-toc-back-links",0,"Link from section header to toc", new ConstSetter<bool>(od.toc.backLinks,true));
264        addarg("disable-toc-back-links",0,"Do not link from section header to toc", new ConstSetter<bool>(od.toc.backLinks,false));
265
266        addPageLoadArgs(od.load);
267        addarg("page-offset",0,"Set the starting page number", new IntSetter(s.pageOffset,"offset",1));
268
269        section("Headers And Footer Options");
270        qthack(true);
271        extended(true);
272        addarg("footer-center",0,"Centered footer text", new QStrSetter(od.footer.center,"text"));
273        addarg("footer-font-name",0,"Set footer font name", new QStrSetter(od.footer.fontName,"name"));;
274        addarg("footer-font-size",0,"Set footer font size", new IntSetter(od.footer.fontSize,"size"));
275        addarg("footer-left",0,"Left aligned footer text", new QStrSetter(od.footer.left,"text"));
276        addarg("footer-line",0,"Display line above the footer", new ConstSetter<bool>(od.footer.line,true));
277        addarg("no-footer-line",0,"Do not display line above the footer", new ConstSetter<bool>(od.footer.line,false));
278        addarg("footer-right",0,"Right aligned footer text", new QStrSetter(od.footer.right,"text"));
279        addarg("footer-spacing",0,"Spacing between footer and content in mm", new FloatSetter(od.footer.spacing,"real"));
280        addarg("footer-html",0,"Adds a html footer", new QStrSetter(od.footer.htmlUrl,"url"));
281        addarg("header-center",0,"Centered header text", new QStrSetter(od.header.center,"text"));
282        addarg("header-font-name",0,"Set header font name", new QStrSetter(od.header.fontName,"name"));
283        addarg("header-font-size",0,"Set header font size", new IntSetter(od.header.fontSize,"size"));
284        addarg("header-left",0,"Left aligned header text", new QStrSetter(od.header.left,"text"));
285        addarg("header-line",0,"Display line below the header", new ConstSetter<bool>(od.header.line,true));
286        addarg("no-header-line",0,"Do not display line below the header", new ConstSetter<bool>(od.header.line,false));
287        addarg("header-right",0,"Right aligned header text", new QStrSetter(od.header.right,"text"));
288        addarg("header-spacing",0,"Spacing between header and content in mm", new FloatSetter(od.header.spacing,"real"));
289        addarg("header-html",0,"Adds a html header", new QStrSetter(od.header.htmlUrl,"url"));
290
291        addarg("replace",0, "Replace [name] with value in header and footer (repeatable)", new MapSetter<>(od.replacements, "name", "value"));
292
293        section("TOC Options");
294        mode(toc);
295        extended(true);
296        qthack(true);
297        addarg("xsl-style-sheet",0,"Use the supplied xsl style sheet for printing the table of content", new QStrSetter(od.tocXsl,"file"));
298
299        addarg("toc-header-text",0,"The header text of the toc", new QStrSetter(od.toc.captionText, "text"));
300        addarg("disable-toc-links",0,"Do not link from toc to sections", new ConstSetter<bool>(od.toc.forwardLinks, false));
301        addarg("disable-dotted-lines",0,"Do not use dotted lines in the toc", new ConstSetter<bool>(od.toc.useDottedLines,false));
302        addarg("toc-text-size-shrink",0,"For each level of headings in the toc the font is scaled by this factor", new FloatSetter(od.toc.fontScale, "real"));
303        addarg("toc-level-indentation",0,"For each level of headings in the toc indent by this length", new QStrSetter(od.toc.indentation, "width"));
304}
Note: See TracBrowser for help on using the repository browser.