commit 87397d2c29c603045d7cec83abb7a95cbc47dd59 Author: Juergen Spitzmueller <sp...@lyx.org> Date: Sun Feb 23 10:17:09 2025 +0100
tex2lyx support for nw textcolors Also some further color parsing improvements --- src/tex2lyx/Preamble.cpp | 29 +++++++++++++++++++++--- src/tex2lyx/text.cpp | 57 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/src/tex2lyx/Preamble.cpp b/src/tex2lyx/Preamble.cpp index 3267a96a89..f453f993b5 100644 --- a/src/tex2lyx/Preamble.cpp +++ b/src/tex2lyx/Preamble.cpp @@ -185,9 +185,14 @@ const char * const known_if_commands[] = {"if", "ifarydshln", "ifbraket", "ifcancel", "ifcolortbl", "ifeurosym", "ifmarginnote", "ifmmode", "ifpdf", "ifsidecap", "ifupgreek", 0}; -const char * const known_basic_colors[] = {"black", "blue", "brown", "cyan", - "darkgray", "gray", "green", "lightgray", "lime", "magenta", "orange", "olive", - "pink", "purple", "red", "teal", "violet", "white", "yellow", 0}; +/// LaTeX names for basic colors +const char * const known_basic_colors[] = {"black", "blue", "cyan", + "green", "magenta", "red", "white", "yellow", 0}; + +/// LaTeX names for additional basic colors provided by xcolor +const char * const known_basic_xcolors[] = {"brown", "darkgray", "gray", + "lightgray", "lime", "orange", "olive", "pink", "purple", + "teal", "violet", 0}; /// LaTeX names for (xcolor) colors beyond the base ones char const * const known_textcolors[] = { "Apricot", "Aquamarine", "Bittersweet", "Black", @@ -2635,6 +2640,9 @@ void Preamble::parse(Parser & p, string const & forceclass, if (space.empty() && is_known(argument, known_basic_colors)) { h_fontcolor = argument; registerAutomaticallyLoadedPackage("color"); + } else if (space.empty() && is_known(argument, known_basic_xcolors)) { + h_fontcolor = argument; + registerAutomaticallyLoadedPackage("xcolor"); // With xcolor, svgnames colors get priority with clashing names } else if (svgnames() && (where = is_known(argument, known_svgnames_textcolors))) { h_fontcolor = known_coded_svgnames_textcolors[where - known_svgnames_textcolors]; @@ -2664,6 +2672,9 @@ void Preamble::parse(Parser & p, string const & forceclass, // check the case that a standard color is used if (is_known(argument, known_basic_colors)) { h_backgroundcolor = argument; + } else if (is_known(argument, known_basic_xcolors)) { + h_backgroundcolor = argument; + registerAutomaticallyLoadedPackage("xcolor"); // With xcolor, svgnames colors get priority with clashing names } else if (svgnames() && (where = is_known(argument, known_svgnames_textcolors))) { h_backgroundcolor = known_coded_svgnames_textcolors[where - known_svgnames_textcolors]; @@ -3233,6 +3244,9 @@ void Preamble::parse(Parser & p, string const & forceclass, // check the case that a standard color is used if (is_known(color2, known_basic_colors)) { h_fontcolor = color2; + } else if (is_known(color2, known_basic_xcolors)) { + h_fontcolor = color2; + registerAutomaticallyLoadedPackage("xcolor"); // With xcolor, svgnames colors get priority with clashing names } else if (svgnames() && (where = is_known(color2, known_svgnames_textcolors))) { h_fontcolor = known_coded_svgnames_textcolors[where - known_svgnames_textcolors]; @@ -3246,6 +3260,9 @@ void Preamble::parse(Parser & p, string const & forceclass, // check the case that a standard color is used if (is_known(color2, known_basic_colors)) { h_notefontcolor = color2; + } else if (is_known(color2, known_basic_xcolors)) { + h_notefontcolor = color2; + registerAutomaticallyLoadedPackage("xcolor"); // With xcolor, svgnames colors get priority with clashing names } else if (svgnames() && (where = is_known(color2, known_svgnames_textcolors))) { h_notefontcolor = known_coded_svgnames_textcolors[where - known_svgnames_textcolors]; @@ -3259,6 +3276,9 @@ void Preamble::parse(Parser & p, string const & forceclass, // check the case that a standard color is used if (is_known(color2, known_basic_colors)) { h_backgroundcolor = color2; + } else if (is_known(color2, known_basic_xcolors)) { + h_backgroundcolor = color2; + registerAutomaticallyLoadedPackage("xcolor"); // With xcolor, svgnames colors get priority with clashing names } else if (svgnames() && (where = is_known(color2, known_svgnames_textcolors))) { h_backgroundcolor = known_coded_svgnames_textcolors[where - known_svgnames_textcolors]; @@ -3272,6 +3292,9 @@ void Preamble::parse(Parser & p, string const & forceclass, // check the case that a standard color is used if (is_known(color2, known_basic_colors)) { h_boxbgcolor = color2; + } else if (is_known(color2, known_basic_xcolors)) { + h_boxbgcolor = color2; + registerAutomaticallyLoadedPackage("xcolor"); // With xcolor, svgnames colors get priority with clashing names } else if (svgnames() && (where = is_known(color2, known_svgnames_textcolors))) { h_boxbgcolor = known_coded_svgnames_textcolors[where - known_svgnames_textcolors]; diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp index fd7aa4e1e4..b6a36e758d 100644 --- a/src/tex2lyx/text.cpp +++ b/src/tex2lyx/text.cpp @@ -299,6 +299,15 @@ char const * const known_text_font_shapes[] = { "textit", "textsl", "textsc", char const * const known_coded_font_shapes[] = { "italic", "slanted", "smallcaps", "up", 0}; +/// LaTeX names for basic colors +const char * const known_basic_colors[] = {"black", "blue", "cyan", + "green", "magenta", "red", "white", "yellow", 0}; + +/// LaTeX names for additional basic colors provided by xcolor +const char * const known_basic_xcolors[] = {"brown", "darkgray", "gray", + "lightgray", "lime", "orange", "olive", "pink", "purple", + "teal", "violet", 0}; + /// LaTeX names for (xcolor) colors beyond the base ones char const * const known_textcolors[] = { "Apricot", "Aquamarine", "Bittersweet", "Black", "Blue", "BlueGreen", "BlueViolet", "BrickRed", "Brown", "BurntOrange", "CadetBlue", "CarnationPink", @@ -1271,10 +1280,41 @@ void parse_box(Parser & p, ostream & os, unsigned outer_flags, shadowsize = "4pt"; string framecolor = "black"; string backgroundcolor = "none"; - if (!frame_color.empty()) - framecolor = frame_color; - if (!background_color.empty()) - backgroundcolor = background_color; + char const * const * where = 0; + if (!frame_color.empty()) { + // check the case that a standard color is used + if (is_known(frame_color, known_basic_colors)) { + framecolor = frame_color; + } else if (is_known(frame_color, known_basic_xcolors)) { + framecolor = frame_color; + preamble.registerAutomaticallyLoadedPackage("xcolor"); + // With xcolor, svgnames colors get priority with clashing names + } else if (preamble.svgnames() && (where = is_known(frame_color, known_svgnames_textcolors))) { + framecolor = known_coded_svgnames_textcolors[where - known_svgnames_textcolors]; + preamble.registerAutomaticallyLoadedPackage("xcolor"); + } else if ((where = is_known(frame_color, known_textcolors))) { + framecolor = known_coded_textcolors[where - known_textcolors]; + preamble.registerAutomaticallyLoadedPackage("xcolor"); + } else if (preamble.isCustomColor(frame_color)) + framecolor = frame_color; + } + if (!background_color.empty()) { + // check the case that a standard color is used + if (is_known(background_color, known_basic_colors)) { + backgroundcolor = background_color; + } else if (is_known(frame_color, known_basic_xcolors)) { + backgroundcolor = background_color; + preamble.registerAutomaticallyLoadedPackage("xcolor"); + // With xcolor, svgnames colors get priority with clashing names + } else if (preamble.svgnames() && (where = is_known(background_color, known_svgnames_textcolors))) { + backgroundcolor = known_coded_svgnames_textcolors[where - known_svgnames_textcolors]; + preamble.registerAutomaticallyLoadedPackage("xcolor"); + } else if ((where = is_known(background_color, known_textcolors))) { + backgroundcolor = known_coded_textcolors[where - known_textcolors]; + preamble.registerAutomaticallyLoadedPackage("xcolor"); + } else if (preamble.isCustomColor(background_color)) + backgroundcolor = background_color; + } // if there is a color box around the \begin statements have not yet been parsed // so do this now if (!frame_color.empty() || !background_color.empty()) { @@ -4428,19 +4468,14 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer, // scheme is \textcolor{color name}{text} string const color = p.verbatim_item(); // we support the predefined colors of the color and the xcolor package - if (color == "black" || color == "blue" || color == "cyan" - || color == "green" || color == "magenta" || color == "red" - || color == "white" || color == "yellow") { + if (is_known(color, known_basic_colors)) { context.check_layout(os); os << "\n\\color " << color << "\n"; parse_text_snippet(p, os, FLAG_ITEM, outer, context); context.check_layout(os); os << "\n\\color inherit\n"; preamble.registerAutomaticallyLoadedPackage("color"); - } else if (color == "brown" || color == "darkgray" || color == "gray" - || color == "lightgray" || color == "lime" || color == "olive" - || color == "orange" || color == "pink" || color == "purple" - || color == "teal" || color == "violet") { + } else if (is_known(color, known_basic_xcolors)) { context.check_layout(os); os << "\n\\color " << color << "\n"; parse_text_snippet(p, os, FLAG_ITEM, outer, context); -- lyx-cvs mailing list lyx-cvs@lists.lyx.org https://lists.lyx.org/mailman/listinfo/lyx-cvs