On Wed, Oct 10, 2007 at 12:07:51AM -0500, Bo Peng wrote:
> > The first patch implements the ideas above (without ranges) and
> > works perfectly. You can even mark as greek the chars and get the
> > same behaviour as before.
> 
> I just tested this patch and it works as expected.

Please, try the attached. It should be fully working with ranges
and font attribute changes.

-- 
Enrico
Index: src/Paragraph.cpp
===================================================================
--- src/Paragraph.cpp   (revisione 20890)
+++ src/Paragraph.cpp   (copia locale)
@@ -66,8 +66,10 @@
 namespace lyx {
 
 using support::contains;
+using support::prefixIs;
 using support::suffixIs;
 using support::rsplit;
+using support::rtrim;
 
 
 /////////////////////////////////////////////////////////////////////
@@ -187,6 +189,10 @@
                             unsigned int & column,
                             Font const & font,
                             Layout const & style);
+       /// Output consecutive greek chars starting from \p c to \p os.
+       /// \return the number of characters written.
+       int greekChars(odocstream & os, value_type c, Encoding const &,
+                      pos_type &);
        ///
        void simpleTeXSpecialChars(Buffer const &, BufferParams const &,
                                   odocstream &,
@@ -627,6 +633,54 @@
 }
 
 
+int Paragraph::Pimpl::greekChars(odocstream & os, value_type c,
+                                Encoding const & encoding, pos_type & i)
+{
+       // The latex command is "\textgreek{x}" and we have to retain
+       // "\textgreek{x" for the first char but only "x" for all subsequent
+       // chars (this also works when we are passed untranslated unicode).
+       docstring const latex1 = rtrim(encoding.latexChar(c), "}");
+       int length = latex1.length();
+       os << latex1;
+       while (i < size() - 1) {
+               char_type next = getChar(i + 1);
+               if (Encodings::isGreekChar(next)) {
+                       Font prev_font;
+                       bool found = false;
+                       FontList::const_iterator cit = fontlist.begin();
+                       FontList::const_iterator end = fontlist.end();
+                       for (; cit != end; ++cit) {
+                               if (cit->pos() >= i && !found) {
+                                       prev_font = cit->font();
+                                       found = true;
+                               }
+                               if (cit->pos() >= i + 1)
+                                       break;
+                       }
+                       if (found && cit != end && prev_font != cit->font())
+                               break;
+                       docstring const latex = encoding.latexChar(next);
+                       docstring::size_type const j =
+                               latex.find_first_of(from_ascii("{"));
+                       if (j == docstring::npos)
+                               os << latex.substr(0, 1);
+                       else
+                               os << latex.substr(j + 1, 1);
+                       ++length;
+                       ++i;
+               } else
+                       break;
+       }
+       // When the language is Greek, we are passed the stright unicode,
+       // so we should not try to close the \textgreek command.
+       if (prefixIs(latex1, from_ascii("\\textgreek"))) {
+               os << '}';
+               ++length;
+       }
+       return length;
+}
+
+
 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
 {
        pos_type const len = str.length();
@@ -952,6 +1006,10 @@
                                                break;
                                        }
                                }
+                               if (Encodings::isGreekChar(c)) {
+                                       column += greekChars(os, c, encoding, 
i) - 1;
+                                       break;
+                               }
                                docstring const latex = encoding.latexChar(c);
                                if (latex.length() > 1 &&
                                    latex[latex.length() - 1] != '}') {
Index: src/LaTeXFeatures.cpp
===================================================================
--- src/LaTeXFeatures.cpp       (revisione 20890)
+++ src/LaTeXFeatures.cpp       (copia locale)
@@ -186,7 +186,14 @@
        "\\newcommand{\\lyxadded}[3]{#3}\n"
        "\\newcommand{\\lyxdeleted}[3]{}\n";
 
+static string const textgreek_def =
+       "\\DeclareRobustCommand{\\greektext}{%\n"
+       " \\fontencoding{LGR}\\selectfont\n"
+       " \\def\\encodingdefault{LGR}}\n"
+       "\\DeclareRobustCommand{\\textgreek}[1]{\\leavevmode{\\greektext #1}}\n"
+       "\\DeclareFontEncoding{LGR}{}{}\n";
 
+
 /////////////////////////////////////////////////////////////////////
 //
 // LaTeXFeatures
@@ -618,6 +625,9 @@
        if (mustProvide("lyxarrow"))
                macros << lyxarrow_def << '\n';
 
+       if (mustProvide("textgreek"))
+               macros << textgreek_def << '\n';
+
        // quotes.
        if (mustProvide("quotesinglbase"))
                macros << quotesinglbase_def << '\n';
Index: src/Encoding.cpp
===================================================================
--- src/Encoding.cpp    (revisione 20890)
+++ src/Encoding.cpp    (copia locale)
@@ -401,6 +401,15 @@
 }
 
 
