Board index » delphi » WrapText with TStream

WrapText with TStream

Is there a function like WrapText for a TStream? Needs to be quick so
copying the stream to string and back again won't do.

Thanks
Andrew O
http://www.freesoft.fsnet.co.uk

 

Re:WrapText with TStream


Quote
"Andrew" <a...@freesoft.fsnet.co.uk> wrote in message

news:9ivd7f$ru0$1@news8.svr.pol.co.uk...

Quote
> Is there a function like WrapText for a TStream? Needs to be quick so
> copying the stream to string and back again won't do.

No. You'll need to write one and that will likely involve copying the stream
contents.

Re:WrapText with TStream


In article <9ivd7f$ru...@news8.svr.pol.co.uk>, "Andrew"

Quote
<a...@freesoft.fsnet.co.uk> writes:
>Is there a function like WrapText for a TStream? Needs to be quick so
>copying the stream to string and back again won't do.

Use the windows' API DrawTextEx, but it would have to be in a memory stream or
be copied from another stream to one to be able to supply a PChar for its
operation.

Line-breaking is fairly processor intensive however you do it, and I would
guess that stream copying would not be significant in that context.

Alan Lloyd
alangll...@aol.com

Re:WrapText with TStream


Quote
"AlanGLLoyd" <alangll...@aol.com> wrote in message

news:20010717021335.22572.00001161@nso-fq.aol.com...

Quote
> Use the windows' API DrawTextEx, but it would have to be in a memory
stream or
> be copied from another stream to one to be able to supply a PChar for its
> operation.

Sorry, I think I gave you the wrong impression, I actually want the breaks
placed inside the stream.

Thanks for you reply anyway
Andrew O
http://www.freesoft.fsnet.co.uk

Re:WrapText with TStream


"Andrew" <a...@freesoft.fsnet.co.uk> skrev i melding
news:9j2943$efb$1@newsg2.svr.pol.co.uk...

Quote
> "AlanGLLoyd" <alangll...@aol.com> wrote in message
> news:20010717021335.22572.00001161@nso-fq.aol.com...
> > Use the windows' API DrawTextEx, but it would have to be in a memory
> stream or
> > be copied from another stream to one to be able to supply a PChar for its
> > operation.

> Sorry, I think I gave you the wrong impression, I actually want the breaks
> placed inside the stream.

Now listen -  a Stream is a range of data in this case, a string. It better
be loaded into memory, or read from one file and written to another one. If
you only want to count characters, create a simple procedure that inserts a
CR/LF in the last possible space character (or whatever your rules would be),
reset the line length value and repeat the operation until EOF. Or, do the
same in memory. But - wrapping lines is usually done by taking into
concideration actual string width. That's where DraewTextEx comes into play.
You may load your whole text into a string variable, decide the width of the
text (in pixels), call DrawTextEx and have CR/LF's inserted into your text
without actually drawing enything anywhere.

That's what he tried to tell you...

--
Bjoerge Saether
Consultant / Developer
http://www.itte.no
Asker, Norway
bjorgeremovet...@itte.no (remove the obvious)

Re:WrapText with TStream


In article <xY157.998$KC.133...@news3.oke.nextra.no>, "Bj?rge S?ther"

Quote
<REMOVE_bsaether@THIS_online.no> writes:
>You may load your whole text into a string variable, decide the width of the
>text (in pixels), call DrawTextEx and have CR/LF's inserted into your text
>without actually drawing enything anywhere.

>That's what he tried to tell you...

Not really, Bjorge <g> I did misunderstand him.

I think DrawTextEx can modify the string only if the string is ellipsised by
setting DT_END_ELLIPSIS, DT_PATH_ELLIPSIS, or DT_WORD_ELLIPSIS with
DT_MODIFYSTRING.

Unless my copy of MSDN Library is out of date <g>.

Alan Lloyd
alangll...@aol.com

Re:WrapText with TStream


Quote
"Bj?rge S?ther" <REMOVE_bsaether@THIS_online.no> wrote in message

news:xY157.998$KC.133786@news3.oke.nextra.no...
Quote
> Now listen -  a Stream is a range of data in this case, a string. It

better...

Calm down! If you've been to my website (not try to promote it, honest
:-) ), then you notice a program HTML Compress, it has already got a
wrapping function in it - but it doesn't seem to work entirely correctly.
The purpose of the wapping is to allow se's to handle the "compressed
website" better.

Andrew O
http://www.freesoft.fsnet.co.uk

