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"
19 #include "support/convert.h"
20 #include "support/debug.h"
21 #include "support/lstrings.h"
27 using namespace lyx::support;
32 const string PDFOptions::pagemode_fullscreen("FullScreen");
34 bool PDFOptions::empty() const
36 PDFOptions x; //implicit hyperref settings
38 return author == x.author
40 && subject == x.subject
41 && keywords == x.keywords
42 && pagemode == x.pagemode
43 && quoted_options == x.quoted_options
44 && bookmarks == x.bookmarks
45 && bookmarksnumbered == x.bookmarksnumbered
46 && bookmarksopen == x.bookmarksopen
47 && bookmarksopenlevel == x.bookmarksopenlevel
48 && breaklinks == x.breaklinks
49 && pdfborder == x.pdfborder
50 && colorlinks == x.colorlinks
51 && backref == x.backref
52 && pdfusetitle == x.pdfusetitle;
56 void PDFOptions::writeFile(ostream & os) const
58 os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
59 if (!use_hyperref && empty())
63 os << "\\pdf_title \"" << title << "\"\n";
65 os << "\\pdf_author \"" << author << "\"\n";
67 os << "\\pdf_subject \"" << subject << "\"\n";
68 if (!keywords.empty())
69 os << "\\pdf_keywords \"" << keywords << "\"\n";
72 os << "\\pdf_bookmarks " << convert<string>(bookmarks) << '\n';
73 os << "\\pdf_bookmarksnumbered " << convert<string>(bookmarksnumbered) << '\n';
74 os << "\\pdf_bookmarksopen " << convert<string>(bookmarksopen) << '\n';
75 os << "\\pdf_bookmarksopenlevel " << bookmarksopenlevel << '\n';
77 os << "\\pdf_breaklinks " << convert<string>(breaklinks) << '\n';
78 os << "\\pdf_pdfborder " << convert<string>(pdfborder) << '\n';
79 os << "\\pdf_colorlinks " << convert<string>(colorlinks) << '\n';
80 os << "\\pdf_backref " << backref << '\n';
81 os << "\\pdf_pdfusetitle " << convert<string>(pdfusetitle) << '\n';
83 if (!pagemode.empty())
84 os << "\\pdf_pagemode " << pagemode << '\n';
86 if (!quoted_options.empty())
87 os << "\\pdf_quoted_options \"" << quoted_options << "\"\n";
91 int PDFOptions::writeLaTeX(OutputParams & runparams, odocstream & os,
92 bool hyperref_already_provided) const
98 // since LyX uses unicode, also set the PDF strings to unicode strings with the
99 // hyperref option "unicode"
100 opt += "unicode=true, ";
102 // try to extract author and title from document when none is
104 if (pdfusetitle && title.empty() && author.empty())
105 opt += "pdfusetitle,";
108 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
110 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
111 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
113 opt += "bookmarksopenlevel="
114 + convert<string>(bookmarksopenlevel) + ',';
117 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
119 opt += "pdfborder={0 0 ";
120 opt += (pdfborder ? '0' : '1');
123 opt += "backref=" + backref + ',';
124 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
125 if (!pagemode.empty())
126 opt += "pdfpagemode=" + pagemode + ',';
130 // load the pdftitle etc. as hypersetup, otherwise you'll get
131 // LaTeX-errors when using non-latin characters
134 hyperset += "pdftitle={" + title + "},";
136 hyperset += "\n pdfauthor={" + author + "},";
137 if (!subject.empty())
138 hyperset += "\n pdfsubject={" + subject + "},";
139 if (!keywords.empty())
140 hyperset += "\n pdfkeywords={" + keywords + "},";
141 if (!quoted_options.empty()){
143 hyperset += quoted_options;
145 hyperset = rtrim(hyperset,",");
147 // check if the hyperref settings use an encoding that exceeds
148 // ours. If so, we have to switch to utf8.
149 Encoding const * const enc = runparams.encoding;
150 docstring const hs = from_utf8(hyperset);
151 bool need_unicode = false;
153 for (size_t n = 0; n < hs.size(); ++n) {
154 if (enc->latexChar(hs[n], true) != docstring(1, hs[n]))
159 // use in \\usepackage parameter as not all options can be handled inside \\hypersetup
160 if (!hyperref_already_provided) {
161 opt = rtrim(opt, ",");
162 opt = "\\usepackage[" + opt + "]\n {hyperref}\n";
164 if (!hyperset.empty())
165 opt += "\\hypersetup{" + hyperset + "}\n";
167 // only in case hyperref is already loaded by the current text class
168 // try to put it into hyperset
170 // FIXME: this still does not fix the cases where hyperref is loaded
171 // and the option is active only when part of usepackage parameter
172 // (e.g. pdfusetitle).
174 opt = "\\hypersetup{" + opt + hyperset + "}\n";
177 lines = int(count(opt.begin(), opt.end(), '\n'));
179 // hyperref expects utf8!
180 if (need_unicode && enc && enc->iconvName() != "UTF-8") {
181 os << "\\inputencoding{utf8}\n"
182 << setEncoding("UTF-8");
185 os << from_utf8(opt);
186 if (need_unicode && enc && enc->iconvName() != "UTF-8") {
187 os << setEncoding(enc->iconvName())
188 << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
195 string PDFOptions::readToken(Lexer &lex, string const & token)
197 if (token == "\\use_hyperref") {
199 } else if (token == "\\pdf_title") {
201 } else if (token == "\\pdf_author") {
203 } else if (token == "\\pdf_subject") {
205 } else if (token == "\\pdf_keywords") {
207 } else if (token == "\\pdf_bookmarks") {
209 } else if (token == "\\pdf_bookmarksnumbered") {
210 lex >> bookmarksnumbered;
211 } else if (token == "\\pdf_bookmarksopen") {
212 lex >> bookmarksopen;
213 } else if (token == "\\pdf_bookmarksopenlevel") {
214 lex >> bookmarksopenlevel;
215 } else if (token == "\\pdf_breaklinks") {
217 } else if (token == "\\pdf_pdfborder") {
219 } else if (token == "\\pdf_colorlinks") {
221 } else if (token == "\\pdf_backref") {
223 } else if (token == "\\pdf_pdfusetitle") {
225 } else if (token == "\\pdf_pagemode") {
227 } else if (token == "\\pdf_quoted_options") {
228 lex >> quoted_options;
236 // check the string from UI
237 string PDFOptions::quoted_options_check(string const str) const
239 return subst(str, "\n", "");
243 // set implicit settings for hyperref
244 void PDFOptions::clear()
246 use_hyperref = false;
252 bookmarksnumbered = false;
253 bookmarksopen = false;
254 bookmarksopenlevel = 1;
260 quoted_options.clear();
261 pdfusetitle = true; //in contrast with hyperref