]> git.lyx.org Git - lyx.git/blob - src/PDFOptions.cpp
d16b9cda4254b93fe9d41650317e4ff018229b8a
[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
12 #include "PDFOptions.h"
13
14 #include "support/convert.h"
15
16 #include <sstream>
17 #include <string>
18 #include "support/lstrings.h"
19 #include "Lexer.h"
20
21
22 namespace lyx {
23
24
25 using std::ostream;
26 using std::ostringstream;
27 using std::string;
28
29 const string PDFOptions::pagemode_fullscreen("FullScreen");
30
31 bool PDFOptions::empty() const
32 {
33        return
34                author.empty()  &&
35                title.empty()   &&
36                subject.empty() &&
37                keywords.empty()&&
38                pagemode.empty()&&
39                bookmarksopenlevel.empty() &&
40                quoted_options.empty();
41 }
42
43 void PDFOptions::writeFile(ostream & os) const
44 {
45        os << "\\use_hyperref " << convert<string>(use_hyperref) << '\n';
46        os << "\\pdf_store_options " << convert<string>(store_options) << '\n';
47        if (!use_hyperref && !store_options) return;
48
49        if (!title.empty() )
50                    os << "\\pdf_title \"" << title << "\"\n";
51        if (!author.empty())
52                    os << "\\pdf_author \"" << author << "\"\n";
53        if (!subject.empty())
54                    os << "\\pdf_subject \"" << subject << "\"\n";
55        if (!keywords.empty())
56                    os << "\\pdf_keywords \"" << keywords << "\"\n";
57
58
59        os << "\\pdf_bookmarks " << convert<string>(bookmarks) << '\n';
60        os << "\\pdf_bookmarksnumbered " << convert<string>(bookmarksnumbered) << '\n';
61        os << "\\pdf_bookmarksopen " << convert<string>(bookmarksopen) << '\n';
62        if (bookmarksopenlevel.empty())
63                    os << "\\pdf_bookmarksopenlevel \"" << bookmarksopenlevel << "\"\n";
64
65        os << "\\pdf_breaklinks "       << convert<string>(breaklinks)  << '\n';
66        os << "\\pdf_pdfborder "        << convert<string>(pdfborder)   << '\n';
67        os << "\\pdf_colorlinks "       << convert<string>(colorlinks)  << '\n';
68        os << "\\pdf_backref "          << convert<string>(backref)     << '\n';
69        os << "\\pdf_pagebackref "      << convert<string>(pagebackref) << '\n';
70
71        if (!pagemode.empty())
72                    os << "\\pdf_pagemode " << pagemode << '\n';
73
74        if (!quoted_options.empty())
75                    os << "\\pdf_quoted_options \"" << quoted_options << "\"\n";
76
77 }
78
79 void PDFOptions::writeLaTeX(odocstream &os) const
80 {
81        if (!use_hyperref) return ;
82
83        string opt;
84
85        opt = "\\usepackage[";
86        if (!title.empty())
87                opt += "pdftitle={"   + title + "},\n";
88        if (!author.empty())
89                opt += "pdfauthor={"  + author + "},\n";
90        if (!subject.empty())
91                opt += "pdfsubject={" + subject + "},\n";
92        if (!keywords.empty())
93                opt += "pdfkeywords={" + keywords + "},\n";
94
95        opt += "bookmarks=" + convert<string>(bookmarks) + ',';
96        if (bookmarks) {
97                    opt += "bookmarksnumbered=" +
98                            convert<string>(bookmarksnumbered) + ',';
99            opt += "bookmarksopen=" +
100                            convert<string>(bookmarksopen) + ',';
101
102            if (bookmarksopen && !bookmarksopenlevel.empty())
103                            opt += "bookmarksopennumbered=" +
104                                bookmarksopenlevel + ',';
105        }
106
107        opt += "breaklinks="     + convert<string>(breaklinks) + ',';
108
109        opt += "pdfborder={0 0 " ;
110        opt += (pdfborder ?'1':'0');
111        opt += "},\n";
112
113        opt += "colorlinks="     + convert<string>(colorlinks) + ',';
114        opt += "backref="        + convert<string>(backref) + ',';
115        opt += "pagebackref="    + convert<string>(pagebackref) + ',';
116
117        if (!pagemode.empty())
118                    opt += "pdfpagemode=" + pagemode + ',';
119        opt += quoted_options_get();
120
121        opt = support::rtrim(opt,",");
122        opt += "]{hyperref}\n";
123
124        // FIXME UNICODE
125        os << from_utf8(opt);
126 }
127
128 string PDFOptions::readToken(Lexer &lex, string const & token)
129 {
130        if (token == "\\use_hyperref") {
131                    lex >> use_hyperref;
132        } else if (token == "\\pdf_title") {
133            lex >> title;
134        } else if (token == "\\pdf_author") {
135            lex >> author;
136        } else if (token == "\\pdf_subject") {
137            lex >> subject;
138        } else if (token == "\\pdf_keywords") {
139            lex >> keywords;
140        } else if (token == "\\pdf_bookmarks") {
141            lex >> bookmarks;
142        } else if (token == "\\pdf_bookmarksnumbered") {
143            lex >> bookmarksnumbered;
144        } else if (token == "\\pdf_bookmarksopen") {
145            lex >> bookmarksopenlevel;
146        } else if (token == "\\pdf_bookmarksopenlevel") {
147            lex >> bookmarksopenlevel;
148        } else if (token == "\\pdf_breaklinks") {
149            lex >> breaklinks;
150        } else if (token == "\\pdf_pdfborder") {
151            lex >> pdfborder;
152        } else if (token == "\\pdf_colorlinks") {
153            lex >> colorlinks;
154        } else if (token == "\\pdf_backref") {
155            lex >> backref;
156        } else if (token == "\\pdf_pagebackref") {
157                    lex >> pagebackref;
158        } else if (token == "\\pdf_pagemode") {
159            lex >> pagemode;
160        } else if (token == "\\pdf_quoted_options") {
161            lex >> quoted_options;
162        } else if (token == "\\pdf_store_options") {
163            lex >> store_options;
164        } else {
165                    return token;
166        }
167
168        return string();
169
170 }
171
172 //prepared for check
173 string PDFOptions::quoted_options_get() const
174 {
175        return quoted_options;
176 }
177
178 // Keep implicit hyperref settings
179 void PDFOptions::clear()
180 {
181        use_hyperref            = false;
182        title.clear();
183        author.clear();
184        subject.clear();
185        keywords.clear();
186        bookmarks               = true;
187        bookmarksnumbered       = false;
188        bookmarksopen           = false;
189        bookmarksopenlevel.clear();
190        breaklinks              = false;
191        pdfborder               = false;
192        colorlinks              = false;
193        backref                 = false;
194        pagebackref             = false;
195        pagemode.clear();
196        quoted_options.clear();
197        store_options           = false;
198
199 }
200
201 } // namespace lyx