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 odocstringstream ods;
288 int h = (int)(InsetCollapsable::plaintext(ods, runparams) / 2);
289 docstring result = ods.str();
290 docstring const before = result.substr(0, h);
291 docstring const after = result.substr(h, result.size());
293 if (params_.type == InsetIPADecoParams::Toptiebar) {
298 else if (params_.type == InsetIPADecoParams::Bottomtiebar) {
303 return result.size();
307 int InsetIPADeco::docbook(odocstream & os, OutputParams const & runparams) const
309 // FIXME: Any docbook option here?
310 return InsetCollapsable::docbook(os, runparams);
314 docstring InsetIPADeco::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
316 // FIXME: Like in plaintext, the combining characters "͡" (toptiebar)
317 // or "͜" (bottomtiebar) would need to be inserted just in the mid
318 // of the text string. (How) can this be done with the xhtml stream?
319 return InsetCollapsable::xhtml(xs, runparams);
323 docstring InsetIPADeco::toolTip(BufferView const &, int, int) const
325 return ipadecotranslator_loc().find(params_.type);
329 string InsetIPADeco::params2string(InsetIPADecoParams const & params)
332 data << "IPADeco" << ' ';
338 void InsetIPADeco::string2params(string const & in, InsetIPADecoParams & params)
340 params = InsetIPADecoParams();
345 istringstream data(in);
348 lex.setContext("InsetIPADeco::string2params");
349 lex >> "IPADeco" >> "toptiebar";
355 void InsetIPADeco::validate(LaTeXFeatures & features) const
357 if (!buffer_->params().useNonTeXFonts)
358 features.require("tipa");
359 InsetText::validate(features);
363 bool InsetIPADeco::insetAllowed(InsetCode code) const
366 // code that is allowed
376 /////////////////////////////////////////////////////////////////////////
380 /////////////////////////////////////////////////////////////////////////
383 InsetIPAChar::InsetIPAChar(Kind k)
388 InsetIPAChar::Kind InsetIPAChar::kind() const
394 void InsetIPAChar::metrics(MetricsInfo & mi, Dimension & dim) const
396 frontend::FontMetrics const & fm =
397 theFontMetrics(mi.base.font);
398 dim.asc = fm.maxAscent();
399 dim.des = fm.maxDescent();
405 case TONE_HIGH_RISING:
406 case TONE_LOW_RISING:
407 case TONE_HIGH_RISING_FALLING:
411 docstring ds(s.begin(), s.end());
412 dim.wid = fm.width(ds);
413 setDimCache(mi, dim);
417 void InsetIPAChar::draw(PainterInfo & pi, int x, int y) const
419 FontInfo font = pi.base.font;
420 frontend::FontMetrics const & fm =
421 theFontMetrics(font);
426 int w = fm.width(char_type('-'));
427 int h = fm.ascent(char_type('M'));
431 pi.pain.line(x2, y2, x2, y, Color_foreground);
432 pi.pain.line(x2, y, x, y2, Color_foreground);
437 int w = fm.width(char_type('-'));
438 int h = fm.ascent(char_type('M'));
442 pi.pain.line(x2, y, x2, y2, Color_foreground);
443 pi.pain.line(x2, y2, x, y, Color_foreground);
446 case TONE_HIGH_RISING:
448 int w = fm.width(char_type('-'));
449 int h = fm.ascent(char_type('M'));
452 int y3 = y - (h * 0.75);
454 pi.pain.line(x2, y, x2, y2, Color_foreground);
455 pi.pain.line(x2, y2, x, y3, Color_foreground);
458 case TONE_LOW_RISING:
460 int w = fm.width(char_type('-'));
461 int h = fm.ascent(char_type('M'));
464 int y3 = y - (h * 0.25);
466 pi.pain.line(x2, y, x2, y2, Color_foreground);
467 pi.pain.line(x2, y3, x, y, Color_foreground);
470 case TONE_HIGH_RISING_FALLING:
472 int w = fm.width(char_type('-'));
473 int h = fm.ascent(char_type('M'));
476 int x3 = x + (w * 0.5);
477 int y3 = y - (h * 0.75);
479 pi.pain.line(x2, y, x2, y2, Color_foreground);
480 pi.pain.line(x2, y3, x3, y2, Color_foreground);
481 pi.pain.line(x3, y2, x, y3, Color_foreground);
488 void InsetIPAChar::write(ostream & os) const
490 string const command = ipachartranslator().find(kind_);
491 if (command.empty()) {
492 LYXERR0("InsetIPAChar::write: Unknown type");
495 os << "\\IPAChar " << command << "\n";
499 void InsetIPAChar::read(Lexer & lex)
502 string const command = lex.getString();
503 kind_ = ipachartranslator().find(command);
507 void InsetIPAChar::latex(otexstream & os,
508 OutputParams const &) const
510 string const command = ipachartranslator().find(kind_);
515 int InsetIPAChar::plaintext(odocstream & os, OutputParams const &) const
526 case TONE_HIGH_RISING:
530 case TONE_LOW_RISING:
534 case TONE_HIGH_RISING_FALLING:
544 int InsetIPAChar::docbook(odocstream & /*os*/, OutputParams const &) const
548 LYXERR0("IPA tone macros not yet implemented with DocBook!");
551 case TONE_HIGH_RISING:
552 case TONE_LOW_RISING:
553 case TONE_HIGH_RISING_FALLING:
560 docstring InsetIPAChar::xhtml(XHTMLStream & xs, OutputParams const &) const
564 xs << XHTMLStream::ESCAPE_NONE << "˥"
565 << XHTMLStream::ESCAPE_NONE << "˩";
568 xs << XHTMLStream::ESCAPE_NONE << "˩"
569 << XHTMLStream::ESCAPE_NONE << "˥";
571 case TONE_HIGH_RISING:
572 xs << XHTMLStream::ESCAPE_NONE << "˧"
573 << XHTMLStream::ESCAPE_NONE << "˥";
575 case TONE_LOW_RISING:
576 xs << XHTMLStream::ESCAPE_NONE << "˩"
577 << XHTMLStream::ESCAPE_NONE << "˧";
579 case TONE_HIGH_RISING_FALLING:
580 xs << XHTMLStream::ESCAPE_NONE << "˨"
581 << XHTMLStream::ESCAPE_NONE << "˥"
582 << XHTMLStream::ESCAPE_NONE << "˨";
589 void InsetIPAChar::toString(odocstream & os) const
591 plaintext(os, OutputParams(0));
595 void InsetIPAChar::forToc(docstring & os, size_t) const
597 odocstringstream ods;
598 plaintext(ods, OutputParams(0));
603 void InsetIPAChar::validate(LaTeXFeatures & features) const
608 case TONE_HIGH_RISING:
609 case TONE_LOW_RISING:
610 case TONE_HIGH_RISING_FALLING:
611 if (!buffer_->params().useNonTeXFonts)
612 features.require("tone");
620 bool InsetIPAChar::isLetter() const
626 bool InsetIPAChar::isLineSeparator() const