]> git.lyx.org Git - lyx.git/blob - src/FontInfo.cpp
Remove nonsense comment.
[lyx.git] / src / FontInfo.cpp
1 /**
2  * \file src/FontInfo.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  * \author Angus Leeming
9  * \author André Pönitz
10  * \author Dekel Tsur
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "FontInfo.h"
18
19 #include "support/debug.h"
20
21 using namespace std;
22
23 namespace lyx {
24
25 FontInfo const sane_font(
26         ROMAN_FAMILY,
27         MEDIUM_SERIES,
28         UP_SHAPE,
29         FONT_SIZE_NORMAL,
30         Color_none,
31         Color_background,
32         FONT_OFF,
33         FONT_OFF,
34         FONT_OFF,
35         FONT_OFF,
36         FONT_OFF,
37         FONT_OFF,
38         FONT_OFF);
39
40 FontInfo const inherit_font(
41         INHERIT_FAMILY,
42         INHERIT_SERIES,
43         INHERIT_SHAPE,
44         FONT_SIZE_INHERIT,
45         Color_inherit,
46         Color_inherit,
47         FONT_INHERIT,
48         FONT_INHERIT,
49         FONT_INHERIT,
50         FONT_INHERIT,
51         FONT_INHERIT,
52         FONT_INHERIT,
53         FONT_OFF);
54
55 FontInfo const ignore_font(
56         IGNORE_FAMILY,
57         IGNORE_SERIES,
58         IGNORE_SHAPE,
59         FONT_SIZE_IGNORE,
60         Color_ignore,
61         Color_ignore,
62         FONT_IGNORE,
63         FONT_IGNORE,
64         FONT_IGNORE,
65         FONT_IGNORE,
66         FONT_IGNORE,
67         FONT_IGNORE,
68         FONT_IGNORE);
69
70
71 FontInfo::FontInfo()
72 {
73         *this = sane_font;
74 }
75
76 /// Decreases font size_ by one
77 FontInfo & FontInfo::decSize()
78 {
79         switch (size_) {
80         case FONT_SIZE_HUGER:        size_ = FONT_SIZE_HUGE;     break;
81         case FONT_SIZE_HUGE:         size_ = FONT_SIZE_LARGEST;  break;
82         case FONT_SIZE_LARGEST:      size_ = FONT_SIZE_LARGER;   break;
83         case FONT_SIZE_LARGER:       size_ = FONT_SIZE_LARGE;    break;
84         case FONT_SIZE_LARGE:        size_ = FONT_SIZE_NORMAL;   break;
85         case FONT_SIZE_NORMAL:       size_ = FONT_SIZE_SMALL;    break;
86         case FONT_SIZE_SMALL:        size_ = FONT_SIZE_FOOTNOTE; break;
87         case FONT_SIZE_FOOTNOTE:     size_ = FONT_SIZE_SCRIPT;   break;
88         case FONT_SIZE_SCRIPT:       size_ = FONT_SIZE_TINY;     break;
89         case FONT_SIZE_TINY:         break;
90         case FONT_SIZE_INCREASE:
91                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INCREASE");
92                 break;
93         case FONT_SIZE_DECREASE:
94                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_DECREASE");
95                 break;
96         case FONT_SIZE_INHERIT:
97                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INHERIT");
98                 break;
99         case FONT_SIZE_IGNORE:
100                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_IGNORE");
101                 break;
102         }
103         return *this;
104 }
105
106
107 /// Increases font size_ by one
108 FontInfo & FontInfo::incSize()
109 {
110         switch (size_) {
111         case FONT_SIZE_HUGER:   break;
112         case FONT_SIZE_HUGE:         size_ = FONT_SIZE_HUGER;    break;
113         case FONT_SIZE_LARGEST:      size_ = FONT_SIZE_HUGE;     break;
114         case FONT_SIZE_LARGER:       size_ = FONT_SIZE_LARGEST;  break;
115         case FONT_SIZE_LARGE:        size_ = FONT_SIZE_LARGER;   break;
116         case FONT_SIZE_NORMAL:       size_ = FONT_SIZE_LARGE;    break;
117         case FONT_SIZE_SMALL:        size_ = FONT_SIZE_NORMAL;   break;
118         case FONT_SIZE_FOOTNOTE:     size_ = FONT_SIZE_SMALL;    break;
119         case FONT_SIZE_SCRIPT:       size_ = FONT_SIZE_FOOTNOTE; break;
120         case FONT_SIZE_TINY:         size_ = FONT_SIZE_SCRIPT;   break;
121         case FONT_SIZE_INCREASE:
122                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INCREASE");
123                 break;
124         case FONT_SIZE_DECREASE:
125                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_DECREASE");
126                 break;
127         case FONT_SIZE_INHERIT:
128                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INHERIT");
129                 break;
130         case FONT_SIZE_IGNORE:
131                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_IGNORE");
132                 break;
133         }
134         return *this;
135 }
136
137
138 /// Reduce font to fall back to template where possible
139 void FontInfo::reduce(FontInfo const & tmplt)
140 {
141         if (family_ == tmplt.family_)
142                 family_ = INHERIT_FAMILY;
143         if (series_ == tmplt.series_)
144                 series_ = INHERIT_SERIES;
145         if (shape_ == tmplt.shape_)
146                 shape_ = INHERIT_SHAPE;
147         if (size_ == tmplt.size_)
148                 size_ = FONT_SIZE_INHERIT;
149         if (emph_ == tmplt.emph_)
150                 emph_ = FONT_INHERIT;
151         if (underbar_ == tmplt.underbar_)
152                 underbar_ = FONT_INHERIT;
153         if (strikeout_ == tmplt.strikeout_)
154                 strikeout_ = FONT_INHERIT;
155         if (uuline_ == tmplt.uuline_)
156                 uuline_ = FONT_INHERIT;
157         if (uwave_ == tmplt.uwave_)
158                 uwave_ = FONT_INHERIT;
159         if (noun_ == tmplt.noun_)
160                 noun_ = FONT_INHERIT;
161         if (color_ == tmplt.color_)
162                 color_ = Color_inherit;
163         if (background_ == tmplt.background_)
164                 background_ = Color_inherit;
165 }
166
167
168 /// Realize font from a template
169 FontInfo & FontInfo::realize(FontInfo const & tmplt)
170 {
171         if ((*this) == inherit_font) {
172                 operator=(tmplt);
173                 return *this;
174         }
175
176         if (family_ == INHERIT_FAMILY)
177                 family_ = tmplt.family_;
178
179         if (series_ == INHERIT_SERIES)
180                 series_ = tmplt.series_;
181
182         if (shape_ == INHERIT_SHAPE)
183                 shape_ = tmplt.shape_;
184
185         if (size_ == FONT_SIZE_INHERIT)
186                 size_ = tmplt.size_;
187
188         if (emph_ == FONT_INHERIT)
189                 emph_ = tmplt.emph_;
190
191         if (underbar_ == FONT_INHERIT)
192                 underbar_ = tmplt.underbar_;
193
194         if (strikeout_ == FONT_INHERIT)
195                 strikeout_ = tmplt.strikeout_;
196
197         if (uuline_ == FONT_INHERIT)
198                 uuline_ = tmplt.uuline_;
199
200         if (uwave_ == FONT_INHERIT)
201                 uwave_ = tmplt.uwave_;
202
203         if (noun_ == FONT_INHERIT)
204                 noun_ = tmplt.noun_;
205
206         if (color_ == Color_inherit)
207                 color_ = tmplt.color_;
208
209         if (background_ == Color_inherit)
210                 background_ = tmplt.background_;
211
212         return *this;
213 }
214
215
216 /// Updates a misc setting according to request
217 static FontState setMisc(FontState newfont,
218         FontState org)
219 {
220         if (newfont == FONT_TOGGLE) {
221                 if (org == FONT_ON)
222                         return FONT_OFF;
223                 else if (org == FONT_OFF)
224                         return FONT_ON;
225                 else {
226                         LYXERR0("Font::setMisc: Need state"
227                                 " FONT_ON or FONT_OFF to toggle. Setting to FONT_ON");
228                         return FONT_ON;
229                 }
230         } else if (newfont == FONT_IGNORE)
231                 return org;
232         else
233                 return newfont;
234 }
235
236 /// Updates font settings according to request
237 void FontInfo::update(FontInfo const & newfont, bool toggleall)
238 {
239         if (newfont.family_ == family_ && toggleall)
240                 setFamily(INHERIT_FAMILY); // toggle 'back'
241         else if (newfont.family_ != IGNORE_FAMILY)
242                 setFamily(newfont.family_);
243         // else it's IGNORE_SHAPE
244
245         // "Old" behaviour: "Setting" bold will toggle bold on/off.
246         switch (newfont.series_) {
247         case BOLD_SERIES:
248                 // We toggle...
249                 if (series_ == BOLD_SERIES && toggleall)
250                         setSeries(MEDIUM_SERIES);
251                 else
252                         setSeries(BOLD_SERIES);
253                 break;
254         case MEDIUM_SERIES:
255         case INHERIT_SERIES:
256                 setSeries(newfont.series_);
257                 break;
258         case IGNORE_SERIES:
259                 break;
260         }
261
262         if (newfont.shape_ == shape_ && toggleall)
263                 shape_ = INHERIT_SHAPE; // toggle 'back'
264         else if (newfont.shape_ != IGNORE_SHAPE)
265                 shape_ = newfont.shape_;
266         // else it's IGNORE_SHAPE
267
268         if (newfont.size_ != FONT_SIZE_IGNORE) {
269                 if (newfont.size_ == FONT_SIZE_INCREASE)
270                         incSize();
271                 else if (newfont.size_ == FONT_SIZE_DECREASE)
272                         decSize();
273                 else
274                         size_ = newfont.size_;
275         }
276
277         setEmph(setMisc(newfont.emph_, emph_));
278         setUnderbar(setMisc(newfont.underbar_, underbar_));
279         setStrikeout(setMisc(newfont.strikeout_, strikeout_));
280         setUuline(setMisc(newfont.uuline_, uuline_));
281         setUwave(setMisc(newfont.uwave_, uwave_));
282         setNoun(setMisc(newfont.noun_, noun_));
283         setNumber(setMisc(newfont.number_, number_));
284
285         if (newfont.color_ == color_ && toggleall)
286                 setColor(Color_inherit); // toggle 'back'
287         else if (newfont.color_ != Color_ignore)
288                 setColor(newfont.color_);
289
290         if (newfont.background_ == background_ && toggleall)
291                 setBackground(Color_inherit); // toggle 'back'
292         else if (newfont.background_ != Color_ignore)
293                 setBackground(newfont.background_);
294 }
295
296 /// Is font resolved?
297 bool FontInfo::resolved() const
298 {
299         return (family_ != INHERIT_FAMILY && series_ != INHERIT_SERIES
300                 && shape_ != INHERIT_SHAPE && size_ != FONT_SIZE_INHERIT
301                 && emph_ != FONT_INHERIT && underbar_ != FONT_INHERIT
302                 && uuline_ != FONT_INHERIT && uwave_ != FONT_INHERIT
303                 && strikeout_ != FONT_INHERIT && noun_ != FONT_INHERIT
304                 && color_ != Color_inherit
305                 && background_ != Color_inherit);
306 }
307
308
309 Color FontInfo::realColor() const
310 {
311         if (paint_color_ != Color_none)
312                 return paint_color_;
313         if (color_ == Color_none)
314                 return Color_foreground;
315         return color_;
316 }
317
318 } // namespace lyx