]> git.lyx.org Git - lyx.git/blob - src/lyxlength.C
Change the latex font names in order to match the names of type1inst.
[lyx.git] / src / lyxlength.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *      
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "lyxlength.h"
18 #include "lengthcommon.h"
19 #include "lyxrc.h"
20
21 #include "Lsstream.h"
22
23 #include <cstdlib>
24
25
26 LyXLength::LyXLength()
27         : val_(0), unit_(LyXLength::PT)
28 {}
29
30
31 LyXLength::LyXLength(double v, LyXLength::UNIT u)
32         : val_(v), unit_(u)
33 {}
34
35
36 LyXLength::LyXLength(string const & data)
37         : val_(0), unit_(LyXLength::PT)
38 {
39         LyXLength tmp;
40         
41         if (!isValidLength (data, &tmp))
42                 return; // should raise an exception
43
44         val_  = tmp.val_;
45         unit_ = tmp.unit_;
46 }
47
48
49 string const LyXLength::asString() const
50 {
51         ostringstream buffer;
52         buffer << val_ << unit_name[unit_]; // setw?
53         return buffer.str().c_str();
54 }
55
56
57 string const LyXLength::asLatexString() const
58 {
59         ostringstream buffer;
60         switch(unit_) {
61         case PW:
62         case PE:
63             buffer << abs(static_cast<int>(val_/100)) << "."
64                    << abs(static_cast<int>(val_)%100) << "\\columnwidth";
65             break;
66         case PP:
67             buffer << abs(static_cast<int>(val_/100)) << "."
68                    << abs(static_cast<int>(val_)%100) << "\\paperwidth";
69             break;
70         case PL:
71             buffer << abs(static_cast<int>(val_/100)) << "."
72                    << abs(static_cast<int>(val_)%100) << "\\linewidth";
73             break;
74         default:
75             buffer << val_ << unit_name[unit_]; // setw?
76             break;
77         }
78         return buffer.str().c_str();
79 }
80
81
82 double LyXLength::value() const
83 {
84         return val_;
85 }
86
87
88 LyXLength::UNIT LyXLength::unit() const
89 {
90         return unit_;
91 }
92
93
94 void LyXLength::value(double v)
95 {
96         val_ = v;
97 }
98
99
100 void LyXLength::unit(LyXLength::UNIT u)
101 {
102         unit_ = u;
103 }
104
105
106 bool LyXLength::zero() const 
107 {
108         return val_ == 0.0;
109 }
110
111
112 int LyXLength::inPixels(int default_width, int default_height) const
113 {
114         // Zoom factor specified by user in percent
115         double const zoom = lyxrc.zoom / 100.0; // [percent]
116
117         // DPI setting for monitor: pixels/inch
118         double const dpi = lyxrc.dpi; // screen resolution [pixels/inch]
119
120         // Pixel values are scaled so that the ratio
121         // between lengths and font sizes on the screen
122         // is the same as on paper.
123
124         // we don't care about sign of value, we
125         // display negative space with text too
126         double result = 0.0;
127         int val_sign = val_ < 0.0 ? -1 : 1;
128                 
129         switch (unit_) {
130         case LyXLength::SP:
131                 // Scaled point: sp = 1/65536 pt
132                 result = zoom * dpi * val_
133                         / (72.27 * 65536); // 4736286.7
134                 break;
135         case LyXLength::PT:
136                 // Point: 1 pt = 1/72.27 inch
137                 result = zoom * dpi * val_
138                         / 72.27; // 72.27
139                 break;
140         case LyXLength::BP:
141                 // Big point: 1 bp = 1/72 inch
142                 result = zoom * dpi * val_
143                         / 72; // 72
144                 break;
145         case LyXLength::DD:
146                 // Didot: 1157dd = 1238 pt?
147                 result = zoom * dpi * val_
148                         / (72.27 / (0.376 * 2.845)); // 67.559735
149                 break;
150         case LyXLength::MM:
151                 // Millimeter: 1 mm = 1/25.4 inch
152                 result = zoom * dpi * val_
153                         / 25.4; // 25.4
154                 break;
155         case LyXLength::PC:
156                 // Pica: 1 pc = 12 pt
157                 result = zoom * dpi * val_
158                         / (72.27 / 12); // 6.0225
159                 break;
160         case LyXLength::CC:
161                 // Cicero: 1 cc = 12 dd
162                 result = zoom * dpi * val_
163                         / (72.27 / (12 * 0.376 * 2.845)); // 5.6299779
164                 break;
165         case LyXLength::CM:
166                 // Centimeter: 1 cm = 1/2.54 inch
167                 result = zoom * dpi * val_
168                         / 2.54; // 2.54
169                 break;
170         case LyXLength::IN:
171                 // Inch
172                 result = zoom * dpi * val_;
173                 break;
174         case LyXLength::EX:
175                 // Ex: The height of an "x"
176                 result = zoom * val_ * default_height / 2; // what to / width?
177                 break;
178         case LyXLength::EM: // what to / width?
179                 // Em: The width of an "m"
180                 result = zoom * val_ * default_height / 2; // Why 2?
181                 break;
182         case LyXLength::MU: // This is probably only allowed in
183                 // math mode
184                 result = zoom * val_ * default_height;
185                 break;
186         case LyXLength::PW: // Always % of workarea
187         case LyXLength::PE:
188         case LyXLength::PP:
189         case LyXLength::PL:
190                 result = val_ * default_width / 100;
191                 break;
192         case LyXLength::UNIT_NONE:
193                 result = 0;  // this cannot happen
194                 break;
195         }
196         return static_cast<int>(result * val_sign + 0.5);
197 }
198
199
200 bool operator==(LyXLength const & l1, LyXLength const & l2)
201 {
202         return l1.value() == l2.value() && l1.unit() == l2.unit();
203 }
204
205
206 bool operator!=(LyXLength const & l1, LyXLength const & l2)
207 {
208         return !(l1 == l2);
209 }
210