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"
26 #include "texstream.h"
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/Translator.h"
42 typedef Translator<string, InsetIPADecoParams::Type> IPADecoTranslator;
43 typedef Translator<docstring, InsetIPADecoParams::Type> IPADecoTranslatorLoc;
45 IPADecoTranslator const init_ipadecotranslator()
47 IPADecoTranslator translator("toptiebar", InsetIPADecoParams::Toptiebar);
48 translator.addPair("bottomtiebar", InsetIPADecoParams::Bottomtiebar);
53 IPADecoTranslatorLoc const init_ipadecotranslator_loc()
55 IPADecoTranslatorLoc translator(_("Top tie bar"), InsetIPADecoParams::Toptiebar);
56 translator.addPair(_("Bottom tie bar"), InsetIPADecoParams::Bottomtiebar);
61 IPADecoTranslator const & ipadecotranslator()
63 static IPADecoTranslator const decotranslator =
64 init_ipadecotranslator();
65 return decotranslator;
69 IPADecoTranslatorLoc const & ipadecotranslator_loc()
71 static IPADecoTranslatorLoc const translator =
72 init_ipadecotranslator_loc();
77 typedef Translator<string, InsetIPAChar::Kind> IPACharTranslator;
79 IPACharTranslator const init_ipachartranslator()
81 IPACharTranslator translator("\\tone{51}", InsetIPAChar::TONE_FALLING);
82 translator.addPair("\\tone{15}", InsetIPAChar::TONE_RISING);
83 translator.addPair("\\tone{45}", InsetIPAChar::TONE_HIGH_RISING);
84 translator.addPair("\\tone{12}", InsetIPAChar::TONE_LOW_RISING);
85 translator.addPair("\\tone{454}", InsetIPAChar::TONE_HIGH_RISING_FALLING);
90 IPACharTranslator const & ipachartranslator()
92 static IPACharTranslator const chartranslator =
93 init_ipachartranslator();
94 return chartranslator;
100 InsetIPADecoParams::InsetIPADecoParams()
105 void InsetIPADecoParams::write(ostream & os) const
107 string const label = ipadecotranslator().find(type);
108 os << "IPADeco " << label << "\n";
112 void InsetIPADecoParams::read(Lexer & lex)
117 type = ipadecotranslator().find(label);
121 /////////////////////////////////////////////////////////////////////
125 /////////////////////////////////////////////////////////////////////
127 InsetIPADeco::InsetIPADeco(Buffer * buf, string const & label)
128 : InsetCollapsable(buf)
131 setFrameColor(Color_insetframe);
132 params_.type = ipadecotranslator().find(label);
136 InsetIPADeco::~InsetIPADeco()
140 docstring InsetIPADeco::layoutName() const
142 return from_ascii("IPADeco:" + ipadecotranslator().find(params_.type));
146 void InsetIPADeco::metrics(MetricsInfo & mi, Dimension & dim) const
148 InsetText::metrics(mi, dim);
150 if (params_.type == InsetIPADecoParams::Toptiebar) {
151 // consider width of the inset label
152 FontInfo font(getLayout().labelfont());
153 font.realize(sane_font);
159 docstring const label(1, char_type(0x2040));
160 theFontMetrics(font).rectText(label, w, a, d);
161 dim.asc += int(a * 0.5);
163 if (params_.type == InsetIPADecoParams::Bottomtiebar) {
164 // consider width of the inset label
165 FontInfo font(getLayout().labelfont());
166 font.realize(sane_font);
172 docstring const label(1, char_type(0x203f));
173 theFontMetrics(font).rectText(label, w, a, d);
174 dim.des += int(d * 1.5);
177 // cache the inset dimension
178 setDimCache(mi, dim);
182 void InsetIPADeco::draw(PainterInfo & pi, int x, int y) const
185 InsetCollapsable::draw(pi, x, y);
187 // draw the inset marker
188 drawMarkers(pi, x, y);
190 Dimension const dim = Inset::dimension(*pi.base.bv);
192 if (params_.type == InsetIPADecoParams::Toptiebar) {
193 FontInfo font(getLayout().labelfont());
194 font.realize(sane_font);
200 int asc = dim.ascent();
201 docstring const label(1, char_type(0x2040));
202 theFontMetrics(font).rectText(label, w, a, d);
203 int const ww = max(dim.wid, w);
204 pi.pain.rectText(x + (ww - w) / 2, y - int(asc / 2.5),
205 label, font, Color_none, Color_none);
208 if (params_.type == InsetIPADecoParams::Bottomtiebar) {
209 FontInfo font(getLayout().labelfont());
210 font.realize(sane_font);
216 int desc = dim.descent();
217 docstring const label(1, char_type(0x203f));
218 theFontMetrics(font).rectText(label, w, a, d);
219 int const ww = max(dim.wid, w);
220 pi.pain.rectText(x + (ww - w) / 2, y + int(desc / 1.5),
221 label, font, Color_none, Color_none);
226 void InsetIPADeco::write(ostream & os) const
229 InsetCollapsable::write(os);
233 void InsetIPADeco::read(Lexer & lex)
236 InsetCollapsable::read(lex);
240 void InsetIPADeco::doDispatch(Cursor & cur, FuncRequest & cmd)
242 switch (cmd.action()) {
243 case LFUN_QUOTE_INSERT: {
244 FuncRequest fr(LFUN_SELF_INSERT, "\"");
245 InsetText::doDispatch(cur, fr);
249 InsetText::doDispatch(cur, cmd);
255 bool InsetIPADeco::getStatus(Cursor & cur, FuncRequest const & cmd,
256 FuncStatus & flag) const
258 switch (cmd.action()) {
259 case LFUN_SCRIPT_INSERT: {
260 if (cmd.argument() == "subscript") {
261 flag.setEnabled(false);
267 flag.setEnabled(true);
273 return InsetText::getStatus(cur, cmd, flag);
277 void InsetIPADeco::latex(otexstream & os, OutputParams const & runparams) const
279 if (params_.type == InsetIPADecoParams::Toptiebar)
280 os << "\\texttoptiebar{";
281 else if (params_.type == InsetIPADecoParams::Bottomtiebar)
282 os << "\\textbottomtiebar{";
283 InsetCollapsable::latex(os, runparams);
288 int InsetIPADeco::plaintext(odocstringstream & os,
289 OutputParams const & runparams, size_t max_length) const
291 odocstringstream ods;
292 int h = (int)(InsetCollapsable::plaintext(ods, runparams, max_length) / 2);
293 docstring result = ods.str();
294 docstring const before = result.substr(0, h);
295 docstring const after = result.substr(h, result.size());
297 if (params_.type == InsetIPADecoParams::Toptiebar) {
302 else if (params_.type == InsetIPADecoParams::Bottomtiebar) {
307 return result.size();
311 int InsetIPADeco::docbook(odocstream & os, OutputParams const & runparams) const
313 // FIXME: Any docbook option here?
314 return InsetCollapsable::docbook(os, runparams);
318 docstring InsetIPADeco::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
320 // FIXME: Like in plaintext, the combining characters "͡" (toptiebar)
321 // or "͜" (bottomtiebar) would need to be inserted just in the mid
322 // of the text string. (How) can this be done with the xhtml stream?
323 return InsetCollapsable::xhtml(xs, runparams);
327 docstring InsetIPADeco::toolTip(BufferView const &, int, int) const
329 return ipadecotranslator_loc().find(params_.type);
333 string InsetIPADeco::params2string(InsetIPADecoParams const & params)
336 data << "IPADeco" << ' ';
342 void InsetIPADeco::string2params(string const & in, InsetIPADecoParams & params)
344 params = InsetIPADecoParams();
349 istringstream data(in);
352 lex.setContext("InsetIPADeco::string2params");
353 lex >> "IPADeco" >> "toptiebar";
359 void InsetIPADeco::validate(LaTeXFeatures & features) const
361 features.require("tipa");
362 InsetText::validate(features);
366 bool InsetIPADeco::insetAllowed(InsetCode code) const
369 // code that is allowed
379 /////////////////////////////////////////////////////////////////////////
383 /////////////////////////////////////////////////////////////////////////
386 InsetIPAChar::InsetIPAChar(Kind k)
391 InsetIPAChar::Kind InsetIPAChar::kind() const
397 void InsetIPAChar::metrics(MetricsInfo & mi, Dimension & dim) const
399 frontend::FontMetrics const & fm =
400 theFontMetrics(mi.base.font);
401 dim.asc = fm.maxAscent();
402 dim.des = fm.maxDescent();
408 case TONE_HIGH_RISING:
409 case TONE_LOW_RISING:
410 case TONE_HIGH_RISING_FALLING:
414 docstring ds(s.begin(), s.end());
415 dim.wid = fm.width(ds);
416 setDimCache(mi, dim);
420 void InsetIPAChar::draw(PainterInfo & pi, int x, int y) const
422 FontInfo font = pi.base.font;
423 frontend::FontMetrics const & fm =
424 theFontMetrics(font);
429 int w = fm.width(char_type('-'));
430 int h = fm.ascent(char_type('M'));
434 pi.pain.line(x2, y2, x2, y, Color_foreground);
435 pi.pain.line(x2, y, x, y2, Color_foreground);
440 int w = fm.width(char_type('-'));
441 int h = fm.ascent(char_type('M'));
445 pi.pain.line(x2, y, x2, y2, Color_foreground);
446 pi.pain.line(x2, y2, x, y, Color_foreground);
449 case TONE_HIGH_RISING:
451 int w = fm.width(char_type('-'));
452 int h = fm.ascent(char_type('M'));
455 int y3 = y - int(h * 0.75);
457 pi.pain.line(x2, y, x2, y2, Color_foreground);
458 pi.pain.line(x2, y2, x, y3, Color_foreground);
461 case TONE_LOW_RISING:
463 int w = fm.width(char_type('-'));
464 int h = fm.ascent(char_type('M'));
467 int y3 = y - int(h * 0.25);
469 pi.pain.line(x2, y, x2, y2, Color_foreground);
470 pi.pain.line(x2, y3, x, y, Color_foreground);
473 case TONE_HIGH_RISING_FALLING:
475 int w = fm.width(char_type('-'));
476 int h = fm.ascent(char_type('M'));
479 int x3 = x + int(w * 0.5);
480 int y3 = y - int(h * 0.75);
482 pi.pain.line(x2, y, x2, y2, Color_foreground);
483 pi.pain.line(x2, y3, x3, y2, Color_foreground);
484 pi.pain.line(x3, y2, x, y3, Color_foreground);
491 void InsetIPAChar::write(ostream & os) const
493 string const command = ipachartranslator().find(kind_);
494 if (command.empty()) {
495 LYXERR0("InsetIPAChar::write: Unknown type");
498 os << "\\IPAChar " << command << "\n";
502 void InsetIPAChar::read(Lexer & lex)
505 string const command = lex.getString();
506 kind_ = ipachartranslator().find(command);
510 void InsetIPAChar::latex(otexstream & os,
511 OutputParams const &) const
513 string const command = ipachartranslator().find(kind_);
518 int InsetIPAChar::plaintext(odocstringstream & os, OutputParams const &, size_t) const
529 case TONE_HIGH_RISING:
533 case TONE_LOW_RISING:
537 case TONE_HIGH_RISING_FALLING:
547 int InsetIPAChar::docbook(odocstream & /*os*/, OutputParams const &) const
551 LYXERR0("IPA tone macros not yet implemented with DocBook!");
554 case TONE_HIGH_RISING:
555 case TONE_LOW_RISING:
556 case TONE_HIGH_RISING_FALLING:
563 docstring InsetIPAChar::xhtml(XHTMLStream & xs, OutputParams const &) const
567 xs << XHTMLStream::ESCAPE_NONE << "˥"
568 << XHTMLStream::ESCAPE_NONE << "˩";
571 xs << XHTMLStream::ESCAPE_NONE << "˩"
572 << XHTMLStream::ESCAPE_NONE << "˥";
574 case TONE_HIGH_RISING:
575 xs << XHTMLStream::ESCAPE_NONE << "˧"
576 << XHTMLStream::ESCAPE_NONE << "˥";
578 case TONE_LOW_RISING:
579 xs << XHTMLStream::ESCAPE_NONE << "˩"
580 << XHTMLStream::ESCAPE_NONE << "˧";
582 case TONE_HIGH_RISING_FALLING:
583 xs << XHTMLStream::ESCAPE_NONE << "˨"
584 << XHTMLStream::ESCAPE_NONE << "˥"
585 << XHTMLStream::ESCAPE_NONE << "˨";
592 void InsetIPAChar::toString(odocstream & os) const
594 odocstringstream ods;
595 plaintext(ods, OutputParams(0));
600 void InsetIPAChar::forOutliner(docstring & os, size_t const, bool const) const
602 odocstringstream ods;
603 plaintext(ods, OutputParams(0));
608 void InsetIPAChar::validate(LaTeXFeatures & features) const
613 case TONE_HIGH_RISING:
614 case TONE_LOW_RISING:
615 case TONE_HIGH_RISING_FALLING:
616 features.require("tone");