xBrowse a column containing richedit

User avatar
MarcoBoschi
Posts: 925
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy
Contact:

xBrowse a column containing richedit

Post by MarcoBoschi »

Hi,
I Have a column in a xbrowse list that refers to a memo field containing a richedit text.

oCol = oBrw:AddCol()
oCol:bStrData = { || note->nota }
oCol:cHeader = "Nota"
oCol:nEditType = EDIT_GET
oCol:nWidth = 200

note->nota contains a string like this
===========================
{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fswiss\fprq2\fcharset0 Arial;}{\f1\fnil\fcharset0 Arial;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\cf1\b\i\f0\fs36 Ciao marco\cf0\b0\i0\f1\fs20\par
}
============================

How can I extract pure text of this memo field?

Thanks in advance

Marco
Marco Boschi
info@marcoboschi.it
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: xBrowse a column containing richedit

Post by nageswaragunupudi »

I am also interested in a function like Rtf2Text(). I started coding a straight function but I haven't completed it. I shall post it again after doing some more work.

Meanwhile I can suggest a function using a hidden richedit control. Can you please help by testing this function?

Code: Select all

function Rtf2text( cRtf )

   static hDLL, oWnd, oRich
   local  cText

   if ValType( cRtf ) == 'C'
      if hDLL == nil
         hDLL     := LoadLibrary( "riched20.dll" )
         DEFINE WINDOW oWnd
         @ 0,0 RICHEDIT oRich VAR cRtf OF oWnd SIZE 50,50 PIXEL
         cText    := oRich:GetText()
         ACTIVATE WINDOW oWnd HIDDEN
      else
         oRich:LoadRTF( cRTF )
         cText    := oRich:GetText()
      endif
   else
      if hDLL != nil
         oWnd:End()
         oWnd     := nil
         oRich    := nil
         FreeLibrary( hDLL )
         hDLL     := nil
      endif
   endif

return cText

//----------------------------------------------------------------------------//
 
After done finally call Rtf2Text() without any parameters to close the window and DLL.

I would be glad to know the test results,

Meanwhile if any one can provide a direct function to strip of all rtf codes, that would be greatly welcome.
Regards

G. N. Rao.
Hyderabad, India
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: xBrowse a column containing richedit

Post by reinaldocrespo »

The only drawback of MR. Rao's suggestion is that you must create the richtext control in order to extract the text. In my case I was having to report on thousands of records containing richtext. So I wrote a very short and simple function to extract only text from richtext using a regular expression.

Here is the code:

Code: Select all

*---------------------------------------------------------------------------------------------
#DEFINE _cREGEX "\{?\\([a-z]{1,32}[0-9]* ?)([A-z, ]*;)?|}"   //includes fonts

function GetTextFromRTF( cRtfText, lKeepCRLFs )
local aRet
local cStrip, i

   DEFAULT cRtfText := ""
   DEFAULT lKeepCRLFs := .t.

   aRet := hb_RegExAll( _cREGEX, cRtfText, .f. )

   if aRet == Nil    ;return cRtfText    ;endif

   aSort( aRet,,, { |x,y| len( x[1] ) > len( y[1] ) } )
   for i := 1 to len( aRet )

      cRtfText := StrTran( cRtfText, aRet[ i, 1 ], "" )

   Next

   if !lKeepCRLFs
      cRtfText := StrTran( cRtfText, CRLF, " " )
   endif

Return cRtfText

 

Hope that helps,


Reinaldo.
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: xBrowse a column containing richedit

Post by nageswaragunupudi »

Mr. Reinaldocrespo

Thank you very much. This is the kind of function I needed too.
Yes I agree with your comments about the disadvantage of creating control.
Regards

G. N. Rao.
Hyderabad, India
User avatar
MarcoBoschi
Posts: 925
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy
Contact:

Re: xBrowse a column containing richedit

Post by MarcoBoschi »

Reinaldo,

Your function is very good but ther is a little problem.

If I pass a string containing one of these characters "àèéìòù"

for instance the days of week in Italian :

"Lunedì
Martedì
Mercoledì
Giovedì
Venerdì
"

I have these string as return value
Marc\'e0 Marc\'e8 Marc\'ec Marc\'f2 Marc\'f9

nageswaragunupudi,
your function works very well.
But I put this function in a column of a listbox to display the content of a memo field containing a richedit text.
When I test it with larger table I can tell you if it works without problem

Thanks to all
marco
Marco Boschi
info@marcoboschi.it
User avatar
Silvio
Posts: 3107
Joined: Fri Oct 07, 2005 6:28 pm
Location: Teramo,Italy

Re: xBrowse a column containing richedit

Post by Silvio »

Marco Boschi,
can you send me a test sample with this feature please , ? I'd like try it
Best Regards, Saludos

Falconi Silvio
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: xBrowse a column containing richedit

Post by nageswaragunupudi »

Mr Marco

I don't think my method would be good in case of large tables. Function posted by Mr. Reinaldo is the way to go. Though this function has limitations as of now, we can improve it. I myself need a good function for this purpose. When I get time I try to improve that function solving some of the problems I already noticed.
Regards

G. N. Rao.
Hyderabad, India
User avatar
MarcoBoschi
Posts: 925
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy
Contact:

Re: xBrowse a column containing richedit

Post by MarcoBoschi »

nageswaragunupudi,
I confirm the problem when in the memo field there are words containing these character
widely used in italian language àèéìòù

Lunedì is Monday
Martedì is etc. etc.


