* lyx_1_6.py:
[lyx.git] / src / FontInfo.cpp
blob00a9da165dfe538b817d5a2aaf979326f1c071ac
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.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
8 * \author Angus Leeming
9 * \author André Pönitz
10 * \author Dekel Tsur
12 * Full author contact details are available in file CREDITS.
15 #include <config.h>
17 #include "FontInfo.h"
19 #include "support/debug.h"
21 using namespace std;
23 namespace lyx {
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);
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);
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);
62 FontInfo::FontInfo()
64 *this = sane_font;
67 /// Decreases font size_ by one
68 FontInfo & FontInfo::decSize()
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;
94 return *this;
98 /// Increases font size_ by one
99 FontInfo & FontInfo::incSize()
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;
125 return *this;
129 /// Reduce font to fall back to template where possible
130 void FontInfo::reduce(FontInfo const & tmplt)
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;
153 /// Realize font from a template
154 FontInfo & FontInfo::realize(FontInfo const & tmplt)
156 if ((*this) == inherit_font) {
157 operator=(tmplt);
158 return *this;
161 if (family_ == INHERIT_FAMILY)
162 family_ = tmplt.family_;
164 if (series_ == INHERIT_SERIES)
165 series_ = tmplt.series_;
167 if (shape_ == INHERIT_SHAPE)
168 shape_ = tmplt.shape_;
170 if (size_ == FONT_SIZE_INHERIT)
171 size_ = tmplt.size_;
173 if (emph_ == FONT_INHERIT)
174 emph_ = tmplt.emph_;
176 if (underbar_ == FONT_INHERIT)
177 underbar_ = tmplt.underbar_;
179 if (noun_ == FONT_INHERIT)
180 noun_ = tmplt.noun_;
182 if (color_ == Color_inherit)
183 color_ = tmplt.color_;
185 if (background_ == Color_inherit)
186 background_ = tmplt.background_;
188 return *this;
192 /// Updates a misc setting according to request
193 static FontState setMisc(FontState newfont,
194 FontState org)
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;
206 } else if (newfont == FONT_IGNORE)
207 return org;
208 else
209 return newfont;
212 /// Updates font settings according to request
213 void FontInfo::update(FontInfo const & newfont, bool toggleall)
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
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;
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
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_;
253 setEmph(setMisc(newfont.emph_, emph_));
254 setUnderbar(setMisc(newfont.underbar_, underbar_));
255 setNoun(setMisc(newfont.noun_, noun_));
256 setNumber(setMisc(newfont.number_, number_));
258 if (newfont.color_ == color_ && toggleall)
259 setColor(Color_inherit); // toggle 'back'
260 else if (newfont.color_ != Color_ignore)
261 setColor(newfont.color_);
263 if (newfont.background_ == background_ && toggleall)
264 setBackground(Color_inherit); // toggle 'back'
265 else if (newfont.background_ != Color_ignore)
266 setBackground(newfont.background_);
269 /// Is font resolved?
270 bool FontInfo::resolved() const
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);
281 Color FontInfo::realColor() const
283 if (paint_color_ != Color_none)
284 return paint_color_;
285 if (color_ == Color_none)
286 return Color_foreground;
287 return color_;
290 } // namespace lyx