]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
Add a fixme, in effect, about dots.
[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 "Lexer.h"
16
17 #include "support/convert.h"
18 #include "support/debug.h"
19 #include "support/lstrings.h"
20
21 #include <sstream>
22 #include <string>
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28
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                 && 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 "     << backref << '\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
89 void PDFOptions::writeLaTeX(odocstream & os, bool hyperref_already_provided) const
90 {
91         string opt;
92         
93         // since LyX uses unicode, also set the PDF strings to unicode strings with the
94         // hyperref option "unicode"
95         opt += "unicode=true, ";
96         
97         // try to extract author and title from document when none is
98         // explicitely given
99         if (pdfusetitle && title.empty() && author.empty())
100                 opt += "pdfusetitle,";
101         opt += "\n ";
102
103         opt += "bookmarks=" + convert<string>(bookmarks) + ',';
104         if (bookmarks) {
105                 opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
106                 opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
107                 if (bookmarksopen)
108                         opt += "bookmarksopenlevel=" + convert<string>(bookmarksopenlevel) + ',';
109         }
110         opt += "\n ";
111         opt += "breaklinks="     + convert<string>(breaklinks) + ',';
112
113         opt += "pdfborder={0 0 " ;
114         opt += (pdfborder ?'0':'1');
115         opt += "},";
116
117         opt += "backref=" + backref + ',';
118         opt += "colorlinks="     + convert<string>(colorlinks) + ',';
119         if (!pagemode.empty())
120                 opt += "pdfpagemode=" + pagemode + ',';
121         
122
123
124         // load the pdftitle etc. as hypersetup, otherwise you'll get
125         // LaTeX-errors when using non-latin characters
126         string hyperset;
127         if (!title.empty())
128                 hyperset += "pdftitle={"   + title + "},";
129         if (!author.empty())
130                 hyperset += "\n pdfauthor={"  + author + "},";
131         if (!subject.empty())
132                 hyperset += "\n pdfsubject={" + subject + "},";
133         if (!keywords.empty())
134                 hyperset += "\n pdfkeywords={" + keywords + "},";
135         if (!quoted_options.empty()){
136                 hyperset += "\n ";
137                 hyperset += quoted_options;
138         }
139         hyperset = rtrim(hyperset,",");
140
141
142         // use in \\usepackage parameter as not all options can be handled inside \\hypersetup
143         if (!hyperref_already_provided) {
144                 opt = rtrim(opt,",");
145                 opt = "\\usepackage[" + opt + "]\n {hyperref}\n";
146
147                 if (!hyperset.empty())
148                         opt += "\\hypersetup{" + hyperset + "}\n ";
149         } else
150                 // only in case hyperref is already loaded by the current text class
151                 // try to put it into hyperset
152                 //
153                 // FIXME: this still does not fix the cases where hyperref is loaded
154                 //        and the option is active only when part of usepackage parameter
155                 //        (e.g. pdfusetitle).
156                 {
157                         opt = "\\hypersetup{" + opt + hyperset + "}\n ";
158                 }
159         
160         // FIXME UNICODE
161         os << from_utf8(opt);
162 }
163
164
165 string PDFOptions::readToken(Lexer &lex, string const & token)
166 {
167         if (token == "\\use_hyperref") {
168                 lex >> use_hyperref;
169         } else if (token == "\\pdf_title") {
170                 lex >> title;
171         } else if (token == "\\pdf_author") {
172                 lex >> author;
173         } else if (token == "\\pdf_subject") {
174                 lex >> subject;
175         } else if (token == "\\pdf_keywords") {
176                 lex >> keywords;
177         } else if (token == "\\pdf_bookmarks") {
178                 lex >> bookmarks;
179         } else if (token == "\\pdf_bookmarksnumbered") {
180                 lex >> bookmarksnumbered;
181         } else if (token == "\\pdf_bookmarksopen") {
182                 lex >> bookmarksopen;
183         } else if (token == "\\pdf_bookmarksopenlevel") {
184                 lex >> bookmarksopenlevel;
185         } else if (token == "\\pdf_breaklinks") {
186                 lex >> breaklinks;
187         } else if (token == "\\pdf_pdfborder") {
188                 lex >> pdfborder;
189         } else if (token == "\\pdf_colorlinks") {
190                 lex >> colorlinks;
191         } else if (token == "\\pdf_backref") {
192                 lex >> backref;
193         } else if (token == "\\pdf_pdfusetitle") {
194                 lex >> pdfusetitle;
195         } else if (token == "\\pdf_pagemode") {
196                 lex >> pagemode;
197         } else if (token == "\\pdf_quoted_options") {
198                 lex >> quoted_options;
199         } else {
200                 return token;
201         }
202         return string();
203 }
204
205
206 // check the string from UI
207 string PDFOptions::quoted_options_check(string const str) const
208 {
209         return subst(str, "\n", "");
210 }
211
212
213 // set implicit settings for hyperref
214 void PDFOptions::clear()
215 {
216         use_hyperref            = false;
217         title.clear();
218         author.clear();
219         subject.clear();
220         keywords.clear();
221         bookmarks               = true;
222         bookmarksnumbered       = false;
223         bookmarksopen           = false;
224         bookmarksopenlevel      = 1;
225         breaklinks              = false;
226         pdfborder               = false;
227         colorlinks              = false;
228         backref                 = "false";
229         pagemode.clear();
230         quoted_options.clear();
231         pdfusetitle             = true;  //in contrast with hyperref
232 }
233
234 } // namespace lyx