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