]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
307c9b169f05c89e8a4e4748f1b2e64b90e83fd0
[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 "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 void PDFOptions::writeFile(ostream & os) const
54 {
55         os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
56         if (!use_hyperref && empty())
57                 return;
58         
59         if (!title.empty() )
60                 os << "\\pdf_title \"" << title << "\"\n";
61         if (!author.empty())
62                 os << "\\pdf_author \"" << author << "\"\n";
63         if (!subject.empty())
64                 os << "\\pdf_subject \"" << subject << "\"\n";
65         if (!keywords.empty())
66                 os << "\\pdf_keywords \"" << keywords << "\"\n";
67         
68         
69         os << "\\pdf_bookmarks " << convert<string>(bookmarks) << '\n';
70         os << "\\pdf_bookmarksnumbered " << convert<string>(bookmarksnumbered) << '\n';
71         os << "\\pdf_bookmarksopen " << convert<string>(bookmarksopen) << '\n';
72         os << "\\pdf_bookmarksopenlevel " << bookmarksopenlevel << '\n';
73         
74         os << "\\pdf_breaklinks "  << convert<string>(breaklinks)  << '\n';
75         os << "\\pdf_pdfborder "   << convert<string>(pdfborder)   << '\n';
76         os << "\\pdf_colorlinks "  << convert<string>(colorlinks)  << '\n';
77         os << "\\pdf_backref "     << convert<string>(backref)     << '\n';
78         os << "\\pdf_pagebackref " << convert<string>(pagebackref) << '\n';
79         os << "\\pdf_pdfusetitle " << convert<string>(pdfusetitle) << '\n';
80         
81         if (!pagemode.empty())
82                 os << "\\pdf_pagemode " << pagemode << '\n';
83         
84         if (!quoted_options.empty())
85                 os << "\\pdf_quoted_options \"" << quoted_options << "\"\n";
86 }
87
88 void PDFOptions::writeLaTeX(odocstream & os, bool hyper_required) const
89 {
90         if (!use_hyperref && !hyper_required)
91                 return;
92         
93         string opt;
94         
95         opt = "\\usepackage[";
96         // since LyX uses unicode, also set the PDF strings to unicode strings with the
97         // hyperref option "unicode"
98         opt += "unicode=true, ";
99         
100         // try to extract author and title from document when none is
101         // explicitely given
102         if (pdfusetitle && title.empty() && author.empty())
103                 opt += "pdfusetitle,";
104         opt += "\n ";
105
106         opt += "bookmarks=" + convert<string>(bookmarks) + ',';
107         if (bookmarks) {
108                 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
109                 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
110                 if (bookmarksopen)
111                         opt += "bookmarksopenlevel=" + convert<string>(bookmarksopenlevel) + ',';
112         }
113         opt += "\n ";
114         opt += "breaklinks="     + convert<string>(breaklinks) + ',';
115
116         opt += "pdfborder={0 0 " ;
117         opt += (pdfborder ?'0':'1');
118         opt += "},";
119
120         opt += "backref="        + convert<string>(backref) + ',';
121         opt += "pagebackref="    + convert<string>(pagebackref) + ',';
122         opt += "\n ";
123         opt += "colorlinks="     + convert<string>(colorlinks) + ',';
124         if (!pagemode.empty())
125                 opt += "pdfpagemode=" + pagemode + ',';
126         
127         opt = rtrim(opt,",");
128         opt += "]\n {hyperref}\n";
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_get();
144         }
145         hyperset = rtrim(hyperset,",");
146         if (!hyperset.empty())
147                 opt += "\\hypersetup{" + hyperset + "}\n ";
148         
149         // FIXME UNICODE
150         os << from_utf8(opt);
151 }
152
153
154 string PDFOptions::readToken(Lexer &lex, string const & token)
155 {
156         if (token == "\\use_hyperref") {
157                 lex >> use_hyperref;
158         } else if (token == "\\pdf_title") {
159                 lex >> title;
160         } else if (token == "\\pdf_author") {
161                 lex >> author;
162         } else if (token == "\\pdf_subject") {
163                 lex >> subject;
164         } else if (token == "\\pdf_keywords") {
165                 lex >> keywords;
166         } else if (token == "\\pdf_bookmarks") {
167                 lex >> bookmarks;
168         } else if (token == "\\pdf_bookmarksnumbered") {
169                 lex >> bookmarksnumbered;
170         } else if (token == "\\pdf_bookmarksopen") {
171                 lex >> bookmarksopen;
172         } else if (token == "\\pdf_bookmarksopenlevel") {
173                 lex >> bookmarksopenlevel;
174         } else if (token == "\\pdf_breaklinks") {
175                 lex >> breaklinks;
176         } else if (token == "\\pdf_pdfborder") {
177                 lex >> pdfborder;
178         } else if (token == "\\pdf_colorlinks") {
179                 lex >> colorlinks;
180         } else if (token == "\\pdf_backref") {
181                 lex >> backref;
182         } else if (token == "\\pdf_pagebackref") {
183                 lex >> pagebackref;
184         } else if (token == "\\pdf_pdfusetitle") {
185                 lex >> pdfusetitle;
186         } else if (token == "\\pdf_pagemode") {
187                 lex >> pagemode;
188         } else if (token == "\\pdf_quoted_options") {
189                 lex >> quoted_options;
190         } else {
191                 return token;
192         }
193         return string();
194 }
195
196
197 // prepared for check
198 string PDFOptions::quoted_options_get() const
199 {
200         return quoted_options;
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