PDA

View Full Version : Help! Wordwrap in DelphiX?



mikade
01-07-2010, 07:36 PM
Daygreet. I decided to lurk even less and post a question. Hope this is the right section. My question is:

Is there a relatively painless way of printing out large chunks of text to the screen, using DelphiX? I'm using Delphi 7, not sure what version of DelphiX I have...

Up till now I've been using TextOut but I'd like something that lets me wordWrap. Any advice anyone can give me will be appreciated. :)
Apologies in advance if this is a stupid question. I tried searching but didn't find any answers I liked. >_<

JSoftware
01-07-2010, 10:22 PM
Try searching a while back on the forums here. There was a large topic about word wrapping

User137
02-07-2010, 08:04 AM
TextOut is what you have to use, no way around. It is slightly more tricky to do if you want to cut from spaces but this is roughly how you can do it by cutting everything:
var
i,charW,charH: integer;
cur,position: TPoint;
text: string; // This you can read from TStringList.Text or anything
line: string;
//maxWrapWidth: integer; // How wide the text can be before wrap
begin
with SomeOfYourSurfaces.Canvas do begin
position:=Point(3,3); // This is upper corner of printed text
cur:=Point(0,0);
line:='';
charH:=TextHeight('I'); // or you can manually type any row height
for i:=1 to length(text) do begin
charW:=TextWidth(text[i]);
if cur.X+charW>maxWrapWidth then begin
TextOut(position.X, position.Y+cur.Y, line);
cur.X:=0;
cur.Y:=cur.Y+charH;
line:=text[i];
end else begin
cur.X:=cur.X+charW;
line:=line+text[i];
end;
end;
TextOut(position.X, position.Y+cur.Y, line);
Release; // Needed with DelphiX surface, not with normal canvas
end;
end;
Hope i didn't forget anything ::)

mikade
02-07-2010, 10:33 AM
Ah, thanks. That works. ;D

I wrote a similar wrapper in another project, but I was worried about speed. Should be fine tho. Good to know I'm not doing anything wrong by using TextOut. xx(

Thanks again.

WILL
02-07-2010, 05:12 PM
When it comes to software there is no wrong, only some techniques are more right than others. ;)

pstudio
02-07-2010, 06:19 PM
When it comes to software there is no wrong, only some techniques are more right than others. ;)

Sorry but I have to adress this ;)

Though I do get what you're trying to say it's wrong to say that there is no wrong in software. A trivial example would be a calculator program that gives you the wrong answer when you're solving the equation 2+2. A less transparent (and more serious) error would be an algorithm using floating points but not accounting for precision errors. Results can suddenly become totally wrong even though your algorithm is mathematically correct.

But I agree that if a solution works in your context, there's no reason to improve it until you need to improve it.

WILL
03-07-2010, 06:28 PM
Well sure you can always do something wrong. lol I've had my share. But if it is working then you could consider it right. Until you instead find a more right way of doing the same job, but better. :nerd:

User137
03-07-2010, 08:45 PM
I'm gonna need to do better version of this for my UI Editor's memo component. What is still needed is character checks to not print characters under #32, #13 to force a line change and #32 to make a "delayed line change" which i thought of using another temp string for current line.

Edit: Actually... there may be some automatic kind of wrapping on TStringList, in addition to already premade lines. So it should be easier than what i wrote above.

mikade
06-07-2010, 12:41 AM
Ah, really? Let me know how it goes.

czar
07-07-2010, 02:26 AM
If you are going to make a unit to print text to screen then I suggest having it also handle text effects, like changing colour.

We made a nice unit at work that can print paragraphs to the screen you can set the width of the paragraph so that it wraps at a given point, you can also set bold and italic etc. Don't use delphix much anymore except to maintain older software.

User137
07-07-2010, 01:38 PM
I'm not "going to make", i already did (the text printing part) ;D

The lib is using OpenGL so simple glColor() works with it, as well as blending, scaling effects. Distortion (ripple/wave etc) effects need custom rendering but i realize that i still miss simple bold/italic switches... And enough of that offtopic this thread...

User137
14-08-2010, 11:46 AM
A little news from the OpenGL based Memo component... It came to be different than i expected but here's the key things:
- TStringList does not have any automatic feature for wrapping. VCL TMemo might have but i wont use it.
- I can't simply go through characters and do line changes where i want, then the scrollbars would not work line-by-line.
- The line changes are made in separate Update procedure, by adding for example character #30 (almost anything between 0..31 except 10 or 13) and a real line-break char. So then, when memo is resized bigger and wrapping is changed, the character and line change can be removed knowing where it is. This character is ignored when drawing, and removed when saving.
All in all: TStringList.Strings&#91;] array will be properly wrapped before rendering.
- Using scrollbar up and down is easy this way, but left and right (when wrapping is disabled) is more complicated. This requires to "virtually" move the caret from left side until first visible letter comes.

harrypitfall
27-08-2010, 12:13 AM
If you know the max width to display 'each' line of text, there is a function on Delphi called WrapText(), it's break to be displayed on that width...

User137
27-08-2010, 01:36 PM
My functions are all finished now, and WrapText() doesn't seem to give a alternate solution. It cuts between certain number of characters, not by how much they take in pixels. Also i would need to keep in memory both unwrapped and wrapped strings if i wanted to resize the text area. (edit: or worse/slower, call WrapText() each time Drawing happens)