]> git.lyx.org Git - lyx.git/blob - src/Spacing.cpp
Provide proper fallback if a bibliography processor is not found
[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 string cmdName(Spacing::Space space, bool useSetSpace)
103 {
104         static char const * const cmd_names[]
105                 = { "SingleSpacing", "OnehalfSpacing", "DoubleSpacing", "SetStretch", ""};
106         string const name = cmd_names[space];
107
108         if (useSetSpace && name == "SetStretch")
109                 return "setSpacing";
110
111         return useSetSpace ? name : support::ascii_lowercase(name);
112 }
113
114 } // namespace
115
116 string const Spacing::writeEnvirBegin(bool useSetSpace) const
117 {
118         string const name = envName(space, useSetSpace);
119         if (space == Other)
120                 return "\\begin{" + name + "}{" + getValueAsString() + '}';
121         else
122                 return name.empty() ? string() : "\\begin{" + name + '}';
123 }
124
125
126 string const Spacing::writeEnvirEnd(bool useSetSpace) const
127 {
128         string const name = envName(space, useSetSpace);
129         return name.empty() ? string() : "\\end{" + name + '}';
130 }
131
132
133 string const Spacing::writeCmd(bool useSetSpace) const
134 {
135         string const name = cmdName(space, useSetSpace);
136         if (space == Other)
137                 return "\\" + name + "{" + getValueAsString() + '}';
138         else
139                 return name.empty() ? string() : "\\" + name + "{}";
140 }
141
142
143 string const Spacing::writePreamble(bool useSetSpace) const
144 {
145         string preamble;
146         switch (space) {
147         case Default:
148         case Single:
149                 // we dont use setspace.sty so dont print anything
150                 //return "\\singlespacing\n";
151                 break;
152         case Onehalf:
153                 preamble = useSetSpace ? "\\OnehalfSpacing\n"
154                         : "\\onehalfspacing\n";
155                 break;
156         case Double:
157                 preamble = useSetSpace ? "\\DoubleSpacing\n"
158                         : "\\doublespacing\n";
159                 break;
160         case Other:
161                 preamble = (useSetSpace ? "\\setSpacing{" : "\\setstretch{")
162                         + getValueAsString() + "}\n";
163                 break;
164         }
165         return preamble;
166 }
167
168 } // namespace lyx