]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
fdc2ff7cdbe58b888b2bac17991358bfdc258ad4
[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  *
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 "debug.h"
18 #include "Lexer.h"
19
20 #include <sstream>
21 #include <string>
22
23 namespace lyx {
24
25
26 using std::ostream;
27 using std::ostringstream;
28 using std::string;
29
30 const string PDFOptions::pagemode_fullscreen("FullScreen");
31
32 bool PDFOptions::empty() const
33 {
34         PDFOptions x; //implicit hyperref settings
35
36         return  author == x.author
37                 && title == x.title
38                 && subject == x.subject
39                 && keywords == x.keywords
40                 && pagemode == x.pagemode
41                 && quoted_options == x.quoted_options
42                 && bookmarks == x.bookmarks
43                 && bookmarksnumbered == x.bookmarksnumbered
44                 && bookmarksopen == x.bookmarksopen
45                 && bookmarksopenlevel == x.bookmarksopenlevel
46                 && breaklinks == x.breaklinks
47                 && pdfborder == x.pdfborder
48                 && colorlinks == x.colorlinks
49                 && backref == x.backref
50                 && pagebackref == x.pagebackref
51                 && pdfusetitle == x.pdfusetitle;
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 void PDFOptions::writeLaTeX(odocstringstream &os) const
90 {
91         if (!use_hyperref)
92                 return;
93         
94         string opt;
95         
96         opt = "\\usepackage[";
97         // since LyX uses unicode, also set the PDF strings to unicode strings with the
98         // hyperref option "unicode"
99         opt += "unicode=true, ";
100         
101         // try to extract author and title from document when none is
102         // explicitely given
103         if (pdfusetitle && title.empty() && author.empty())
104                 opt += "pdfusetitle,";
105         opt += "\n ";
106
107         opt += "bookmarks=" + convert<string>(bookmarks) + ',';
108         if (bookmarks) {
109                 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
110                 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
111                 if (bookmarksopen)
112                         opt += "bookmarksopenlevel=" + convert<string>(bookmarksopenlevel) + ',';
113         }
114         opt += "\n ";
115         opt += "breaklinks="     + convert<string>(breaklinks) + ',';
116
117         opt += "pdfborder={0 0 " ;
118         opt += (pdfborder ?'0':'1');
119         opt += "},";
120
121         opt += "backref="        + convert<string>(backref) + ',';
122         opt += "pagebackref="    + convert<string>(pagebackref) + ',';
123         opt += "\n ";
124         opt += "colorlinks="     + convert<string>(colorlinks) + ',';
125         if (!pagemode.empty())
126                 opt += "pdfpagemode=" + pagemode + ',';
127         
128         opt = support::rtrim(opt,",");
129         opt += "]\n {hyperref}\n";
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_get();
145         }
146         hyperset = support::rtrim(hyperset,",");
147         if (!hyperset.empty())
148                 opt += "\\hypersetup{" + hyperset + "}\n ";
149         
150         // FIXME UNICODE
151         os << from_utf8(opt);
152 }
153
154
155 string PDFOptions::readToken(Lexer &lex, string const & token)
156 {
157         if (token == "\\use_hyperref") {
158                 lex >> use_hyperref;
159         } else if (token == "\\pdf_title") {
160                 lex >> title;
161         } else if (token == "\\pdf_author") {
162                 lex >> author;
163         } else if (token == "\\pdf_subject") {
164                 lex >> subject;
165         } else if (token == "\\pdf_keywords") {
166                 lex >> keywords;
167         } else if (token == "\\pdf_bookmarks") {
168                 lex >> bookmarks;
169         } else if (token == "\\pdf_bookmarksnumbered") {
170                 lex >> bookmarksnumbered;
171         } else if (token == "\\pdf_bookmarksopen") {
172                 lex >> bookmarksopen;
173         } else if (token == "\\pdf_bookmarksopenlevel") {
174                 lex >> bookmarksopenlevel;
175         } else if (token == "\\pdf_breaklinks") {
176                 lex >> breaklinks;
177         } else if (token == "\\pdf_pdfborder") {
178                 lex >> pdfborder;
179         } else if (token == "\\pdf_colorlinks") {
180                 lex >> colorlinks;
181         } else if (token == "\\pdf_backref") {
182                 lex >> backref;
183         } else if (token == "\\pdf_pagebackref") {
184                 lex >> pagebackref;
185         } else if (token == "\\pdf_pdfusetitle") {
186                 lex >> pdfusetitle;
187         } else if (token == "\\pdf_pagemode") {
188                 lex >> pagemode;
189         } else if (token == "\\pdf_quoted_options") {
190                 lex >> quoted_options;
191         } else {
192                 return token;
193         }
194         return string();
195 }
196
197
198 // prepared for check
199 string PDFOptions::quoted_options_get() const
200 {
201         return quoted_options;
202 }
203
204 // set implicit settings for hyperref
205 void PDFOptions::clear()
206 {
207         use_hyperref            = false;
208         title.clear();
209         author.clear();
210         subject.clear();
211         keywords.clear();
212         bookmarks               = true;
213         bookmarksnumbered       = false;
214         bookmarksopen           = false;
215         bookmarksopenlevel      = 1;
216         breaklinks              = false;
217         pdfborder               = false;
218         colorlinks              = false;
219         backref                 = false;
220         pagebackref             = false;
221         pagemode.clear();
222         quoted_options.clear();
223         pdfusetitle             = true;  //in contrast with hyperref
224 }
225
226 } // namespace lyx