]> git.lyx.org Git - lyx.git/blob - src/Spacing.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / Spacing.cpp
1 /**
2  * \file Spacing.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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Spacing.h"
15 #include "support/convert.h"
16 #include "support/lstrings.h"
17 #include "support/lyxlib.h"
18
19 #include <ostream>
20
21 using namespace std;
22
23 namespace lyx {
24
25
26 string const Spacing::spacing_string[]
27         = {"single", "onehalf", "double", "other"};
28
29
30 string const Spacing::getValueAsString() const
31 {
32         switch (space) {
33         case Default: // nothing special should happen with this...
34         case Single: return "1.0";
35         case Onehalf: return "1.25";
36         case Double: return "1.667";
37         case Other: return value;
38         }
39         return "1.0";
40 }
41
42
43 double Spacing::getValue() const
44 {
45         return convert<double>(getValueAsString());
46 }
47
48
49 void Spacing::set(Spacing::Space sp, double val)
50 {
51         set(sp, convert<string>(val));
52 }
53
54
55 void Spacing::set(Spacing::Space sp, string const & val)
56 {
57         space = sp;
58         if (sp == Other) {
59                 switch (support::iround(convert<double>(val) * 1000)) {
60                 case 1000:
61                         space = Single;
62                         break;
63                 case 1250:
64                         space = Onehalf;
65                         break;
66                 case 1667:
67                         space = Double;
68                         break;
69                 default:
70                         value = val;
71                         break;
72                 }
73         }
74 }
75
76
77 void Spacing::writeFile(ostream & os, bool para) const
78 {
79         if (space == Default) return;
80
81         string cmd = para ? "\\paragraph_spacing " : "\\spacing ";
82
83         if (getSpace() == Spacing::Other) {
84                 os << cmd << spacing_string[getSpace()]
85                    << ' ' << getValueAsString() << "\n";
86         } else {
87                 os << cmd << spacing_string[getSpace()] << "\n";
88         }
89 }
90
91
92 namespace {
93
94 string envName(Spacing::Space space, bool useSetSpace)
95 {
96         static char const * const env_names[]
97                 = { "SingleSpace", "OnehalfSpace", "DoubleSpace", "Spacing", ""};
98         string const name = env_names[space];
99
100         return useSetSpace ? name : support::ascii_lowercase(name);
101 }
102
103 string cmdName(Spacing::Space space, bool useSetSpace)
104 {
105         static char const * const cmd_names[]
106                 = { "SingleSpacing", "OnehalfSpacing", "DoubleSpacing", "SetStretch", ""};
107         string const name = cmd_names[space];
108
109         if (useSetSpace && name == "SetStretch")
110                 return "setSpacing";
111
112         return useSetSpace ? name : support::ascii_lowercase(name);
113 }
114
115 } // namespace
116
117 string const Spacing::writeEnvirBegin(bool useSetSpace) const
118 {
119         string const name = envName(space, useSetSpace);
120         if (space == Other)
121                 return "\\begin{" + name + "}{" + getValueAsString() + '}';
122         else
123                 return name.empty() ? string() : "\\begin{" + name + '}';
124 }
125
126
127 string const Spacing::writeEnvirEnd(bool useSetSpace) const
128 {
129         string const name = envName(space, useSetSpace);
130         return name.empty() ? string() : "\\end{" + name + '}';
131 }
132
133
134 string const Spacing::writeCmd(bool useSetSpace) const
135 {
136         string const name = cmdName(space, useSetSpace);
137         if (space == Other)
138                 return "\\" + name + "{" + getValueAsString() + '}';
139         else
140                 return name.empty() ? string() : "\\" + name + "{}";
141 }
142
143
144 string const Spacing::writePreamble(bool useSetSpace) const
145 {
146         string preamble;
147         switch (space) {
148         case Default:
149         case Single:
150                 // we dont use setspace.sty so dont print anything
151                 //return "\\singlespacing\n";
152                 break;
153         case Onehalf:
154                 preamble = useSetSpace ? "\\OnehalfSpacing\n"
155                         : "\\onehalfspacing\n";
156                 break;
157         case Double:
158                 preamble = useSetSpace ? "\\DoubleSpacing\n"
159                         : "\\doublespacing\n";
160                 break;
161         case Other:
162                 preamble = (useSetSpace ? "\\setSpacing{" : "\\setstretch{")
163                         + getValueAsString() + "}\n";
164                 break;
165         }
166         return preamble;
167 }
168
169 } // namespace lyx