[Kst] extragear/graphics/kst/src

George Staikos staikos at kde.org
Fri May 12 10:37:05 CEST 2006


SVN commit 539987 by staikos:


Implement \textcolor{color}{text}.  The color can be one of:
1) An entry from X11 rgb.txt
2) #RRGGBB hex

RR,GG,BB is unimplemented but will come one day.

This varies from latex significantly, but I think it's more practical for us.


 M  +5 -0      libkstapp/labelrenderer.cpp  
 M  +33 -5     libkstmath/labelparser.cpp  
 M  +4 -1      libkstmath/labelparser.h  


--- trunk/extragear/graphics/kst/src/libkstapp/labelrenderer.cpp #539986:539987
@@ -55,6 +55,11 @@
     f.setBold(fi->attributes.bold);
     f.setItalic(fi->attributes.italic);
     f.setUnderline(fi->attributes.underline);
+    if (rc.p && fi->attributes.color.isValid()) {
+      rc.p->setPen(fi->attributes.color);
+    } else if (rc.p) {
+      rc.p->setPen(QColor(0, 0, 0));
+    }
     rc.setFont(f);
 
     if (fi->linebreak) {
--- trunk/extragear/graphics/kst/src/libkstmath/labelparser.cpp #539986:539987
@@ -18,6 +18,7 @@
 #include "labelparser.h"
 
 #include <assert.h>
+#include <stdlib.h>
 
 #include <qstring.h>
 
@@ -112,6 +113,23 @@
 }
 
 
+inline QColor parseColor(const QString& txt, int *skip) {
+  const int end = txt.find('}');
+  if (skip) {
+    *skip = end;
+  }
+  if (end == -1) {
+    return QColor();
+  }
+  if (txt[0].isNumber()) {
+    abort(); // FIXME: implement RR,GG,BB hex
+  } else if (txt[0].isLetter() || txt[0] == '#') {
+    return QColor(txt.left(end)); // Use the X11 rgb database, or #RRGGBB
+  }
+  return QColor();
+}
+
+
 static Chunk *parseInternal(Chunk *ctail, const QString& txt, uint& start, uint cnt, bool interpretNewLine);
 
 #define EXPAND_GREEK(L_U, L_L, REST, SKIP, UCODE)    \
@@ -311,12 +329,22 @@
     case 't':
       x = 0x20;
     case 'T':
-      if (txt.mid(from + 1).startsWith("extcolor")) { // \textcolor{color}{text}
-        *skip = 9;
-        if (!*tail || !(*tail)->text.isEmpty() || (*tail)->locked() || !(*tail)->attributes.empty()) {
-          *tail = new Chunk(*tail, Chunk::None, true, true);
+      if (txt.mid(from + 1).startsWith("extcolor{")) { // \textcolor{color}{text}
+        if ((*tail)->group) {
+          *tail = new Chunk(*tail, Chunk::None, false, true);
         }
-        // FIXME: parse out the color
+        Chunk *working = new Chunk(*tail, Chunk::None, true, true);
+        dumpattr(working, "start group for textcolor");
+        uint parseStart = from + 10;
+        int firstSkip = 0;
+        working->attributes.color = parseColor(txt.mid(parseStart), &firstSkip);
+        if (!working->attributes.color.isValid() || txt[parseStart + firstSkip + 1] != '{') {
+          return false;
+        }
+        parseStart += firstSkip + 1;
+        parseInternal(working, txt, parseStart, txt.length(), interpretNewLine);
+        *skip = parseStart - from + 1;
+        dumpattr(working, "end group for textcolor");
         return true;
       } else if (txt.mid(from + 1).startsWith("extbf{")) {
         if ((*tail)->group) {
--- trunk/extragear/graphics/kst/src/libkstmath/labelparser.h #539986:539987
@@ -22,6 +22,8 @@
 #include "kststring.h"
 #include "kst_export.h"
 
+#include <qcolor.h>
+
 typedef Q_UINT16 KstLJustifyType;
 typedef Q_UINT8  KstLHJustifyType;
 typedef Q_UINT8  KstLVJustifyType;
@@ -43,10 +45,11 @@
 namespace Label {
   struct KST_EXPORT ChunkAttributes {
     ChunkAttributes() : bold(false), italic(false), underline(false) {}
-    inline bool empty() const { return !bold && !italic && !underline; }
+    inline bool empty() const { return !bold && !italic && !underline && !color.isValid(); }
     bool bold;
     bool italic;
     bool underline;
+    QColor color;
   };
 
   struct KST_EXPORT Chunk {


More information about the Kst mailing list