Fwd: selection of justified text

David Faure faure at kde.org
Mon Dec 2 00:56:18 GMT 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

This patch from Pascal Letourneau addresses the rendering
of selected text when the text is justified. He mentions that it has to be
checked for RTL languages, and that the patch should also fix some
problems with underlined links (in justified text), etc.

I know nothing about that code - can someone who does, comment on it?

- ----------  Forwarded Message  ----------

Subject: Re: sélection dans khtml
Date: Monday 04 November 2002 21:09
From: Pascal LĂ©tourneau <pletourn at globetrotter.net>
To: David Faure <faure at kde.org>

Index: font.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/rendering/font.cpp,v
retrieving revision 1.16
diff -u -3 -p -r1.16 font.cpp
- --- font.cpp    2002/10/21 22:04:14     1.16
+++ font.cpp    2002/11/04 19:51:43
@@ -93,6 +93,42 @@ void Font::drawText( QPainter *p, int x,
     }
 }

+int Font::offset( QChar *str, int slen, int pos, int len, int width, int toAdd ) const
+{
+    kdDebug() << "           slen=" <<  slen << " pos=" << pos << " len=" << len << " width=" << width << " toAdd=" << toAdd << endl;
+    QString qstr = QConstString( str, slen ).string();
+    int numSpaces = 0;
+    int x = 0;
+    if ( toAdd ) {
+        for( int i = 0; i < len; i++ )
+           if ( str[i+pos].direction() == QChar::DirWS )
+              numSpaces++;
+    }
+    kdDebug() << "           numSpaces=" << numSpaces << endl;
+    for( int i = 0; i < len; i++ ) {
+        int chw = fm.charWidth( qstr, pos+i );
+        if ( letterSpacing )
+           chw += letterSpacing;
+        if ( (wordSpacing || toAdd) && str[i+pos].isSpace() ) {
+           chw += wordSpacing;
+           if ( numSpaces ) {
+               int a = toAdd/numSpaces;
+               chw += a;
+               toAdd -= a;
+               numSpaces--;
+           }
+       }
+        int chw2 = chw/2;
+       chw -= chw2;
+
+       x += chw;
+       if ( x >= width )
+           return i;
+       x += chw2;
+       kdDebug() << "          x=" << x << " char=" << QString( qstr[ pos+i ] ) << endl;
+    }
+    return len - 1;
+}

 int Font::width( QChar *chs, int, int pos, int len ) const
 {
Index: font.h
===================================================================
RCS file: /home/kde/kdelibs/khtml/rendering/font.h,v
retrieving revision 1.8
diff -u -3 -p -r1.8 font.h
- --- font.h      2002/03/22 17:04:14     1.8
+++ font.h      2002/11/04 19:51:43
@@ -82,6 +82,8 @@ public:
     void drawText( QPainter *p, int x, int y, QChar *str, int slen, int pos, int len, int width,
                    QPainter::TextDirection d, int from=-1, int to=-1, QColor bg=QColor() ) const;

+    int offset( QChar *str, int slen, int pos, int len, int width, int toAdd ) const;
+
     int width( QChar *str, int slen, int pos, int len ) const;
     int width( QChar *str, int slen, int pos ) const;

Index: render_text.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/rendering/render_text.cpp,v
retrieving revision 1.194
diff -u -3 -p -r1.194 render_text.cpp
- --- render_text.cpp     2002/10/27 02:21:35     1.194
+++ render_text.cpp     2002/11/04 19:51:45
@@ -145,32 +145,12 @@ FindSelectionResult TextSlave::checkSele
     }

     int delta = _x - (_tx + m_x);
- -    //kdDebug(6040) << "TextSlave::checkSelectionPoint delta=" << delta << endl;
- -    int pos = 0;
- -    if ( m_reversed ) {
+    if ( m_reversed )
        delta -= m_width;
- -       while(pos < m_len) {
- -           int w = f->width( text->str->s, text->str->l, m_start + pos);
- -           int w2 = w/2;
- -           w -= w2;
- -           delta += w2;
- -           if(delta >= 0) break;
- -           pos++;
- -           delta += w;
- -       }
- -    } else {
- -       while(pos < m_len) {
- -           int w = f->width( text->str->s, text->str->l, m_start + pos);
- -           int w2 = w/2;
- -           w -= w2;
- -           delta -= w2;
- -           if(delta <= 0) break;
- -           pos++;
- -           delta -= w;
- -       }
- -    }
+
+    //kdDebug(6040) << "TextSlave::checkSelectionPoint delta=" << delta << endl;
+    offset = f->offset( text->str->s, text->str->l, m_start, m_len, delta, m_toAdd );
 //     kdDebug( 6040 ) << " Text  --> inside at position " << pos << endl;
- -    offset = pos;
     return SelectionPointInside;
 }

