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 with the
105 // hyperref option "unicode"
106 opt += "unicode=true,";
108 // only use the hyperref settings if hyperref is enabled by the user
111 // try to extract author and title from document when none is
113 if (pdfusetitle && title.empty() && author.empty())
114 opt += "pdfusetitle,";
116 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
118 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
119 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
121 opt += "bookmarksopenlevel="
122 + convert<string>(bookmarksopenlevel) + ',';
125 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
126 opt += "pdfborder={0 0 ";
127 opt += (pdfborder ? '0' : '1');
130 opt += "pdfborderstyle={},";
131 opt += "backref=" + backref + ',';
132 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
133 if (!pagemode.empty())
134 opt += "pdfpagemode=" + pagemode + ',';
136 // load the pdftitle etc. as hypersetup, otherwise you'll get
137 // LaTeX-errors when using non-latin characters
139 hyperset += "pdftitle={" + title + "},";
141 hyperset += "\n pdfauthor={" + author + "},";
142 if (!subject.empty())
143 hyperset += "\n pdfsubject={" + subject + "},";
144 if (!keywords.empty())
145 hyperset += "\n pdfkeywords={" + keywords + "},";
146 if (!quoted_options.empty()){
148 hyperset += quoted_options;
150 hyperset = rtrim(hyperset,",");
153 // check if the hyperref settings use an encoding that exceeds
154 // ours. If so, we have to switch to utf8.
155 Encoding const * const enc = runparams.encoding;
156 docstring const hs = from_utf8(hyperset);
157 bool need_unicode = false;
159 for (size_t n = 0; n < hs.size(); ++n) {
160 if (!enc->encodable(hs[n]))
165 // use in \\usepackage parameter as not all options can be handled inside \\hypersetup
166 if (!hyperref_already_provided) {
167 opt = rtrim(opt, ",");
168 opt = "\\usepackage[" + opt + "]\n {hyperref}\n";
170 if (!hyperset.empty())
171 opt += "\\hypersetup{" + hyperset + "}\n";
173 // only in case hyperref is already loaded by the current text class
174 // try to put it into hyperset
176 // FIXME: rename in this case the PDF settings dialog checkbox
177 // label from "Use Hyperref" to "Customize Hyperref Settings"
178 // as discussd in bug #6293
179 opt = "\\hypersetup{" + rtrim(opt + hyperset, ",") + "}\n";
182 // hyperref expects LICR macros for non-ASCII chars.
183 // Usually, "(lua)inputenc" converts the input to LICR, with XeTeX utf-8 works, too.
184 // As hyperref provides good coverage for \inputencoding{utf8}, we can try
185 // this if the current input encoding does not support a character.
186 // FIXME: don't use \inputencoding if "inputenc" is not loaded (#9839).
187 if (need_unicode && enc && enc->iconvName() != "UTF-8") {
188 if (runparams.flavor != OutputParams::XETEX)
189 os << "\\inputencoding{utf8}\n";
190 os << setEncoding("UTF-8");
192 // If hyperref is loaded by the document class, we output
193 // \hypersetup \AtBeginDocument if hypersetup is not (yet)
194 // defined. In this case, the class loads hyperref late
196 if (hyperref_already_provided && !opt.empty()) {
197 os << "\\ifx\\hypersetup\\undefined\n"
198 << " \\AtBeginDocument{%\n "
205 os << from_utf8(opt);
206 if (need_unicode && enc && enc->iconvName() != "UTF-8") {
207 os << setEncoding(enc->iconvName());
208 if (runparams.flavor != OutputParams::XETEX)
209 os << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
214 string PDFOptions::readToken(Lexer &lex, string const & token)
216 if (token == "\\use_hyperref") {
218 } else if (token == "\\pdf_title") {
221 title = lex.getString();
223 } else if (token == "\\pdf_author") {
226 author = lex.getString();
228 } else if (token == "\\pdf_subject") {
231 subject = lex.getString();
233 } else if (token == "\\pdf_keywords") {
236 keywords = lex.getString();
238 } else if (token == "\\pdf_bookmarks") {
240 } else if (token == "\\pdf_bookmarksnumbered") {
241 lex >> bookmarksnumbered;
242 } else if (token == "\\pdf_bookmarksopen") {
243 lex >> bookmarksopen;
244 } else if (token == "\\pdf_bookmarksopenlevel") {
245 lex >> bookmarksopenlevel;
246 } else if (token == "\\pdf_breaklinks") {
248 } else if (token == "\\pdf_pdfborder") {
250 } else if (token == "\\pdf_colorlinks") {
252 } else if (token == "\\pdf_backref") {
254 } else if (token == "\\pdf_pdfusetitle") {
256 } else if (token == "\\pdf_pagemode") {
258 } else if (token == "\\pdf_quoted_options") {
261 quoted_options = lex.getString();
270 // check the string from UI
271 string PDFOptions::quoted_options_check(string const & str) const
273 return subst(str, "\n", "");
277 // set implicit settings for hyperref
278 void PDFOptions::clear()
280 use_hyperref = false;
286 bookmarksnumbered = false;
287 bookmarksopen = false;
288 bookmarksopenlevel = 1;
294 quoted_options.clear();
295 pdfusetitle = true; //in contrast with hyperref