Bye
I hope that reinaldo read this post
Marco Boschi
info@marcoboschi.it
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: xBrowse a column containing richedit

Post by reinaldocrespo »

Marco;

I apologize for being so busy. Please send me a rich edit memotext I can do tests with.

Send me for example something like:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 ARIAL;}}
\viewkind4\uc1\pard\f0\fs20 The specimen is received fresh, wrapped in plastic, is labeled "LEFT LEG BELOW AMPUTATION" and consists of a below the knee amputation specimen measuring 30 cm from tibial amputation site to medial malleolus. The specimen is notable for gangrenous ulcers involving all toes and anterior aspect of the dorsum of the foot. The proximal soft tissue is beefy red and viable. Sections through the vessels show atheromatous plaque. (RSS 1 CASS) VCV\par
\pard\fs20\par
}
BTW and FYI - all the function does is remove all instances of richtext coded tags from the memo field sent as parameter returning everything else. For more information on regular expressions you can search the web. It is a good idea to become acquainted with regex as they are a powerful and very fast way to parse text for anything.

Again, please send me some sample richtext that does not work at your end and I will gladly test it against the function here.

Best regards,



Reinaldo.
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: xBrowse a column containing richedit

Post by nageswaragunupudi »

I have problems with the following files in \fwh\samples folder.

MemoEdit( GetTextFromRTF( MemoRead( "testrtf.rtf" ) ) )
MemoEdit( GetTextFromRTF( MemoRead( "fiveodbc.rtf" ) ) )
Regards

G. N. Rao.
Hyderabad, India
User avatar
MarcoBoschi
Posts: 925
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy
Contact:

Re: xBrowse a column containing richedit

Post by MarcoBoschi »

{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Arial;}}
\viewkind4\uc1\pard\fs20 Oggi \'e8 marted\'ec\par
domani \'e8 mercoled\'ec\par
dopodomani gioved\'ec\par
che ora \'e8\par
Come sar\'e0 il tempo domani?\par
}

àèéìòù

Reinaldo: too kind , no hurry
Marco Boschi
info@marcoboschi.it
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: xBrowse a column containing richedit

Post by reinaldocrespo »

Marco;

I see your point. I wasn't aware of how richtext implemented international characters, but I find a simple solution without having to re-touch the regular expression. Change the function to this:

Code: Select all

function GetTextFromRTF( cRtfText, lKeepCRLFs )
local aRet
local cStrip, i

   DEFAULT cRtfText := ""
   DEFAULT lKeepCRLFs := .t.

   aRet := hb_RegExAll( _cREGEX, cRtfText, .f. )

   if aRet == Nil    ;return cRtfText    ;endif

   aSort( aRet,,, { |x,y| len( x[1] ) > len( y[1] ) } )
   for i := 1 to len( aRet )

      cRtfText := StrTran( cRtfText, aRet[ i, 1 ], "" )

   Next

   if !lKeepCRLFs
      cRtfText := StrTran( cRtfText, CRLF, " " )
   endif

    //International chars
    cRtfText := StrTran( cRtfText, "\'ec", chr(141) )   //inverted accented i
    cRtfText := StrTran( cRtfText, "\'e8", chr(138) )   //inverted accented e
    cRtfText := StrTran( cRtfText, "\'e0", chr(133) )   //inverted accented a
    
Return cRtfText

 
It should work.

For Mr. Rao, I find that my windows 7 wordpad does not want to open testrtf.rtf. In fact, my windows installation will NOT open that file. But it does open fiveodbc.rtf. So I will work with fiveodbc.rtf as soon as I get a chance.


Reinaldo.
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: xBrowse a column containing richedit

Post by nageswaragunupudi »

Mr. Reinaldo

The function posted by you does most of the work and works well on most normal rich text. It is just that it needs to be improved a bit further.

I can open testrtf.rtf in Word 2007, WordPad and FWH RichText control on my Windows 7 (32-bit).

Image

What distinguishes these two files from other normal richtext files is that testrtf.rtf contains image and fiveodbc.rtf contains a table. We need to include logic to handle these features in rtf.
Regards

G. N. Rao.
Hyderabad, India
User avatar
MarcoBoschi
Posts: 925
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy
Contact:

Re: xBrowse a column containing richedit

Post by MarcoBoschi »

Reinaldo,
with this sequence of replace the problem is solved!

cRtfText := StrTran( cRtfText, "\'e0", "à" )
cRtfText := StrTran( cRtfText, "\'e8", "è" )
cRtfText := StrTran( cRtfText, "\'e9", "é" )
cRtfText := StrTran( cRtfText, "\'ec", "ì" )
cRtfText := StrTran( cRtfText, "\'f2", "ò" )
cRtfText := StrTran( cRtfText, "\'f9", "ù" )


Many Thanks

Marco
Marco Boschi
info@marcoboschi.it
User avatar
reinaldocrespo
Posts: 918
Joined: Thu Nov 17, 2005 5:49 pm
Location: Fort Lauderdale, FL

Re: xBrowse a column containing richedit

Post by reinaldocrespo »

Marco -Good!

Mr. Rao. Good morning. I see the problem you are pointing to. There is no easy fix. I would have to revisit my regular expression which is already a bit cryptic. I must take care of some pressing issues today. I'm thinking perhaps tomorrow I should be able to spend time rewriting the regular expression in order to catch imbedded tables and images. Another possibility would be to use a lex or yacc utility as means to catch streams of data delimited by tags.


Reinaldo.
Post Reply