]> git.lyx.org Git - lyx.git/blob - src/Spacing.C
If I ever see another licence blurb again, it'll be too soon...
[lyx.git] / src / Spacing.C
1 /**
2  * \file Spacing.C
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 "Lsstream.h"
15 #include "Spacing.h"
16 #include "LString.h"
17
18 using std::ios;
19 using std::ostream;
20
21 string const Spacing::spacing_string[]
22         = {"single", "onehalf", "double", "other"};
23
24 float Spacing::getValue() const
25 {
26         switch (space) {
27         case Default: // nothing special should happen with this...
28         case Single: return 1.0;
29         case Onehalf: return 1.25;
30         case Double: return 1.667;
31         case Other: return value;
32         }
33         return 1.0;
34 }
35
36
37 void Spacing::set(Spacing::Space sp, float val)
38 {
39         space = sp;
40         if (sp == Other) {
41                 switch (int(val * 1000 + 0.5)) {
42                 case 1000: space = Single; break;
43                 case 1250: space = Onehalf; break;
44                 case 1667: space = Double; break;
45                 default: value = val; break;
46                 }
47         }
48 }
49
50
51 void Spacing::set(Spacing::Space sp, string const & val)
52 {
53         float fval = 0.0;
54         istringstream istr(val.c_str());
55         istr >> fval;
56         set(sp, fval);
57 }
58
59
60 void Spacing::writeFile(ostream & os, bool para) const
61 {
62         if (space == Default) return;
63
64         string cmd = para ? "\\paragraph_spacing " : "\\spacing ";
65
66         if (getSpace() == Spacing::Other) {
67                 os.setf(ios::showpoint|ios::fixed);
68                 os.precision(2);
69                 os << cmd << spacing_string[getSpace()]
70                    << ' ' << getValue() << " \n";
71         } else {
72                 os << cmd << spacing_string[getSpace()] << " \n";
73         }
74 }
75
76
77 string const Spacing::writeEnvirBegin() const
78 {
79         switch (space) {
80         case Default: break; // do nothing
81         case Single:
82                 return "\\begin{singlespace}";
83         case Onehalf:
84                 return "\\begin{onehalfspace}";
85         case Double:
86                 return "\\begin{doublespace}";
87         case Other:
88         {
89                 ostringstream ost;
90                 ost << "\\begin{spacing}{"
91                     << getValue() << '}';
92                 return STRCONV(ost.str());
93         }
94         }
95         return string();
96 }
97
98
99 string const Spacing::writeEnvirEnd() const
100 {
101         switch (space) {
102         case Default: break; // do nothing
103         case Single:
104                 return "\\end{singlespace}";
105         case Onehalf:
106                 return "\\end{onehalfspace}";
107         case Double:
108                 return "\\end{doublespace}";
109         case Other:
110                 return "\\end{spacing}";
111         }
112         return string();
113 }