2 * \file InsetIPAMacro.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jürgen Spitzmüller
8 * Full author contact details are available in file CREDITS.
13 #include "InsetIPAMacro.h"
16 #include "BufferParams.h"
17 #include "Dimension.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "LaTeXFeatures.h"
24 #include "MetricsInfo.h"
25 #include "output_xhtml.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/Translator.h"
41 typedef Translator<string, InsetIPADecoParams::Type> IPADecoTranslator;
42 typedef Translator<docstring, InsetIPADecoParams::Type> IPADecoTranslatorLoc;
44 IPADecoTranslator const init_ipadecotranslator()
46 IPADecoTranslator translator("toptiebar", InsetIPADecoParams::Toptiebar);
47 translator.addPair("bottomtiebar", InsetIPADecoParams::Bottomtiebar);
52 IPADecoTranslatorLoc const init_ipadecotranslator_loc()
54 IPADecoTranslatorLoc translator(_("Top tie bar"), InsetIPADecoParams::Toptiebar);
55 translator.addPair(_("Bottom tie bar"), InsetIPADecoParams::Bottomtiebar);
60 IPADecoTranslator const & ipadecotranslator()
62 static IPADecoTranslator decotranslator = init_ipadecotranslator();
63 return decotranslator;
67 IPADecoTranslatorLoc const & ipadecotranslator_loc()
69 static IPADecoTranslatorLoc translator = init_ipadecotranslator_loc();
74 typedef Translator<string, InsetIPAChar::Kind> IPACharTranslator;
76 IPACharTranslator const init_ipachartranslator()
78 IPACharTranslator translator("\\tone{51}", InsetIPAChar::TONE_FALLING);
79 translator.addPair("\\tone{15}", InsetIPAChar::TONE_RISING);
80 translator.addPair("\\tone{45}", InsetIPAChar::TONE_HIGH_RISING);
81 translator.addPair("\\tone{12}", InsetIPAChar::TONE_LOW_RISING);
82 translator.addPair("\\tone{454}", InsetIPAChar::TONE_HIGH_RISING_FALLING);
87 IPACharTranslator const & ipachartranslator()
89 static IPACharTranslator chartranslator = init_ipachartranslator();
90 return chartranslator;
96 InsetIPADecoParams::InsetIPADecoParams()
101 void InsetIPADecoParams::write(ostream & os) const
103 string const label = ipadecotranslator().find(type);
104 os << "IPADeco " << label << "\n";
108 void InsetIPADecoParams::read(Lexer & lex)
113 type = ipadecotranslator().find(label);
117 /////////////////////////////////////////////////////////////////////
121 /////////////////////////////////////////////////////////////////////
123 InsetIPADeco::InsetIPADeco(Buffer * buf, string const & label)
124 : InsetCollapsable(buf)
127 setFrameColor(Color_insetframe);
128 params_.type = ipadecotranslator().find(label);
132 InsetIPADeco::~InsetIPADeco()
136 docstring InsetIPADeco::layoutName() const
138 return from_ascii("IPADeco:" + ipadecotranslator().find(params_.type));
142 void InsetIPADeco::metrics(MetricsInfo & mi, Dimension & dim) const
144 InsetText::metrics(mi, dim);
146 if (params_.type == InsetIPADecoParams::Toptiebar) {
147 // consider width of the inset label
148 FontInfo font(getLayout().labelfont());
149 font.realize(sane_font);
155 docstring const label(1, char_type(0x2040));
156 theFontMetrics(font).rectText(label, w, a, d);
159 if (params_.type == InsetIPADecoParams::Bottomtiebar) {
160 // consider width of the inset label
161 FontInfo font(getLayout().labelfont());
162 font.realize(sane_font);
168 docstring const label(1, char_type(0x203f));
169 theFontMetrics(font).rectText(label, w, a, d);
173 // cache the inset dimension
174 setDimCache(mi, dim);
178 void InsetIPADeco::draw(PainterInfo & pi, int x, int y) const
181 InsetCollapsable::draw(pi, x, y);
183 // draw the inset marker
184 drawMarkers(pi, x, y);
186 Dimension const dim = Inset::dimension(*pi.base.bv);
188 if (params_.type == InsetIPADecoParams::Toptiebar) {
189 FontInfo font(getLayout().labelfont());
190 font.realize(sane_font);
196 int asc = dim.ascent();
197 docstring const label(1, char_type(0x2040));
198 theFontMetrics(font).rectText(label, w, a, d);
199 int const ww = max(dim.wid, w);
200 pi.pain.rectText(x + (ww - w) / 2, y - (asc / 2.5),
201 label, font, Color_none, Color_none);
204 if (params_.type == InsetIPADecoParams::Bottomtiebar) {
205 FontInfo font(getLayout().labelfont());
206 font.realize(sane_font);
212 int desc = dim.descent();
213 docstring const label(1, char_type(0x203f));
214 theFontMetrics(font).rectText(label, w, a, d);
215 int const ww = max(dim.wid, w);
216 pi.pain.rectText(x + (ww - w) / 2, y + (desc / 1.5),
217 label, font, Color_none, Color_none);
222 void InsetIPADeco::write(ostream & os) const
225 InsetCollapsable::write(os);
229 void InsetIPADeco::read(Lexer & lex)
232 InsetCollapsable::read(lex);
236 void InsetIPADeco::doDispatch(Cursor & cur, FuncRequest & cmd)
238 switch (cmd.action()) {
239 case LFUN_QUOTE_INSERT: {
240 FuncRequest fr(LFUN_SELF_INSERT, "\"");
241 InsetText::doDispatch(cur, fr);
245 InsetText::doDispatch(cur, cmd);
251 bool InsetIPADeco::getStatus(Cursor & cur, FuncRequest const & cmd,
252 FuncStatus & flag) const
254 switch (cmd.action()) {
255 case LFUN_SCRIPT_INSERT: {
256 if (cmd.argument() == "subscript") {
257 flag.setEnabled(false);
263 flag.setEnabled(true);
269 return InsetText::getStatus(cur, cmd, flag);
273 void InsetIPADeco::latex(otexstream & os, OutputParams const & runparams) const
275 if (params_.type == InsetIPADecoParams::Toptiebar)
276 os << "\\texttoptiebar{";
277 else if (params_.type == InsetIPADecoParams::Bottomtiebar)
278 os << "\\textbottomtiebar{";
279 InsetCollapsable::latex(os, runparams);
284 int InsetIPADeco::plaintext(odocstream & os,
285 OutputParams const & runparams) const
287 // FIXME: Any plaintext option here?
288 return InsetCollapsable::plaintext(os, runparams);
292 int InsetIPADeco::docbook(odocstream & os, OutputParams const & runparams) const
294 // FIXME: Any docbook option here?
295 return InsetCollapsable::docbook(os, runparams);
299 docstring InsetIPADeco::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
301 // FIXME: Any xhtml option here?
302 return InsetCollapsable::xhtml(xs, runparams);
306 docstring InsetIPADeco::toolTip(BufferView const &, int, int) const
308 return ipadecotranslator_loc().find(params_.type);
312 string InsetIPADeco::params2string(InsetIPADecoParams const & params)
315 data << "IPADeco" << ' ';
321 void InsetIPADeco::string2params(string const & in, InsetIPADecoParams & params)
323 params = InsetIPADecoParams();
328 istringstream data(in);
331 lex.setContext("InsetIPADeco::string2params");
332 lex >> "IPADeco" >> "toptiebar";
338 void InsetIPADeco::validate(LaTeXFeatures & features) const
340 if (!buffer_->params().useNonTeXFonts)
341 features.require("tipa");
342 InsetText::validate(features);
346 bool InsetIPADeco::insetAllowed(InsetCode code) const
349 // code that is allowed
359 /////////////////////////////////////////////////////////////////////////
363 /////////////////////////////////////////////////////////////////////////
366 InsetIPAChar::InsetIPAChar(Kind k)
371 InsetIPAChar::Kind InsetIPAChar::kind() const
377 void InsetIPAChar::metrics(MetricsInfo & mi, Dimension & dim) const
379 frontend::FontMetrics const & fm =
380 theFontMetrics(mi.base.font);
381 dim.asc = fm.maxAscent();
382 dim.des = fm.maxDescent();
388 case TONE_HIGH_RISING:
389 case TONE_LOW_RISING:
390 case TONE_HIGH_RISING_FALLING:
394 docstring ds(s.begin(), s.end());
395 dim.wid = fm.width(ds);
396 setDimCache(mi, dim);
400 void InsetIPAChar::draw(PainterInfo & pi, int x, int y) const
402 FontInfo font = pi.base.font;
403 frontend::FontMetrics const & fm =
404 theFontMetrics(font);
409 int w = fm.width(char_type('-'));
410 int h = fm.ascent(char_type('M'));
414 pi.pain.line(x2, y2, x2, y, Color_foreground);
415 pi.pain.line(x2, y, x, y2, Color_foreground);
420 int w = fm.width(char_type('-'));
421 int h = fm.ascent(char_type('M'));
425 pi.pain.line(x2, y, x2, y2, Color_foreground);
426 pi.pain.line(x2, y2, x, y, Color_foreground);
429 case TONE_HIGH_RISING:
431 int w = fm.width(char_type('-'));
432 int h = fm.ascent(char_type('M'));
435 int y3 = y - (h * 0.75);
437 pi.pain.line(x2, y, x2, y2, Color_foreground);
438 pi.pain.line(x2, y2, x, y3, Color_foreground);
441 case TONE_LOW_RISING:
443 int w = fm.width(char_type('-'));
444 int h = fm.ascent(char_type('M'));
447 int y3 = y - (h * 0.25);
449 pi.pain.line(x2, y, x2, y2, Color_foreground);
450 pi.pain.line(x2, y3, x, y, Color_foreground);
453 case TONE_HIGH_RISING_FALLING:
455 int w = fm.width(char_type('-'));
456 int h = fm.ascent(char_type('M'));
459 int x3 = x + (w * 0.5);
460 int y3 = y - (h * 0.75);
462 pi.pain.line(x2, y, x2, y2, Color_foreground);
463 pi.pain.line(x2, y3, x3, y2, Color_foreground);
464 pi.pain.line(x3, y2, x, y3, Color_foreground);
471 void InsetIPAChar::write(ostream & os) const
473 string const command = ipachartranslator().find(kind_);
474 if (command.empty()) {
475 LYXERR0("InsetIPAChar::write: Unknown type");
478 os << "\\IPAChar " << command << "\n";
482 void InsetIPAChar::read(Lexer & lex)
485 string const command = lex.getString();
486 kind_ = ipachartranslator().find(command);
490 void InsetIPAChar::latex(otexstream & os,
491 OutputParams const &) const
493 string const command = ipachartranslator().find(kind_);
498 int InsetIPAChar::plaintext(odocstream & os, OutputParams const &) const
509 case TONE_HIGH_RISING:
513 case TONE_LOW_RISING:
517 case TONE_HIGH_RISING_FALLING:
527 int InsetIPAChar::docbook(odocstream & /*os*/, OutputParams const &) const
533 case TONE_HIGH_RISING:
534 case TONE_LOW_RISING:
535 case TONE_HIGH_RISING_FALLING:
542 docstring InsetIPAChar::xhtml(XHTMLStream & xs, OutputParams const &) const
546 xs << XHTMLStream::ESCAPE_NONE << "˥"
547 << XHTMLStream::ESCAPE_NONE << "˩";
550 xs << XHTMLStream::ESCAPE_NONE << "˩"
551 << XHTMLStream::ESCAPE_NONE << "˥";
553 case TONE_HIGH_RISING:
554 xs << XHTMLStream::ESCAPE_NONE << "˧"
555 << XHTMLStream::ESCAPE_NONE << "˥";
557 case TONE_LOW_RISING:
558 xs << XHTMLStream::ESCAPE_NONE << "˩"
559 << XHTMLStream::ESCAPE_NONE << "˧";
561 case TONE_HIGH_RISING_FALLING:
562 xs << XHTMLStream::ESCAPE_NONE << "˨"
563 << XHTMLStream::ESCAPE_NONE << "˥"
564 << XHTMLStream::ESCAPE_NONE << "˨";
571 void InsetIPAChar::toString(odocstream & os) const
573 plaintext(os, OutputParams(0));
577 void InsetIPAChar::forToc(docstring & os, size_t) const
579 odocstringstream ods;
580 plaintext(ods, OutputParams(0));
585 void InsetIPAChar::validate(LaTeXFeatures & features) const
590 case TONE_HIGH_RISING:
591 case TONE_LOW_RISING:
592 case TONE_HIGH_RISING_FALLING:
593 if (!buffer_->params().useNonTeXFonts)
594 features.require("tone");
602 bool InsetIPAChar::isLetter() const
608 bool InsetIPAChar::isLineSeparator() const