]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
Amend 9f04eeae032d
[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         // Driver needed by specific converters
101         if (!runparams.hyperref_driver.empty())
102                 opt += runparams.hyperref_driver + ",";
103
104         // Since LyX uses unicode, also set the PDF strings to unicode strings
105         // with the hyperref option "unicode". (With Xe/LuaTeX and pTeX,
106         // unicode=true is the default, with Japanese (platex), the option
107         // leads to errors (even if the input encoding is UTF-8).)
108         if (!runparams.isFullUnicode() && !runparams.use_japanese)
109                 opt += "unicode=true,";
110
111         // only use the hyperref settings if hyperref is enabled by the user
112         // see bug #7052
113         if (use_hyperref) {
114                 // try to extract author and title from document when none is
115                 // explicitly given
116                 if (pdfusetitle && title.empty() && author.empty())
117                         opt += "pdfusetitle,";
118                 opt += "\n ";
119                 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
120                 if (bookmarks) {
121                         opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
122                         opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
123                         if (bookmarksopen)
124                                 opt += "bookmarksopenlevel="
125                                 + convert<string>(bookmarksopenlevel) + ',';
126                 }
127                 opt += "\n ";
128                 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
129                 opt += "pdfborder={0 0 ";
130                 opt += (pdfborder ? '0' : '1');
131                 opt += "},";
132                 if (pdfborder)
133                         opt += "pdfborderstyle={},";
134                 opt += "backref=" + backref + ',';
135                 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
136                 if (!pagemode.empty())
137                         opt += "pdfpagemode=" + pagemode + ',';
138
139                 // load the pdftitle etc. as hypersetup, otherwise you'll get
140                 // LaTeX-errors when using non-latin characters
141                 if (!title.empty())
142                         hyperset += "pdftitle={" + title + "},";
143                 if (!author.empty())
144                         hyperset += "\n pdfauthor={" + author + "},";
145                 if (!subject.empty())
146                         hyperset += "\n pdfsubject={" + subject + "},";
147                 if (!keywords.empty())
148                         hyperset += "\n pdfkeywords={" + keywords + "},";
149                 if (!quoted_options.empty()){
150                         hyperset += "\n ";
151                         hyperset += quoted_options;
152                 }
153                 hyperset = rtrim(hyperset,",");
154         }
155
156         // check if the hyperref settings use an encoding that exceeds
157         // ours. If so, we have to switch to utf8.
158         Encoding const * const enc = runparams.encoding;
159         docstring const hs = from_utf8(hyperset);
160         bool need_unicode = false;
161         if (enc) {
162                 for (size_t n = 0; n < hs.size(); ++n) {
163                         if (!enc->encodable(hs[n]))
164                                 need_unicode = true;
165                 }
166         }
167
168         // use in \\usepackage parameter as not all options can be handled inside \\hypersetup
169         if (!hyperref_already_provided) {
170                 opt = rtrim(opt, ",");
171                 opt = "\\usepackage[" + opt + "]\n {hyperref}\n";
172
173                 if (!hyperset.empty())
174                         opt += "\\hypersetup{" + hyperset + "}\n";
175         } else {
176                 // only in case hyperref is already loaded by the current text class
177                 // try to put it into hyperset
178                 //
179                 // FIXME: rename in this case the PDF settings dialog checkbox
180                 //  label from "Use Hyperref" to "Customize Hyperref Settings"
181                 //  as discussd in bug #6293
182                 opt = "\\hypersetup{" + rtrim(opt + hyperset, ",") + "}\n";
183         }
184
185         // hyperref expects LICR macros for non-ASCII chars.
186         // Usually, "(lua)inputenc" converts the input to LICR, with XeTeX utf-8 works, too.
187         // As hyperref provides good coverage for \inputencoding{utf8}, we can try
188         // this if the current input encoding does not support a character.
189         // FIXME: don't use \inputencoding if "inputenc" is not loaded (#9839).
190         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
191             if (runparams.flavor != OutputParams::XETEX)
192                         os << "\\inputencoding{utf8}\n";
193                 os << setEncoding("UTF-8");
194         }
195         // If hyperref is loaded by the document class, we output
196         // \hypersetup \AtBeginDocument if hypersetup is not (yet)
197         // defined. In this case, the class loads hyperref late
198         // (see bug #7048).
199         if (hyperref_already_provided && !opt.empty()) {
200                 os << "\\ifx\\hypersetup\\undefined\n"
201                    << "  \\AtBeginDocument{%\n    "
202                    << from_utf8(opt)
203                    << "  }\n"
204                    << "\\else\n  "
205                    << from_utf8(opt)
206                    << "\\fi\n";
207         } else
208                 os << from_utf8(opt);
209         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
210                 os << setEncoding(enc->iconvName());
211             if (runparams.flavor != OutputParams::XETEX)
212                         os << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
213         }
214 }
215
216
217 string PDFOptions::readToken(Lexer &lex, string const & token)
218 {
219         if (token == "\\use_hyperref") {
220                 lex >> use_hyperref;
221         } else if (token == "\\pdf_title") {
222                 if (lex.isOK()) {
223                         lex.next(true);
224                         title = lex.getString();
225                 }
226         } else if (token == "\\pdf_author") {
227                 if (lex.isOK()) {
228                         lex.next(true);
229                         author = lex.getString();
230                 }
231         } else if (token == "\\pdf_subject") {
232                 if (lex.isOK()) {
233                         lex.next(true);
234                         subject = lex.getString();
235                 }
236         } else if (token == "\\pdf_keywords") {
237                 if (lex.isOK()) {
238                         lex.next(true);
239                         keywords = lex.getString();
240                 }
241         } else if (token == "\\pdf_bookmarks") {
242                 lex >> bookmarks;
243         } else if (token == "\\pdf_bookmarksnumbered") {
244                 lex >> bookmarksnumbered;
245         } else if (token == "\\pdf_bookmarksopen") {
246                 lex >> bookmarksopen;
247         } else if (token == "\\pdf_bookmarksopenlevel") {
248                 lex >> bookmarksopenlevel;
249         } else if (token == "\\pdf_breaklinks") {
250                 lex >> breaklinks;
251         } else if (token == "\\pdf_pdfborder") {
252                 lex >> pdfborder;
253         } else if (token == "\\pdf_colorlinks") {
254                 lex >> colorlinks;
255         } else if (token == "\\pdf_backref") {
256                 lex >> backref;
257         } else if (token == "\\pdf_pdfusetitle") {
258                 lex >> pdfusetitle;
259         } else if (token == "\\pdf_pagemode") {
260                 lex >> pagemode;
261         } else if (token == "\\pdf_quoted_options") {
262                 if (lex.isOK()) {
263                         lex.next(true);
264                         quoted_options = lex.getString();
265                 }
266         } else {
267                 return token;
268         }
269         return string();
270 }
271
272
273 // check the string from UI
274 string PDFOptions::quoted_options_check(string const & str) const
275 {
276         return subst(str, "\n", "");
277 }
278
279
280 // set implicit settings for hyperref
281 void PDFOptions::clear()
282 {
283         use_hyperref            = false;
284         title.clear();
285         author.clear();
286         subject.clear();
287         keywords.clear();
288         bookmarks               = true;
289         bookmarksnumbered       = false;
290         bookmarksopen           = false;
291         bookmarksopenlevel      = 1;
292         breaklinks              = false;
293         pdfborder               = false;
294         colorlinks              = false;
295         backref                 = "false";
296         pagemode.clear();
297         quoted_options.clear();
298         pdfusetitle             = true;  //in contrast with hyperref
299 }
300
301 } // namespace lyx