]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
Merge branch 'master' of git.lyx.org:lyx
[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
19 #include "support/convert.h"
20 #include "support/debug.h"
21 #include "support/lstrings.h"
22
23 #include <sstream>
24 #include <string>
25 #include <algorithm>
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32
33 const string PDFOptions::pagemode_fullscreen("FullScreen");
34
35 bool PDFOptions::empty() const
36 {
37         PDFOptions x; //implicit hyperref settings
38
39         return  author == x.author
40                 && title == x.title
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;
54 }
55
56
57 void PDFOptions::writeFile(ostream & os) const
58 {
59         os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
60         if (!use_hyperref && empty())
61                 return;
62         
63         if (!title.empty() )
64                 os << "\\pdf_title " << Lexer::quoteString(title) << '\n';
65         if (!author.empty())
66                 os << "\\pdf_author " << Lexer::quoteString(author) << '\n';
67         if (!subject.empty())
68                 os << "\\pdf_subject " << Lexer::quoteString(subject) << '\n';
69         if (!keywords.empty())
70                 os << "\\pdf_keywords " << Lexer::quoteString(keywords) << '\n';
71         
72         
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';
77         
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';
83         
84         if (!pagemode.empty())
85                 os << "\\pdf_pagemode " << pagemode << '\n';
86         
87         if (!quoted_options.empty())
88                 os << "\\pdf_quoted_options " << Lexer::quoteString(quoted_options) << '\n';
89 }
90
91
92 void PDFOptions::writeLaTeX(OutputParams & runparams, otexstream & os,
93                             bool hyperref_already_provided) const
94 {
95         // FIXME Unicode
96         string opt;
97         string hyperset;
98         
99         // since LyX uses unicode, also set the PDF strings to unicode strings with the
100         // hyperref option "unicode"
101         opt += "unicode=true,";
102
103         // only use the hyperref settings if hyperref is enabled by the user
104         // see bug #7052
105         if (use_hyperref) {
106                 // try to extract author and title from document when none is
107                 // explicitly given
108                 if (pdfusetitle && title.empty() && author.empty())
109                         opt += "pdfusetitle,";
110                 opt += "\n ";
111                 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
112                 if (bookmarks) {
113                         opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
114                         opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
115                         if (bookmarksopen)
116                                 opt += "bookmarksopenlevel="
117                                 + convert<string>(bookmarksopenlevel) + ',';
118                 }
119                 opt += "\n ";
120                 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
121                 opt += "pdfborder={0 0 ";
122                 opt += (pdfborder ? '0' : '1');
123                 opt += "},";
124                 if (pdfborder)
125                         opt += "pdfborderstyle={},";
126                 opt += "backref=" + backref + ',';
127                 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
128                 if (!pagemode.empty())
129                         opt += "pdfpagemode=" + pagemode + ',';
130
131                 // load the pdftitle etc. as hypersetup, otherwise you'll get
132                 // LaTeX-errors when using non-latin characters
133                 if (!title.empty())
134                         hyperset += "pdftitle={" + title + "},";
135                 if (!author.empty())                    
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()){
142                         hyperset += "\n ";
143                         hyperset += quoted_options;
144                 }
145                 hyperset = rtrim(hyperset,",");
146         }
147
148         // check if the hyperref settings use an encoding that exceeds
149         // ours. If so, we have to switch to utf8.
150         Encoding const * const enc = runparams.encoding;
151         docstring const hs = from_utf8(hyperset);
152         bool need_unicode = false;
153         if (enc) {
154                 for (size_t n = 0; n < hs.size(); ++n) {
155                         if (!enc->encodable(hs[n]))
156                                 need_unicode = true;
157                 }
158         }
159
160         // use in \\usepackage parameter as not all options can be handled inside \\hypersetup
161         if (!hyperref_already_provided) {
162                 opt = rtrim(opt, ",");
163                 opt = "\\usepackage[" + opt + "]\n {hyperref}\n";
164
165                 if (!hyperset.empty())
166                         opt += "\\hypersetup{" + hyperset + "}\n";
167         } else {
168                 // only in case hyperref is already loaded by the current text class
169                 // try to put it into hyperset
170                 //
171                 // FIXME: rename in this case the PDF settings dialog checkbox
172                 //  label from "Use Hyperref" to "Customize Hyperref Settings"
173                 //  as discussd in bug #6293
174                 opt = "\\hypersetup{" + rtrim(opt + hyperset, ",") + "}\n";
175         }
176
177         // hyperref expects LICR macros for non-ASCII chars.
178         // Usually, "(lua)inputenc" converts the input to LICR, with XeTeX utf-8 works, too.
179         // As hyperref provides good coverage for \inputencoding{utf8}, we can try
180         // this if the current input encoding does not support a character.
181         // FIXME: don't use \inputencoding if "inputenc" is not loaded (#9839).
182         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
183             if (runparams.flavor != OutputParams::XETEX)
184                         os << "\\inputencoding{utf8}\n";
185                 os << setEncoding("UTF-8");
186         }
187         // If hyperref is loaded by the document class, we output
188         // \hypersetup \AtBeginDocument if hypersetup is not (yet)
189         // defined. In this case, the class loads hyperref late
190         // (see bug #7048).
191         if (hyperref_already_provided && !opt.empty()) {
192                 os << "\\ifx\\hypersetup\\undefined\n"
193                    << "  \\AtBeginDocument{%\n    "
194                    << from_utf8(opt)
195                    << "  }\n"
196                    << "\\else\n  "
197                    << from_utf8(opt)
198                    << "\\fi\n";
199         } else
200                 os << from_utf8(opt);
201         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
202                 os << setEncoding(enc->iconvName());
203             if (runparams.flavor != OutputParams::XETEX)
204                         os << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
205         }
206 }
207
208
209 string PDFOptions::readToken(Lexer &lex, string const & token)
210 {
211         if (token == "\\use_hyperref") {
212                 lex >> use_hyperref;
213         } else if (token == "\\pdf_title") {
214                 if (lex.isOK()) {
215                         lex.next(true);
216                         title = lex.getString();
217                 }
218         } else if (token == "\\pdf_author") {
219                 if (lex.isOK()) {
220                         lex.next(true);
221                         author = lex.getString();
222                 }
223         } else if (token == "\\pdf_subject") {
224                 if (lex.isOK()) {
225                         lex.next(true);
226                         subject = lex.getString();
227                 }
228         } else if (token == "\\pdf_keywords") {
229                 if (lex.isOK()) {
230                         lex.next(true);
231                         keywords = lex.getString();
232                 }
233         } else if (token == "\\pdf_bookmarks") {
234                 lex >> bookmarks;
235         } else if (token == "\\pdf_bookmarksnumbered") {
236                 lex >> bookmarksnumbered;
237         } else if (token == "\\pdf_bookmarksopen") {
238                 lex >> bookmarksopen;
239         } else if (token == "\\pdf_bookmarksopenlevel") {
240                 lex >> bookmarksopenlevel;
241         } else if (token == "\\pdf_breaklinks") {
242                 lex >> breaklinks;
243         } else if (token == "\\pdf_pdfborder") {
244                 lex >> pdfborder;
245         } else if (token == "\\pdf_colorlinks") {
246                 lex >> colorlinks;
247         } else if (token == "\\pdf_backref") {
248                 lex >> backref;
249         } else if (token == "\\pdf_pdfusetitle") {
250                 lex >> pdfusetitle;
251         } else if (token == "\\pdf_pagemode") {
252                 lex >> pagemode;
253         } else if (token == "\\pdf_quoted_options") {
254                 if (lex.isOK()) {
255                         lex.next(true);
256                         quoted_options = lex.getString();
257                 }
258         } else {
259                 return token;
260         }
261         return string();
262 }
263
264
265 // check the string from UI
266 string PDFOptions::quoted_options_check(string const & str) const
267 {
268         return subst(str, "\n", "");
269 }
270
271
272 // set implicit settings for hyperref
273 void PDFOptions::clear()
274 {
275         use_hyperref            = false;
276         title.clear();
277         author.clear();
278         subject.clear();
279         keywords.clear();
280         bookmarks               = true;
281         bookmarksnumbered       = false;
282         bookmarksopen           = false;
283         bookmarksopenlevel      = 1;
284         breaklinks              = false;
285         pdfborder               = false;
286         colorlinks              = false;
287         backref                 = "false";
288         pagemode.clear();
289         quoted_options.clear();
290         pdfusetitle             = true;  //in contrast with hyperref
291 }
292
293 } // namespace lyx