]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
RCS case is more complicated
[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 \"" << title << "\"\n";
65         if (!author.empty())
66                 os << "\\pdf_author \"" << author << "\"\n";
67         if (!subject.empty())
68                 os << "\\pdf_subject \"" << subject << "\"\n";
69         if (!keywords.empty())
70                 os << "\\pdf_keywords \"" << 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 \"" << quoted_options << "\"\n";
89 }
90
91
92 int PDFOptions::writeLaTeX(OutputParams & runparams, odocstream & os,
93                             bool hyperref_already_provided) const
94 {
95         int lines = 0;
96         // FIXME Unicode
97         string opt;
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         // try to extract author and title from document when none is
104         // explicitly given
105         if (pdfusetitle && title.empty() && author.empty())
106                 opt += "pdfusetitle,";
107         opt += "\n ";
108
109         opt += "bookmarks=" + convert<string>(bookmarks) + ',';
110         if (bookmarks) {
111                 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
112                 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
113                 if (bookmarksopen)
114                         opt += "bookmarksopenlevel="
115                             + convert<string>(bookmarksopenlevel) + ',';
116         }
117         opt += "\n ";
118         opt += "breaklinks=" + convert<string>(breaklinks) + ',';
119
120         opt += "pdfborder={0 0 ";
121         opt += (pdfborder ? '0' : '1');
122         opt += "},";
123
124         opt += "backref=" + backref + ',';
125         opt += "colorlinks=" + convert<string>(colorlinks) + ',';
126         if (!pagemode.empty())
127                 opt += "pdfpagemode=" + pagemode + ',';
128         
129
130
131         // load the pdftitle etc. as hypersetup, otherwise you'll get
132         // LaTeX-errors when using non-latin characters
133         string hyperset;
134         if (!title.empty())
135                 hyperset += "pdftitle={" + title + "},";
136         if (!author.empty())
137                 hyperset += "\n pdfauthor={" + author + "},";
138         if (!subject.empty())
139                 hyperset += "\n pdfsubject={" + subject + "},";
140         if (!keywords.empty())
141                 hyperset += "\n pdfkeywords={" + keywords + "},";
142         if (!quoted_options.empty()){
143                 hyperset += "\n ";
144                 hyperset += quoted_options;
145         }
146         hyperset = rtrim(hyperset,",");
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->latexChar(hs[n], true) != docstring(1, 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: this still does not fix the cases where hyperref is loaded
172                 //        and the option is active only when part of usepackage parameter
173                 //        (e.g. pdfusetitle).
174                 {
175                         opt = "\\hypersetup{" + opt + hyperset + "}\n";
176                 }
177
178         lines = int(count(opt.begin(), opt.end(), '\n'));
179
180         // hyperref expects utf8!
181         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
182                 os << "\\inputencoding{utf8}\n"
183                    << setEncoding("UTF-8");
184                 ++lines;
185         }
186         os << from_utf8(opt);
187         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
188                 os << setEncoding(enc->iconvName())
189                    << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
190                 ++lines;
191         }
192         return lines;
193 }
194
195
196 string PDFOptions::readToken(Lexer &lex, string const & token)
197 {
198         if (token == "\\use_hyperref") {
199                 lex >> use_hyperref;
200         } else if (token == "\\pdf_title") {
201                 lex >> title;
202         } else if (token == "\\pdf_author") {
203                 lex >> author;
204         } else if (token == "\\pdf_subject") {
205                 lex >> subject;
206         } else if (token == "\\pdf_keywords") {
207                 lex >> keywords;
208         } else if (token == "\\pdf_bookmarks") {
209                 lex >> bookmarks;
210         } else if (token == "\\pdf_bookmarksnumbered") {
211                 lex >> bookmarksnumbered;
212         } else if (token == "\\pdf_bookmarksopen") {
213                 lex >> bookmarksopen;
214         } else if (token == "\\pdf_bookmarksopenlevel") {
215                 lex >> bookmarksopenlevel;
216         } else if (token == "\\pdf_breaklinks") {
217                 lex >> breaklinks;
218         } else if (token == "\\pdf_pdfborder") {
219                 lex >> pdfborder;
220         } else if (token == "\\pdf_colorlinks") {
221                 lex >> colorlinks;
222         } else if (token == "\\pdf_backref") {
223                 lex >> backref;
224         } else if (token == "\\pdf_pdfusetitle") {
225                 lex >> pdfusetitle;
226         } else if (token == "\\pdf_pagemode") {
227                 lex >> pagemode;
228         } else if (token == "\\pdf_quoted_options") {
229                 lex >> quoted_options;
230         } else {
231                 return token;
232         }
233         return string();
234 }
235
236
237 // check the string from UI
238 string PDFOptions::quoted_options_check(string const str) const
239 {
240         return subst(str, "\n", "");
241 }
242
243
244 // set implicit settings for hyperref
245 void PDFOptions::clear()
246 {
247         use_hyperref            = false;
248         title.clear();
249         author.clear();
250         subject.clear();
251         keywords.clear();
252         bookmarks               = true;
253         bookmarksnumbered       = false;
254         bookmarksopen           = false;
255         bookmarksopenlevel      = 1;
256         breaklinks              = false;
257         pdfborder               = false;
258         colorlinks              = false;
259         backref                 = "false";
260         pagemode.clear();
261         quoted_options.clear();
262         pdfusetitle             = true;  //in contrast with hyperref
263 }
264
265 } // namespace lyx