]> git.lyx.org Git - lyx.git/blob - src/Floating.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / Floating.cpp
1 /**
2  * \file Floating.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14 #include <set>
15
16 #include "Floating.h"
17
18 #include "support/debug.h"
19 #include "support/lstrings.h"
20 #include "support/textutils.h"
21
22 using namespace std;
23
24
25 namespace lyx {
26
27
28 Floating::Floating(string const & type, string const & placement,
29                    string const & ext, string const & within,
30                    string const & style, string const & name,
31                    string const & listName, std::string const & listCmd,
32                    string const & refPrefix, std::string const & allowedplacement,
33                    string const & htmlTag, string const & htmlAttrib,
34                    docstring const & htmlStyle, std::string const & docbookTag,
35                    string const & docbookAttr, string const & docbookTagType,
36                    std::string const & docbookFloatType, std::string const & docbookCaption,
37                    string const & required, bool usesfloat, bool ispredefined,
38                    bool allowswide, bool allowssideways)
39         : floattype_(type), placement_(placement), ext_(ext), within_(within),
40           style_(style), name_(name), listname_(listName), listcommand_(listCmd),
41           refprefix_(refPrefix), allowedplacement_(allowedplacement), required_(required),
42           usesfloatpkg_(usesfloat), ispredefined_(ispredefined),
43           allowswide_(allowswide), allowssideways_(allowssideways),
44           html_tag_(htmlTag), html_attrib_(htmlAttrib), html_style_(htmlStyle),
45           docbook_caption_(docbookCaption), docbook_tag_(docbookTag),
46           docbook_tag_type_(docbookTagType)
47 {
48         // Implement some edge cases for DocBook. Both docbook_float_type_ and docbook_attr_ must be computed
49         // based on the given value of docbookFloatType; docbook_tag_ can still be guessed without correlation.
50
51         // Determine the value of docbook_float_type_.
52         {
53                 // These are the allowed values for docbook_float_type_. Both docbook_attr_ and docbook_tag_type_
54                 // depend on this list.
55                 static std::set<std::string> allowedFloatTypes{"figure", "table", "algorithm", "video", "example"};
56
57                 // If some type is predetermined in the layout, use it.
58                 if (!docbookFloatType.empty() && allowedFloatTypes.find(docbookFloatType) != allowedFloatTypes.end())
59                         docbook_float_type_ = docbookFloatType;
60                 // Otherwise, try to guess the DocBook type based on the float type.
61                 else if (floattype_ == "figure" || floattype_ == "graph" || floattype_ == "chart" || floattype_ == "scheme" ||
62                                 floattype_ == "marginfigure") {
63                         docbook_float_type_ = "figure";
64                 } else if (floattype_ == "table" || floattype_ == "tableau" || floattype_ == "margintable") {
65                         docbook_float_type_ = "table";
66                 } else if (floattype_ == "algorithm") {
67                         docbook_float_type_ = "algorithm";
68                 } else if (floattype_ == "video") {
69                         docbook_float_type_ = "video";
70                 } else {
71                         // If nothing matches, return something that will not be valid.
72                         LYXERR(Debug::OUTFILE, "Float type '" + floattype_ + "' unknown to DocBook!");
73                         docbook_float_type_ = "unknown";
74                 }
75         }
76
77         // Determine the value of docbook_attr_.
78         {
79                 std::set<std::string> achemso = {"chart", "graph", "scheme"};
80                 bool hasType = docbook_attr_.find("type=") != std::string::npos;
81
82                 // For algorithms, a type attribute must be mentioned, if not already present in docbook_attr_.
83                 if (docbook_float_type_ == "algorithm" && !hasType)
84                         docbook_attr_ += " type='algorithm'";
85                 // Specific floats for achemso.
86                 else if (docbook_float_type_ == "figure" && achemso.find(floattype_) != achemso.end())
87                         docbook_attr_ += " type='" + floattype_ + "'";
88
89                 // Finally, merge in the attributes given in argument.
90                 if (!docbookAttr.empty())
91                         docbook_attr_ += " " + docbookAttr;
92         }
93 }
94
95
96 string const & Floating::htmlAttrib() const
97 {
98         if (html_attrib_.empty())
99                 html_attrib_ = "class='" + defaultCSSClass() + "'";
100         return html_attrib_;
101 }
102
103
104 string const & Floating::htmlTag() const
105 {
106         if (html_tag_.empty())
107                 html_tag_ = "div";
108         return html_tag_;
109 }
110
111
112 string Floating::defaultCSSClass() const
113 {
114         if (!defaultcssclass_.empty())
115                 return defaultcssclass_;
116         string d;
117         string n = floattype_;
118         string::const_iterator it = n.begin();
119         string::const_iterator en = n.end();
120         for (; it != en; ++it) {
121                 if (!isAlphaASCII(*it))
122                         d += "_";
123                 else if (isLower(*it))
124                         d += *it;
125                 else
126                         d += support::lowercase(*it);
127         }
128         // are there other characters we need to remove?
129         defaultcssclass_ = "float-" + d;
130         return defaultcssclass_;
131 }
132
133
134 std::string Floating::docbookFloatType() const
135 {
136         // All the work is done in the constructor.
137         return docbook_float_type_;
138 }
139
140
141 string Floating::docbookAttr() const
142 {
143         return docbook_attr_;
144 }
145
146
147 string Floating::docbookTag(bool hasTitle) const
148 {
149         // If there is a preconfigured tag, use it.
150         if (!docbook_tag_.empty())
151                 return docbook_tag_;
152
153         // Otherwise, guess it.
154         if (docbookFloatType() == "figure" || docbookFloatType() == "algorithm" || docbookFloatType() == "video") {
155                 return hasTitle ? "figure" : "informalfigure";
156         } else if (docbookFloatType() == "example") {
157                 return hasTitle ? "example" : "informalexample";
158         } else if (docbookFloatType() == "table") {
159                 return hasTitle ? "table" : "informaltable";
160         } else {
161                 // If nothing matches, return something that will not be valid.
162                 LYXERR(Debug::OUTFILE, "Float type '" + floattype() + "' unknown to DocBook!");
163                 return "float";
164         }
165 }
166
167
168 string const & Floating::docbookTagType() const
169 {
170         if (docbook_tag_type_ != "block" && docbook_tag_type_ != "paragraph" && docbook_tag_type_ != "inline")
171                 docbook_tag_type_ = "block";
172         return docbook_tag_type_;
173 }
174
175
176 string const & Floating::docbookCaption() const
177 {
178         if (!docbook_caption_.empty())
179                 return docbook_caption_;
180
181         if (docbook_float_type_ == "figure" || docbook_float_type_ == "video" ||
182                         docbook_float_type_ == "algorithm" || docbook_float_type_ == "example")
183                 docbook_caption_ = "title";
184         else if (floattype_ == "table" || floattype_ == "tableau")
185                 docbook_caption_ = "caption";
186         return docbook_caption_;
187 }
188
189
190 } // namespace lyx