Re:WrapText with TStream


Quote
"Andrew" <a...@freesoft.fsnet.co.uk> wrote in message

news:9j4lqk$2l$1@newsg2.svr.pol.co.uk...

Quote
> "Bj?rge S?ther" <REMOVE_bsaether@THIS_online.no> wrote in message
> news:xY157.998$KC.133786@news3.oke.nextra.no...
> > Now listen -  a Stream is a range of data in this case, a string. It
> better...

> Calm down! If you've been to my website (not try to promote it, honest
> :-) ), then you notice a program HTML Compress, it has already got a
> wrapping function in it - but it doesn't seem to work entirely correctly.
> The purpose of the wapping is to allow se's to handle the "compressed
> website" better.

If you are inserting line breaks into the stream then the simplest is to
copy the input stream to an output stream in segments, adding the line
breaks.

Re:WrapText with TStream


"Andrew" <a...@freesoft.fsnet.co.uk> skrev i melding
news:9j4lqk$2l$1@newsg2.svr.pol.co.uk...

Quote
> "Bj?rge S?ther" <REMOVE_bsaether@THIS_online.no> wrote in message
> news:xY157.998$KC.133786@news3.oke.nextra.no...
> > Now listen -  a Stream is a range of data in this case, a string. It
> better...

> Calm down! If you've been to my website (not try to promote it, honest
> :-) ), then you notice a program HTML Compress, it has already got a
> wrapping function in it - but it doesn't seem to work entirely correctly.
> The purpose of the wapping is to allow se's to handle the "compressed
> website" better.

I'm cool...;-)
You never told what kind of "wrapping" you were talking about, just counting
characters up to a max linelength or calculating them from a max. visible
extent, say 400 pixels...also you didn't mention how wraps should be done: In
spaces, after commas, periods? How to split long words ?
Wrapping text *inside* a Stream does not seem like a good idea, inserting one
or two bytes at a time, thus moving the rest of the stream conents
forwards...a *lot* of times...

...checking out your HTML Compress right now...

--
Bjoerge Saether
Consultant / Developer
http://www.itte.no
Asker, Norway
bjorgeremovet...@itte.no (remove the obvious)

Re:WrapText with TStream


"Bj?rge S?ther" <REMOVE_bsaether@THIS_online.no> skrev i melding
news:fJw57.25$%1.866@news3.oke.nextra.no...

Quote
> "Andrew" <a...@freesoft.fsnet.co.uk> skrev i melding
> news:9j4lqk$2l$1@newsg2.svr.pol.co.uk...
> > "Bj?rge S?ther" <REMOVE_bsaether@THIS_online.no> wrote in message
> > news:xY157.998$KC.133786@news3.oke.nextra.no...
> > > Now listen -  a Stream is a range of data in this case, a string. It
> > better...

> > Calm down! If you've been to my website (not try to promote it, honest
> > :-) ), then you notice a program HTML Compress, it has already got a
> > wrapping function in it - but it doesn't seem to work entirely correctly.
> > The purpose of the wapping is to allow se's to handle the "compressed
> > website" better.

> I'm cool...;-)
> You never told what kind of "wrapping" you were talking about, just
counting
> characters up to a max linelength or calculating them from a max. visible
> extent, say 400 pixels...also you didn't mention how wraps should be done:
In
> spaces, after commas, periods? How to split long words ?
> Wrapping text *inside* a Stream does not seem like a good idea, inserting
one
> or two bytes at a time, thus moving the rest of the stream conents
> forwards...a *lot* of times...

> ...checking out your HTML Compress right now...

An interesting application !

ISTM that your first task would have to be defining how wraps are to be made:
a) Where can you spilt ?
b) priority: e.g.:
pri 1 = Before a start tag within the last 5% of max. linelength
pri 2 = In a splittable place within the last 5%
pri 3 = the last splittable place
if you run into a too long element - let it be unwrapped, of course
c) spaces are substituted with CRLF when splitting
(this is an example, I'm not certain what the rules of an HTML file)

..once this is decided, you may do your wrapping...trying pri1, then pri2 if
no success with pri1, etc...

A few suggestions:
a) I would turn it the other way around: Leave source files untouched, put
output in another directory. This way it's easier to use just before
uploading, without having to copy thye originals for next editing. You don't
want to edit the compressed version.
b) Open list file from last run
c) Suggested directory for lst file: last source directory...
d) Option for removing what isn't needed of comments. This would make it a
killer...
--
Bjoerge Saether
Consultant / Developer
http://www.itte.no
Asker, Norway
bjorgeremovet...@itte.no (remove the obvious)