[pletourn 14:51:35 kdelibs/khtml/rendering]$ vim font.cpp
[pletourn 15:06:27 kdelibs/khtml/rendering]$ cvs diff font.* render_text.*
Index: font.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/rendering/font.cpp,v
retrieving revision 1.16
diff -u -3 -p -r1.16 font.cpp
- --- font.cpp    2002/10/21 22:04:14     1.16
+++ font.cpp    2002/11/04 20:06:55
@@ -93,6 +93,39 @@ void Font::drawText( QPainter *p, int x,
     }
 }

+int Font::offset( QChar *str, int slen, int pos, int len, int width, int toAdd ) const
+{
+    QString qstr = QConstString( str, slen ).string();
+    int numSpaces = 0;
+    int x = 0;
+    if ( toAdd ) {
+        for( int i = 0; i < len; i++ )
+           if ( str[i+pos].direction() == QChar::DirWS )
+              numSpaces++;
+    }
+    for( int i = 0; i < len; i++ ) {
+        int chw = fm.charWidth( qstr, pos+i );
+        if ( letterSpacing )
+           chw += letterSpacing;
+        if ( (wordSpacing || toAdd) && str[i+pos].isSpace() ) {
+           chw += wordSpacing;
+           if ( numSpaces ) {
+               int a = toAdd/numSpaces;
+               chw += a;
+               toAdd -= a;
+               numSpaces--;
+           }
+       }
+        int chw2 = chw/2;
+       chw -= chw2;
+
+       x += chw;
+       if ( x >= width )
+           return i;
+       x += chw2;
+    }
+    return len - 1;
+}

 int Font::width( QChar *chs, int, int pos, int len ) const
 {
Index: font.h
===================================================================
RCS file: /home/kde/kdelibs/khtml/rendering/font.h,v
retrieving revision 1.8
diff -u -3 -p -r1.8 font.h
- --- font.h      2002/03/22 17:04:14     1.8
+++ font.h      2002/11/04 20:06:55
@@ -82,6 +82,8 @@ public:
     void drawText( QPainter *p, int x, int y, QChar *str, int slen, int pos, 
int len, int width,
                    QPainter::TextDirection d, int from=-1, int to=-1, QColor bg=QColor() ) const;

+    int offset( QChar *str, int slen, int pos, int len, int width, int toAdd ) const;
+
     int width( QChar *str, int slen, int pos, int len ) const;
     int width( QChar *str, int slen, int pos ) const;

Index: render_text.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/rendering/render_text.cpp,v
retrieving revision 1.194
diff -u -3 -p -r1.194 render_text.cpp
- --- render_text.cpp     2002/10/27 02:21:35     1.194
+++ render_text.cpp     2002/11/04 20:06:57
@@ -145,32 +145,12 @@ FindSelectionResult TextSlave::checkSele
     }

     int delta = _x - (_tx + m_x);
- -    //kdDebug(6040) << "TextSlave::checkSelectionPoint delta=" << delta << endl;
- -    int pos = 0;
- -    if ( m_reversed ) {
+    if ( m_reversed )
        delta -= m_width;
- -       while(pos < m_len) {
- -           int w = f->width( text->str->s, text->str->l, m_start + pos);
- -           int w2 = w/2;
- -           w -= w2;
- -           delta += w2;
- -           if(delta >= 0) break;
- -           pos++;
- -           delta += w;
- -       }
- -    } else {
- -       while(pos < m_len) {
- -           int w = f->width( text->str->s, text->str->l, m_start + pos);
- -           int w2 = w/2;
- -           w -= w2;
- -           delta -= w2;
- -           if(delta <= 0) break;
- -           pos++;
- -           delta -= w;
- -       }
- -    }
+
+    //kdDebug(6040) << "TextSlave::checkSelectionPoint delta=" << delta << endl;
+    offset = f->offset( text->str->s, text->str->l, m_start, m_len, delta, m_toAdd );
 //     kdDebug( 6040 ) << " Text  --> inside at position " << pos << endl;
- -    offset = pos;
     return SelectionPointInside;
 }

Index: render_text.h
===================================================================
RCS file: /home/kde/kdelibs/khtml/rendering/render_text.h,v
retrieving revision 1.76
diff -u -3 -p -r1.76 render_text.h
- --- render_text.h       2002/09/19 12:22:54     1.76
+++ render_text.h       2002/11/04 20:06:58
@@ -52,7 +52,7 @@ public:
         m_start = start;
         m_len = len;
         m_baseline = baseline;
- -        m_width = width;
+        m_width = width + toAdd;
         m_reversed = reversed;
         m_firstLine = firstLine;
         m_toAdd = toAdd;



- -------------------------------------------------------

- -- 
David FAURE, david at mandrakesoft.com, faure at kde.org
http://people.mandrakesoft.com/~david/
Contributing to: http://www.konqueror.org/, http://www.koffice.org/
Get the latest KOffice - http://download.kde.org/stable/koffice-1.2/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE96q+z72KcVAmwbhARAneLAJ4+vGoixqxh30YL2i3hKZT6l7Xr5ACgg6z+
7xISgoRKSx60z8KQu2Oq8QE=
=88aE
-----END PGP SIGNATURE-----





More information about the kfm-devel mailing list