]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
Get rid of all-insets-toggle and explain how to replace it with inset-forall.
[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
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31
32 const string PDFOptions::pagemode_fullscreen("FullScreen");
33
34 bool PDFOptions::empty() const
35 {
36         PDFOptions x; //implicit hyperref settings
37
38         return  author == x.author
39                 && title == x.title
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;
53 }
54
55
56 void PDFOptions::writeFile(ostream & os) const
57 {
58         os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
59         if (!use_hyperref && empty())
60                 return;
61         
62         if (!title.empty() )
63                 os << "\\pdf_title \"" << title << "\"\n";
64         if (!author.empty())
65                 os << "\\pdf_author \"" << author << "\"\n";
66         if (!subject.empty())
67                 os << "\\pdf_subject \"" << subject << "\"\n";
68         if (!keywords.empty())
69                 os << "\\pdf_keywords \"" << keywords << "\"\n";
70         
71         
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';
76         
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';
82         
83         if (!pagemode.empty())
84                 os << "\\pdf_pagemode " << pagemode << '\n';
85         
86         if (!quoted_options.empty())
87                 os << "\\pdf_quoted_options \"" << quoted_options << "\"\n";
88 }
89
90
91 int PDFOptions::writeLaTeX(OutputParams & runparams, odocstream & os,
92                             bool hyperref_already_provided) const
93 {
94         int lines = 0;
95         // FIXME Unicode
96         string opt;
97         
98         // since LyX uses unicode, also set the PDF strings to unicode strings with the
99         // hyperref option "unicode"
100         opt += "unicode=true, ";
101         
102         // try to extract author and title from document when none is
103         // explicitly given
104         if (pdfusetitle && title.empty() && author.empty())
105                 opt += "pdfusetitle,";
106         opt += "\n ";
107
108         opt += "bookmarks=" + convert<string>(bookmarks) + ',';
109         if (bookmarks) {
110                 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
111                 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
112                 if (bookmarksopen)
113                         opt += "bookmarksopenlevel="
114                             + convert<string>(bookmarksopenlevel) + ',';
115         }
116         opt += "\n ";
117         opt += "breaklinks=" + convert<string>(breaklinks) + ',';
118
119         opt += "pdfborder={0 0 ";
120         opt += (pdfborder ? '0' : '1');
121         opt += "},";
122
123         opt += "backref=" + backref + ',';
124         opt += "colorlinks=" + convert<string>(colorlinks) + ',';
125         if (!pagemode.empty())
126                 opt += "pdfpagemode=" + pagemode + ',';
127         
128
129
130         // load the pdftitle etc. as hypersetup, otherwise you'll get
131         // LaTeX-errors when using non-latin characters
132         string hyperset;
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         // 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;
152         if (enc) {
153                 for (size_t n = 0; n < hs.size(); ++n) {
154                         if (enc->latexChar(hs[n], true) != docstring(1, hs[n]))
155                                 need_unicode = true;
156                 }
157         }
158
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";
163
164                 if (!hyperset.empty())
165                         opt += "\\hypersetup{" + hyperset + "}\n";
166         } else
167                 // only in case hyperref is already loaded by the current text class
168                 // try to put it into hyperset
169                 //
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).
173                 {
174                         opt = "\\hypersetup{" + opt + hyperset + "}\n";
175                 }
176
177         lines = int(count(opt.begin(), opt.end(), '\n'));
178
179         // hyperref expects utf8!
180         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
181                 os << "\\inputencoding{utf8}\n"
182                    << setEncoding("UTF-8");
183                 ++lines;
184         }
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";
189                 ++lines;
190         }
191         return lines;
192 }
193
194
195 string PDFOptions::readToken(Lexer &lex, string const & token)
196 {
197         if (token == "\\use_hyperref") {
198                 lex >> use_hyperref;
199         } else if (token == "\\pdf_title") {
200                 lex >> title;
201         } else if (token == "\\pdf_author") {
202                 lex >> author;
203         } else if (token == "\\pdf_subject") {
204                 lex >> subject;
205         } else if (token == "\\pdf_keywords") {
206                 lex >> keywords;
207         } else if (token == "\\pdf_bookmarks") {
208                 lex >> 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") {
216                 lex >> breaklinks;
217         } else if (token == "\\pdf_pdfborder") {
218                 lex >> pdfborder;
219         } else if (token == "\\pdf_colorlinks") {
220                 lex >> colorlinks;
221         } else if (token == "\\pdf_backref") {
222                 lex >> backref;
223         } else if (token == "\\pdf_pdfusetitle") {
224                 lex >> pdfusetitle;
225         } else if (token == "\\pdf_pagemode") {
226                 lex >> pagemode;
227         } else if (token == "\\pdf_quoted_options") {
228                 lex >> quoted_options;
229         } else {
230                 return token;
231         }
232         return string();
233 }
234
235
236 // check the string from UI
237 string PDFOptions::quoted_options_check(string const str) const
238 {
239         return subst(str, "\n", "");
240 }
241
242
243 // set implicit settings for hyperref
244 void PDFOptions::clear()
245 {
246         use_hyperref            = false;
247         title.clear();
248         author.clear();
249         subject.clear();
250         keywords.clear();
251         bookmarks               = true;
252         bookmarksnumbered       = false;
253         bookmarksopen           = false;
254         bookmarksopenlevel      = 1;
255         breaklinks              = false;
256         pdfborder               = false;
257         colorlinks              = false;
258         backref                 = "false";
259         pagemode.clear();
260         quoted_options.clear();
261         pdfusetitle             = true;  //in contrast with hyperref
262 }
263
264 } // namespace lyx