Re:WrapText with TStream


Quote
"Bj?rge S?ther" <REMOVE_bsaether@THIS_online.no> wrote in message

news:ibx57.35$%1.1505@news3.oke.nextra.no...

Quote
> An interesting application !

Thanks.

Quote
> ISTM that your first task would have to be defining how wraps are to be

made:
What does ISTM stand for - me ca thick.

Quote
> a) Where can you spilt ?

Before '<' or after '>' or at a whitespace

Quote
> b) priority: e.g.:
> pri 1 = Before a start tag within the last 5% of max. linelength
> pri 2 = In a splittable place within the last 5%
> pri 3 = the last splittable place

Last splittable place before max col length.

Quote
> if you run into a too long element - let it be unwrapped, of course
> c) spaces are substituted with CRLF when splitting
> (this is an example, I'm not certain what the rules of an HTML file)

> ..once this is decided, you may do your wrapping...trying pri1, then pri2
if
> no success with pri1, etc...

Now this is the problem, I just canny wrap - I'm looking for source to get
some ideas for how to do it.

Quote
> A few suggestions:
> a) I would turn it the other way around: Leave source files untouched, put
> output in another directory. This way it's easier to use just before
> uploading, without having to copy thye originals for next editing. You
don't
> want to edit the compressed version.

You are quite right originally i planned this but I ran into some trouble
and later removed it - if you decide to hack the resources - I suggest
Resource hacker - you'll probably see there is a couple of hidden Output
Directory boxes, mind you they don't do anything.

Quote
> b) Open list file from last run

Good idea, maybe an autosave as well kinda like WinAMP so automatically save
and reopen it. Though there is a slight problem, what if someone tries to an
entire drive - it would take absolute ages to load? Mind you, I suppose I
could set a limit of about 2 seconds
Quote
> c) Suggested directory for lst file: last source directory...

Yup, just been too lazy, well to e{*word*277}d about other things, thinking
hieracrchal structure allowing tags to inherit options for parents unless
options are set - making better use of pattern matching

Quote
> d) Option for removing what isn't needed of comments. This would make it a
> killer...

I slightly miffed at what you mean exactly.

Thank you sssooo much for your suggestions :-)

Andrew O
http://www.freesoft.fsnet.co.uk

Re:WrapText with TStream


Quote
> "Bj?rge S?ther" <REMOVE_bsaether@THIS_online.no> wrote in message
> news:ibx57.35$%1.1505@news3.oke.nextra.no...
> > ISTM that your first task would have to be defining how wraps are to be
> made:
> What does ISTM stand for - me ca thick.

(I)t (S)eems (T)o (M)e

Quote
> > a) Where can you spilt ?
> Before '<' or after '>' or at a whitespace

> > b) priority: e.g.:
> > pri 1 = Before a start tag within the last 5% of max. linelength
> > pri 2 = In a splittable place within the last 5%
> > pri 3 = the last splittable place
> Last splittable place before max col length.

> > if you run into a too long element - let it be unwrapped, of course
> > c) spaces are substituted with CRLF when splitting
> > (this is an example, I'm not certain what the rules of an HTML file)

> > ..once this is decided, you may do your wrapping...trying pri1, then pri2
> if
> > no success with pri1, etc...
> Now this is the problem, I just canny wrap - I'm looking for source to get
> some ideas for how to do it.

An attempt:
---------------------------
- A space is replaced with the NewLine Chars. To Change this behaviour, call
DoSplit(false) instead.
- First, all CR & LF chars are removed.

const
  CR = #13;
  LF = #10;

  NewLnText = #13#10;

procedure WrapText(const MaxChars: integer; var Text: String);
var
  ATotalLength: integer;
  ALargePos: integer;
  ALastSplit: integer;
  ASmallPos: integer;
  InsLength: integer;
  InsText: string;

  procedure DoSplit(Replace: boolean);
  var
    i     : integer;
  begin
    if Replace then begin
      Insert(InsText, Text, ASmallPos);
      for i:=0 to Length(NewLnText)-1 do
        Text[ASmallPos + i]:=NewLnText[i + 1];
      inc(ATotalLength, InsLength-1); // to prevent both insert *and*
delete...
    end
    else begin
      Insert(NewLnText, Text, ASmallPos);
      inc(ATotalLength, InsLength);
    end;
    ALastSplit:=ASmallPos+2; // Need to get past the NewLn inserted.
    ALargePos:=ALastSplit;
  end;

begin
  InsLength:=Length(NewLnText);
  SetLength(InsText, InsLength-1);
  FillChar(InsText[1], InsLength, 'X');
  ALargePos:=MaxChars;
  ALastSplit:=1;
  Text:=StringReplace(Text, CR, '', [rfReplaceAll]);
  Text:=StringReplace(Text, LF, '', [rfReplaceAll]);
  ATotalLength:=Length(Text);
  while ALargePos < ATotalLength do begin // while not EOF...
    ASmallPos:=Min(ALargePos, ATotalLength);
    while ASmallPos > ALastSplit do begin // Starts from max linelength, boes
backwards
      case Text[ASmallPos] of
        ' ': DoSplit(true); // 'til it finds a space
        '<': DoSplit(False); // or a '<'
      end;
      dec(ASmallPos);
    end;
    inc(ALargePos, MaxChars);
  end;
end;

One thing it fails to handle correctly, is lines that may actually not be
splitted within MaxLength. Then you need another section going right from the
prefferred max., looking for the first allowed character for splitting.
...not bug free, I guess, but it may be a start...

Quote
> > d) Option for removing what isn't needed of comments. This would make it
a
> > killer...
> I slightly miffed at what you mean exactly.

