1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1997 Asger Alstrup
8 * ====================================================== */
13 #pragma implementation
16 #include "insetspecialchar.h"
18 #include "LaTeXFeatures.h"
19 #include "BufferView.h"
28 InsetSpecialChar::InsetSpecialChar(Kind k)
33 InsetSpecialChar::Kind InsetSpecialChar::kind() const
38 int InsetSpecialChar::ascent(BufferView *, LyXFont const & font) const
40 return lyxfont::maxAscent(font);
44 int InsetSpecialChar::descent(BufferView *, LyXFont const & font) const
46 return lyxfont::maxDescent(font);
50 int InsetSpecialChar::width(BufferView *, LyXFont const & font) const
55 int w = lyxfont::width('-', font);
57 w -= 2; // to make it look shorter
62 return lyxfont::width('|', font);
66 return lyxfont::width('.', font);
70 return lyxfont::width(". . .", font);
74 return lyxfont::width(" x ", font);
76 case PROTECTED_SEPARATOR:
78 return lyxfont::width('x', font);
82 return 1; // To shut up gcc
86 void InsetSpecialChar::draw(BufferView * bv, LyXFont const & f,
87 int baseline, float & x, bool) const
89 Painter & pain = bv->painter();
95 font.setColor(LColor::special);
96 pain.text(int(x), baseline, "-", font);
102 font.setColor(LColor::special);
103 pain.text(int(x), baseline, "|", font);
104 x += width(bv, font);
107 case END_OF_SENTENCE:
109 font.setColor(LColor::special);
110 pain.text(int(x), baseline, ".", font);
111 x += width(bv, font);
116 font.setColor(LColor::special);
117 pain.text(int(x), baseline, ". . .", font);
118 x += width(bv, font);
123 // A triangle the width and height of an 'x'
124 int w = lyxfont::width('x', font);
125 int ox = lyxfont::width(' ', font) + int(x);
126 int h = lyxfont::ascent('x', font);
129 xp[0] = ox; yp[0] = baseline;
130 xp[1] = ox; yp[1] = baseline - h;
131 xp[2] = ox + w; yp[2] = baseline - h/2;
132 xp[3] = ox; yp[3] = baseline;
134 pain.lines(xp, yp, 4, LColor::special);
135 x += width(bv, font);
138 case PROTECTED_SEPARATOR:
140 float w = width(bv, font);
141 int h = lyxfont::ascent('x', font);
145 yp[0] = baseline - max(h / 4, 1);
154 yp[3] = baseline - max(h / 4, 1);
156 pain.lines(xp, yp, 4, LColor::special);
164 // In lyxf3 this will be just LaTeX
165 void InsetSpecialChar::write(Buffer const *, ostream & os) const
173 command = "\\textcompwordmark{}";
175 case END_OF_SENTENCE:
179 command = "\\ldots{}";
182 command = "\\menuseparator";
184 case PROTECTED_SEPARATOR:
185 //command = "\\protected_separator";
189 os << "\\SpecialChar " << command << "\n";
193 // This function will not be necessary when lyx3
194 void InsetSpecialChar::read(Buffer const *, LyXLex & lex)
197 string const command = lex.getString();
199 if (command == "\\-")
201 else if (command == "\\textcompwordmark{}")
202 kind_ = LIGATURE_BREAK;
203 else if (command == "\\@.")
204 kind_ = END_OF_SENTENCE;
205 else if (command == "\\ldots{}")
207 else if (command == "\\menuseparator")
208 kind_ = MENU_SEPARATOR;
209 else if (command == "\\protected_separator"
211 kind_ = PROTECTED_SEPARATOR;
213 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
217 int InsetSpecialChar::latex(Buffer const *, ostream & os, bool /*fragile*/,
218 bool free_space) const
225 os << "\\textcompwordmark{}";
227 case END_OF_SENTENCE:
234 os << "\\lyxarrow{}";
236 case PROTECTED_SEPARATOR:
237 os << (free_space ? " " : "~");
244 int InsetSpecialChar::ascii(Buffer const *, ostream & os, int) const
250 case END_OF_SENTENCE:
259 case PROTECTED_SEPARATOR:
267 int InsetSpecialChar::linuxdoc(Buffer const *, ostream & os) const
273 case END_OF_SENTENCE:
282 case PROTECTED_SEPARATOR:
290 int InsetSpecialChar::docbook(Buffer const *, ostream & os) const
296 case END_OF_SENTENCE:
305 case PROTECTED_SEPARATOR:
313 Inset * InsetSpecialChar::clone(Buffer const &, bool) const
315 return new InsetSpecialChar(kind_);
319 void InsetSpecialChar::validate(LaTeXFeatures & features) const
321 if (kind_ == MENU_SEPARATOR) {
322 features.require("lyxarrow");
327 bool InsetSpecialChar::isChar() const
333 bool InsetSpecialChar::isLetter() const
335 return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
339 bool InsetSpecialChar::isSpace() const
341 return kind_ == PROTECTED_SEPARATOR;
345 bool InsetSpecialChar::isLineSeparator() const
347 return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;