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 "texstream.h"
20 #include "support/convert.h"
21 #include "support/debug.h"
22 #include "support/lstrings.h"
29 using namespace lyx::support;
34 const string PDFOptions::pagemode_fullscreen("FullScreen");
36 bool PDFOptions::empty() const
38 PDFOptions x; //implicit hyperref settings
40 return author == x.author
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;
58 void PDFOptions::writeFile(ostream & os) const
60 os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
61 if (!use_hyperref && empty())
65 os << "\\pdf_title " << Lexer::quoteString(title) << '\n';
67 os << "\\pdf_author " << Lexer::quoteString(author) << '\n';
69 os << "\\pdf_subject " << Lexer::quoteString(subject) << '\n';
70 if (!keywords.empty())
71 os << "\\pdf_keywords " << Lexer::quoteString(keywords) << '\n';
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';
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';
85 if (!pagemode.empty())
86 os << "\\pdf_pagemode " << pagemode << '\n';
88 if (!quoted_options.empty())
89 os << "\\pdf_quoted_options " << Lexer::quoteString(quoted_options) << '\n';
93 void PDFOptions::writeLaTeX(OutputParams & runparams, otexstream & os,
94 bool hyperref_already_provided) const
100 // Driver needed by specific converters
101 if (!runparams.hyperref_driver.empty())
102 opt += runparams.hyperref_driver + ",";
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,";
111 // only use the hyperref settings if hyperref is enabled by the user
114 // try to extract author and title from document when none is
116 if (pdfusetitle && title.empty() && author.empty())
117 opt += "pdfusetitle,";
119 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
121 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
122 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
124 opt += "bookmarksopenlevel="
125 + convert<string>(bookmarksopenlevel) + ',';
128 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
129 opt += "pdfborder={0 0 ";
130 opt += (pdfborder ? '0' : '1');
133 opt += "pdfborderstyle={},";
134 opt += "backref=" + backref + ',';
135 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
136 if (!pagemode.empty())
137 opt += "pdfpagemode=" + pagemode + ',';
139 // load the pdftitle etc. as hypersetup, otherwise you'll get
140 // LaTeX-errors when using non-latin characters
142 hyperset += "pdftitle={" + title + "},";
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()){
151 hyperset += quoted_options;
153 hyperset = rtrim(hyperset,",");
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;
162 for (size_t n = 0; n < hs.size(); ++n) {
163 if (!enc->encodable(hs[n]))
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";
173 if (!hyperset.empty())
174 opt += "\\hypersetup{" + hyperset + "}\n";
176 // only in case hyperref is already loaded by the current text class
177 // try to put it into hyperset
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";
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");
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
199 if (hyperref_already_provided && !opt.empty()) {
200 os << "\\ifx\\hypersetup\\undefined\n"
201 << " \\AtBeginDocument{%\n "
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";
217 string PDFOptions::readToken(Lexer &lex, string const & token)
219 if (token == "\\use_hyperref") {
221 } else if (token == "\\pdf_title") {
224 title = lex.getString();
226 } else if (token == "\\pdf_author") {
229 author = lex.getString();
231 } else if (token == "\\pdf_subject") {
234 subject = lex.getString();
236 } else if (token == "\\pdf_keywords") {
239 keywords = lex.getString();
241 } else if (token == "\\pdf_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") {
251 } else if (token == "\\pdf_pdfborder") {
253 } else if (token == "\\pdf_colorlinks") {
255 } else if (token == "\\pdf_backref") {
257 } else if (token == "\\pdf_pdfusetitle") {
259 } else if (token == "\\pdf_pagemode") {
261 } else if (token == "\\pdf_quoted_options") {
264 quoted_options = lex.getString();
273 // check the string from UI
274 string PDFOptions::quoted_options_check(string const & str) const
276 return subst(str, "\n", "");
280 // set implicit settings for hyperref
281 void PDFOptions::clear()
283 use_hyperref = false;
289 bookmarksnumbered = false;
290 bookmarksopen = false;
291 bookmarksopenlevel = 1;
297 quoted_options.clear();
298 pdfusetitle = true; //in contrast with hyperref