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".
106 // (With Xe/LuaTeX and pTeX, unicode=true is the default
107 // and the option leads to errors with some Japanese document classes)
108 if (!runparams.isFullUnicode()
109 && (!runparams.use_japanese || runparams.encoding->iconvName() == "UTF-8"))
110 opt += "unicode=true,";
112 // only use the hyperref settings if hyperref is enabled by the user
115 // try to extract author and title from document when none is
117 if (pdfusetitle && title.empty() && author.empty())
118 opt += "pdfusetitle,";
120 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
122 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
123 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
125 opt += "bookmarksopenlevel="
126 + convert<string>(bookmarksopenlevel) + ',';
129 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
130 opt += "pdfborder={0 0 ";
131 opt += (pdfborder ? '0' : '1');
134 opt += "pdfborderstyle={},";
135 opt += "backref=" + backref + ',';
136 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
137 if (!pagemode.empty())
138 opt += "pdfpagemode=" + pagemode + ',';
140 // load the pdftitle etc. as hypersetup, otherwise you'll get
141 // LaTeX-errors when using non-latin characters
143 hyperset += "pdftitle={" + title + "},";
145 hyperset += "\n pdfauthor={" + author + "},";
146 if (!subject.empty())
147 hyperset += "\n pdfsubject={" + subject + "},";
148 if (!keywords.empty())
149 hyperset += "\n pdfkeywords={" + keywords + "},";
150 if (!quoted_options.empty()){
152 hyperset += quoted_options;
154 hyperset = rtrim(hyperset,",");
157 // check if the hyperref settings use an encoding that exceeds
158 // ours. If so, we have to switch to utf8.
159 Encoding const * const enc = runparams.encoding;
160 docstring const hs = from_utf8(hyperset);
161 bool need_unicode = false;
163 for (size_t n = 0; n < hs.size(); ++n) {
164 if (!enc->encodable(hs[n]))
169 // use in \\usepackage parameter as not all options can be handled inside \\hypersetup
170 if (!hyperref_already_provided) {
171 opt = rtrim(opt, ",");
172 opt = "\\usepackage[" + opt + "]\n {hyperref}\n";
174 if (!hyperset.empty())
175 opt += "\\hypersetup{" + hyperset + "}\n";
177 // only in case hyperref is already loaded by the current text class
178 // try to put it into hyperset
180 // FIXME: rename in this case the PDF settings dialog checkbox
181 // label from "Use Hyperref" to "Customize Hyperref Settings"
182 // as discussd in bug #6293
183 opt = "\\hypersetup{" + rtrim(opt + hyperset, ",") + "}\n";
186 // hyperref expects LICR macros for non-ASCII chars.
187 // Usually, "(lua)inputenc" converts the input to LICR, with XeTeX utf-8 works, too.
188 // As hyperref provides good coverage for \inputencoding{utf8}, we can try
189 // this if the current input encoding does not support a character.
190 // FIXME: don't use \inputencoding if "inputenc" is not loaded (#9839).
191 if (need_unicode && enc && enc->iconvName() != "UTF-8") {
192 if (runparams.flavor != OutputParams::XETEX)
193 os << "\\inputencoding{utf8}\n";
194 os << setEncoding("UTF-8");
196 // If hyperref is loaded by the document class, we output
197 // \hypersetup \AtBeginDocument if hypersetup is not (yet)
198 // defined. In this case, the class loads hyperref late
200 if (hyperref_already_provided && !opt.empty()) {
201 os << "\\ifx\\hypersetup\\undefined\n"
202 << " \\AtBeginDocument{%\n "
209 os << from_utf8(opt);
210 if (need_unicode && enc && enc->iconvName() != "UTF-8") {
211 os << setEncoding(enc->iconvName());
212 if (runparams.flavor != OutputParams::XETEX)
213 os << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
218 string PDFOptions::readToken(Lexer &lex, string const & token)
220 if (token == "\\use_hyperref") {
222 } else if (token == "\\pdf_title") {
225 title = lex.getString();
227 } else if (token == "\\pdf_author") {
230 author = lex.getString();
232 } else if (token == "\\pdf_subject") {
235 subject = lex.getString();
237 } else if (token == "\\pdf_keywords") {
240 keywords = lex.getString();
242 } else if (token == "\\pdf_bookmarks") {
244 } else if (token == "\\pdf_bookmarksnumbered") {
245 lex >> bookmarksnumbered;
246 } else if (token == "\\pdf_bookmarksopen") {
247 lex >> bookmarksopen;
248 } else if (token == "\\pdf_bookmarksopenlevel") {
249 lex >> bookmarksopenlevel;
250 } else if (token == "\\pdf_breaklinks") {
252 } else if (token == "\\pdf_pdfborder") {
254 } else if (token == "\\pdf_colorlinks") {
256 } else if (token == "\\pdf_backref") {
258 } else if (token == "\\pdf_pdfusetitle") {
260 } else if (token == "\\pdf_pagemode") {
262 } else if (token == "\\pdf_quoted_options") {
265 quoted_options = lex.getString();
274 // check the string from UI
275 string PDFOptions::quoted_options_check(string const & str) const
277 return subst(str, "\n", "");
281 // set implicit settings for hyperref
282 void PDFOptions::clear()
284 use_hyperref = false;
290 bookmarksnumbered = false;
291 bookmarksopen = false;
292 bookmarksopenlevel = 1;
298 quoted_options.clear();
299 pdfusetitle = true; //in contrast with hyperref