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