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"
18 #include "OutputParams.h"
19 #include "texstream.h"
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"
32 using namespace lyx::support;
37 const string PDFOptions::pagemode_fullscreen("FullScreen");
39 bool PDFOptions::empty() const
41 PDFOptions x; //implicit hyperref settings
43 return author == x.author
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;
61 void PDFOptions::writeFile(ostream & os) const
63 os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
64 if (!use_hyperref && empty())
68 os << "\\pdf_title " << Lexer::quoteString(title) << '\n';
70 os << "\\pdf_author " << Lexer::quoteString(author) << '\n';
72 os << "\\pdf_subject " << Lexer::quoteString(subject) << '\n';
73 if (!keywords.empty())
74 os << "\\pdf_keywords " << Lexer::quoteString(keywords) << '\n';
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';
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';
88 if (!pagemode.empty())
89 os << "\\pdf_pagemode " << pagemode << '\n';
91 if (!quoted_options.empty())
92 os << "\\pdf_quoted_options " << Lexer::quoteString(quoted_options) << '\n';
96 void PDFOptions::writeLaTeX(OutputParams & runparams, otexstream & os,
97 bool hyperref_already_provided) const
103 // Driver needed by specific converters
104 if (!runparams.hyperref_driver.empty())
105 opt += runparams.hyperref_driver + ",";
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,";
114 // only use the hyperref settings if hyperref is enabled by the user
117 // try to extract author and title from document when none is
119 if (pdfusetitle && title.empty() && author.empty())
120 opt += "pdfusetitle,";
122 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
124 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
125 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
127 opt += "bookmarksopenlevel="
128 + convert<string>(bookmarksopenlevel) + ',';
131 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
132 opt += "pdfborder={0 0 ";
133 opt += (pdfborder ? '0' : '1');
136 opt += "pdfborderstyle={},";
137 opt += "backref=" + backref + ',';
138 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
139 if (!pagemode.empty())
140 opt += "pdfpagemode=" + pagemode + ',';
142 // load the pdftitle etc. as hypersetup, otherwise you'll get
143 // LaTeX-errors when using non-latin characters
145 hyperset += "pdftitle={" + title + "},";
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()){
154 hyperset += quoted_options;
156 hyperset = rtrim(hyperset,",");
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;
165 for (char_type h : hs) {
166 if (!enc->encodable(h))
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";
176 if (!hyperset.empty())
177 opt += "\\hypersetup{" + hyperset + "}\n";
179 // only in case hyperref is already loaded by the current text class
180 // try to put it into hyperset
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";
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");
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
202 if (hyperref_already_provided && !opt.empty()) {
203 os << "\\ifx\\hypersetup\\undefined\n"
204 << " \\AtBeginDocument{%\n "
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";
220 string PDFOptions::readToken(Lexer &lex, string const & token)
222 if (token == "\\use_hyperref") {
224 } else if (token == "\\pdf_title") {
227 title = lex.getString();
229 } else if (token == "\\pdf_author") {
232 author = lex.getString();
234 } else if (token == "\\pdf_subject") {
237 subject = lex.getString();
239 } else if (token == "\\pdf_keywords") {
242 keywords = lex.getString();
244 } else if (token == "\\pdf_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") {
254 } else if (token == "\\pdf_pdfborder") {
256 } else if (token == "\\pdf_colorlinks") {
258 } else if (token == "\\pdf_backref") {
260 } else if (token == "\\pdf_pdfusetitle") {
262 } else if (token == "\\pdf_pagemode") {
264 } else if (token == "\\pdf_quoted_options") {
267 quoted_options = lex.getString();
276 // check the string from UI
277 string PDFOptions::quoted_options_check(string const & str) const
279 return subst(str, "\n", "");
283 // set implicit settings for hyperref
284 void PDFOptions::clear()
286 use_hyperref = false;
292 bookmarksnumbered = false;
293 bookmarksopen = false;
294 bookmarksopenlevel = 1;
300 quoted_options.clear();
301 pdfusetitle = true; //in contrast with hyperref