3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Matthias Ettrich
8 * Full author contact details are available in file CREDITS.
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "support/gettext.h"
21 #include "TextMetrics.h" // for defaultRowHeight()
23 #include "support/convert.h"
24 #include "support/lstrings.h"
26 #include "support/lassert.h"
29 using namespace lyx::support;
39 : kind_(DEFSKIP), len_(), keep_(false)
43 VSpace::VSpace(VSpaceKind k)
44 : kind_(k), len_(), keep_(false)
48 VSpace::VSpace(Length const & l)
49 : kind_(LENGTH), len_(l), keep_(false)
53 VSpace::VSpace(GlueLength const & l)
54 : kind_(LENGTH), len_(l), keep_(false)
58 VSpace::VSpace(string const & data)
59 : kind_(DEFSKIP), len_(), keep_(false)
64 string input = rtrim(data);
66 size_t const length = input.length();
68 if (length > 1 && input[length - 1] == '*') {
70 input.erase(length - 1);
73 if (prefixIs(input, "defskip"))
75 else if (prefixIs(input, "smallskip"))
77 else if (prefixIs(input, "medskip"))
79 else if (prefixIs(input, "bigskip"))
81 else if (prefixIs(input, "vfill"))
83 else if (isValidGlueLength(input, &len_))
85 else if (isStrDbl(input)) {
86 // This last one is for reading old .lyx files
87 // without units in added_space_top/bottom.
88 // Let unit default to centimeters here.
90 len_ = GlueLength(Length(convert<double>(input), Length::CM));
95 bool VSpace::operator==(VSpace const & other) const
97 if (kind_ != other.kind_)
101 return this->keep_ == other.keep_;
103 if (len_ != other.len_)
106 return keep_ == other.keep_;
110 string const VSpace::asLyXCommand() const
114 case DEFSKIP: result = "defskip"; break;
115 case SMALLSKIP: result = "smallskip"; break;
116 case MEDSKIP: result = "medskip"; break;
117 case BIGSKIP: result = "bigskip"; break;
118 case VFILL: result = "vfill"; break;
119 case LENGTH: result = len_.asString(); break;
127 string const VSpace::asLatexCommand(BufferParams const & params) const
131 return params.getDefSkip().asLatexCommand(params);
134 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
137 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
140 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
143 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
146 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
147 : "\\vspace{" + len_.asLatexString() + '}';
151 // fall through in release mode
157 docstring const VSpace::asGUIName() const
162 result = _("Default skip");
165 result = _("Small skip");
168 result = _("Medium skip");
171 result = _("Big skip");
174 result = _("Vertical fill");
177 result = from_ascii(len_.asString());
181 result += ", " + _("protected");
186 string VSpace::asHTMLLength() const
190 case DEFSKIP: result = "2ex"; break;
191 case SMALLSKIP: result = "1ex"; break;
192 case MEDSKIP: result = "3ex"; break;
193 case BIGSKIP: result = "5ex"; break;
195 Length tmp = len_.len();
197 result = tmp.asHTMLString();
205 int VSpace::inPixels(BufferView const & bv) const
207 // Height of a normal line in pixels (zoom factor considered)
208 int const default_height = defaultRowHeight();
213 return bv.buffer().params().getDefSkip().inPixels(bv);
215 // This is how the skips are normally defined by LaTeX.
216 // But there should be some way to change this per document.
218 return int(default_height / 4);
221 return int(default_height / 2);
224 return default_height;
227 // leave space for the vfill symbol
228 return 3 * default_height;
231 return bv.inPixels(len_.len());
235 // fall through in release mode