+bool Encodings::isGreekChar(char_type c)
+{
+       CharInfoMap::const_iterator const it = unicodesymbols.find(c);
+       if (it != unicodesymbols.end())
+               return it->second.preamble == "textgreek";
+       return false;
+}
+
+
 Encoding const * Encodings::getFromLyXName(string const & name) const
 {
        EncodingList::const_iterator it = encodinglist.find(name);
Index: src/Encoding.h
===================================================================
--- src/Encoding.h      (revisione 20890)
+++ src/Encoding.h      (copia locale)
@@ -142,6 +142,8 @@
        static char_type transformChar(char_type c, Letter_Form form);
        /// Is this a combining char?
        static bool isCombiningChar(char_type c);
+       /// Is this a greek char?
+       static bool isGreekChar(char_type c);
        /**
         * Add the preamble snippet needed for the output of \p c to
         * \p features.
Index: lib/unicodesymbols
===================================================================
--- lib/unicodesymbols  (revisione 20890)
+++ lib/unicodesymbols  (copia locale)
@@ -684,6 +684,55 @@
 #0x036d ""                         "" "combining" # COMBINING LATIN SMALL 
LETTER T
 #0x036e ""                         "" "combining" # COMBINING LATIN SMALL 
LETTER V
 #0x036f ""                         "" "combining" # COMBINING LATIN SMALL 
LETTER X
+0x0391 "\\textgreek{A}"           "textgreek" "" # GREEK CAPITAL LETTER ALPHA
+0x0392 "\\textgreek{B}"           "textgreek" "" # GREEK CAPITAL LETTER BETA
+0x0393 "\\textgreek{G}"           "textgreek" "" # GREEK CAPITAL LETTER GAMMA
+0x0394 "\\textgreek{D}"           "textgreek" "" # GREEK CAPITAL LETTER DELTA
+0x0395 "\\textgreek{E}"           "textgreek" "" # GREEK CAPITAL LETTER EPSILON
+0x0396 "\\textgreek{Z}"           "textgreek" "" # GREEK CAPITAL LETTER ZETA
+0x0397 "\\textgreek{H}"           "textgreek" "" # GREEK CAPITAL LETTER ETA
+0x0398 "\\textgreek{J}"           "textgreek" "" # GREEK CAPITAL LETTER THETA
+0x0399 "\\textgreek{I}"           "textgreek" "" # GREEK CAPITAL LETTER IOTA
+0x039a "\\textgreek{K}"           "textgreek" "" # GREEK CAPITAL LETTER KAPPA
+0x039b "\\textgreek{L}"           "textgreek" "" # GREEK CAPITAL LETTER LAMDA
+0x039c "\\textgreek{M}"           "textgreek" "" # GREEK CAPITAL LETTER MU
+0x039d "\\textgreek{N}"           "textgreek" "" # GREEK CAPITAL LETTER NU
+0x039e "\\textgreek{X}"           "textgreek" "" # GREEK CAPITAL LETTER XI
+0x039f "\\textgreek{O}"           "textgreek" "" # GREEK CAPITAL LETTER OMICRON
+0x03a0 "\\textgreek{P}"           "textgreek" "" # GREEK CAPITAL LETTER PI
+0x03a1 "\\textgreek{R}"           "textgreek" "" # GREEK CAPITAL LETTER RHO
+0x03a3 "\\textgreek{S}"           "textgreek" "" # GREEK CAPITAL LETTER SIGMA
+0x03a4 "\\textgreek{T}"           "textgreek" "" # GREEK CAPITAL LETTER TAU
+0x03a5 "\\textgreek{U}"           "textgreek" "" # GREEK CAPITAL LETTER UPSILON
+0x03a6 "\\textgreek{F}"           "textgreek" "" # GREEK CAPITAL LETTER PHI
+0x03a7 "\\textgreek{Q}"           "textgreek" "" # GREEK CAPITAL LETTER CHI
+0x03a8 "\\textgreek{Y}"           "textgreek" "" # GREEK CAPITAL LETTER PSI
+0x03a9 "\\textgreek{W}"           "textgreek" "" # GREEK CAPITAL LETTER OMEGA
+0x03b1 "\\textgreek{a}"           "textgreek" "" # GREEK SMALL LETTER ALPHA
+0x03b2 "\\textgreek{b}"           "textgreek" "" # GREEK SMALL LETTER BETA
+0x03b3 "\\textgreek{g}"           "textgreek" "" # GREEK SMALL LETTER GAMMA
+0x03b4 "\\textgreek{d}"           "textgreek" "" # GREEK SMALL LETTER DELTA
+0x03b5 "\\textgreek{e}"           "textgreek" "" # GREEK SMALL LETTER EPSILON
+0x03b6 "\\textgreek{z}"           "textgreek" "" # GREEK SMALL LETTER ZETA
+0x03b7 "\\textgreek{h}"           "textgreek" "" # GREEK SMALL LETTER ETA
+0x03b8 "\\textgreek{j}"           "textgreek" "" # GREEK SMALL LETTER THETA
+0x03b9 "\\textgreek{i}"           "textgreek" "" # GREEK SMALL LETTER IOTA
+0x03ba "\\textgreek{k}"           "textgreek" "" # GREEK SMALL LETTER KAPPA
+0x03bb "\\textgreek{l}"           "textgreek" "" # GREEK SMALL LETTER LAMDA
+0x03bc "\\textgreek{m}"           "textgreek" "" # GREEK SMALL LETTER MU
+0x03bd "\\textgreek{n}"           "textgreek" "" # GREEK SMALL LETTER NU
+0x03be "\\textgreek{x}"           "textgreek" "" # GREEK SMALL LETTER XI
+0x03bf "\\textgreek{o}"           "textgreek" "" # GREEK SMALL LETTER OMICRON
+0x03c0 "\\textgreek{p}"           "textgreek" "" # GREEK SMALL LETTER PI
+0x03c1 "\\textgreek{r}"           "textgreek" "" # GREEK SMALL LETTER RHO
+0x03c2 "\\textgreek{c}"           "textgreek" "" # GREEK SMALL LETTER FINAL 
SIGMA
+0x03c3 "\\textgreek{s}"           "textgreek" "" # GREEK SMALL LETTER SIGMA
+0x03c4 "\\textgreek{t}"           "textgreek" "" # GREEK SMALL LETTER TAU
+0x03c5 "\\textgreek{u}"           "textgreek" "" # GREEK SMALL LETTER UPSILON
+0x03c6 "\\textgreek{f}"           "textgreek" "" # GREEK SMALL LETTER PHI
+0x03c7 "\\textgreek{q}"           "textgreek" "" # GREEK SMALL LETTER CHI
+0x03c8 "\\textgreek{y}"           "textgreek" "" # GREEK SMALL LETTER PSI
+0x03c9 "\\textgreek{w}"           "textgreek" "" # GREEK SMALL LETTER OMEGA
 0x0e3f "\\textbaht"               "textcomp" "" # THAI CURRENCY SYMBOL BAHT
 0x1e00 "\\textsubring{A}"         "tipa" "" # LATIN CAPITAL LETTER A WITH RING 
BELOW
 0x1e01 "\\textsubring{a}"         "tipa" "" # LATIN SMALL LETTER A WITH RING 
BELOW

Reply via email to