]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
d64cc2975e90ffc8f9057c14cca6ecd22faa5b7b
[lyx.git] / src / PDFOptions.cpp
1 /**
2  * \file PDFoptions.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Pavel Sanda
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "PDFOptions.h"
15
16 #include "Encoding.h"
17 #include "Lexer.h"
18 #include "texstream.h"
19
20 #include "support/convert.h"
21 #include "support/debug.h"
22 #include "support/lstrings.h"
23
24 #include <sstream>
25 #include <string>
26 #include <algorithm>
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32
33
34 const string PDFOptions::pagemode_fullscreen("FullScreen");
35
36 bool PDFOptions::empty() const
37 {
38         PDFOptions x; //implicit hyperref settings
39
40         return  author == x.author
41                 && title == x.title
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;
55 }
56
57
58 void PDFOptions::writeFile(ostream & os) const
59 {
60         os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
61         if (!use_hyperref && empty())
62                 return;
63
64         if (!title.empty() )
65                 os << "\\pdf_title " << Lexer::quoteString(title) << '\n';
66         if (!author.empty())
67                 os << "\\pdf_author " << Lexer::quoteString(author) << '\n';
68         if (!subject.empty())
69                 os << "\\pdf_subject " << Lexer::quoteString(subject) << '\n';
70         if (!keywords.empty())
71                 os << "\\pdf_keywords " << Lexer::quoteString(keywords) << '\n';
72
73
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';
78
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';
84
85         if (!pagemode.empty())
86                 os << "\\pdf_pagemode " << pagemode << '\n';
87
88         if (!quoted_options.empty())
89                 os << "\\pdf_quoted_options " << Lexer::quoteString(quoted_options) << '\n';
90 }
91
92
93 void PDFOptions::writeLaTeX(OutputParams & runparams, otexstream & os,
94                             bool hyperref_already_provided) const
95 {
96         // FIXME Unicode
97         string opt;
98         string hyperset;
99
100         // Driver needed by specific converters
101         if (!runparams.hyperref_driver.empty())
102                 opt += runparams.hyperref_driver + ",";
103
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,";
111
112         // only use the hyperref settings if hyperref is enabled by the user
113         // see bug #7052
114         if (use_hyperref) {
115                 // try to extract author and title from document when none is
116                 // explicitly given
117                 if (pdfusetitle && title.empty() && author.empty())
118                         opt += "pdfusetitle,";
119                 opt += "\n ";
120                 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
121                 if (bookmarks) {
122                         opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
123                         opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
124                         if (bookmarksopen)
125                                 opt += "bookmarksopenlevel="
126                                 + convert<string>(bookmarksopenlevel) + ',';
127                 }
128                 opt += "\n ";
129                 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
130                 opt += "pdfborder={0 0 ";
131                 opt += (pdfborder ? '0' : '1');
132                 opt += "},";
133                 if (pdfborder)
134                         opt += "pdfborderstyle={},";
135                 opt += "backref=" + backref + ',';
136                 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
137                 if (!pagemode.empty())
138                         opt += "pdfpagemode=" + pagemode + ',';
139
140                 // load the pdftitle etc. as hypersetup, otherwise you'll get
141                 // LaTeX-errors when using non-latin characters
142                 if (!title.empty())
143                         hyperset += "pdftitle={" + title + "},";
144                 if (!author.empty())
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()){
151                         hyperset += "\n ";
152                         hyperset += quoted_options;
153                 }
154                 hyperset = rtrim(hyperset,",");
155         }
156
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;
162         if (enc) {
163                 for (size_t n = 0; n < hs.size(); ++n) {
164                         if (!enc->encodable(hs[n]))
165                                 need_unicode = true;
166                 }
167         }
168
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";
173
174                 if (!hyperset.empty())
175                         opt += "\\hypersetup{" + hyperset + "}\n";
176         } else {
177                 // only in case hyperref is already loaded by the current text class
178                 // try to put it into hyperset
179                 //
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";
184         }
185
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");
195         }
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
199         // (see bug #7048).
200         if (hyperref_already_provided && !opt.empty()) {
201                 os << "\\ifx\\hypersetup\\undefined\n"
202                    << "  \\AtBeginDocument{%\n    "
203                    << from_utf8(opt)
204                    << "  }\n"
205                    << "\\else\n  "
206                    << from_utf8(opt)
207                    << "\\fi\n";
208         } else
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";
214         }
215 }
216
217
218 string PDFOptions::readToken(Lexer &lex, string const & token)
219 {
220         if (token == "\\use_hyperref") {
221                 lex >> use_hyperref;
222         } else if (token == "\\pdf_title") {
223                 if (lex.isOK()) {
224                         lex.next(true);
225                         title = lex.getString();
226                 }
227         } else if (token == "\\pdf_author") {
228                 if (lex.isOK()) {
229                         lex.next(true);
230                         author = lex.getString();
231                 }
232         } else if (token == "\\pdf_subject") {
233                 if (lex.isOK()) {
234                         lex.next(true);
235                         subject = lex.getString();
236                 }
237         } else if (token == "\\pdf_keywords") {
238                 if (lex.isOK()) {
239                         lex.next(true);
240                         keywords = lex.getString();
241                 }
242         } else if (token == "\\pdf_bookmarks") {
243                 lex >> 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") {
251                 lex >> breaklinks;
252         } else if (token == "\\pdf_pdfborder") {
253                 lex >> pdfborder;
254         } else if (token == "\\pdf_colorlinks") {
255                 lex >> colorlinks;
256         } else if (token == "\\pdf_backref") {
257                 lex >> backref;
258         } else if (token == "\\pdf_pdfusetitle") {
259                 lex >> pdfusetitle;
260         } else if (token == "\\pdf_pagemode") {
261                 lex >> pagemode;
262         } else if (token == "\\pdf_quoted_options") {
263                 if (lex.isOK()) {
264                         lex.next(true);
265                         quoted_options = lex.getString();
266                 }
267         } else {
268                 return token;
269         }
270         return string();
271 }
272
273
274 // check the string from UI
275 string PDFOptions::quoted_options_check(string const & str) const
276 {
277         return subst(str, "\n", "");
278 }
279
280
281 // set implicit settings for hyperref
282 void PDFOptions::clear()
283 {
284         use_hyperref            = false;
285         title.clear();
286         author.clear();
287         subject.clear();
288         keywords.clear();
289         bookmarks               = true;
290         bookmarksnumbered       = false;
291         bookmarksopen           = false;
292         bookmarksopenlevel      = 1;
293         breaklinks              = false;
294         pdfborder               = false;
295         colorlinks              = false;
296         backref                 = "false";
297         pagemode.clear();
298         quoted_options.clear();
299         pdfusetitle             = true;  //in contrast with hyperref
300 }
301
302 } // namespace lyx