Position of Text in a TextBox - winapi

Hi I have a textbox containing some text. I am looking to replicate the red spelling mistake squiggle type behaviour.
Using WinAPI I can
draw the squiggle between 2 points.
find out the height and width of
the word to be "squiggled".
What is the api call (or perhaps the methodology if it is more than a single api call) to find the position of that word in the text box so that I can position the sqiggle undeneath it.
Also, what are the messages I need to trap to ensure that the squiggle is redrawn. I'm currently only using WM_PAINT, which obviously isn't good enough.
EDIT (3 Sept 2012):
FYI, Here's where I got to so far. Needs a lot of refining but shows basic principles
https://gist.github.com/3607272
Many thx
S

What might work is using an auto-sized label. Make sure the fonts in the label and textbox are identical.
Detect the number of rows that are before the sentence containing the misspelled word.
Fill the label caption with the number linefeeds (vbcrlf) you got from question 1.
Append the words from the misspelled line (up to the misspelled word) to the labels caption.
The label size should now be identical with the beginning of the misspelled word.
Example text:
This is my first line.
And my second line.
And over here i have my mispeled word.
Label caption output should be (ignore the dots, they are empty lines):
.
.
And over here i have my
The labels height and width should match the position in the textbox, unless you have scrollbars. If the textbox has borders then you should add a fixed value to the height and width to get an exact match.

Related

Prevent ugly kerning when using DrawText

I use simple GDI DrawText to output blocks of text to a printer.
The font used in the sample is Segoe UI. But you can use Arial or others too. It doesn't matter.
The algorithm for large text blocks is simple. DrawText is called with DT_CALCRECT with a kind of binary search for the length to get the largest possible text to print. Than DrawText is called without DT_CALCRECT to print the block.
Simple one line text column text is written with one call to DrawText with the given coordinates of the rectangle.
The result is real strange and can be seen in this sample PDF.
Just look on the first line after the header. You can see the text "Test, Test" and you can see the strange kerning here perfectly. The kerning os sometimes so bad, that you can't even read the words.
How to get around this? Is it a problem with the used printer? Is it a problem with DrawText?
The distance between some chars in a word seem to be random in some case. Some spacing are wide other to narrow. The letter combination looks strange unreadable and ugly.
I tried different fonts and printers but the problem just varies but it is always present.
I know about ExTextOut and the capabilities to define the distance/kerning between all chars, but frankly I don't want to care about this. I just want that DrawText behaves on the printer like on the screen. The stuff works on the screen perfectly.
Added 2018-08-23 08:49 GMT+2*
To the code (it is a complex printing engine).
1.Fonts to print are created simply with CFont::CreatePointFont, so the LOGFONT structure is cleared to zero and no additional flags are used except point and face.
2.The mapping mode is MM_ANISOTROPIC. To scale what is seen on the screen and what is to be printed I just use the size of a komparable object (textblock) on the printer and the same size on the screen. The real values for the sample printout to the Microsoft PDF Printer are as follows, the real way I calculate them is not of interest:
m_pDC->SetMapMode(MM_ANISOTROPIC);
m_pDC->SetViewportExt(2363,100);
m_pDC->SetWindowExt(355,13);
This has the effect that the height of a line in LPs is 13, the average character width in LPs is 6...

Text is printed incorrectly using ZPL

I print a text using ZPL but they are printed incorrectly. It has gaps in the middle. They are printed correctly to the real printer via the ZebraDesigner. But they are wrong when printing via ZPL:
^FT246,70^A0R,16,16
^FB900,2,,
^FDMaisstaerke, Austernuce, Cashewnuss,Pilz, Mais, Lattich, Pack Choi,Sojasprossen^FS
It is printed as 'Maisstaerke, Austernuce, Cashewnuss,Pilz, Mais, Lattich, Pack Choi, Sojasprossen'
(there is a big gap after 'Mais,').
http://screencast.com/t/sTiaxC3cMg
http://screencast.com/t/0QkypnsPyF89
Please help.
Thanks
I'm unable to reproduce the issue, but the field block width seems rather large; you didn't specify any label dimensions in the original post. If you use the following code--a combination from the original post and your comments:
^XA
^PW560
^LL560
^LS0
^FO246,70^GB28,300,,^FS
^FT246,70^A0R,16,16 ^FB300,2,, ^FDMaisstaerke, Austernuce, Cashewnuss,Pilz, Mais, Lattich, Pack Choi,Sojasprossen^FS
^PQ1,0,1,Y
^XZ
I can reproduce what is possibly the issue--the field block automatic word wrapping--by reducing the field block width to 300 dots.
I also drew a box around the block for visualization. You can test in on the Labelary Viewer.
Finally, if you're somehow composing the data make sure there are no special (non printable, such as tab, non-UTF-8 or accented) characters in it, and if so, you will have to pre-escape them.

