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"
28 using namespace lyx::support;
33 const string PDFOptions::pagemode_fullscreen("FullScreen");
35 bool PDFOptions::empty() const
37 PDFOptions x; //implicit hyperref settings
39 return author == x.author
41 && subject == x.subject
42 && keywords == x.keywords
43 && pagemode == x.pagemode
44 && quoted_options == x.quoted_options
45 && bookmarks == x.bookmarks
46 && bookmarksnumbered == x.bookmarksnumbered
47 && bookmarksopen == x.bookmarksopen
48 && bookmarksopenlevel == x.bookmarksopenlevel
49 && breaklinks == x.breaklinks
50 && pdfborder == x.pdfborder
51 && colorlinks == x.colorlinks
52 && backref == x.backref
53 && pdfusetitle == x.pdfusetitle;
57 void PDFOptions::writeFile(ostream & os) const
59 os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
60 if (!use_hyperref && empty())
64 os << "\\pdf_title \"" << title << "\"\n";
66 os << "\\pdf_author \"" << author << "\"\n";
68 os << "\\pdf_subject \"" << subject << "\"\n";
69 if (!keywords.empty())
70 os << "\\pdf_keywords \"" << keywords << "\"\n";
73 os << "\\pdf_bookmarks " << convert<string>(bookmarks) << '\n';
74 os << "\\pdf_bookmarksnumbered " << convert<string>(bookmarksnumbered) << '\n';
75 os << "\\pdf_bookmarksopen " << convert<string>(bookmarksopen) << '\n';
76 os << "\\pdf_bookmarksopenlevel " << bookmarksopenlevel << '\n';
78 os << "\\pdf_breaklinks " << convert<string>(breaklinks) << '\n';
79 os << "\\pdf_pdfborder " << convert<string>(pdfborder) << '\n';
80 os << "\\pdf_colorlinks " << convert<string>(colorlinks) << '\n';
81 os << "\\pdf_backref " << backref << '\n';
82 os << "\\pdf_pdfusetitle " << convert<string>(pdfusetitle) << '\n';
84 if (!pagemode.empty())
85 os << "\\pdf_pagemode " << pagemode << '\n';
87 if (!quoted_options.empty())
88 os << "\\pdf_quoted_options \"" << quoted_options << "\"\n";
92 void PDFOptions::writeLaTeX(OutputParams & runparams, otexstream & os,
93 bool hyperref_already_provided) const
99 // since LyX uses unicode, also set the PDF strings to unicode strings with the
100 // hyperref option "unicode"
101 opt += "unicode=true,";
103 // only use the hyperref settings if hyperref is enabled by the user
106 // try to extract author and title from document when none is
108 if (pdfusetitle && title.empty() && author.empty())
109 opt += "pdfusetitle,";
111 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
113 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
114 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
116 opt += "bookmarksopenlevel="
117 + convert<string>(bookmarksopenlevel) + ',';
120 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
121 opt += "pdfborder={0 0 ";
122 opt += (pdfborder ? '0' : '1');
124 opt += "backref=" + backref + ',';
125 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
126 if (!pagemode.empty())
127 opt += "pdfpagemode=" + pagemode + ',';
129 // load the pdftitle etc. as hypersetup, otherwise you'll get
130 // LaTeX-errors when using non-latin characters
132 hyperset += "pdftitle={" + title + "},";
134 hyperset += "\n pdfauthor={" + author + "},";
135 if (!subject.empty())
136 hyperset += "\n pdfsubject={" + subject + "},";
137 if (!keywords.empty())
138 hyperset += "\n pdfkeywords={" + keywords + "},";
139 if (!quoted_options.empty()){
141 hyperset += quoted_options;
143 hyperset = rtrim(hyperset,",");
146 // check if the hyperref settings use an encoding that exceeds
147 // ours. If so, we have to switch to utf8.
148 Encoding const * const enc = runparams.encoding;
149 docstring const hs = from_utf8(hyperset);
150 bool need_unicode = false;
152 for (size_t n = 0; n < hs.size(); ++n) {
153 if (!enc->encodable(hs[n]))
158 // use in \\usepackage parameter as not all options can be handled inside \\hypersetup
159 if (!hyperref_already_provided) {
160 opt = rtrim(opt, ",");
161 opt = "\\usepackage[" + opt + "]\n {hyperref}\n";
163 if (!hyperset.empty())
164 opt += "\\hypersetup{" + hyperset + "}\n";
166 // only in case hyperref is already loaded by the current text class
167 // try to put it into hyperset
169 // FIXME: rename in this case the PDF settings dialog checkbox
170 // label from "Use Hyperref" to "Customize Hyperref Settings"
171 // as discussd in bug #6293
172 opt = "\\hypersetup{" + rtrim(opt + hyperset, ",") + "}\n";
175 // hyperref expects utf8!
176 if (need_unicode && enc && enc->iconvName() != "UTF-8"
177 &&!runparams.isFullUnicode()) {
178 os << "\\inputencoding{utf8}\n"
179 << setEncoding("UTF-8");
181 // If hyperref is loaded by the document class, we output
182 // \hypersetup \AtBeginDocument if hypersetup is not (yet)
183 // defined. In this case, the class loads hyperref late
185 if (hyperref_already_provided && !opt.empty()) {
186 os << "\\ifx\\hypersetup\\undefined\n"
187 << " \\AtBeginDocument{%\n "
194 os << from_utf8(opt);
196 if (need_unicode && enc && enc->iconvName() != "UTF-8"
197 &&!runparams.isFullUnicode()) {
198 os << setEncoding(enc->iconvName())
199 << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
204 string PDFOptions::readToken(Lexer &lex, string const & token)
206 if (token == "\\use_hyperref") {
208 } else if (token == "\\pdf_title") {
210 } else if (token == "\\pdf_author") {
212 } else if (token == "\\pdf_subject") {
214 } else if (token == "\\pdf_keywords") {
216 } else if (token == "\\pdf_bookmarks") {
218 } else if (token == "\\pdf_bookmarksnumbered") {
219 lex >> bookmarksnumbered;
220 } else if (token == "\\pdf_bookmarksopen") {
221 lex >> bookmarksopen;
222 } else if (token == "\\pdf_bookmarksopenlevel") {
223 lex >> bookmarksopenlevel;
224 } else if (token == "\\pdf_breaklinks") {
226 } else if (token == "\\pdf_pdfborder") {
228 } else if (token == "\\pdf_colorlinks") {
230 } else if (token == "\\pdf_backref") {
232 } else if (token == "\\pdf_pdfusetitle") {
234 } else if (token == "\\pdf_pagemode") {
236 } else if (token == "\\pdf_quoted_options") {
237 lex >> quoted_options;
245 // check the string from UI
246 string PDFOptions::quoted_options_check(string const & str) const
248 return subst(str, "\n", "");
252 // set implicit settings for hyperref
253 void PDFOptions::clear()
255 use_hyperref = false;
261 bookmarksnumbered = false;
262 bookmarksopen = false;
263 bookmarksopenlevel = 1;
269 quoted_options.clear();
270 pdfusetitle = true; //in contrast with hyperref