]> git.lyx.org Git - features.git/blob - src/PDFOptions.cpp
Fix hyperref-soul conflict and sanitize hyperref management
[features.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "PDFOptions.h"
14
15 #include "support/convert.h"
16 #include "support/lstrings.h"
17 #include "support/debug.h"
18 #include "Lexer.h"
19
20 #include <sstream>
21 #include <string>
22
23 using namespace std;
24 using namespace lyx::support;
25
26 namespace lyx {
27
28
29 const string PDFOptions::pagemode_fullscreen("FullScreen");
30
31 bool PDFOptions::empty() const
32 {
33         PDFOptions x; //implicit hyperref settings
34
35         return  author == x.author
36                 && title == x.title
37                 && subject == x.subject
38                 && keywords == x.keywords
39                 && pagemode == x.pagemode
40                 && quoted_options == x.quoted_options
41                 && bookmarks == x.bookmarks
42                 && bookmarksnumbered == x.bookmarksnumbered
43                 && bookmarksopen == x.bookmarksopen
44                 && bookmarksopenlevel == x.bookmarksopenlevel
45                 && breaklinks == x.breaklinks
46                 && pdfborder == x.pdfborder
47                 && colorlinks == x.colorlinks
48                 && backref == x.backref
49                 && pagebackref == x.pagebackref
50                 && pdfusetitle == x.pdfusetitle;
51 }
52
53
54 void PDFOptions::writeFile(ostream & os) const
55 {
56         os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
57         if (!use_hyperref && empty())
58                 return;
59         
60         if (!title.empty() )
61                 os << "\\pdf_title \"" << title << "\"\n";
62         if (!author.empty())
63                 os << "\\pdf_author \"" << author << "\"\n";
64         if (!subject.empty())
65                 os << "\\pdf_subject \"" << subject << "\"\n";
66         if (!keywords.empty())
67                 os << "\\pdf_keywords \"" << keywords << "\"\n";
68         
69         
70         os << "\\pdf_bookmarks " << convert<string>(bookmarks) << '\n';
71         os << "\\pdf_bookmarksnumbered " << convert<string>(bookmarksnumbered) << '\n';
72         os << "\\pdf_bookmarksopen " << convert<string>(bookmarksopen) << '\n';
73         os << "\\pdf_bookmarksopenlevel " << bookmarksopenlevel << '\n';
74         
75         os << "\\pdf_breaklinks "  << convert<string>(breaklinks)  << '\n';
76         os << "\\pdf_pdfborder "   << convert<string>(pdfborder)   << '\n';
77         os << "\\pdf_colorlinks "  << convert<string>(colorlinks)  << '\n';
78         os << "\\pdf_backref "     << convert<string>(backref)     << '\n';
79         os << "\\pdf_pagebackref " << convert<string>(pagebackref) << '\n';
80         os << "\\pdf_pdfusetitle " << convert<string>(pdfusetitle) << '\n';
81         
82         if (!pagemode.empty())
83                 os << "\\pdf_pagemode " << pagemode << '\n';
84         
85         if (!quoted_options.empty())
86                 os << "\\pdf_quoted_options \"" << quoted_options << "\"\n";
87 }
88
89
90 void PDFOptions::writeLaTeX(odocstream & os) const
91 {
92         string opt;
93         
94         opt = "\\usepackage[";
95         // since LyX uses unicode, also set the PDF strings to unicode strings with the
96         // hyperref option "unicode"
97         opt += "unicode=true, ";
98         
99         // try to extract author and title from document when none is
100         // explicitely given
101         if (pdfusetitle && title.empty() && author.empty())
102                 opt += "pdfusetitle,";
103         opt += "\n ";
104
105         opt += "bookmarks=" + convert<string>(bookmarks) + ',';
106         if (bookmarks) {
107                 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
108                 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
109                 if (bookmarksopen)
110                         opt += "bookmarksopenlevel=" + convert<string>(bookmarksopenlevel) + ',';
111         }
112         opt += "\n ";
113         opt += "breaklinks="     + convert<string>(breaklinks) + ',';
114
115         opt += "pdfborder={0 0 " ;
116         opt += (pdfborder ?'0':'1');
117         opt += "},";
118
119         opt += "backref="        + convert<string>(backref) + ',';
120         opt += "pagebackref="    + convert<string>(pagebackref) + ',';
121         opt += "\n ";
122         opt += "colorlinks="     + convert<string>(colorlinks) + ',';
123         if (!pagemode.empty())
124                 opt += "pdfpagemode=" + pagemode + ',';
125         
126         opt = rtrim(opt,",");
127         opt += "]\n {hyperref}\n";
128
129         // load the pdftitle etc. as hypersetup, otherwise you'll get
130         // LaTeX-errors when using non-latin characters
131         string hyperset;
132         if (!title.empty())
133                 hyperset += "pdftitle={"   + title + "},";
134         if (!author.empty())
135                 hyperset += "\n pdfauthor={"  + author + "},";
136         if (!subject.empty())
137                 hyperset += "\n pdfsubject={" + subject + "},";
138         if (!keywords.empty())
139                 hyperset += "\n pdfkeywords={" + keywords + "},";
140         if (!quoted_options.empty()){
141                 hyperset += "\n ";
142                 hyperset += quoted_options_get();
143         }
144         hyperset = rtrim(hyperset,",");
145         if (!hyperset.empty())
146                 opt += "\\hypersetup{" + hyperset + "}\n ";
147         
148         // FIXME UNICODE
149         os << from_utf8(opt);
150 }
151
152
153 string PDFOptions::readToken(Lexer &lex, string const & token)
154 {
155         if (token == "\\use_hyperref") {
156                 lex >> use_hyperref;
157         } else if (token == "\\pdf_title") {
158                 lex >> title;
159         } else if (token == "\\pdf_author") {
160                 lex >> author;
161         } else if (token == "\\pdf_subject") {
162                 lex >> subject;
163         } else if (token == "\\pdf_keywords") {
164                 lex >> keywords;
165         } else if (token == "\\pdf_bookmarks") {
166                 lex >> bookmarks;
167         } else if (token == "\\pdf_bookmarksnumbered") {
168                 lex >> bookmarksnumbered;
169         } else if (token == "\\pdf_bookmarksopen") {
170                 lex >> bookmarksopen;
171         } else if (token == "\\pdf_bookmarksopenlevel") {
172                 lex >> bookmarksopenlevel;
173         } else if (token == "\\pdf_breaklinks") {
174                 lex >> breaklinks;
175         } else if (token == "\\pdf_pdfborder") {
176                 lex >> pdfborder;
177         } else if (token == "\\pdf_colorlinks") {
178                 lex >> colorlinks;
179         } else if (token == "\\pdf_backref") {
180                 lex >> backref;
181         } else if (token == "\\pdf_pagebackref") {
182                 lex >> pagebackref;
183         } else if (token == "\\pdf_pdfusetitle") {
184                 lex >> pdfusetitle;
185         } else if (token == "\\pdf_pagemode") {
186                 lex >> pagemode;
187         } else if (token == "\\pdf_quoted_options") {
188                 lex >> quoted_options;
189         } else {
190                 return token;
191         }
192         return string();
193 }
194
195
196 // prepared for check
197 string PDFOptions::quoted_options_get() const
198 {
199         return quoted_options;
200 }
201
202
203 // set implicit settings for hyperref
204 void PDFOptions::clear()
205 {
206         use_hyperref            = false;
207         title.clear();
208         author.clear();
209         subject.clear();
210         keywords.clear();
211         bookmarks               = true;
212         bookmarksnumbered       = false;
213         bookmarksopen           = false;
214         bookmarksopenlevel      = 1;
215         breaklinks              = false;
216         pdfborder               = false;
217         colorlinks              = false;
218         backref                 = false;
219         pagebackref             = false;
220         pagemode.clear();
221         quoted_options.clear();
222         pdfusetitle             = true;  //in contrast with hyperref
223 }
224
225 } // namespace lyx