ZPL Center text vertically

Is there a way in ZPL to center text vertically?
I know I can use ^FO0,0^ASN,50^FB200,3,0,C,0^FD TEXT ^FS. But how can I make that print on the 2nd line if it's short enough?
There isn't a vertical centering feature, but you can fake it, if you know in advance that your string is short enough to fit on the middle line.
Your ^FB defines a field block that is three lines. So your best option is to determine if your string fits on one line... and if it does, insert a "\&" before your string.
^FO0,0^ASN,50^FB200,3,0,C,0^FD\&TEXT^FS

iTextSharp stamper wraps text

I'm using iTextSharp to fill in some stamper AcroFields.
stamper.AcroFields.SetField("Title", "Lipsum");
I created the pdf in illustrator and the form fields with Adobe Acrobat X Pro. The problem is that although the text fields are the width of the page, in the saved pdf the text wraps at about 1 third of the width.
Another question would be if it's possible the have the textfield autoSize in height, or a way to handle the overflow of the text.
1) I'd like to see that PDF. I suspect the fields aren't as wide as you think they are.
2) You can set a field's font size to zero to enable "auto sizing", which works both within Reader and iText. However, it sizes to the actual field size, not what you think it might be.
I'm guessing you drew a spiffy form field background in Illustrator, then put a field over it in Acrobat Pro, but didn't size the field width to match the spiffy illustrator background. Could be wrong, but that's my hunch.
That's the flattened PDF. Can I see the original with the form field still intact? Sorry I wasn't more specific. None the less, I can learn a little from reading this PDF:
Looking at the bounding boxes for the flattened field XObject and it's internal clipping rectangle, it looks like it should be using most of the page:
The page is ~600 points wide by ~850 tall.
The flattened field XObject is ~560 points wide by ~100 tall.
I wonder if there's some non-standard carriage return characters in your text that iText picks up on by Acrobat does not...
Anyway, I'd like to see the unflattened PDF. Filled in is good, but not flattened.
Okay, looked at the template. I don't see anything that would cause the line breaking you're seeing... which makes me think my second guess was right: new line characters.
Looking at the text layout code might give me a hint. Each of your lines of text goes like this (for example):
1 0 0 1 2 88.24 Tm 0 g (Die Semmerrolle der l{e4}nge nach zu einer grossen Roulade)Tj
n n n n n n Tm: text matrix
g: gray (0 g: black)
(...)Tj: show text
That's consistent with the code path when you set a text field value in the trunk of iText (and the most recent release[s]). That code (ColumnText) is quite good at breaking text properly, and used all over the place. The bounding box is correct (as shown in a couple places of the flattened PDF).
Check your input.

How can I resize an NSTextField to fit the text that it holds?

I'm using an NSTextField to display lines of text that will be printed on a printer with a variable paper size (the printer prints to a spool, which is cut to the appropriate length when the job is done).
I can generate the text no problem, but I need to know how big to make the NSTextField (vertically) so that it exactly contains all of the lines of text. Is there an easy way of going about this?
The text is prepared as an NSAttributedString, and the lines are all of a fixed width (no wrapping), if that helps.
There is a great category on NSString/NSAttributedString by Jerry Krinock which allows you to calculate the height of text based on its width and vice versa:
http://www.sheepsystems.com/sourceCode/sourceStringGeometrics.html
I've used this often and it works very well indeed.

Resources