]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSeparator.cpp
Circumvent odd stmary font metrics (part of #9990).
[lyx.git] / src / insets / InsetSeparator.cpp
1 /**
2  * \file InsetSeparator.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Enrico Forestieri
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetSeparator.h"
14
15 #include "Cursor.h"
16 #include "Dimension.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "Lexer.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23
24 #include "frontends/Application.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
27
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/docstring.h"
31
32 using namespace std;
33 using namespace lyx::frontend;
34
35 namespace lyx {
36
37 InsetSeparator::InsetSeparator() : Inset(0)
38 {}
39
40
41 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
42         : Inset(0), params_(params)
43 {}
44
45
46 void InsetSeparatorParams::write(ostream & os) const
47 {
48         switch (kind) {
49         case InsetSeparatorParams::PLAIN:
50                 os <<  "plain";
51                 break;
52         case InsetSeparatorParams::PARBREAK:
53                 os <<  "parbreak";
54                 break;
55         }
56 }
57
58
59 void InsetSeparatorParams::read(Lexer & lex)
60 {
61         string token;
62         lex.setContext("InsetSeparatorParams::read");
63         lex >> token;
64         if (token == "plain")
65                 kind = InsetSeparatorParams::PLAIN;
66         else if (token == "parbreak")
67                 kind = InsetSeparatorParams::PARBREAK;
68         else
69                 lex.printError("Unknown kind: `$$Token'");
70 }
71
72
73 void InsetSeparator::write(ostream & os) const
74 {
75         os << "Separator ";
76         params_.write(os);
77 }
78
79
80 void InsetSeparator::read(Lexer & lex)
81 {
82         params_.read(lex);
83         lex >> "\\end_inset";
84 }
85
86
87 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
88 {
89         switch (cmd.action()) {
90
91         case LFUN_INSET_MODIFY: {
92                 InsetSeparatorParams params;
93                 cur.recordUndo();
94                 string2params(to_utf8(cmd.argument()), params);
95                 params_.kind = params.kind;
96                 break;
97         }
98
99         default:
100                 Inset::doDispatch(cur, cmd);
101                 break;
102         }
103 }
104
105
106 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
107         FuncStatus & status) const
108 {
109         switch (cmd.action()) {
110         // we handle these
111         case LFUN_INSET_MODIFY: {
112                 if (cmd.getArg(0) != "separator")
113                         break;
114                 InsetSeparatorParams params;
115                 string2params(to_utf8(cmd.argument()), params);
116                 status.setOnOff(params_.kind == params.kind);
117                 status.setEnabled(true);
118                 return true;
119         }
120         default:
121                 return Inset::getStatus(cur, cmd, status);
122         }
123         return false;
124 }
125
126
127 ColorCode InsetSeparator::ColorName() const
128 {
129         return Color_latex;
130 }
131
132
133 void InsetSeparator::latex(otexstream & os, OutputParams const &) const
134 {
135         // Do nothing if a paragraph break was just output
136         if (!os.afterParbreak()) {
137                 switch (params_.kind) {
138                         case InsetSeparatorParams::PLAIN:
139                                 os << breakln << "%\n";
140                                 break;
141                         case InsetSeparatorParams::PARBREAK:
142                                 os << breakln << "\n";
143                                 break;
144                         default:
145                                 os << breakln << "%\n";
146                                 break;
147                 }
148         }
149 }
150
151
152 int InsetSeparator::plaintext(odocstringstream & os,
153         OutputParams const &, size_t) const
154 {
155         os << '\n';
156         return PLAINTEXT_NEWLINE;
157 }
158
159
160 int InsetSeparator::docbook(odocstream & os, OutputParams const &) const
161 {
162         os << '\n';
163         return 0;
164 }
165
166
167 docstring InsetSeparator::xhtml(XHTMLStream & xs, OutputParams const &) const
168 {
169         xs << html::CR() << html::CompTag("br") << html::CR();
170         return docstring();
171 }
172
173
174 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
175 {
176         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
177         dim.asc = fm.maxAscent();
178         dim.des = fm.maxDescent();
179         dim.wid = fm.width('n');
180         if (params_.kind == InsetSeparatorParams::PLAIN)
181                 dim.wid *= 8;
182 }
183
184
185 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
186 {
187         FontInfo font;
188         font.setColor(ColorName());
189
190         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
191         int const wid = fm.width('n');
192         int const asc = fm.maxAscent();
193
194         int xp[7];
195         int yp[7];
196
197         if (params_.kind == InsetSeparatorParams::PLAIN) {
198                 yp[0] = int(y - 0.500 * asc * 0.75);
199                 yp[1] = yp[0];
200
201                 xp[0] = int(x);
202                 xp[1] = int(x + wid * 8);
203
204                 pi.pain.lines(xp, yp, 2, ColorName());
205         } else {
206                 yp[0] = int(y - 0.500 * asc * 0.5);
207                 yp[1] = int(y - 0.250 * asc * 0.5);
208                 yp[2] = int(y);
209
210                 if (pi.ltr_pos) {
211                         xp[0] = int(x + 1 + wid * 0.375);
212                         xp[1] = int(x + 1);
213                 } else {
214                         xp[0] = int(x - 1 + wid * 0.625);
215                         xp[1] = int(x - 1 + wid);
216                 }
217                 xp[2] = xp[0];
218
219                 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
220
221                 yp[0] = yp[1];
222                 yp[2] = int(y - 0.850 * asc * 0.5);
223                 yp[3] = int(y - 1.250 * asc * 0.5);
224                 yp[4] = yp[3];
225                 yp[5] = yp[2];
226                 yp[6] = yp[5];
227
228                 xp[0] = xp[1];
229                 if (pi.ltr_pos) {
230                         xp[1] = int(x + 1 + wid * 0.50);
231                         xp[2] = int(x + wid);
232                         xp[3] = xp[2];
233                         xp[4] = int(x + wid * 0.75);
234                 } else {
235                         xp[1] = int(x + wid * 0.50);
236                         xp[2] = int(x);
237                         xp[3] = xp[2];
238                         xp[4] = int(x + wid * 0.25);
239                 }
240                 xp[5] = xp[4];
241                 xp[6] = xp[2];
242
243                 int c1x[7];
244                 int c1y[7];
245                 int c2x[7];
246                 int c2y[7];
247
248                 for (int i = 1; i < 7; ++i) {
249                         c1x[i] = xp[i - 1];
250                         c1y[i] = yp[i - 1];
251                         c2x[i] = xp[i];
252                         c2y[i] = yp[i];
253                 }
254
255                 int d = pi.ltr_pos ? yp[4] - yp[5] : yp[5] - yp[4];
256
257                 c1x[2] = xp[2];
258                 c2y[2] = int(y - 0.500 * asc * 0.5);
259                 c1x[5] += d;
260                 c2x[5] += d;
261
262                 pi.pain.path(xp, yp, c1x, c1y, c2x, c2y, 7, ColorName());
263         }
264 }
265
266
267 string InsetSeparator::contextMenuName() const
268 {
269         return "context-separator";
270 }
271
272
273 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
274 {
275         params = InsetSeparatorParams();
276         if (in.empty())
277                 return;
278         istringstream data(in);
279         Lexer lex;
280         lex.setStream(data);
281         lex.setContext("InsetSeparator::string2params");
282         lex >> "separator";
283         params.read(lex);
284 }
285
286
287 string InsetSeparator::params2string(InsetSeparatorParams const & params)
288 {
289         ostringstream data;
290         data << "separator" << ' ';
291         params.write(data);
292         return data.str();
293 }
294
295
296 } // namespace lyx