3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "PDFOptions.h"
17 #include "LaTeXFeatures.h"
19 #include "OutputParams.h"
20 #include "texstream.h"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/docstream.h"
25 #include "support/docstring.h"
26 #include "support/lstrings.h"
33 using namespace lyx::support;
38 const string PDFOptions::pagemode_fullscreen("FullScreen");
40 bool PDFOptions::empty() const
42 PDFOptions x; //implicit hyperref settings
44 return author == x.author
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;
62 void PDFOptions::writeFile(ostream & os) const
64 os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
65 if (!use_hyperref && empty())
69 os << "\\pdf_title " << Lexer::quoteString(title) << '\n';
71 os << "\\pdf_author " << Lexer::quoteString(author) << '\n';
73 os << "\\pdf_subject " << Lexer::quoteString(subject) << '\n';
74 if (!keywords.empty())
75 os << "\\pdf_keywords " << Lexer::quoteString(keywords) << '\n';
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';
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';
89 if (!pagemode.empty())
90 os << "\\pdf_pagemode " << pagemode << '\n';
92 if (!quoted_options.empty())
93 os << "\\pdf_quoted_options " << Lexer::quoteString(quoted_options) << '\n';
97 void PDFOptions::writeLaTeX(OutputParams & runparams, otexstream & os,
98 bool hyperref_already_provided) const
104 // Driver needed by specific converters
105 if (!runparams.hyperref_driver.empty())
106 opt += runparams.hyperref_driver + ",";
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::isAvailable("hyperref-2021/02/04")
112 && !runparams.isFullUnicode() && !runparams.use_japanese)
113 opt += "unicode=true,";
115 // only use the hyperref settings if hyperref is enabled by the user
118 // try to extract author and title from document when none is
120 if (pdfusetitle && title.empty() && author.empty())
121 opt += "pdfusetitle,";
124 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
126 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
127 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
129 opt += "bookmarksopenlevel="
130 + convert<string>(bookmarksopenlevel) + ',';
134 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
135 opt += "pdfborder={0 0 ";
136 opt += (pdfborder ? '0' : '1');
139 opt += "pdfborderstyle={},";
140 opt += "backref=" + backref + ',';
141 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
142 if (!pagemode.empty())
143 opt += "pdfpagemode=" + pagemode + ',';
145 // load the pdftitle etc. as hypersetup, otherwise you'll get
146 // LaTeX-errors when using non-latin characters
148 hyperset += "pdftitle={" + title + "},";
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()){
157 hyperset += quoted_options;
159 hyperset = rtrim(hyperset,",");
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;
168 for (char_type h : hs) {
169 if (!enc->encodable(h))
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";
179 if (!hyperset.empty())
180 opt += "\\hypersetup{" + hyperset + "}\n";
182 // only in case hyperref is already loaded by the current text class
183 // try to put it into hyperset
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";
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");
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
205 if (hyperref_already_provided && !opt.empty()) {
206 os << "\\ifx\\hypersetup\\undefined\n"
207 << " \\AtBeginDocument{%\n "
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";
223 string PDFOptions::readToken(Lexer &lex, string const & token)
225 if (token == "\\use_hyperref") {
227 } else if (token == "\\pdf_title") {
230 title = lex.getString();
232 } else if (token == "\\pdf_author") {
235 author = lex.getString();
237 } else if (token == "\\pdf_subject") {
240 subject = lex.getString();
242 } else if (token == "\\pdf_keywords") {
245 keywords = lex.getString();
247 } else if (token == "\\pdf_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") {
257 } else if (token == "\\pdf_pdfborder") {
259 } else if (token == "\\pdf_colorlinks") {
261 } else if (token == "\\pdf_backref") {
263 } else if (token == "\\pdf_pdfusetitle") {
265 } else if (token == "\\pdf_pagemode") {
267 } else if (token == "\\pdf_quoted_options") {
270 quoted_options = lex.getString();
279 // check the string from UI
280 string PDFOptions::quoted_options_check(string const & str) const
282 return subst(str, "\n", "");
286 // set implicit settings for hyperref
287 void PDFOptions::clear()
289 use_hyperref = false;
295 bookmarksnumbered = false;
296 bookmarksopen = false;
297 bookmarksopenlevel = 1;
303 quoted_options.clear();
304 pdfusetitle = true; //in contrast with hyperref