]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSpace.cpp
53fda518dad65fd28bfe89f998ea21e1a0b5904f
[lyx.git] / src / insets / InsetSpace.cpp
1 /**
2  * \file InsetSpace.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  * \author Jean-Marc Lasgouttes
8  * \author Lars Gullik Bjønnes
9  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "InsetSpace.h"
17
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "Dimension.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Language.h"
24 #include "LaTeXFeatures.h"
25 #include "Lexer.h"
26 #include "MetricsInfo.h"
27 #include "texstream.h"
28 #include "xml.h"
29
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lassert.h"
34 #include "support/Length.h"
35 #include "support/lstrings.h"
36 #include "support/qstring_helpers.h"
37
38 #include "frontends/Application.h"
39 #include "frontends/FontMetrics.h"
40 #include "frontends/Painter.h"
41
42 using namespace std;
43
44 namespace lyx {
45
46
47 InsetSpace::InsetSpace(InsetSpaceParams const & params)
48         : Inset(nullptr), params_(params)
49 {}
50
51
52 InsetSpaceParams::Kind InsetSpace::kind() const
53 {
54         return params_.kind;
55 }
56
57
58 GlueLength InsetSpace::length() const
59 {
60         return params_.length;
61 }
62
63
64 docstring InsetSpace::toolTip(BufferView const &, int, int) const
65 {
66         docstring message;
67         switch (params_.kind) {
68         case InsetSpaceParams::NORMAL:
69                 message = _("Normal Space");
70                 break;
71         case InsetSpaceParams::PROTECTED:
72                 message = _("Non-Breaking Normal Space");
73                 break;
74         case InsetSpaceParams::VISIBLE:
75                 message = _("Non-Breaking Visible Normal Space");
76                 break;
77         case InsetSpaceParams::THIN:
78                 message = _("Non-Breaking Thin Space (1/6 em)");
79                 break;
80         case InsetSpaceParams::MEDIUM:
81                 message = _("Non-Breaking Medium Space (2/9 em)");
82                 break;
83         case InsetSpaceParams::THICK:
84                 message = _("Non-Breaking Thick Space (5/18 em)");
85                 break;
86         case InsetSpaceParams::QUAD:
87                 message = _("Quad Space (1 em)");
88                 break;
89         case InsetSpaceParams::QQUAD:
90                 message = _("Double Quad Space (2 em)");
91                 break;
92         case InsetSpaceParams::ENSPACE:
93                 message = _("Non-Breaking Half Quad Space (1/2 em)");
94                 break;
95         case InsetSpaceParams::ENSKIP:
96                 message = _("Half Quad Space (1/2 em)");
97                 break;
98         case InsetSpaceParams::NEGTHIN:
99                 message = _("Non-Breaking Negative Thin Space (-1/6 em)");
100                 break;
101         case InsetSpaceParams::NEGMEDIUM:
102                 message = _("Non-Breaking Negative Medium Space (-2/9 em)");
103                 break;
104         case InsetSpaceParams::NEGTHICK:
105                 message = _("Non-Breaking Negative Thick Space (-5/18 em)");
106                 break;
107         case InsetSpaceParams::HFILL:
108                 message = _("Horizontal Fill");
109                 break;
110         case InsetSpaceParams::HFILL_PROTECTED:
111                 message = _("Non-Breaking Horizontal Fill");
112                 break;
113         case InsetSpaceParams::DOTFILL:
114                 message = _("Non-Breaking Horizontal Fill (Dots)");
115                 break;
116         case InsetSpaceParams::HRULEFILL:
117                 message = _("Non-Breaking Horizontal Fill (Rule)");
118                 break;
119         case InsetSpaceParams::LEFTARROWFILL:
120                 message = _("Non-Breaking Horizontal Fill (Left Arrow)");
121                 break;
122         case InsetSpaceParams::RIGHTARROWFILL:
123                 message = _("Non-Breaking Horizontal Fill (Right Arrow)");
124                 break;
125         case InsetSpaceParams::UPBRACEFILL:
126                 message = _("Non-Breaking Horizontal Fill (Up Brace)");
127                 break;
128         case InsetSpaceParams::DOWNBRACEFILL:
129                 message = _("Non-Breaking Horizontal Fill (Down Brace)");
130                 break;
131         case InsetSpaceParams::CUSTOM:
132                 // FIXME unicode
133                 message = support::bformat(_("Horizontal Space (%1$s)"),
134                                            locLengthDocString(from_ascii(params_.length.asString())));
135                 break;
136         case InsetSpaceParams::CUSTOM_PROTECTED:
137                 // FIXME unicode
138                 message = support::bformat(_("Non-Breaking Horizontal Space (%1$s)"),
139                                            locLengthDocString(from_ascii(params_.length.asString())));
140                 break;
141         }
142         return message;
143 }
144
145
146 void InsetSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
147 {
148         switch (cmd.action()) {
149
150         case LFUN_INSET_MODIFY: {
151                 cur.recordUndo();
152                 string arg = to_utf8(cmd.argument());
153                 if (arg == "space \\hspace{}")
154                         arg += params_.length.len().empty()
155                                 ? " \\length 1" + string(stringFromUnit(Length::defaultUnit()))
156                                 : " \\length " + params_.length.asString();
157                 string2params(arg, params_);
158                 break;
159         }
160
161         case LFUN_INSET_DIALOG_UPDATE:
162                 cur.bv().updateDialog("space", params2string(params()));
163                 break;
164
165         default:
166                 Inset::doDispatch(cur, cmd);
167                 break;
168         }
169 }
170
171
172 bool InsetSpace::getStatus(Cursor & cur, FuncRequest const & cmd,
173         FuncStatus & status) const
174 {
175         switch (cmd.action()) {
176         // we handle these
177         case LFUN_INSET_MODIFY:
178                 if (cmd.getArg(0) == "space") {
179                         InsetSpaceParams params;
180                         string2params(to_utf8(cmd.argument()), params);
181                         status.setOnOff(params_.kind == params.kind);
182                         status.setEnabled(true);
183                 } else
184                         status.setEnabled(false);
185                 return true;
186
187         case LFUN_INSET_DIALOG_UPDATE:
188                 status.setEnabled(true);
189                 return true;
190         default:
191                 return Inset::getStatus(cur, cmd, status);
192         }
193 }
194
195
196 int InsetSpace::rowFlags() const
197 {
198         switch (params_.kind) {
199                 case InsetSpaceParams::PROTECTED:
200                 case InsetSpaceParams::CUSTOM_PROTECTED:
201                 case InsetSpaceParams::HFILL_PROTECTED:
202                 case InsetSpaceParams::THIN:
203                 case InsetSpaceParams::NEGTHIN:
204                 case InsetSpaceParams::MEDIUM:
205                 case InsetSpaceParams::NEGMEDIUM:
206                 case InsetSpaceParams::THICK:
207                 case InsetSpaceParams::NEGTHICK:
208                 case InsetSpaceParams::ENSPACE:
209                 case InsetSpaceParams::VISIBLE:
210                         // no break after these
211                         return Inline;
212                 case InsetSpaceParams::NORMAL:
213                 case InsetSpaceParams::QUAD:
214                 case InsetSpaceParams::QQUAD:
215                 case InsetSpaceParams::ENSKIP:
216                 case InsetSpaceParams::CUSTOM:
217                 case InsetSpaceParams::HFILL:
218                 case InsetSpaceParams::DOTFILL:
219                 case InsetSpaceParams::HRULEFILL:
220                 case InsetSpaceParams::LEFTARROWFILL:
221                 case InsetSpaceParams::RIGHTARROWFILL:
222                 case InsetSpaceParams::UPBRACEFILL:
223                 case InsetSpaceParams::DOWNBRACEFILL:
224                         // these allow line breaking
225                         break;
226         }
227         return CanBreakAfter;
228 }
229
230
231 namespace {
232 int const arrow_size = 8;
233 }
234
235
236 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
237 {
238         if (isHfill()) {
239                 // The width for hfills is calculated externally in
240                 // TextMetrics::setRowAlignment. The value of 5 is the
241                 // minimal value when the hfill is not active.
242                 dim = Dimension(5, 10, 10);
243                 return;
244         }
245
246         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
247         dim.asc = fm.maxAscent();
248         dim.des = fm.maxDescent();
249         int const em = fm.em();
250
251         switch (params_.kind) {
252                 case InsetSpaceParams::THIN:
253                 case InsetSpaceParams::NEGTHIN:
254                         dim.wid = em / 6;
255                         break;
256                 case InsetSpaceParams::MEDIUM:
257                 case InsetSpaceParams::NEGMEDIUM:
258                         dim.wid = em / 4;
259                         break;
260                 case InsetSpaceParams::THICK:
261                 case InsetSpaceParams::NEGTHICK:
262                         dim.wid = em / 2;
263                         break;
264                 case InsetSpaceParams::PROTECTED:
265                 case InsetSpaceParams::VISIBLE:
266                 case InsetSpaceParams::NORMAL:
267                         dim.wid = fm.width(char_type(' '));
268                         break;
269                 case InsetSpaceParams::QUAD:
270                         dim.wid = em;
271                         break;
272                 case InsetSpaceParams::QQUAD:
273                         dim.wid = 2 * em;
274                         break;
275                 case InsetSpaceParams::ENSPACE:
276                 case InsetSpaceParams::ENSKIP:
277                         dim.wid = int(0.5 * em);
278                         break;
279                 case InsetSpaceParams::CUSTOM:
280                 case InsetSpaceParams::CUSTOM_PROTECTED: {
281                         int const w = mi.base.inPixels(params_.length.len());
282                         int const minw = (w < 0) ? 3 * arrow_size : 4;
283                         dim.wid = max(minw, abs(w));
284                         break;
285                 }
286                 case InsetSpaceParams::HFILL:
287                 case InsetSpaceParams::HFILL_PROTECTED:
288                 case InsetSpaceParams::DOTFILL:
289                 case InsetSpaceParams::HRULEFILL:
290                 case InsetSpaceParams::LEFTARROWFILL:
291                 case InsetSpaceParams::RIGHTARROWFILL:
292                 case InsetSpaceParams::UPBRACEFILL:
293                 case InsetSpaceParams::DOWNBRACEFILL:
294                         // shut up compiler
295                         break;
296         }
297 }
298
299
300 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
301 {
302         Dimension const dim = dimension(*pi.base.bv);
303
304         if (isHfill() || params_.length.len().value() < 0) {
305                 int const asc = theFontMetrics(pi.base.font).ascent('M');
306                 int const desc = theFontMetrics(pi.base.font).descent('M');
307                 // Pixel height divisible by 2 for prettier fill graphics:
308                 int const oddheight = (asc ^ desc) & 1;
309                 int const x0 = x + 1;
310                 int const x1 = x + dim.wid - 2;
311                 int const y0 = y + desc - 1;
312                 int const y1 = y - asc + oddheight - 1;
313                 int const y2 = (y0 + y1) / 2;
314                 int xoffset = (y0 - y1) / 2;
315
316                 // Two tests for very narrow insets
317                 if (xoffset > x1 - x0
318                      && (params_.kind == InsetSpaceParams::LEFTARROWFILL
319                          || params_.kind == InsetSpaceParams::RIGHTARROWFILL))
320                                 xoffset = x1 - x0;
321                 if (xoffset * 6 > (x1 - x0)
322                      && (params_.kind == InsetSpaceParams::UPBRACEFILL
323                          || params_.kind == InsetSpaceParams::DOWNBRACEFILL))
324                                 xoffset = (x1 - x0) / 6;
325
326                 int const x2 = x0 + xoffset;
327                 int const x3 = x1 - xoffset;
328                 int const xm = (x0 + x1) / 2;
329                 int const xml = xm - xoffset;
330                 int const xmr = xm + xoffset;
331
332                 if (params_.kind == InsetSpaceParams::HFILL) {
333                         pi.pain.line(x0, y1, x0, y0, Color_added_space);
334                         pi.pain.line(x0, y2, x1, y2, Color_added_space,
335                                 frontend::Painter::line_onoffdash);
336                         pi.pain.line(x1, y1, x1, y0, Color_added_space);
337                 } else if (params_.kind == InsetSpaceParams::HFILL_PROTECTED) {
338                         pi.pain.line(x0, y1, x0, y0, Color_latex);
339                         pi.pain.line(x0, y2, x1, y2, Color_latex,
340                                 frontend::Painter::line_onoffdash);
341                         pi.pain.line(x1, y1, x1, y0, Color_latex);
342                 } else if (params_.kind == InsetSpaceParams::DOTFILL) {
343                         pi.pain.line(x0, y1, x0, y0, Color_special);
344                         pi.pain.line(x0, y0, x1, y0, Color_special,
345                                 frontend::Painter::line_onoffdash);
346                         pi.pain.line(x1, y1, x1, y0, Color_special);
347                 } else if (params_.kind == InsetSpaceParams::HRULEFILL) {
348                         pi.pain.line(x0, y1, x0, y0, Color_special);
349                         pi.pain.line(x0, y0, x1, y0, Color_special);
350                         pi.pain.line(x1, y1, x1, y0, Color_special);
351                 } else if (params_.kind == InsetSpaceParams::LEFTARROWFILL) {
352                         pi.pain.line(x2, y1 + 1 , x0 + 1, y2, Color_special);
353                         pi.pain.line(x0 + 1, y2 + 1 , x2, y0, Color_special);
354                         pi.pain.line(x0, y2 , x1, y2, Color_special);
355                 } else if (params_.kind == InsetSpaceParams::RIGHTARROWFILL) {
356                         pi.pain.line(x3 + 1, y1 + 1 , x1, y2, Color_special);
357                         pi.pain.line(x1, y2 + 1 , x3 + 1, y0, Color_special);
358                         pi.pain.line(x0, y2 , x1, y2, Color_special);
359                 } else if (params_.kind == InsetSpaceParams::UPBRACEFILL) {
360                         pi.pain.line(x0 + 1, y1 + 1 , x2, y2, Color_special);
361                         pi.pain.line(x2, y2 , xml, y2, Color_special);
362                         pi.pain.line(xml + 1, y2 + 1 , xm, y0, Color_special);
363                         pi.pain.line(xm + 1, y0 , xmr, y2 + 1, Color_special);
364                         pi.pain.line(xmr, y2 , x3, y2, Color_special);
365                         pi.pain.line(x3 + 1, y2 , x1, y1 + 1, Color_special);
366                 } else if (params_.kind == InsetSpaceParams::DOWNBRACEFILL) {
367                         pi.pain.line(x0 + 1, y0 , x2, y2 + 1, Color_special);
368                         pi.pain.line(x2, y2 , xml, y2, Color_special);
369                         pi.pain.line(xml + 1, y2 , xm, y1 + 1, Color_special);
370                         pi.pain.line(xm + 1, y1 + 1 , xmr, y2, Color_special);
371                         pi.pain.line(xmr, y2 , x3, y2, Color_special);
372                         pi.pain.line(x3 + 1, y2 + 1 , x1, y0, Color_special);
373                 } else if (params_.kind == InsetSpaceParams::CUSTOM) {
374                         pi.pain.line(x0, y1 + 1 , x2 + 1, y2, Color_special);
375                         pi.pain.line(x2 + 1, y2 + 1 , x0, y0, Color_special);
376                         pi.pain.line(x1 + 1, y1 + 1 , x3, y2, Color_special);
377                         pi.pain.line(x3, y2 + 1 , x1 + 1, y0, Color_special);
378                         pi.pain.line(x2, y2 , x3, y2, Color_special);
379                 } else if (params_.kind == InsetSpaceParams::CUSTOM_PROTECTED) {
380                         pi.pain.line(x0, y1 + 1 , x2 + 1, y2, Color_latex);
381                         pi.pain.line(x2 + 1, y2 + 1 , x0, y0, Color_latex);
382                         pi.pain.line(x1 + 1, y1 + 1 , x3, y2, Color_latex);
383                         pi.pain.line(x3, y2 + 1 , x1 + 1, y0, Color_latex);
384                         pi.pain.line(x2, y2 , x3, y2, Color_latex);
385                 }
386                 return;
387         }
388
389         int const w = dim.wid;
390         int const h = theFontMetrics(pi.base.font).xHeight();
391         int xp[4], yp[4];
392
393         xp[0] = x;
394         yp[0] = y - max(h / 4, 1);
395         if (params_.kind == InsetSpaceParams::NORMAL ||
396             params_.kind == InsetSpaceParams::PROTECTED ||
397             params_.kind == InsetSpaceParams::VISIBLE) {
398                 xp[1] = x;         yp[1] = y;
399                 xp[2] = x + w - 1; yp[2] = y;
400         } else {
401                 xp[1] = x;         yp[1] = y + max(h / 4, 1);
402                 xp[2] = x + w - 1; yp[2] = y + max(h / 4, 1);
403         }
404         xp[3] = x + w - 1;
405         yp[3] = y - max(h / 4, 1);
406
407         Color col = Color_special;
408         if (params_.kind == InsetSpaceParams::PROTECTED ||
409             params_.kind == InsetSpaceParams::ENSPACE ||
410             params_.kind == InsetSpaceParams::THIN ||
411             params_.kind == InsetSpaceParams::NEGTHIN ||
412             params_.kind == InsetSpaceParams::MEDIUM ||
413             params_.kind == InsetSpaceParams::NEGMEDIUM ||
414             params_.kind == InsetSpaceParams::THICK ||
415             params_.kind == InsetSpaceParams::NEGTHICK ||
416             params_.kind == InsetSpaceParams::CUSTOM_PROTECTED)
417                 col = Color_latex;
418         else if (params_.kind == InsetSpaceParams::VISIBLE)
419                 col =  Color_foreground;
420
421         pi.pain.lines(xp, yp, 4, col);
422 }
423
424
425 void InsetSpaceParams::write(ostream & os) const
426 {
427         switch (kind) {
428         case InsetSpaceParams::NORMAL:
429                 os << "\\space{}";
430                 break;
431         case InsetSpaceParams::PROTECTED:
432                 os <<  "~";
433                 break;
434         case InsetSpaceParams::VISIBLE:
435                 os <<  "\\textvisiblespace{}";
436                 break;
437         case InsetSpaceParams::THIN:
438                 os <<  "\\thinspace{}";
439                 break;
440         case InsetSpaceParams::MEDIUM:
441                 os <<  "\\medspace{}";
442                 break;
443         case InsetSpaceParams::THICK:
444                 os <<  "\\thickspace{}";
445                 break;
446         case InsetSpaceParams::QUAD:
447                 os <<  "\\quad{}";
448                 break;
449         case InsetSpaceParams::QQUAD:
450                 os <<  "\\qquad{}";
451                 break;
452         case InsetSpaceParams::ENSPACE:
453                 os <<  "\\enspace{}";
454                 break;
455         case InsetSpaceParams::ENSKIP:
456                 os <<  "\\enskip{}";
457                 break;
458         case InsetSpaceParams::NEGTHIN:
459                 os <<  "\\negthinspace{}";
460                 break;
461         case InsetSpaceParams::NEGMEDIUM:
462                 os <<  "\\negmedspace{}";
463                 break;
464         case InsetSpaceParams::NEGTHICK:
465                 os <<  "\\negthickspace{}";
466                 break;
467         case InsetSpaceParams::HFILL:
468                 os <<  "\\hfill{}";
469                 break;
470         case InsetSpaceParams::HFILL_PROTECTED:
471                 os <<  "\\hspace*{\\fill}";
472                 break;
473         case InsetSpaceParams::DOTFILL:
474                 os <<  "\\dotfill{}";
475                 break;
476         case InsetSpaceParams::HRULEFILL:
477                 os <<  "\\hrulefill{}";
478                 break;
479         case InsetSpaceParams::LEFTARROWFILL:
480                 os <<  "\\leftarrowfill{}";
481                 break;
482         case InsetSpaceParams::RIGHTARROWFILL:
483                 os <<  "\\rightarrowfill{}";
484                 break;
485         case InsetSpaceParams::UPBRACEFILL:
486                 os <<  "\\upbracefill{}";
487                 break;
488         case InsetSpaceParams::DOWNBRACEFILL:
489                 os <<  "\\downbracefill{}";
490                 break;
491         case InsetSpaceParams::CUSTOM:
492                 os <<  "\\hspace{}";
493                 break;
494         case InsetSpaceParams::CUSTOM_PROTECTED:
495                 os <<  "\\hspace*{}";
496                 break;
497         }
498
499         if (!length.len().empty())
500                 os << "\n\\length " << length.asString();
501 }
502
503
504 void InsetSpaceParams::read(Lexer & lex)
505 {
506         lex.setContext("InsetSpaceParams::read");
507         string command;
508         lex >> command;
509
510         // The tests for math might be disabled after a file format change
511         if (command == "\\space{}")
512                 kind = InsetSpaceParams::NORMAL;
513         else if (command == "~")
514                 kind = InsetSpaceParams::PROTECTED;
515         else if (command == "\\textvisiblespace{}")
516                 kind = InsetSpaceParams::VISIBLE;
517         else if (command == "\\thinspace{}")
518                 kind = InsetSpaceParams::THIN;
519         else if (command == "\\medspace{}")
520                 kind = InsetSpaceParams::MEDIUM;
521         else if (command == "\\thickspace{}")
522                 kind = InsetSpaceParams::THICK;
523         else if (command == "\\quad{}")
524                 kind = InsetSpaceParams::QUAD;
525         else if (command == "\\qquad{}")
526                 kind = InsetSpaceParams::QQUAD;
527         else if (command == "\\enspace{}")
528                 kind = InsetSpaceParams::ENSPACE;
529         else if (command == "\\enskip{}")
530                 kind = InsetSpaceParams::ENSKIP;
531         else if (command == "\\negthinspace{}")
532                 kind = InsetSpaceParams::NEGTHIN;
533         else if (command == "\\negmedspace{}")
534                 kind = InsetSpaceParams::NEGMEDIUM;
535         else if (command == "\\negthickspace{}")
536                 kind = InsetSpaceParams::NEGTHICK;
537         else if (command == "\\hfill{}")
538                 kind = InsetSpaceParams::HFILL;
539         else if (command == "\\hspace*{\\fill}")
540                 kind = InsetSpaceParams::HFILL_PROTECTED;
541         else if (command == "\\dotfill{}")
542                 kind = InsetSpaceParams::DOTFILL;
543         else if (command == "\\hrulefill{}")
544                 kind = InsetSpaceParams::HRULEFILL;
545         else if (command == "\\hspace{}")
546                 kind = InsetSpaceParams::CUSTOM;
547         else if (command == "\\leftarrowfill{}")
548                 kind = InsetSpaceParams::LEFTARROWFILL;
549         else if (command == "\\rightarrowfill{}")
550                 kind = InsetSpaceParams::RIGHTARROWFILL;
551         else if (command == "\\upbracefill{}")
552                 kind = InsetSpaceParams::UPBRACEFILL;
553         else if (command == "\\downbracefill{}")
554                 kind = InsetSpaceParams::DOWNBRACEFILL;
555         else if (command == "\\hspace*{}")
556                 kind = InsetSpaceParams::CUSTOM_PROTECTED;
557         else
558                 lex.printError("InsetSpace: Unknown kind: `$$Token'");
559
560         if (lex.checkFor("\\length"))
561                 lex >> length;
562 }
563
564
565 void InsetSpace::write(ostream & os) const
566 {
567         os << "space ";
568         params_.write(os);
569 }
570
571
572 void InsetSpace::read(Lexer & lex)
573 {
574         params_.read(lex);
575         lex >> "\\end_inset";
576 }
577
578
579 void InsetSpace::latex(otexstream & os, OutputParams const & runparams) const
580 {
581         switch (params_.kind) {
582         case InsetSpaceParams::NORMAL:
583                 if (runparams.find_effective())
584                         os << "~";
585                 else
586                         os << (runparams.free_spacing ? " " : "\\ ");
587                 break;
588         case InsetSpaceParams::PROTECTED:
589                 if (runparams.find_effective())
590                         os.put(0xa0);
591                 else if (getLocalOrDefaultLang(runparams)->lang() == "polutonikogreek")
592                         // in babel's polutonikogreek, ~ is active
593                         os << (runparams.free_spacing ? " " : "\\nobreakspace{}");
594                 else
595                         os << (runparams.free_spacing ? ' ' : '~');
596                 break;
597         case InsetSpaceParams::VISIBLE:
598                 if (runparams.find_effective())
599                         os.put(0x2423);
600                 else
601                         os << (runparams.free_spacing ? " " : "\\textvisiblespace{}");
602                 break;
603         case InsetSpaceParams::THIN:
604                 if (runparams.find_effective())
605                         os.put(0x202f);
606                 else
607                         os << (runparams.free_spacing ? " " : "\\,");
608                 break;
609         case InsetSpaceParams::MEDIUM:
610                 if (runparams.find_effective())
611                         os.put(0x2005);
612                 else if (params_.math)
613                         os << (runparams.free_spacing ? " " : "\\:");
614                 else
615                         os << (runparams.free_spacing ? " " : "\\medspace{}");
616                 break;
617         case InsetSpaceParams::THICK:
618                 if (runparams.find_effective())
619                         os.put(0x2004);
620                 else if (params_.math)
621                         os << (runparams.free_spacing ? " " : "\\;");
622                 else
623                         os << (runparams.free_spacing ? " " : "\\thickspace{}");
624                 break;
625         case InsetSpaceParams::QUAD:
626                 if (runparams.find_effective())
627                         os.put(0x2003);
628                 else
629                         os << (runparams.free_spacing ? " " : "\\quad{}");
630                 break;
631         case InsetSpaceParams::QQUAD:
632                 if (runparams.find_effective()) {
633                         os.put(0x2003);
634                         os.put(0x2003);
635                 }
636                 else
637                         os << (runparams.free_spacing ? " " : "\\qquad{}");
638                 break;
639         case InsetSpaceParams::ENSPACE:
640                 if (runparams.find_effective())
641                         os.put(0x2002);
642                 else
643                         os << (runparams.free_spacing ? " " : "\\enspace{}");
644                 break;
645         case InsetSpaceParams::ENSKIP:
646                 if (runparams.find_effective())
647                         os.put(0x2002);
648                 else
649                         os << (runparams.free_spacing ? " " : "\\enskip{}");
650                 break;
651         case InsetSpaceParams::NEGTHIN:
652                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\negthinspace{}");
653                 break;
654         case InsetSpaceParams::NEGMEDIUM:
655                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\negmedspace{}");
656                 break;
657         case InsetSpaceParams::NEGTHICK:
658                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\negthickspace{}");
659                 break;
660         case InsetSpaceParams::HFILL:
661                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\hfill{}");
662                 break;
663         case InsetSpaceParams::HFILL_PROTECTED:
664                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\hspace*{\\fill}");
665                 break;
666         case InsetSpaceParams::DOTFILL:
667                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\dotfill{}");
668                 break;
669         case InsetSpaceParams::HRULEFILL:
670                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\hrulefill{}");
671                 break;
672         case InsetSpaceParams::LEFTARROWFILL:
673                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\leftarrowfill{}");
674                 break;
675         case InsetSpaceParams::RIGHTARROWFILL:
676                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\rightarrowfill{}");
677                 break;
678         case InsetSpaceParams::UPBRACEFILL:
679                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\upbracefill{}");
680                 break;
681         case InsetSpaceParams::DOWNBRACEFILL:
682                 os << (runparams.free_spacing && runparams.find_effective() ? " " : "\\downbracefill{}");
683                 break;
684         case InsetSpaceParams::CUSTOM:
685                 if (runparams.find_effective())
686                         os.put(0x00a0);
687                 else if (runparams.free_spacing)
688                         os << " ";
689                 else
690                         os << "\\hspace{" << from_ascii(params_.length.asLatexString()) << "}";
691                 break;
692         case InsetSpaceParams::CUSTOM_PROTECTED:
693                 if (runparams.find_effective())
694                         os.put(0x00a0);
695                 else if (runparams.free_spacing)
696                         os << " ";
697                 else
698                         os << "\\hspace*{" << from_ascii(params_.length.asLatexString()) << "}";
699                 break;
700         }
701 }
702
703
704 int InsetSpace::plaintext(odocstringstream & os,
705         OutputParams const & rp, size_t) const
706 {
707         switch (params_.kind) {
708         case InsetSpaceParams::HFILL:
709         case InsetSpaceParams::HFILL_PROTECTED:
710                 os << "     ";
711                 return 5;
712         case InsetSpaceParams::DOTFILL:
713                 os << ".....";
714                 return 5;
715         case InsetSpaceParams::HRULEFILL:
716                 os << "_____";
717                 return 5;
718         case InsetSpaceParams::LEFTARROWFILL:
719                 os << "<----";
720                 return 5;
721         case InsetSpaceParams::RIGHTARROWFILL:
722                 os << "---->";
723                 return 5;
724         case InsetSpaceParams::UPBRACEFILL:
725                 os << "\\-v-/";
726                 return 5;
727         case InsetSpaceParams::DOWNBRACEFILL:
728                 os << "/-^-\\";
729                 return 5;
730         case InsetSpaceParams::VISIBLE:
731                 os.put(0x2423);
732                 return 1;
733         case InsetSpaceParams::ENSKIP:
734                 os.put(0x2002);
735                 return 1;
736         case InsetSpaceParams::ENSPACE:
737                 os.put(0x2060); // WORD JOINER, makes the breakable en space unbreakable
738                 os.put(0x2002);
739                 os.put(0x2060); // WORD JOINER, makes the breakable en space unbreakable
740                 return 3;
741         case InsetSpaceParams::QUAD:
742                 os.put(0x2003);
743                 return 1;
744         case InsetSpaceParams::QQUAD:
745                 os.put(0x2003);
746                 os.put(0x2003);
747                 return 2;
748         case InsetSpaceParams::THIN:
749                 if (rp.find_effective())
750                         // simple search
751                         os << ' ';
752                 else
753                         os.put(0x202f);
754                 return 1;
755         case InsetSpaceParams::MEDIUM:
756                 os.put(0x200b); // ZERO WIDTH SPACE, makes the unbreakable medium space breakable
757                 os.put(0x2005);
758                 os.put(0x200b); // ZERO WIDTH SPACE, makes the unbreakable medium space breakable
759                 return 1;
760         case InsetSpaceParams::THICK:
761                 os.put(0x200b); // ZERO WIDTH SPACE, makes the unbreakable thick space breakable
762                 os.put(0x2004);
763                 os.put(0x200b); // ZERO WIDTH SPACE, makes the unbreakable thick space breakable
764                 return 1;
765         case InsetSpaceParams::PROTECTED:
766         case InsetSpaceParams::CUSTOM_PROTECTED:
767                 if (rp.find_effective())
768                         // simple search
769                         os << ' ';
770                 else
771                         os.put(0x00a0);
772                 return 1;
773         case InsetSpaceParams::NEGTHIN:
774         case InsetSpaceParams::NEGMEDIUM:
775         case InsetSpaceParams::NEGTHICK:
776                 return 0;
777         default:
778                 os << ' ';
779                 return 1;
780         }
781 }
782
783
784 namespace {
785 std::string spaceToXMLEntity(InsetSpaceParams::Kind kind) {
786         switch (kind) {
787         case InsetSpaceParams::NORMAL:
788                 return " ";
789         case InsetSpaceParams::QUAD:
790                 return "&#x2003;"; // HTML: &emsp;
791         case InsetSpaceParams::QQUAD:
792                 return "&#x2003;&#x2003;"; // HTML: &emsp;&emsp;
793         case InsetSpaceParams::ENSKIP:
794                 return "&#x2002;"; // HTML: &ensp;
795         case InsetSpaceParams::VISIBLE:
796                 return "&#x2423;";
797         case InsetSpaceParams::ENSPACE: // HTML: &#x2060;&ensp;&#x2060; (word joiners)
798                 return "&#x2060;&#x2002;&#x2060;";
799         case InsetSpaceParams::THIN:
800                 return "&#x202F;"; // HTML: &thinspace;
801         case InsetSpaceParams::MEDIUM:
802                 return "&#x2005;"; // HTML: &emsp14;
803         case InsetSpaceParams::THICK:
804                 return "&#x2004;"; // HTML: &emsp13;
805         case InsetSpaceParams::PROTECTED:
806         case InsetSpaceParams::NEGTHIN:
807         case InsetSpaceParams::NEGMEDIUM:
808         case InsetSpaceParams::NEGTHICK:
809                 return "&#xA0;"; // HTML: &nbsp;
810         case InsetSpaceParams::CUSTOM_PROTECTED:
811                 // FIXME XHTML/DocBook
812                 // Probably we could do some sort of blank span?
813                 return "&#160;";
814         case InsetSpaceParams::HFILL:
815         case InsetSpaceParams::HFILL_PROTECTED:
816         case InsetSpaceParams::DOTFILL:
817         case InsetSpaceParams::HRULEFILL:
818         case InsetSpaceParams::LEFTARROWFILL:
819         case InsetSpaceParams::RIGHTARROWFILL:
820         case InsetSpaceParams::UPBRACEFILL:
821         case InsetSpaceParams::DOWNBRACEFILL:
822         case InsetSpaceParams::CUSTOM:
823                 // FIXME XHTML/DocBook
824                 // Can we do anything with those?
825                 return "\n";
826         }
827         return "";
828 }
829 }
830
831
832 void InsetSpace::docbook(XMLStream & xs, OutputParams const &) const
833 {
834         xs << XMLStream::ESCAPE_NONE << from_ascii(spaceToXMLEntity(params_.kind));
835 }
836
837
838 docstring InsetSpace::xhtml(XMLStream & xs, OutputParams const &) const
839 {
840         xs << XMLStream::ESCAPE_NONE << from_ascii(spaceToXMLEntity(params_.kind));
841         return docstring();
842 }
843
844
845 void InsetSpace::validate(LaTeXFeatures & features) const
846 {
847         if (features.isAvailableAtLeastFrom("LaTeX", 2020, 10))
848                 // As of this version, the LaTeX kernel
849                 // includes all spaces.
850                 return;
851
852         // In earlier versions, we require amsmath
853         // for some text and math spaces
854         if ((params_.kind == InsetSpaceParams::NEGMEDIUM
855              || params_.kind == InsetSpaceParams::NEGTHICK)
856             || (!params_.math
857                 && (params_.kind == InsetSpaceParams::MEDIUM
858                     || params_.kind == InsetSpaceParams::THICK)))
859                 features.require("amsmath");
860 }
861
862
863 void InsetSpace::toString(odocstream & os) const
864 {
865         odocstringstream ods;
866         plaintext(ods, OutputParams(0));
867         os << ods.str();
868 }
869
870
871 void InsetSpace::forOutliner(docstring & os, size_t const, bool const) const
872 {
873         // There's no need to be cute here.
874         os += " ";
875 }
876
877
878 bool InsetSpace::isHfill() const
879 {
880         return params_.kind == InsetSpaceParams::HFILL
881                 || params_.kind == InsetSpaceParams::HFILL_PROTECTED
882                 || params_.kind == InsetSpaceParams::DOTFILL
883                 || params_.kind == InsetSpaceParams::HRULEFILL
884                 || params_.kind == InsetSpaceParams::LEFTARROWFILL
885                 || params_.kind == InsetSpaceParams::RIGHTARROWFILL
886                 || params_.kind == InsetSpaceParams::UPBRACEFILL
887                 || params_.kind == InsetSpaceParams::DOWNBRACEFILL;
888 }
889
890
891 string InsetSpace::contextMenuName() const
892 {
893         return "context-space";
894 }
895
896
897 void InsetSpace::string2params(string const & in, InsetSpaceParams & params)
898 {
899         params = InsetSpaceParams();
900         if (in.empty())
901                 return;
902
903         istringstream data(in);
904         Lexer lex;
905         lex.setStream(data);
906         lex.setContext("InsetSpace::string2params");
907         lex.next();
908         string const name = lex.getString();
909         if (name == "mathspace")
910                 params.math = true;
911         else {
912                 params.math = false;
913                 // we can try to read this even if the name is wrong
914                 LATTEST(name == "space");
915         }
916
917         // There are cases, such as when we are called via getStatus() from
918         // Dialog::canApply(), where we are just called with "space" rather
919         // than a full "space \type{}\n\\end_inset".
920         if (lex.isOK())
921                 params.read(lex);
922 }
923
924
925 string InsetSpace::params2string(InsetSpaceParams const & params)
926 {
927         ostringstream data;
928         if (params.math)
929                 data << "math";
930         data << "space" << ' ';
931         params.write(data);
932         return data.str();
933 }
934
935
936 } // namespace lyx