Beranda > Delphi > WordWrap and align text to block

WordWrap and align text to block

Procedure WordWrap wrap input string at margin, storing the result in output string and the remainder in overlap string.

Function StringExpand expand input string to length RightMargin inserting spaces among words. It can be usefull to align text to block as is showed in example.

procedure WordWrap(InSt : string; var OutSt,
Overlap : string; Margin : Integer);
var
InStLen  : Integer;
OutStLen : Integer;
OvrLen   : Integer;
EOS, BOS : Integer;
begin
{find the end of the output string}
InStLen := length(InSt);

if InStLen > Margin then begin
{find the end of the word at the margin, if any}
EOS := Margin;
while (EOS <= InStLen) and (InSt[EOS] <> ' ') do
Inc(EOS);
if EOS > InStLen then
EOS := InStLen;

{trim trailing blanks}
while (InSt[EOS] = ' ') and (EOS > 0) do
Dec(EOS);

if EOS > Margin then begin
{look for the space before the current word}
while (EOS > 0) and (InSt[EOS] <> ' ') do
Dec(EOS);

{if EOS = 0 then we can't wrap it}
if EOS = 0 then
EOS := Margin
else
{trim trailing blanks}
while (InSt[EOS] = ' ') and (EOS > 0) do
Dec(EOS);
end;
end
else
EOS := InStLen;

{copy the unwrapped portion of the line}
OutStLen := EOS;
SetLength(OutSt, OutStLen);
Move(InSt[1], OutSt[1], OutStLen);

{find the start of the next word in the line}
BOS := EOS+1;
while (BOS <= InStLen) and (InSt[BOS] = ' ') do
Inc(BOS);

if BOS > InStLen then
Overlap := ''
else begin
{copy from the start of the next word
to the end of the line}
OvrLen := Succ(InStLen-BOS);
SetLength(OverLap,OvrLen);
Move(InSt[BOS], Overlap[1], OvrLen);
end;
end;

function StringExpand(InpLine : string;
RightMargin : Integer) : string;
var
outs       : string;
s          : string;
mpos       : integer;
louts      : integer;
begin
s := trim(InpLine);
if length(s) = RightMargin then
begin
// Length is OK, no need expand
Result := s;
exit;
end;

mpos := pos(' ',s);

if mpos = 0 then
// There is no space for insert ' ' into string
Result := s
else
begin

outs := s;

repeat

repeat
s[mpos] := #255;
insert(#255, s, mpos );
insert(' ', outs, mpos );
mpos := pos(' ', s);
louts := length(outs);
until (mpos = 0) or (louts = RightMargin);

if louts < RightMargin then
begin
s := outs;
mpos := pos(' ',s);
end;

until louts >= RightMargin;

Result := outs;

end;

end;

Example of align to block

{NEUROMANCER  William  Gibson  Chapter One The
 sky  above  the  port  was    the   color  of
 television,  tuned  to  a dead channel. "It's
 not  like I'm using," Case heard someone say,
 as  he  shouldered  his way through the crowd
 around  the  door  of the Chat. "It's like my
 body's    developed    this    massive   drug
 deficiency."  It  was  a  Sprawl  voice and a
 Sprawl  joke.  The  Chatsubo  was  a  bar for
 professional  expatriates;  you  could  drink
 there  for a week and never hear two words in
 Japanese.}


var
R_Margin : integer;
InpLine  : string;
OutLine : string;
s        : string;
i        : integer;
begin

R_Margin := 45; {Right margin, shoud be more then
length of the longest word in text
(input string)}

s := ‘Very long string…     ..ie whole paragraph’;

Memo1.Clear; {some container for result, use
non-proportional font}

repeat

InpLine := trim(s);

WordWrap( InpLine, OutLine, s, R_Margin);
{break input string to Outline < R_margin and
and remaind part store to S}
OutLine := trim(OutLine);

Memo1.Lines.Add( StringExpand(OutLine, R_Margin));
{StringExpand inserts spaces in to Outline between
words until length Outline is = R_Margin} {Repeat all if remaind in S is longer
than line (R_Margin) }
until Length(s) <= R_Margin;

if s <> ” then Memo1.Lines.Add(s);
{dont forget add last part of string! (if any)}

end;

Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: