]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
Account for old versions of Pygments
[lyx.git] / src / PDFOptions.cpp
1 /**
2  * \file PDFoptions.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Pavel Sanda
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "PDFOptions.h"
15
16 #include "Encoding.h"
17 #include "Lexer.h"
18 #include "texstream.h"
19
20 #include "support/convert.h"
21 #include "support/debug.h"
22 #include "support/lstrings.h"
23
24 #include <sstream>
25 #include <string>
26 #include <algorithm>
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32
33
34 const string PDFOptions::pagemode_fullscreen("FullScreen");
35
36 bool PDFOptions::empty() const
37 {
38         PDFOptions x; //implicit hyperref settings
39
40         return  author == x.author
41                 && title == x.title
42                 && subject == x.subject
43                 && keywords == x.keywords
44                 && pagemode == x.pagemode
45                 && quoted_options == x.quoted_options
46                 && bookmarks == x.bookmarks
47                 && bookmarksnumbered == x.bookmarksnumbered
48                 && bookmarksopen == x.bookmarksopen
49                 && bookmarksopenlevel == x.bookmarksopenlevel
50                 && breaklinks == x.breaklinks
51                 && pdfborder == x.pdfborder
52                 && colorlinks == x.colorlinks
53                 && backref == x.backref
54                 && pdfusetitle == x.pdfusetitle;
55 }
56
57
58 void PDFOptions::writeFile(ostream & os) const
59 {
60         os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
61         if (!use_hyperref && empty())
62                 return;
63         
64         if (!title.empty() )
65                 os << "\\pdf_title " << Lexer::quoteString(title) << '\n';
66         if (!author.empty())
67                 os << "\\pdf_author " << Lexer::quoteString(author) << '\n';
68         if (!subject.empty())
69                 os << "\\pdf_subject " << Lexer::quoteString(subject) << '\n';
70         if (!keywords.empty())
71                 os << "\\pdf_keywords " << Lexer::quoteString(keywords) << '\n';
72         
73         
74         os << "\\pdf_bookmarks " << convert<string>(bookmarks) << '\n';
75         os << "\\pdf_bookmarksnumbered " << convert<string>(bookmarksnumbered) << '\n';
76         os << "\\pdf_bookmarksopen " << convert<string>(bookmarksopen) << '\n';
77         os << "\\pdf_bookmarksopenlevel " << bookmarksopenlevel << '\n';
78         
79         os << "\\pdf_breaklinks "  << convert<string>(breaklinks)  << '\n';
80         os << "\\pdf_pdfborder "   << convert<string>(pdfborder)   << '\n';
81         os << "\\pdf_colorlinks "  << convert<string>(colorlinks)  << '\n';
82         os << "\\pdf_backref "     << backref << '\n';
83         os << "\\pdf_pdfusetitle " << convert<string>(pdfusetitle) << '\n';
84         
85         if (!pagemode.empty())
86                 os << "\\pdf_pagemode " << pagemode << '\n';
87         
88         if (!quoted_options.empty())
89                 os << "\\pdf_quoted_options " << Lexer::quoteString(quoted_options) << '\n';
90 }
91
92
93 void PDFOptions::writeLaTeX(OutputParams & runparams, otexstream & os,
94                             bool hyperref_already_provided) const
95 {
96         // FIXME Unicode
97         string opt;
98         string hyperset;
99         
100         // since LyX uses unicode, also set the PDF strings to unicode strings with the
101         // hyperref option "unicode"
102         opt += "unicode=true,";
103
104         // only use the hyperref settings if hyperref is enabled by the user
105         // see bug #7052
106         if (use_hyperref) {
107                 // try to extract author and title from document when none is
108                 // explicitly given
109                 if (pdfusetitle && title.empty() && author.empty())
110                         opt += "pdfusetitle,";
111                 opt += "\n ";
112                 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
113                 if (bookmarks) {
114                         opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
115                         opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
116                         if (bookmarksopen)
117                                 opt += "bookmarksopenlevel="
118                                 + convert<string>(bookmarksopenlevel) + ',';
119                 }
120                 opt += "\n ";
121                 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
122                 opt += "pdfborder={0 0 ";
123                 opt += (pdfborder ? '0' : '1');
124                 opt += "},";
125                 if (pdfborder)
126                         opt += "pdfborderstyle={},";
127                 opt += "backref=" + backref + ',';
128                 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
129                 if (!pagemode.empty())
130                         opt += "pdfpagemode=" + pagemode + ',';
131
132                 // load the pdftitle etc. as hypersetup, otherwise you'll get
133                 // LaTeX-errors when using non-latin characters
134                 if (!title.empty())
135                         hyperset += "pdftitle={" + title + "},";
136                 if (!author.empty())                    
137                         hyperset += "\n pdfauthor={" + author + "},";
138                 if (!subject.empty())
139                         hyperset += "\n pdfsubject={" + subject + "},";
140                 if (!keywords.empty())
141                         hyperset += "\n pdfkeywords={" + keywords + "},";
142                 if (!quoted_options.empty()){
143                         hyperset += "\n ";
144                         hyperset += quoted_options;
145                 }
146                 hyperset = rtrim(hyperset,",");
147         }
148
149         // check if the hyperref settings use an encoding that exceeds
150         // ours. If so, we have to switch to utf8.
151         Encoding const * const enc = runparams.encoding;
152         docstring const hs = from_utf8(hyperset);
153         bool need_unicode = false;
154         if (enc) {
155                 for (size_t n = 0; n < hs.size(); ++n) {
156                         if (!enc->encodable(hs[n]))
157                                 need_unicode = true;
158                 }
159         }
160
161         // use in \\usepackage parameter as not all options can be handled inside \\hypersetup
162         if (!hyperref_already_provided) {
163                 opt = rtrim(opt, ",");
164                 opt = "\\usepackage[" + opt + "]\n {hyperref}\n";
165
166                 if (!hyperset.empty())
167                         opt += "\\hypersetup{" + hyperset + "}\n";
168         } else {
169                 // only in case hyperref is already loaded by the current text class
170                 // try to put it into hyperset
171                 //
172                 // FIXME: rename in this case the PDF settings dialog checkbox
173                 //  label from "Use Hyperref" to "Customize Hyperref Settings"
174                 //  as discussd in bug #6293
175                 opt = "\\hypersetup{" + rtrim(opt + hyperset, ",") + "}\n";
176         }
177
178         // hyperref expects LICR macros for non-ASCII chars.
179         // Usually, "(lua)inputenc" converts the input to LICR, with XeTeX utf-8 works, too.
180         // As hyperref provides good coverage for \inputencoding{utf8}, we can try
181         // this if the current input encoding does not support a character.
182         // FIXME: don't use \inputencoding if "inputenc" is not loaded (#9839).
183         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
184             if (runparams.flavor != OutputParams::XETEX)
185                         os << "\\inputencoding{utf8}\n";
186                 os << setEncoding("UTF-8");
187         }
188         // If hyperref is loaded by the document class, we output
189         // \hypersetup \AtBeginDocument if hypersetup is not (yet)
190         // defined. In this case, the class loads hyperref late
191         // (see bug #7048).
192         if (hyperref_already_provided && !opt.empty()) {
193                 os << "\\ifx\\hypersetup\\undefined\n"
194                    << "  \\AtBeginDocument{%\n    "
195                    << from_utf8(opt)
196                    << "  }\n"
197                    << "\\else\n  "
198                    << from_utf8(opt)
199                    << "\\fi\n";
200         } else
201                 os << from_utf8(opt);
202         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
203                 os << setEncoding(enc->iconvName());
204             if (runparams.flavor != OutputParams::XETEX)
205                         os << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
206         }
207 }
208
209
210 string PDFOptions::readToken(Lexer &lex, string const & token)
211 {
212         if (token == "\\use_hyperref") {
213                 lex >> use_hyperref;
214         } else if (token == "\\pdf_title") {
215                 if (lex.isOK()) {
216                         lex.next(true);
217                         title = lex.getString();
218                 }
219         } else if (token == "\\pdf_author") {
220                 if (lex.isOK()) {
221                         lex.next(true);
222                         author = lex.getString();
223                 }
224         } else if (token == "\\pdf_subject") {
225                 if (lex.isOK()) {
226                         lex.next(true);
227                         subject = lex.getString();
228                 }
229         } else if (token == "\\pdf_keywords") {
230                 if (lex.isOK()) {
231                         lex.next(true);
232                         keywords = lex.getString();
233                 }
234         } else if (token == "\\pdf_bookmarks") {
235                 lex >> bookmarks;
236         } else if (token == "\\pdf_bookmarksnumbered") {
237                 lex >> bookmarksnumbered;
238         } else if (token == "\\pdf_bookmarksopen") {
239                 lex >> bookmarksopen;
240         } else if (token == "\\pdf_bookmarksopenlevel") {
241                 lex >> bookmarksopenlevel;
242         } else if (token == "\\pdf_breaklinks") {
243                 lex >> breaklinks;
244         } else if (token == "\\pdf_pdfborder") {
245                 lex >> pdfborder;
246         } else if (token == "\\pdf_colorlinks") {
247                 lex >> colorlinks;
248         } else if (token == "\\pdf_backref") {
249                 lex >> backref;
250         } else if (token == "\\pdf_pdfusetitle") {
251                 lex >> pdfusetitle;
252         } else if (token == "\\pdf_pagemode") {
253                 lex >> pagemode;
254         } else if (token == "\\pdf_quoted_options") {
255                 if (lex.isOK()) {
256                         lex.next(true);
257                         quoted_options = lex.getString();
258                 }
259         } else {
260                 return token;
261         }
262         return string();
263 }
264
265
266 // check the string from UI
267 string PDFOptions::quoted_options_check(string const & str) const
268 {
269         return subst(str, "\n", "");
270 }
271
272
273 // set implicit settings for hyperref
274 void PDFOptions::clear()
275 {
276         use_hyperref            = false;
277         title.clear();
278         author.clear();
279         subject.clear();
280         keywords.clear();
281         bookmarks               = true;
282         bookmarksnumbered       = false;
283         bookmarksopen           = false;
284         bookmarksopenlevel      = 1;
285         breaklinks              = false;
286         pdfborder               = false;
287         colorlinks              = false;
288         backref                 = "false";
289         pagemode.clear();
290         quoted_options.clear();
291         pdfusetitle             = true;  //in contrast with hyperref
292 }
293
294 } // namespace lyx