]> git.lyx.org Git - lyx.git/blob - src/FontInfo.cpp
english_language was not used at all.
[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
37 FontInfo const inherit_font(
38         INHERIT_FAMILY,
39         INHERIT_SERIES,
40         INHERIT_SHAPE,
41         FONT_SIZE_INHERIT,
42         Color_inherit,
43         Color_inherit,
44         FONT_INHERIT,
45         FONT_INHERIT,
46         FONT_INHERIT,
47         FONT_OFF);
48
49 FontInfo const ignore_font(
50         IGNORE_FAMILY,
51         IGNORE_SERIES,
52         IGNORE_SHAPE,
53         FONT_SIZE_IGNORE,
54         Color_ignore,
55         Color_ignore,
56         FONT_IGNORE,
57         FONT_IGNORE,
58         FONT_IGNORE,
59         FONT_IGNORE);
60
61
62 FontInfo::FontInfo()
63 {
64         *this = sane_font;
65 }
66
67 /// Decreases font size_ by one
68 FontInfo & FontInfo::decSize()
69 {
70         switch (size_) {
71         case FONT_SIZE_HUGER:        size_ = FONT_SIZE_HUGE;     break;
72         case FONT_SIZE_HUGE:         size_ = FONT_SIZE_LARGEST;  break;
73         case FONT_SIZE_LARGEST:      size_ = FONT_SIZE_LARGER;   break;
74         case FONT_SIZE_LARGER:       size_ = FONT_SIZE_LARGE;    break;
75         case FONT_SIZE_LARGE:        size_ = FONT_SIZE_NORMAL;   break;
76         case FONT_SIZE_NORMAL:       size_ = FONT_SIZE_SMALL;    break;
77         case FONT_SIZE_SMALL:        size_ = FONT_SIZE_FOOTNOTE; break;
78         case FONT_SIZE_FOOTNOTE:     size_ = FONT_SIZE_SCRIPT;   break;
79         case FONT_SIZE_SCRIPT:       size_ = FONT_SIZE_TINY;     break;
80         case FONT_SIZE_TINY:         break;
81         case FONT_SIZE_INCREASE:
82                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INCREASE");
83                 break;
84         case FONT_SIZE_DECREASE:
85                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_DECREASE");
86                 break;
87         case FONT_SIZE_INHERIT:
88                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_INHERIT");
89                 break;
90         case FONT_SIZE_IGNORE:
91                 LYXERR0("Can't FontInfo::decSize on FONT_SIZE_IGNORE");
92                 break;
93         }
94         return *this;
95 }
96
97
98 /// Increases font size_ by one
99 FontInfo & FontInfo::incSize()
100 {
101         switch (size_) {
102         case FONT_SIZE_HUGER:   break;
103         case FONT_SIZE_HUGE:         size_ = FONT_SIZE_HUGER;    break;
104         case FONT_SIZE_LARGEST:      size_ = FONT_SIZE_HUGE;     break;
105         case FONT_SIZE_LARGER:       size_ = FONT_SIZE_LARGEST;  break;
106         case FONT_SIZE_LARGE:        size_ = FONT_SIZE_LARGER;   break;
107         case FONT_SIZE_NORMAL:       size_ = FONT_SIZE_LARGE;    break;
108         case FONT_SIZE_SMALL:        size_ = FONT_SIZE_NORMAL;   break;
109         case FONT_SIZE_FOOTNOTE:     size_ = FONT_SIZE_SMALL;    break;
110         case FONT_SIZE_SCRIPT:       size_ = FONT_SIZE_FOOTNOTE; break;
111         case FONT_SIZE_TINY:         size_ = FONT_SIZE_SCRIPT;   break;
112         case FONT_SIZE_INCREASE:
113                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INCREASE");
114                 break;
115         case FONT_SIZE_DECREASE:
116                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_DECREASE");
117                 break;
118         case FONT_SIZE_INHERIT:
119                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_INHERIT");
120                 break;
121         case FONT_SIZE_IGNORE:
122                 LYXERR0("Can't FontInfo::incSize on FONT_SIZE_IGNORE");
123                 break;
124         }
125         return *this;
126 }
127
128
129 /// Reduce font to fall back to template where possible
130 void FontInfo::reduce(FontInfo const & tmplt)
131 {
132         if (family_ == tmplt.family_)
133                 family_ = INHERIT_FAMILY;
134         if (series_ == tmplt.series_)
135                 series_ = INHERIT_SERIES;
136         if (shape_ == tmplt.shape_)
137                 shape_ = INHERIT_SHAPE;
138         if (size_ == tmplt.size_)
139                 size_ = FONT_SIZE_INHERIT;
140         if (emph_ == tmplt.emph_)
141                 emph_ = FONT_INHERIT;
142         if (underbar_ == tmplt.underbar_)
143                 underbar_ = FONT_INHERIT;
144         if (noun_ == tmplt.noun_)
145                 noun_ = FONT_INHERIT;
146         if (color_ == tmplt.color_)
147                 color_ = Color_inherit;
148         if (background_ == tmplt.background_)
149                 background_ = Color_inherit;
150 }
151
152
153 /// Realize font from a template
154 FontInfo & FontInfo::realize(FontInfo const & tmplt)
155 {
156         if ((*this) == inherit_font) {
157                 operator=(tmplt);
158                 return *this;
159         }
160
161         if (family_ == INHERIT_FAMILY)
162                 family_ = tmplt.family_;
163
164         if (series_ == INHERIT_SERIES)
165                 series_ = tmplt.series_;
166
167         if (shape_ == INHERIT_SHAPE)
168                 shape_ = tmplt.shape_;
169
170         if (size_ == FONT_SIZE_INHERIT)
171                 size_ = tmplt.size_;
172
173         if (emph_ == FONT_INHERIT)
174                 emph_ = tmplt.emph_;
175
176         if (underbar_ == FONT_INHERIT)
177                 underbar_ = tmplt.underbar_;
178
179         if (noun_ == FONT_INHERIT)
180                 noun_ = tmplt.noun_;
181
182         if (color_ == Color_inherit)
183                 color_ = tmplt.color_;
184
185         if (background_ == Color_inherit)
186                 background_ = tmplt.background_;
187
188         return *this;
189 }
190
191
192 /// Updates a misc setting according to request
193 static FontState setMisc(FontState newfont,
194         FontState org)
195 {
196         if (newfont == FONT_TOGGLE) {
197                 if (org == FONT_ON)
198                         return FONT_OFF;
199                 else if (org == FONT_OFF)
200                         return FONT_ON;
201                 else {
202                         LYXERR0("Font::setMisc: Need state"
203                                 " FONT_ON or FONT_OFF to toggle. Setting to FONT_ON");
204                         return FONT_ON;
205                 }
206         } else if (newfont == FONT_IGNORE)
207                 return org;
208         else
209                 return newfont;
210 }
211
212 /// Updates font settings according to request
213 void FontInfo::update(FontInfo const & newfont, bool toggleall)
214 {
215         if (newfont.family_ == family_ && toggleall)
216                 setFamily(INHERIT_FAMILY); // toggle 'back'
217         else if (newfont.family_ != IGNORE_FAMILY)
218                 setFamily(newfont.family_);
219         // else it's IGNORE_SHAPE
220
221         // "Old" behaviour: "Setting" bold will toggle bold on/off.
222         switch (newfont.series_) {
223         case BOLD_SERIES:
224                 // We toggle...
225                 if (series_ == BOLD_SERIES && toggleall)
226                         setSeries(MEDIUM_SERIES);
227                 else
228                         setSeries(BOLD_SERIES);
229                 break;
230         case MEDIUM_SERIES:
231         case INHERIT_SERIES:
232                 setSeries(newfont.series_);
233                 break;
234         case IGNORE_SERIES:
235                 break;
236         }
237
238         if (newfont.shape_ == shape_ && toggleall)
239                 shape_ = INHERIT_SHAPE; // toggle 'back'
240         else if (newfont.shape_ != IGNORE_SHAPE)
241                 shape_ = newfont.shape_;
242         // else it's IGNORE_SHAPE
243
244         if (newfont.size_ != FONT_SIZE_IGNORE) {
245                 if (newfont.size_ == FONT_SIZE_INCREASE)
246                         incSize();
247                 else if (newfont.size_ == FONT_SIZE_DECREASE)
248                         decSize();
249                 else
250                         size_ = newfont.size_;
251         }
252
253         setEmph(setMisc(newfont.emph_, emph_));
254         setUnderbar(setMisc(newfont.underbar_, underbar_));
255         setNoun(setMisc(newfont.noun_, noun_));
256         setNumber(setMisc(newfont.number_, number_));
257
258         if (newfont.color_ == color_ && toggleall)
259                 setColor(Color_inherit); // toggle 'back'
260         else if (newfont.color_ != Color_ignore)
261                 setColor(newfont.color_);
262
263         if (newfont.background_ == background_ && toggleall)
264                 setBackground(Color_inherit); // toggle 'back'
265         else if (newfont.background_ != Color_ignore)
266                 setBackground(newfont.background_);
267 }
268
269 /// Is font resolved?
270 bool FontInfo::resolved() const
271 {
272         return (family_ != INHERIT_FAMILY && series_ != INHERIT_SERIES
273                 && shape_ != INHERIT_SHAPE && size_ != FONT_SIZE_INHERIT
274                 && emph_ != FONT_INHERIT && underbar_ != FONT_INHERIT
275                 && noun_ != FONT_INHERIT
276                 && color_ != Color_inherit
277                 && background_ != Color_inherit);
278 }
279
280
281 ColorCode FontInfo::realColor() const
282 {
283         if (color_ == Color_none)
284                 return Color_foreground;
285         return color_;
286 }
287
288 } // namespace lyx