While not understanding "miffed", what I maeant was:
- If you are able to remove all comments from the HTML file, *Comments*
comments that is, not JavaScript, etc.. One doesn't want to write a lot of
comments in an HTML file because a) it may be too revealing, b) it means
overhead.

--
Bjoerge Saether
Consultant / Developer
http://www.itte.no
Asker, Norway
bjorge@takethisaway_itte.no (remve the obvious)
"Andrew" <a...@freesoft.fsnet.co.uk> skrev i melding
news:9jaaf1$4b0$1@newsg3.svr.pol.co.uk...

Re:WrapText with TStream


Quote
"Bj?rge S?ther" <REMOVE_bsaether@THIS_online.no> wrote in message

news:Dof67.522$4E6.10732@news1.oke.nextra.no...
Quote
> An attempt:
> ---------------------------
> - A space is replaced with the NewLine Chars. To Change this behaviour,
call
> DoSplit(false) instead.
> - First, all CR & LF chars are removed.

> const
>   CR = #13;
>   LF = #10;

>   NewLnText = #13#10;

> procedure WrapText(const MaxChars: integer; var Text: String);
> var
>   ATotalLength: integer;
>   ALargePos: integer;
>   ALastSplit: integer;
>   ASmallPos: integer;
>   InsLength: integer;
>   InsText: string;

>   procedure DoSplit(Replace: boolean);
>   var
>     i     : integer;
>   begin
>     if Replace then begin
>       Insert(InsText, Text, ASmallPos);
>       for i:=0 to Length(NewLnText)-1 do
>         Text[ASmallPos + i]:=NewLnText[i + 1];
>       inc(ATotalLength, InsLength-1); // to prevent both insert *and*
> delete...
>     end
>     else begin
>       Insert(NewLnText, Text, ASmallPos);
>       inc(ATotalLength, InsLength);
>     end;
>     ALastSplit:=ASmallPos+2; // Need to get past the NewLn inserted.
>     ALargePos:=ALastSplit;
>   end;

> begin
>   InsLength:=Length(NewLnText);
>   SetLength(InsText, InsLength-1);
>   FillChar(InsText[1], InsLength, 'X');
>   ALargePos:=MaxChars;
>   ALastSplit:=1;
>   Text:=StringReplace(Text, CR, '', [rfReplaceAll]);

                          ^--------Slight problem with this, TStream, though
I suppose I could switch to a multipass system.

- Show quoted text -

Quote
>   Text:=StringReplace(Text, LF, '', [rfReplaceAll]);
>   ATotalLength:=Length(Text);
>   while ALargePos < ATotalLength do begin // while not EOF...
>     ASmallPos:=Min(ALargePos, ATotalLength);
>     while ASmallPos > ALastSplit do begin // Starts from max linelength,
boes
> backwards
>       case Text[ASmallPos] of
>         ' ': DoSplit(true); // 'til it finds a space
>         '<': DoSplit(False); // or a '<'
>       end;
>       dec(ASmallPos);
>     end;
>     inc(ALargePos, MaxChars);
>   end;
> end;

