From abf9e5eabc0a544e848376773f2c1585fe18d90f Mon Sep 17 00:00:00 2001 From: spitz Date: Fri, 7 Aug 2009 05:25:22 +0000 Subject: [PATCH] Catch encoding exception when performing instant preview on an uncodable char (bug 6116). * MathStream{.cpp, h}: - replace bool dryrun() by enum output that also knows whether the stream is for instant preview * InsetMathHull.cpp: - tell the stream whether we use it for instant preview. * MathString.cpp (write): - gracefully catch encoding exception for instant preview. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@30895 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Cursor.cpp | 2 +- src/mathed/InsetFormulaMacro.cpp | 9 +++++---- src/mathed/InsetMath.cpp | 6 +++--- src/mathed/InsetMathHull.cpp | 12 ++++++------ src/mathed/InsetMathNest.cpp | 5 +++-- src/mathed/InsetMathString.cpp | 20 ++++++++++++++++---- src/mathed/MacroTable.cpp | 2 +- src/mathed/MathMacroTemplate.cpp | 2 +- src/mathed/MathStream.cpp | 6 +++--- src/mathed/MathStream.h | 14 ++++++++++---- 10 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/Cursor.cpp b/src/Cursor.cpp index 4785ce3264..f12f442fd8 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -1590,7 +1590,7 @@ void Cursor::normalize() << pos() << ' ' << lastpos() << " in idx: " << idx() << " in atom: '"; odocstringstream os; - WriteStream wi(os, false, true, false); + WriteStream wi(os, false, true, WriteStream::wsDefault); inset().asInsetMath()->write(wi); lyxerr << to_utf8(os.str()) << endl; pos() = lastpos(); diff --git a/src/mathed/InsetFormulaMacro.cpp b/src/mathed/InsetFormulaMacro.cpp index 1d80cbedbc..eaa89d70e3 100644 --- a/src/mathed/InsetFormulaMacro.cpp +++ b/src/mathed/InsetFormulaMacro.cpp @@ -65,7 +65,7 @@ Inset * InsetFormulaMacro::clone() const void InsetFormulaMacro::write(ostream & os) const { os << "FormulaMacro\n"; - WriteStream wi(os, false, false, false); + WriteStream wi(os, false, false, WriteStream::wsDefault); tmpl()->write(wi); } @@ -74,8 +74,9 @@ int InsetFormulaMacro::latex(odocstream & os, OutputParams const & runparams) const { //lyxerr << "InsetFormulaMacro::latex" << endl; - WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun, - runparams.encoding); + WriteStream wi(os, runparams.moving_arg, true, + runparams.dryrun ? WriteStream::wsDryrun: WriteStream::wsDefault, + runparams.encoding); tmpl()->write(wi); return 2; } @@ -84,7 +85,7 @@ int InsetFormulaMacro::latex(odocstream & os, int InsetFormulaMacro::plaintext(odocstream & os, OutputParams const & runparams) const { odocstringstream oss; - WriteStream wi(oss, false, true, false, runparams.encoding); + WriteStream wi(oss, false, true, WriteStream::wsDefault, runparams.encoding); tmpl()->write(wi); docstring const str = oss.str(); diff --git a/src/mathed/InsetMath.cpp b/src/mathed/InsetMath.cpp index 139defa9b5..d6c40d7e30 100644 --- a/src/mathed/InsetMath.cpp +++ b/src/mathed/InsetMath.cpp @@ -47,7 +47,7 @@ void InsetMath::dump() const { lyxerr << "---------------------------------------------" << endl; odocstringstream os; - WriteStream wi(os, false, true, false); + WriteStream wi(os, false, true, WriteStream::wsDefault); write(wi); lyxerr << to_utf8(os.str()); lyxerr << "\n---------------------------------------------" << endl; @@ -136,7 +136,7 @@ HullType InsetMath::getType() const ostream & operator<<(ostream & os, MathAtom const & at) { odocstringstream oss; - WriteStream wi(oss, false, false, false); + WriteStream wi(oss, false, false, WriteStream::wsDefault); at->write(wi); return os << to_utf8(oss.str()); } @@ -144,7 +144,7 @@ ostream & operator<<(ostream & os, MathAtom const & at) odocstream & operator<<(odocstream & os, MathAtom const & at) { - WriteStream wi(os, false, false, false); + WriteStream wi(os, false, false, WriteStream::wsDefault); at->write(wi); return os; } diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index f632b80288..d22203816f 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -420,7 +420,7 @@ void InsetMathHull::metricsT(TextMetricsInfo const & mi, Dimension & dim) const InsetMathGrid::metricsT(mi, dim); } else { odocstringstream os; - WriteStream wi(os, false, true, false); + WriteStream wi(os, false, true, WriteStream::wsDefault); write(wi); dim.wid = os.str().size(); dim.asc = 1; @@ -435,7 +435,7 @@ void InsetMathHull::drawT(TextPainter & pain, int x, int y) const InsetMathGrid::drawT(pain, x, y); } else { odocstringstream os; - WriteStream wi(os, false, true, false); + WriteStream wi(os, false, true, WriteStream::wsDefault); write(wi); pain.draw(x, y, os.str().c_str()); } @@ -453,7 +453,7 @@ static docstring latexString(InsetMathHull const & inset) static Encoding const * encoding = 0; if (inset.isBufferValid()) encoding = &(inset.buffer().params().encoding()); - WriteStream wi(ls, false, true, false, encoding); + WriteStream wi(ls, false, true, WriteStream::wsPreview, encoding); inset.write(wi); return ls.str(); } @@ -1632,7 +1632,7 @@ bool InsetMathHull::searchForward(BufferView * bv, string const & str, void InsetMathHull::write(ostream & os) const { odocstringstream oss; - WriteStream wi(oss, false, false, false); + WriteStream wi(oss, false, false, WriteStream::wsDefault); oss << "Formula "; write(wi); os << to_utf8(oss.str()); @@ -1670,7 +1670,7 @@ int InsetMathHull::plaintext(odocstream & os, OutputParams const & runparams) co return tpain.textheight(); } else { odocstringstream oss; - WriteStream wi(oss, false, true, false, runparams.encoding); + WriteStream wi(oss, false, true, WriteStream::wsDefault, runparams.encoding); wi << cell(0); docstring const str = oss.str(); @@ -1702,7 +1702,7 @@ int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) cons // Workaround for db2latex: db2latex always includes equations with // \ensuremath{} or \begin{display}\end{display} // so we strip LyX' math environment - WriteStream wi(ls, false, false, false, runparams.encoding); + WriteStream wi(ls, false, false, WriteStream::wsDefault, runparams.encoding); InsetMathGrid::write(wi); ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&"), "<", "<")); ms << ETag("alt"); diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp index 484418ea4a..a0962e972a 100644 --- a/src/mathed/InsetMathNest.cpp +++ b/src/mathed/InsetMathNest.cpp @@ -377,8 +377,9 @@ void InsetMathNest::normalize(NormalStream & os) const int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const { - WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun, - runparams.encoding); + WriteStream wi(os, runparams.moving_arg, true, + runparams.dryrun ? WriteStream::wsDryrun : WriteStream::wsDefault, + runparams.encoding); write(wi); return wi.line(); } diff --git a/src/mathed/InsetMathString.cpp b/src/mathed/InsetMathString.cpp index b1648a505a..8b9db39c81 100644 --- a/src/mathed/InsetMathString.cpp +++ b/src/mathed/InsetMathString.cpp @@ -11,11 +11,13 @@ #include #include "InsetMathString.h" +#include "MathFactory.h" #include "MathStream.h" #include "MathSupport.h" #include "Encoding.h" +#include "support/debug.h" #include "support/gettext.h" #include "support/lstrings.h" #include "support/textutils.h" @@ -159,14 +161,24 @@ void InsetMathString::write(WriteStream & os) const && isAlphaASCII(command[command.size() - 1])) os.pendingSpace(true); } catch (EncodingException & e) { - if (os.dryrun()) { - // FIXME: this is OK for View->Source - // but math preview will likely fail. + switch (os.output()) { + case WriteStream::wsDryrun: { os << "<" << _("LyX Warning: ") << _("uncodable character") << " '"; os << docstring(1, e.failed_char); os << "'>"; - } else { + break; + } + case WriteStream::wsPreview: { + // indicate the encoding error by a boxed '?' + os << "{\\fboxsep=1pt\\fbox{?}}";; + LYXERR0("Uncodable character" << " '" + << docstring(1, e.failed_char) + << "'"); + break; + } + case WriteStream::wsDefault: + default: // throw again throw(e); } diff --git a/src/mathed/MacroTable.cpp b/src/mathed/MacroTable.cpp index df996215fe..1a7ba80295 100644 --- a/src/mathed/MacroTable.cpp +++ b/src/mathed/MacroTable.cpp @@ -165,7 +165,7 @@ void MacroData::write(odocstream & os, bool overwriteRedefinition) const // output template MathMacroTemplate const & tmpl = static_cast(*inset); - WriteStream wi(os, false, true, false); + WriteStream wi(os, false, true, WriteStream::wsDefault); tmpl.write(wi, overwriteRedefinition); } diff --git a/src/mathed/MathMacroTemplate.cpp b/src/mathed/MathMacroTemplate.cpp index 6cccd8fa6a..421ca81463 100644 --- a/src/mathed/MathMacroTemplate.cpp +++ b/src/mathed/MathMacroTemplate.cpp @@ -1121,7 +1121,7 @@ void MathMacroTemplate::read(Lexer & lex) void MathMacroTemplate::write(ostream & os) const { odocstringstream oss; - WriteStream wi(oss, false, false, false); + WriteStream wi(oss, false, false, WriteStream::wsDefault); oss << "FormulaMacro\n"; write(wi); os << to_utf8(oss.str()); diff --git a/src/mathed/MathStream.cpp b/src/mathed/MathStream.cpp index b8a8019d36..bfba25e463 100644 --- a/src/mathed/MathStream.cpp +++ b/src/mathed/MathStream.cpp @@ -107,17 +107,17 @@ WriteStream & operator<<(WriteStream & ws, docstring const & s) } -WriteStream::WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun, +WriteStream::WriteStream(odocstream & os, bool fragile, bool latex, OutputType output, Encoding const * encoding) : os_(os), fragile_(fragile), firstitem_(false), latex_(latex), - dryrun_(dryrun), pendingspace_(false), pendingbrace_(false), + output_(output), pendingspace_(false), pendingbrace_(false), textmode_(false), locked_(0), line_(0), encoding_(encoding) {} WriteStream::WriteStream(odocstream & os) : os_(os), fragile_(false), firstitem_(false), latex_(false), - dryrun_(false), pendingspace_(false), pendingbrace_(false), + output_(wsDefault), pendingspace_(false), pendingbrace_(false), textmode_(false), locked_(0), line_(0), encoding_(0) {} diff --git a/src/mathed/MathStream.h b/src/mathed/MathStream.h index a973975107..e93cef123d 100644 --- a/src/mathed/MathStream.h +++ b/src/mathed/MathStream.h @@ -33,7 +33,13 @@ class MathData; class WriteStream { public: /// - WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun, + enum OutputType { + wsDefault, + wsDryrun, + wsPreview + }; + /// + WriteStream(odocstream & os, bool fragile, bool latex, OutputType output, Encoding const * encoding = 0); /// explicit WriteStream(odocstream & os); @@ -46,7 +52,7 @@ public: /// bool latex() const { return latex_; } /// - bool dryrun() const { return dryrun_; } + OutputType output() const { return output_; } /// odocstream & os() { return os_; } /// @@ -80,8 +86,8 @@ private: bool firstitem_; /// are we writing to .tex? int latex_; - /// is it for preview? - bool dryrun_; + /// output type (default, source preview, instant preview)? + OutputType output_; /// do we have a space pending? bool pendingspace_; /// do we have a brace pending? -- 2.11.4.GIT