]> git.lyx.org Git - lyx.git/blob - src/Spacing.cpp
Bulk cleanup/fix incorrect annotation at the end of namespaces.
[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/lstrings.h"
16 #include "support/convert.h"
17
18 #include <ostream>
19
20 using namespace std;
21
22 namespace lyx {
23
24
25 string const Spacing::spacing_string[]
26         = {"single", "onehalf", "double", "other"};
27
28
29 string const Spacing::getValueAsString() const
30 {
31         switch (space) {
32         case Default: // nothing special should happen with this...
33         case Single: return "1.0";
34         case Onehalf: return "1.25";
35         case Double: return "1.667";
36         case Other: return value;
37         }
38         return "1.0";
39 }
40
41
42 double Spacing::getValue() const
43 {
44         return convert<double>(getValueAsString());
45 }
46
47
48 void Spacing::set(Spacing::Space sp, double val)
49 {
50         set(sp, convert<string>(val));
51 }
52
53
54 void Spacing::set(Spacing::Space sp, string const & val)
55 {
56         space = sp;
57         if (sp == Other) {
58                 switch (int(convert<double>(val) * 1000 + 0.5)) {
59                 case 1000:
60                         space = Single;
61                         break;
62                 case 1250:
63                         space = Onehalf;
64                         break;
65                 case 1667:
66                         space = Double;
67                         break;
68                 default:
69                         value = val;
70                         break;
71                 }
72         }
73 }
74
75
76 void Spacing::writeFile(ostream & os, bool para) const
77 {
78         if (space == Default) return;
79
80         string cmd = para ? "\\paragraph_spacing " : "\\spacing ";
81
82         if (getSpace() == Spacing::Other) {
83                 os << cmd << spacing_string[getSpace()]
84                    << ' ' << getValueAsString() << "\n";
85         } else {
86                 os << cmd << spacing_string[getSpace()] << "\n";
87         }
88 }
89
90
91 namespace {
92
93 string envName(Spacing::Space space, bool useSetSpace)
94 {
95         static char const * const env_names[]
96                 = { "SingleSpace", "OnehalfSpace", "DoubleSpace", "Spacing", ""};
97         string const name = env_names[space];
98
99         return useSetSpace ? name : support::ascii_lowercase(name);
100 }
101
102 } // namespace
103
104 string const Spacing::writeEnvirBegin(bool useSetSpace) const
105 {
106         string const name = envName(space, useSetSpace);
107         if (space == Other)
108                 return "\\begin{" + name + "}{" + getValueAsString() + '}';
109         else
110                 return name.empty() ? string() : "\\begin{" + name + '}';
111 }
112
113
114 string const Spacing::writeEnvirEnd(bool useSetSpace) const
115 {
116         string const name = envName(space, useSetSpace);
117         return name.empty() ? string() : "\\end{" + name + '}';
118 }
119
120
121 string const Spacing::writePreamble(bool useSetSpace) const
122 {
123         string preamble;
124         switch (space) {
125         case Default:
126         case Single:
127                 // we dont use setspace.sty so dont print anything
128                 //return "\\singlespacing\n";
129                 break;
130         case Onehalf:
131                 preamble = useSetSpace ? "\\OnehalfSpacing\n"
132                         : "\\onehalfspacing\n";
133                 break;
134         case Double:
135                 preamble = useSetSpace ? "\\DoubleSpacing\n"
136                         : "\\doublespacing\n";
137                 break;
138         case Other:
139                 preamble = (useSetSpace ? "\\setSpacing{" : "\\setstretch{")
140                         + getValueAsString() + "}\n";
141                 break;
142         }
143         return preamble;
144 }
145
146 } // namespace lyx