]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
Fix text direction issue for InsetInfo in RTL context
[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 with the
105         // hyperref option "unicode"
106         opt += "unicode=true,";
107
108         // only use the hyperref settings if hyperref is enabled by the user
109         // see bug #7052
110         if (use_hyperref) {
111                 // try to extract author and title from document when none is
112                 // explicitly given
113                 if (pdfusetitle && title.empty() && author.empty())
114                         opt += "pdfusetitle,";
115                 opt += "\n ";
116                 opt += "bookmarks=" + convert<string>(bookmarks) + ',';
117                 if (bookmarks) {
118                         opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
119                         opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
120                         if (bookmarksopen)
121                                 opt += "bookmarksopenlevel="
122                                 + convert<string>(bookmarksopenlevel) + ',';
123                 }
124                 opt += "\n ";
125                 opt += "breaklinks=" + convert<string>(breaklinks) + ',';
126                 opt += "pdfborder={0 0 ";
127                 opt += (pdfborder ? '0' : '1');
128                 opt += "},";
129                 if (pdfborder)
130                         opt += "pdfborderstyle={},";
131                 opt += "backref=" + backref + ',';
132                 opt += "colorlinks=" + convert<string>(colorlinks) + ',';
133                 if (!pagemode.empty())
134                         opt += "pdfpagemode=" + pagemode + ',';
135
136                 // load the pdftitle etc. as hypersetup, otherwise you'll get
137                 // LaTeX-errors when using non-latin characters
138                 if (!title.empty())
139                         hyperset += "pdftitle={" + title + "},";
140                 if (!author.empty())
141                         hyperset += "\n pdfauthor={" + author + "},";
142                 if (!subject.empty())
143                         hyperset += "\n pdfsubject={" + subject + "},";
144                 if (!keywords.empty())
145                         hyperset += "\n pdfkeywords={" + keywords + "},";
146                 if (!quoted_options.empty()){
147                         hyperset += "\n ";
148                         hyperset += quoted_options;
149                 }
150                 hyperset = rtrim(hyperset,",");
151         }
152
153         // check if the hyperref settings use an encoding that exceeds
154         // ours. If so, we have to switch to utf8.
155         Encoding const * const enc = runparams.encoding;
156         docstring const hs = from_utf8(hyperset);
157         bool need_unicode = false;
158         if (enc) {
159                 for (size_t n = 0; n < hs.size(); ++n) {
160                         if (!enc->encodable(hs[n]))
161                                 need_unicode = true;
162                 }
163         }
164
165         // use in \\usepackage parameter as not all options can be handled inside \\hypersetup
166         if (!hyperref_already_provided) {
167                 opt = rtrim(opt, ",");
168                 opt = "\\usepackage[" + opt + "]\n {hyperref}\n";
169
170                 if (!hyperset.empty())
171                         opt += "\\hypersetup{" + hyperset + "}\n";
172         } else {
173                 // only in case hyperref is already loaded by the current text class
174                 // try to put it into hyperset
175                 //
176                 // FIXME: rename in this case the PDF settings dialog checkbox
177                 //  label from "Use Hyperref" to "Customize Hyperref Settings"
178                 //  as discussd in bug #6293
179                 opt = "\\hypersetup{" + rtrim(opt + hyperset, ",") + "}\n";
180         }
181
182         // hyperref expects LICR macros for non-ASCII chars.
183         // Usually, "(lua)inputenc" converts the input to LICR, with XeTeX utf-8 works, too.
184         // As hyperref provides good coverage for \inputencoding{utf8}, we can try
185         // this if the current input encoding does not support a character.
186         // FIXME: don't use \inputencoding if "inputenc" is not loaded (#9839).
187         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
188             if (runparams.flavor != OutputParams::XETEX)
189                         os << "\\inputencoding{utf8}\n";
190                 os << setEncoding("UTF-8");
191         }
192         // If hyperref is loaded by the document class, we output
193         // \hypersetup \AtBeginDocument if hypersetup is not (yet)
194         // defined. In this case, the class loads hyperref late
195         // (see bug #7048).
196         if (hyperref_already_provided && !opt.empty()) {
197                 os << "\\ifx\\hypersetup\\undefined\n"
198                    << "  \\AtBeginDocument{%\n    "
199                    << from_utf8(opt)
200                    << "  }\n"
201                    << "\\else\n  "
202                    << from_utf8(opt)
203                    << "\\fi\n";
204         } else
205                 os << from_utf8(opt);
206         if (need_unicode && enc && enc->iconvName() != "UTF-8") {
207                 os << setEncoding(enc->iconvName());
208             if (runparams.flavor != OutputParams::XETEX)
209                         os << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
210         }
211 }
212
213
214 string PDFOptions::readToken(Lexer &lex, string const & token)
215 {
216         if (token == "\\use_hyperref") {
217                 lex >> use_hyperref;
218         } else if (token == "\\pdf_title") {
219                 if (lex.isOK()) {
220                         lex.next(true);
221                         title = lex.getString();
222                 }
223         } else if (token == "\\pdf_author") {
224                 if (lex.isOK()) {
225                         lex.next(true);
226                         author = lex.getString();
227                 }
228         } else if (token == "\\pdf_subject") {
229                 if (lex.isOK()) {
230                         lex.next(true);
231                         subject = lex.getString();
232                 }
233         } else if (token == "\\pdf_keywords") {
234                 if (lex.isOK()) {
235                         lex.next(true);
236                         keywords = lex.getString();
237                 }
238         } else if (token == "\\pdf_bookmarks") {
239                 lex >> bookmarks;
240         } else if (token == "\\pdf_bookmarksnumbered") {
241                 lex >> bookmarksnumbered;
242         } else if (token == "\\pdf_bookmarksopen") {
243                 lex >> bookmarksopen;
244         } else if (token == "\\pdf_bookmarksopenlevel") {
245                 lex >> bookmarksopenlevel;
246         } else if (token == "\\pdf_breaklinks") {
247                 lex >> breaklinks;
248         } else if (token == "\\pdf_pdfborder") {
249                 lex >> pdfborder;
250         } else if (token == "\\pdf_colorlinks") {
251                 lex >> colorlinks;
252         } else if (token == "\\pdf_backref") {
253                 lex >> backref;
254         } else if (token == "\\pdf_pdfusetitle") {
255                 lex >> pdfusetitle;
256         } else if (token == "\\pdf_pagemode") {
257                 lex >> pagemode;
258         } else if (token == "\\pdf_quoted_options") {
259                 if (lex.isOK()) {
260                         lex.next(true);
261                         quoted_options = lex.getString();
262                 }
263         } else {
264                 return token;
265         }
266         return string();
267 }
268
269
270 // check the string from UI
271 string PDFOptions::quoted_options_check(string const & str) const
272 {
273         return subst(str, "\n", "");
274 }
275
276
277 // set implicit settings for hyperref
278 void PDFOptions::clear()
279 {
280         use_hyperref            = false;
281         title.clear();
282         author.clear();
283         subject.clear();
284         keywords.clear();
285         bookmarks               = true;
286         bookmarksnumbered       = false;
287         bookmarksopen           = false;
288         bookmarksopenlevel      = 1;
289         breaklinks              = false;
290         pdfborder               = false;
291         colorlinks              = false;
292         backref                 = "false";
293         pagemode.clear();
294         quoted_options.clear();
295         pdfusetitle             = true;  //in contrast with hyperref
296 }
297
298 } // namespace lyx