From f8e7b01983e8d0cf0ffa366e987614e4a156e364 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Wed, 13 Jul 2005 11:38:55 +0000 Subject: [PATCH] small font tweaks git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@10175 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/tex2lyx/ChangeLog | 6 +++++ src/tex2lyx/context.C | 30 ++++++++++++++++++++---- src/tex2lyx/context.h | 49 +++++++++++++++++++++++---------------- src/tex2lyx/text.C | 54 ++++++++++++++++++------------------------- 4 files changed, 84 insertions(+), 55 deletions(-) diff --git a/src/tex2lyx/ChangeLog b/src/tex2lyx/ChangeLog index c5da277c56..bb4ac78d29 100644 --- a/src/tex2lyx/ChangeLog +++ b/src/tex2lyx/ChangeLog @@ -1,3 +1,9 @@ +2005-07-12 Georg Baum + + * text.C (parse_text): output font changes only if needed + * context.C (begin_layout): output font if it is not the default one + * context.[Ch] (output_font_change): new, needed for the above + 2005-06-01 Georg Baum * text.C (parse_text): eat {} after \ss, \i and \j diff --git a/src/tex2lyx/context.C b/src/tex2lyx/context.C index 882637d480..e094d6585d 100644 --- a/src/tex2lyx/context.C +++ b/src/tex2lyx/context.C @@ -22,9 +22,13 @@ using std::string; namespace { -void begin_layout(ostream & os, LyXLayout_ptr layout) +void begin_layout(ostream & os, LyXLayout_ptr layout, Font const & font, + Font const & normalfont) { os << "\n\\begin_layout " << layout->name() << "\n"; + // FIXME: This is not enough for things like + // \\Huge par1 \\par par2 + output_font_change(os, normalfont, font); } @@ -47,6 +51,23 @@ void end_deeper(ostream & os) } +void output_font_change(ostream & os, Font const & oldfont, + Font const & newfont) +{ + if (oldfont.family != newfont.family) + os << "\n\\family " << newfont.family << '\n'; + if (oldfont.series != newfont.series) + os << "\n\\series " << newfont.series << '\n'; + if (oldfont.shape != newfont.shape) + os << "\n\\shape " << newfont.shape << '\n'; + if (oldfont.size != newfont.size) + os << "\n\\size " << newfont.size << '\n'; +} + + +Font Context::normalfont; + + Context::Context(bool need_layout_, LyXTextClass const & textclass_, LyXLayout_ptr layout_, LyXLayout_ptr parent_layout_, @@ -81,7 +102,7 @@ void Context::check_layout(ostream & os) end_deeper(os); deeper_paragraph = false; } - begin_layout(os, layout); + begin_layout(os, layout, font, normalfont); has_item = false; need_layout=false; need_end_layout = true; @@ -91,14 +112,15 @@ void Context::check_layout(ostream & os) // that this may require a begin_deeper. if (!deeper_paragraph) begin_deeper(os); - begin_layout(os, textclass.defaultLayout()); + begin_layout(os, textclass.defaultLayout(), + font, normalfont); need_layout=false; need_end_layout = true; deeper_paragraph = true; } } else { // No list-like environment - begin_layout(os, layout); + begin_layout(os, layout, font, normalfont); need_layout=false; need_end_layout = true; } diff --git a/src/tex2lyx/context.h b/src/tex2lyx/context.h index 29be22ffb2..5a2edffe9a 100644 --- a/src/tex2lyx/context.h +++ b/src/tex2lyx/context.h @@ -14,6 +14,8 @@ #include "lyxtextclass.h" +#include + /*! * Small helper struct that holds font properties. @@ -43,6 +45,11 @@ public: }; +/// Output changed font parameters if \p oldfont and \p newfont differ +void output_font_change(std::ostream & os, Font const & oldfont, + Font const & newfont); + + // A helper struct class Context { public: @@ -52,20 +59,20 @@ public: LyXLayout_ptr parent_layout_= LyXLayout_ptr(), Font font_ = Font()); - // Output a \begin_layout is requested + /// Output a \\begin_layout if requested void check_layout(std::ostream & os); - // Output a \end_layout if needed + /// Output a \\end_layout if needed void check_end_layout(std::ostream & os); - // Output a \begin_deeper if needed + /// Output a \\begin_deeper if needed void check_deeper(std::ostream & os); - // Output a \end_deeper if needed + /// Output a \\end_deeper if needed void check_end_deeper(std::ostream & os); - // dump content on stream (for debugging purpose), with - // description \c desc. + /// dump content on stream (for debugging purpose), with + /// description \c desc. void dump(std::ostream &, std::string const & desc = "context") const; /// Are we just beginning a new paragraph? @@ -80,32 +87,34 @@ public: /// Add extra stuff if not already there void add_extra_stuff(std::string const &); - // Do we need to output some \begin_layout command before the - // next characters? + /// Do we need to output some \\begin_layout command before the + /// next characters? bool need_layout; - // Do we need to output some \end_layout command + /// Do we need to output some \\end_layout command bool need_end_layout; - // We may need to add something after this \begin_layout command + /// We may need to add something after this \\begin_layout command std::string extra_stuff; - // If there has been an \begin_deeper, we'll need a matching - // \end_deeper + /// If there has been an \\begin_deeper, we'll need a matching + /// \\end_deeper bool need_end_deeper; - // If we are in an itemize-like environment, we need an \item - // for each paragraph, otherwise this has to be a deeper - // paragraph. + /// If we are in an itemize-like environment, we need an \\item + /// for each paragraph, otherwise this has to be a deeper + /// paragraph. bool has_item; - // we are handling a standard paragraph in an itemize-like - // environment + /// we are handling a standard paragraph in an itemize-like + /// environment bool deeper_paragraph; - // The textclass of the document. Could actually be a global variable + /// The textclass of the document. Could actually be a global variable LyXTextClass const & textclass; - // The layout of the current paragraph + /// The layout of the current paragraph LyXLayout_ptr layout; - // The layout of the outer paragraph (for environment layouts) + /// The layout of the outer paragraph (for environment layouts) LyXLayout_ptr parent_layout; /// font attributes of this context Font font; + /// font attributes of normal text + static Font normalfont; }; #endif diff --git a/src/tex2lyx/text.C b/src/tex2lyx/text.C index 88fa5dad4a..2dc7e81b31 100644 --- a/src/tex2lyx/text.C +++ b/src/tex2lyx/text.C @@ -70,7 +70,7 @@ void parse_text_in_inset(Parser & p, ostream & os, unsigned flags, bool outer, } -/// parses a paragraph snippet, useful for example for \emph{...} +/// parses a paragraph snippet, useful for example for \\emph{...} void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer, Context & context) { @@ -356,7 +356,9 @@ void skip_braces(Parser & p) } -void handle_ert(ostream & os, string const & s, Context & context, bool check_layout = true) + +void handle_ert(ostream & os, string const & s, Context & context, + bool check_layout = true) { if (check_layout) { // We must have a valid layout before outputting the ERT inset. @@ -710,7 +712,7 @@ void parse_environment(Parser & p, ostream & os, bool outer, // The single '=' is meant here. else if ((newlayout = findLayout(parent_context.textclass, name)).get() && - newlayout->isEnvironment()) { + newlayout->isEnvironment()) { Context context(true, parent_context.textclass, newlayout, parent_context.layout, parent_context.font); if (parent_context.deeper_paragraph) { @@ -1512,10 +1514,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, os << "\n\\shape " << context.font.shape << "\n"; if (t.cs() == "textnormal") { parse_text_snippet(p, os, FLAG_ITEM, outer, context); + output_font_change(os, context.font, oldFont); context.font = oldFont; - os << "\n\\shape " << oldFont.shape << "\n"; - os << "\n\\series " << oldFont.series << "\n"; - os << "\n\\family " << oldFont.family << "\n"; } else eat_whitespace(p, os, context, false); } @@ -1654,8 +1654,9 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, else if (is_known(t.cs(), known_sizes)) { char const * const * where = is_known(t.cs(), known_sizes); context.check_layout(os); + Font const oldFont = context.font; context.font.size = known_coded_sizes[where - known_sizes]; - os << "\n\\size " << context.font.size << '\n'; + output_font_change(os, oldFont, context.font); eat_whitespace(p, os, context, false); } @@ -1663,10 +1664,10 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, char const * const * where = is_known(t.cs(), known_font_families); context.check_layout(os); + Font const oldFont = context.font; context.font.family = known_coded_font_families[where - known_font_families]; - // FIXME: Only do this if it is necessary - os << "\n\\family " << context.font.family << '\n'; + output_font_change(os, oldFont, context.font); eat_whitespace(p, os, context, false); } @@ -1674,10 +1675,10 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, char const * const * where = is_known(t.cs(), known_font_series); context.check_layout(os); + Font const oldFont = context.font; context.font.series = known_coded_font_series[where - known_font_series]; - // FIXME: Only do this if it is necessary - os << "\n\\series " << context.font.series << '\n'; + output_font_change(os, oldFont, context.font); eat_whitespace(p, os, context, false); } @@ -1685,25 +1686,22 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, char const * const * where = is_known(t.cs(), known_font_shapes); context.check_layout(os); + Font const oldFont = context.font; context.font.shape = known_coded_font_shapes[where - known_font_shapes]; - // FIXME: Only do this if it is necessary - os << "\n\\shape " << context.font.shape << '\n'; + output_font_change(os, oldFont, context.font); eat_whitespace(p, os, context, false); } else if (is_known(t.cs(), known_old_font_families)) { char const * const * where = is_known(t.cs(), known_old_font_families); context.check_layout(os); - string oldsize = context.font.size; + Font const oldFont = context.font; context.font.init(); - context.font.size = oldsize; + context.font.size = oldFont.size; context.font.family = known_coded_font_families[where - known_old_font_families]; - // FIXME: Only do this if it is necessary - os << "\n\\family " << context.font.family << "\n" - << "\\series " << context.font.series << "\n" - << "\\shape " << context.font.shape << "\n"; + output_font_change(os, oldFont, context.font); eat_whitespace(p, os, context, false); } @@ -1711,15 +1709,12 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, char const * const * where = is_known(t.cs(), known_old_font_series); context.check_layout(os); - string oldsize = context.font.size; + Font const oldFont = context.font; context.font.init(); - context.font.size = oldsize; + context.font.size = oldFont.size; context.font.series = known_coded_font_series[where - known_old_font_series]; - // FIXME: Only do this if it is necessary - os << "\n\\family " << context.font.family << "\n" - << "\\series " << context.font.series << "\n" - << "\\shape " << context.font.shape << "\n"; + output_font_change(os, oldFont, context.font); eat_whitespace(p, os, context, false); } @@ -1727,15 +1722,12 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, char const * const * where = is_known(t.cs(), known_old_font_shapes); context.check_layout(os); - string oldsize = context.font.size; + Font const oldFont = context.font; context.font.init(); - context.font.size = oldsize; + context.font.size = oldFont.size; context.font.shape = known_coded_font_shapes[where - known_old_font_shapes]; - // FIXME: Only do this if it is necessary - os << "\n\\family " << context.font.family << "\n" - << "\\series " << context.font.series << "\n" - << "\\shape " << context.font.shape << "\n"; + output_font_change(os, oldFont, context.font); eat_whitespace(p, os, context, false); } -- 2.39.2