> One thing it fails to handle correctly, is lines that may actually not be
> splitted within MaxLength. Then you need another section going right from
the
> prefferred max., looking for the first allowed character for splitting.
> ...not bug free, I guess, but it may be a start...
> While not understanding "miffed", what I maeant was:

"miffed" a.k.a. confused, bad grammar should be "I am miffed"

Quote
> - If you are able to remove all comments from the HTML file, *Comments*
> comments that is, not JavaScript, etc.. One doesn't want to write a lot of
> comments in an HTML file because a) it may be too revealing, b) it means
> overhead.

HTML Compress is supposed to do that and works with the files I've tried so
far.

Thanks,
Andrew O
http://www.freesoft.fsnet.co.uk

Re:WrapText with TStream


"Andrew" <a...@freesoft.fsnet.co.uk> skrev i melding
news:9jcshq$led$1@news8.svr.pol.co.uk...

Quote
> "Bj?rge S?ther" <REMOVE_bsaether@THIS_online.no> wrote in message
> news:Dof67.522$4E6.10732@news1.oke.nextra.no...

> >   Text:=StringReplace(Text, CR, '', [rfReplaceAll]);
>                           ^--------Slight problem with this, TStream,
though
> I suppose I could switch to a multipass system.

TStream hasn't got any methods that allows you to insert or delete anyhing,
AFAIK.
The simplest thing would be reading all of the stream conents into a string,
do the adjustments and write it all back. A more sophisticated scheme is
possible, where you read a chunk at a time, process and write it, and free up
memory for what's allready processed. This is not very simple, but it's
do-able.
--
Bjoerge Saether
Consultant / Developer
http://www.itte.no
Asker, Norway
bjorge@takethisaway_itte.no (remve the obvious)
Quote

> >   Text:=StringReplace(Text, LF, '', [rfReplaceAll]);
> >   ATotalLength:=Length(Text);
> >   while ALargePos < ATotalLength do begin // while not EOF...
> >     ASmallPos:=Min(ALargePos, ATotalLength);
> >     while ASmallPos > ALastSplit do begin // Starts from max linelength,
> boes
> > backwards
> >       case Text[ASmallPos] of
> >         ' ': DoSplit(true); // 'til it finds a space
> >         '<': DoSplit(False); // or a '<'
> >       end;
> >       dec(ASmallPos);
> >     end;
> >     inc(ALargePos, MaxChars);
> >   end;
> > end;

> > One thing it fails to handle correctly, is lines that may actually not be
> > splitted within MaxLength. Then you need another section going right from
> the
> > prefferred max., looking for the first allowed character for splitting.
> > ...not bug free, I guess, but it may be a start...

> > While not understanding "miffed", what I maeant was:

> "miffed" a.k.a. confused, bad grammar should be "I am miffed"

> > - If you are able to remove all comments from the HTML file, *Comments*
> > comments that is, not JavaScript, etc.. One doesn't want to write a lot
of
> > comments in an HTML file because a) it may be too revealing, b) it means
> > overhead.

> HTML Compress is supposed to do that and works with the files I've tried so
> far.

> Thanks,
> Andrew O
> http://www.freesoft.fsnet.co.uk

Re:WrapText with TStream


On 18 Jul 2001 06:55:48 GMT, alangll...@aol.com (AlanGLLoyd) wrote:

Quote
>In article <xY157.998$KC.133...@news3.oke.nextra.no>, "Bj?rge S?ther"
><REMOVE_bsaether@THIS_online.no> writes:

>>You may load your whole text into a string variable, decide the width of the
>>text (in pixels), call DrawTextEx and have CR/LF's inserted into your text
>>without actually drawing enything anywhere.

>>That's what he tried to tell you...

>Not really, Bjorge <g> I did misunderstand him.

>I think DrawTextEx can modify the string only if the string is ellipsised by
>setting DT_END_ELLIPSIS, DT_PATH_ELLIPSIS, or DT_WORD_ELLIPSIS with
>DT_MODIFYSTRING.

It doesn't look to me like DrawTextEx actually modifies any strings at
all - the "modifications" they talk about are differences between
what's in the string and what appears on the DC. If we want
to insert line breaks into a string in memory, which is what I
gather we want, I don't see how DrawTextEx is any use at
all.

Quote
>Unless my copy of MSDN Library is out of date <g>.

>Alan Lloyd
>alangll...@aol.com

David C. Ullrich
***********************
"Sometimes you can have access violations all the
time and the program still works." (Michael Caracena,
comp.lang.pascal.delphi.misc 5/1/01)
Go to page: [1] [2]

Other Threads