From 381b86788d382ffe26a1f24427a668b9ee1d061e Mon Sep 17 00:00:00 2001 From: Pavel Sanda Date: Sun, 3 May 2009 22:45:14 +0000 Subject: [PATCH] Introduce strikeout text style (strike-through) http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg150743.html Somewhat related to http://www.lyx.org/trac/ticket/4248 . git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@29523 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/bind/cua.bind | 1 + lib/lyx2lyx/lyx_2_0.py | 14 +++++++++++-- src/Buffer.cpp | 2 +- src/Font.cpp | 32 +++++++++++++++++++++++++++++- src/FontInfo.cpp | 11 +++++++++- src/FontInfo.h | 8 +++++++- src/FuncCode.h | 6 +++--- src/LyXAction.cpp | 8 ++++++++ src/Text.cpp | 3 +++ src/Text3.cpp | 7 +++++++ src/frontends/qt4/GuiCharacter.cpp | 10 ++++++++++ src/frontends/qt4/GuiCharacter.h | 2 ++ src/frontends/qt4/GuiPainter.cpp | 18 +++++++++++++++++ src/frontends/qt4/GuiPainter.h | 3 +++ src/insets/InsetCollapsable.cpp | 1 + src/insets/InsetTabular.cpp | 1 + 16 files changed, 118 insertions(+), 9 deletions(-) diff --git a/lib/bind/cua.bind b/lib/bind/cua.bind index cd9be2e8be..9e826cc299 100644 --- a/lib/bind/cua.bind +++ b/lib/bind/cua.bind @@ -62,6 +62,7 @@ #\bind "C-k" "font-noun" # 'k' for capitals \bind "C-u" "font-underline" \bind "C-S-P" "font-typewriter" # 'P' for Program +\bind "C-S-O" "font-strikeout" \bind "C-m" "math-mode" \bind "C-S-M" "math-display" diff --git a/lib/lyx2lyx/lyx_2_0.py b/lib/lyx2lyx/lyx_2_0.py index 63280f62a4..85b169cad6 100644 --- a/lib/lyx2lyx/lyx_2_0.py +++ b/lib/lyx2lyx/lyx_2_0.py @@ -604,6 +604,14 @@ def revert_printindexall(document): document.body[i:k+1] = subst i = i + 1 +def revert_strikeout(document): + " Reverts \\strike character style " + while True: + i = find_token(document.body, '\\strikeout', 0) + if i == -1: + return + del document.body[i] + ## # Conversion hub @@ -618,10 +626,12 @@ convert = [[346, []], [351, []], [352, [convert_splitindex]], [353, []], - [354, []] + [354, []], + [355, []] ] -revert = [[353, [revert_printindexall]], +revert = [[354, [revert_strikeout]], + [353, [revert_printindexall]], [352, [revert_subindex]], [351, [revert_splitindex]], [350, [revert_backgroundcolor]], diff --git a/src/Buffer.cpp b/src/Buffer.cpp index bc3585ebe9..077a5e2ff8 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -125,7 +125,7 @@ namespace { // Do not remove the comment below, so we get merge conflict in // independent branches. Instead add your own. -int const LYX_FORMAT = 354; // jspitzm: support for \print[sub]index* +int const LYX_FORMAT = 355; // sanda: support for \\sout typedef map DepClean; typedef map > RefCache; diff --git a/src/Font.cpp b/src/Font.cpp index 234314e05b..814a9a0680 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -175,6 +175,9 @@ docstring const stateText(FontInfo const & f) if (f.underbar() != FONT_INHERIT) os << bformat(_("Underline %1$s, "), _(GUIMiscNames[f.underbar()])); + if (f.strikeout() != FONT_INHERIT) + os << bformat(_("Strikeout %1$s, "), + _(GUIMiscNames[f.strikeout()])); if (f.noun() != FONT_INHERIT) os << bformat(_("Noun %1$s, "), _(GUIMiscNames[f.noun()])); @@ -329,6 +332,8 @@ FontInfo lyxRead(Lexer & lex, FontInfo const & fi) if (ttok == "no_bar") { f.setUnderbar(FONT_OFF); + } else if (ttok == "no_strikeout") { + f.setStrikeout(FONT_OFF); } else if (ttok == "no_emph") { f.setEmph(FONT_OFF); } else if (ttok == "no_noun") { @@ -337,6 +342,8 @@ FontInfo lyxRead(Lexer & lex, FontInfo const & fi) f.setEmph(FONT_ON); } else if (ttok == "underbar") { f.setUnderbar(FONT_ON); + } else if (ttok == "strikeout") { + f.setStrikeout(FONT_ON); } else if (ttok == "noun") { f.setNoun(FONT_ON); } else { @@ -388,6 +395,9 @@ void Font::lyxWriteChanges(Font const & orgfont, break; } } + if (orgfont.fontInfo().strikeout() != bits_.strikeout()) { + os << "\\strikeout " << LyXMiscNames[bits_.strikeout()] << "\n"; + } if (orgfont.fontInfo().noun() != bits_.noun()) { os << "\\noun " << LyXMiscNames[bits_.noun()] << "\n"; } @@ -526,6 +536,11 @@ int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams, count += 10; env = true; //We have opened a new environment } + if (f.strikeout() == FONT_ON) { + os << "\\sout{"; + count += 6; + env = true; //We have opened a new environment + } // \noun{} is a LyX special macro if (f.noun() == FONT_ON) { os << "\\noun{"; @@ -595,6 +610,11 @@ int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams, ++count; env = true; // Size change need not bother about closing env. } + if (f.strikeout() == FONT_ON) { + os << '}'; + ++count; + env = true; // Size change need not bother about closing env. + } if (f.noun() == FONT_ON) { os << '}'; ++count; @@ -654,6 +674,7 @@ string Font::toString(bool const toggle) const << "size " << bits_.size() << '\n' << "emph " << bits_.emph() << '\n' << "underbar " << bits_.underbar() << '\n' + << "strikeout " << bits_.strikeout() << '\n' << "noun " << bits_.noun() << '\n' << "number " << bits_.number() << '\n' << "color " << bits_.color() << '\n' @@ -695,7 +716,8 @@ bool Font::fromString(string const & data, bool & toggle) bits_.setSize(FontSize(next)); } else if (token == "emph" || token == "underbar" || - token == "noun" || token == "number") { + token == "noun" || token == "number" || + token == "strikeout") { int const next = lex.getInteger(); FontState const misc = FontState(next); @@ -704,6 +726,8 @@ bool Font::fromString(string const & data, bool & toggle) bits_.setEmph(misc); else if (token == "underbar") bits_.setUnderbar(misc); + else if (token == "strikeout") + bits_.setStrikeout(misc); else if (token == "noun") bits_.setNoun(misc); else if (token == "number") @@ -747,6 +771,11 @@ void Font::validate(LaTeXFeatures & features) const features.require("noun"); LYXERR(Debug::LATEX, "Noun enabled. Font: " << to_utf8(stateText(0))); } + if (bits_.strikeout() == FONT_ON) { + LYXERR(Debug::LATEX, "font.strikeout: " << bits_.strikeout()); + features.require("ulem"); + LYXERR(Debug::LATEX, "Strikeout enabled. Font: " << to_utf8(stateText(0))); + } switch (bits_.color()) { case Color_none: case Color_inherit: @@ -791,6 +820,7 @@ ostream & operator<<(ostream & os, FontInfo const & f) //<< " background " << f.background() << " emph " << f.emph() << " underbar " << f.underbar() + << " strikeout " << f.strikeout() << " noun " << f.noun() << " number " << f.number(); } diff --git a/src/FontInfo.cpp b/src/FontInfo.cpp index 00a9da165d..a1f30c5e05 100644 --- a/src/FontInfo.cpp +++ b/src/FontInfo.cpp @@ -32,6 +32,7 @@ FontInfo const sane_font( FONT_OFF, FONT_OFF, FONT_OFF, + FONT_OFF, FONT_OFF); FontInfo const inherit_font( @@ -44,6 +45,7 @@ FontInfo const inherit_font( FONT_INHERIT, FONT_INHERIT, FONT_INHERIT, + FONT_INHERIT, FONT_OFF); FontInfo const ignore_font( @@ -56,6 +58,7 @@ FontInfo const ignore_font( FONT_IGNORE, FONT_IGNORE, FONT_IGNORE, + FONT_IGNORE, FONT_IGNORE); @@ -141,6 +144,8 @@ void FontInfo::reduce(FontInfo const & tmplt) emph_ = FONT_INHERIT; if (underbar_ == tmplt.underbar_) underbar_ = FONT_INHERIT; + if (strikeout_ == tmplt.strikeout_) + strikeout_ = FONT_INHERIT; if (noun_ == tmplt.noun_) noun_ = FONT_INHERIT; if (color_ == tmplt.color_) @@ -176,6 +181,9 @@ FontInfo & FontInfo::realize(FontInfo const & tmplt) if (underbar_ == FONT_INHERIT) underbar_ = tmplt.underbar_; + if (strikeout_ == FONT_INHERIT) + strikeout_ = tmplt.strikeout_; + if (noun_ == FONT_INHERIT) noun_ = tmplt.noun_; @@ -252,6 +260,7 @@ void FontInfo::update(FontInfo const & newfont, bool toggleall) setEmph(setMisc(newfont.emph_, emph_)); setUnderbar(setMisc(newfont.underbar_, underbar_)); + setStrikeout(setMisc(newfont.strikeout_, strikeout_)); setNoun(setMisc(newfont.noun_, noun_)); setNumber(setMisc(newfont.number_, number_)); @@ -272,7 +281,7 @@ bool FontInfo::resolved() const return (family_ != INHERIT_FAMILY && series_ != INHERIT_SERIES && shape_ != INHERIT_SHAPE && size_ != FONT_SIZE_INHERIT && emph_ != FONT_INHERIT && underbar_ != FONT_INHERIT - && noun_ != FONT_INHERIT + && strikeout_ != FONT_INHERIT && noun_ != FONT_INHERIT && color_ != Color_inherit && background_ != Color_inherit); } diff --git a/src/FontInfo.h b/src/FontInfo.h index 8db78d5ce1..10d72354ef 100644 --- a/src/FontInfo.h +++ b/src/FontInfo.h @@ -41,11 +41,12 @@ public: ColorCode background, FontState emph, FontState underbar, + FontState strikeout, FontState noun, FontState number) : family_(family), series_(series), shape_(shape), size_(size), color_(color), background_(background), paint_color_(), emph_(emph), - underbar_(underbar), noun_(noun), number_(number) + underbar_(underbar), strikeout_(strikeout), noun_(noun), number_(number) {} /// Decreases font size by one @@ -67,6 +68,8 @@ public: void setEmph(FontState e) { emph_ = e; } FontState underbar() const { return underbar_; } void setUnderbar(FontState u) { underbar_ = u; } + FontState strikeout() const { return strikeout_; } + void setStrikeout(FontState s) { strikeout_ = s; } FontState noun() const { return noun_; } void setNoun(FontState n) { noun_ = n; } FontState number() const { return number_; } @@ -145,6 +148,8 @@ private: /// FontState underbar_; /// + FontState strikeout_; + /// FontState noun_; /// FontState number_; @@ -161,6 +166,7 @@ inline bool operator==(FontInfo const & lhs, FontInfo const & rhs) && lhs.background_ == rhs.background_ && lhs.emph_ == rhs.emph_ && lhs.underbar_ == rhs.underbar_ + && lhs.strikeout_ == rhs.strikeout_ && lhs.noun_ == rhs.noun_ && lhs.number_ == rhs.number_; } diff --git a/src/FuncCode.h b/src/FuncCode.h index d2257a4e37..ee72e93910 100644 --- a/src/FuncCode.h +++ b/src/FuncCode.h @@ -181,13 +181,13 @@ enum FuncCode LFUN_FONT_DEFAULT, // 125 LFUN_FONT_UNDERLINE, + LFUN_FONT_STRIKEOUT, LFUN_FONT_SIZE, LFUN_FONT_STATE, LFUN_WORD_UPCASE, - LFUN_WORD_LOWCASE, // 130 + LFUN_WORD_LOWCASE, LFUN_WORD_CAPITALIZE, - LFUN_LABEL_INSERT, LFUN_DEPTH_DECREMENT, LFUN_DEPTH_INCREMENT, LFUN_MENU_OPEN, // used in bindings as of 20060905 @@ -429,7 +429,7 @@ enum FuncCode LFUN_MATH_BIGDELIM, LFUN_MATH_FONT_STYLE, LFUN_SECTION_SELECT, // vfr, 20090503 - + LFUN_LABEL_INSERT, LFUN_LASTACTION // end of the table }; diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index d6d540d723..9deb2f9cfb 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -1227,6 +1227,14 @@ void LyXAction::init() * \endvar */ { LFUN_FONT_UNDERLINE, "font-underline", Noop, Layout }, +/*! + * \var lyx::FuncCode lyx::LFUN_FONT_STRIKEOUT + * \li Action: Toggles strikeout (strike-through) in the font (selection-wise). + * \li Syntax: font-strikeout + * \li Origin: sanda, 3 May 2009 + * \endvar + */ + { LFUN_FONT_STRIKEOUT, "font-strikeout", Noop, Layout }, /*! * \var lyx::FuncCode lyx::LFUN_FONT_EMPH * \li Action: Toggles the emphasis font style (selection-wise). diff --git a/src/Text.cpp b/src/Text.cpp index 7f1861efb2..c31739f82f 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -188,6 +188,9 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex, else lex.printError("Unknown bar font flag " "`$$Token'"); + } else if (token == "\\strikeout") { + lex.next(); + font.fontInfo().setStrikeout(font.setLyXMisc(lex.getString())); } else if (token == "\\noun") { lex.next(); font.fontInfo().setNoun(font.setLyXMisc(lex.getString())); diff --git a/src/Text3.cpp b/src/Text3.cpp index 1a8eee0f09..3481d5f300 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -1722,6 +1722,12 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd) break; } + case LFUN_FONT_STRIKEOUT: { + Font font(ignore_font, ignore_language); + font.fontInfo().setStrikeout(FONT_TOGGLE); + toggleAndShow(cur, this, font); + break; + } case LFUN_FONT_UNDERLINE: { Font font(ignore_font, ignore_language); font.fontInfo().setUnderbar(FONT_TOGGLE); @@ -2423,6 +2429,7 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd, case LFUN_MATH_SUPERSCRIPT: case LFUN_FONT_DEFAULT: case LFUN_FONT_UNDERLINE: + case LFUN_FONT_STRIKEOUT: case LFUN_FONT_SIZE: case LFUN_LANGUAGE: case LFUN_TEXTSTYLE_APPLY: diff --git a/src/frontends/qt4/GuiCharacter.cpp b/src/frontends/qt4/GuiCharacter.cpp index 28dfdb560c..8aad329218 100644 --- a/src/frontends/qt4/GuiCharacter.cpp +++ b/src/frontends/qt4/GuiCharacter.cpp @@ -76,6 +76,7 @@ static QList barData() bars << BarPair(qt_("No change"), IGNORE); bars << BarPair(qt_("Emph"), EMPH_TOGGLE); bars << BarPair(qt_("Underbar"), UNDERBAR_TOGGLE); + bars << BarPair(qt_("Strikeout"), STRIKEOUT_TOGGLE); bars << BarPair(qt_("Noun"), NOUN_TOGGLE); bars << BarPair(qt_("Reset"), INHERIT); return bars; @@ -276,6 +277,9 @@ static FontState getBar(FontInfo const & fi) if (fi.underbar() == FONT_TOGGLE) return UNDERBAR_TOGGLE; + if (fi.strikeout() == FONT_TOGGLE) + return STRIKEOUT_TOGGLE; + if (fi.noun() == FONT_TOGGLE) return NOUN_TOGGLE; @@ -294,6 +298,7 @@ static void setBar(FontInfo & fi, FontState val) case IGNORE: fi.setEmph(FONT_IGNORE); fi.setUnderbar(FONT_IGNORE); + fi.setStrikeout(FONT_IGNORE); fi.setNoun(FONT_IGNORE); break; @@ -305,6 +310,10 @@ static void setBar(FontInfo & fi, FontState val) fi.setUnderbar(FONT_TOGGLE); break; + case STRIKEOUT_TOGGLE: + fi.setStrikeout(FONT_TOGGLE); + break; + case NOUN_TOGGLE: fi.setNoun(FONT_TOGGLE); break; @@ -312,6 +321,7 @@ static void setBar(FontInfo & fi, FontState val) case INHERIT: fi.setEmph(FONT_INHERIT); fi.setUnderbar(FONT_INHERIT); + fi.setStrikeout(FONT_INHERIT); fi.setNoun(FONT_INHERIT); break; } diff --git a/src/frontends/qt4/GuiCharacter.h b/src/frontends/qt4/GuiCharacter.h index 16cdc23947..02cd2feca4 100644 --- a/src/frontends/qt4/GuiCharacter.h +++ b/src/frontends/qt4/GuiCharacter.h @@ -37,6 +37,8 @@ enum FontState { /// NOUN_TOGGLE, /// + STRIKEOUT_TOGGLE, + /// INHERIT }; diff --git a/src/frontends/qt4/GuiPainter.cpp b/src/frontends/qt4/GuiPainter.cpp index fd2f96b39d..470b928e38 100644 --- a/src/frontends/qt4/GuiPainter.cpp +++ b/src/frontends/qt4/GuiPainter.cpp @@ -336,6 +336,8 @@ int GuiPainter::text(int x, int y, docstring const & s, textwidth = smallCapsText(x, y, str, f); if (f.underbar() == FONT_ON) underline(f, x, y, textwidth); + if (f.strikeout() == FONT_ON) + strikeoutLine(f, x, y, textwidth); return textwidth; } @@ -345,6 +347,8 @@ int GuiPainter::text(int x, int y, docstring const & s, textwidth = fm.width(s); if (f.underbar() == FONT_ON) underline(f, x, y, textwidth); + if (f.strikeout() == FONT_ON) + strikeoutLine(f, x, y, textwidth); if (!isDrawingEnabled()) return textwidth; @@ -534,6 +538,20 @@ void GuiPainter::underline(FontInfo const & f, int x, int y, int width) } +void GuiPainter::strikeoutLine(FontInfo const & f, int x, int y, int width) +{ + FontMetrics const & fm = theFontMetrics(f); + + int const middle = max((fm.maxHeight() / 4), 1); + int const height = middle/3; + + if (height < 2) + line(x, y - middle, x + width, y - middle, f.realColor()); + else + fillRectangle(x, y - middle, width, height, f.realColor()); +} + + void GuiPainter::dashedUnderline(FontInfo const & f, int x, int y, int width) { FontMetrics const & fm = theFontMetrics(f); diff --git a/src/frontends/qt4/GuiPainter.h b/src/frontends/qt4/GuiPainter.h index 232798e11e..8e75438884 100644 --- a/src/frontends/qt4/GuiPainter.h +++ b/src/frontends/qt4/GuiPainter.h @@ -129,6 +129,9 @@ private: /// check the font, and if set, draw an dashed underline void dashedUnderline(FontInfo const & f, int x, int y, int width); + /// check the font, and if set, draw an strike-through line + void strikeoutLine(FontInfo const & f, + int x, int y, int width); /// draw a bevelled button border void buttonFrame(int x, int y, int w, int h); diff --git a/src/insets/InsetCollapsable.cpp b/src/insets/InsetCollapsable.cpp index 8606de4e31..32f7633f20 100644 --- a/src/insets/InsetCollapsable.cpp +++ b/src/insets/InsetCollapsable.cpp @@ -688,6 +688,7 @@ bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd, case LFUN_FONT_SIZE: case LFUN_FONT_STATE: case LFUN_FONT_UNDERLINE: + case LFUN_FONT_STRIKEOUT: case LFUN_FOOTNOTE_INSERT: case LFUN_HYPERLINK_INSERT: case LFUN_INDEX_INSERT: diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp index 1ad481ec41..014ca94911 100644 --- a/src/insets/InsetTabular.cpp +++ b/src/insets/InsetTabular.cpp @@ -3712,6 +3712,7 @@ void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd) case LFUN_TEXTSTYLE_UPDATE: case LFUN_FONT_SIZE: case LFUN_FONT_UNDERLINE: + case LFUN_FONT_STRIKEOUT: case LFUN_LANGUAGE: case LFUN_WORD_CAPITALIZE: case LFUN_WORD_UPCASE: