Link to home
Start Free TrialLog in
Avatar of wehoit
wehoit

asked on

Help centering diagonal Watermark - is not consistent !!

I've created some basic code that creates information along the edges of a page.

It also creates a diagonal 'watermark' across the face of the page.

I'm having trouble keeping the watermark centered when I change the watermark text.
For example, if I use "watermark", the text is positioned differently than if I use "NOT FOR DISTRIBUTION" - and neither are centered as they should be.

My code calculates the font size based on the text length.

Can someone help me figure out why my centering code does not work properly???

I need to be able to use any font, any text and still have the watermark be
centered on the digonal.

Please test your code - I will!

I think that the Printer.TextHeight and Printer.TextWidth methods return inaccurate info...
I've also tried GetTextExtentPoint32 with the same results.

Click this link do download my sample code...
http://whoit.home.comcast.net/TESTrotate.zip

Or contact me by email:
<email address removed - Bingie EE PE>
Avatar of Erick37
Erick37
Flag of United States of America image

I have not seen the algorithm yet, but a quick question or two:

If the text is not rotated, does it center correctly?

If the text is rotated, do you adjust it's position according to the sine of the angle?
Avatar of wehoit
wehoit

ASKER

Erick -

1) Yes

2) Yes - and the Cosine

Check out the code - it's very short and clear, plus it's easy to run and test.
Well I ran your code.

It output "NOT FOR PRODUCTION" on a diagonal across an 8.5x11 size paper.
Running a ruler diagonally across the paper, from corner to corner, lines up with the tops of the letters.
I am assuming you want the alignment through the center of the text?

My printer is an HP Laserjet 5P
Avatar of wehoit

ASKER

I'm looking for centering - left/right and top bottom, along the diagonal, with the alignment through the center.

It should work for any typeface (or certainly the most common) , upper/lower/mixed case, etc....

I'd like to continue to calculate the font size dynamically so that it fills the available diagonal space inside the margins.
It should not conflict with the border text, regardless of how many lines there are on each page edge.

I have Acrobat, so I typically print to a PDF file for testing - saves a lot of paper if you can do it.

Please feel free to ask any more questions....
Avatar of wehoit

ASKER

Is anyone working on this?

'Cause if not, I'll delete it and re-submit.

I'm raising the point value this time.
I could not get it working correctly in the time I had to look at it.  I believe the problem lies in calculating the hypotenuse according to the height of the text.  As the textheight increases the triangle defined by (topleft margin) - (currentx currenty) - (the intersection of hyp and the top of page) decreases.

As the height increases:
1) the hypotenuse should decrease
2) CurrentX should remain at the left border
3) CurrentY should decrease

That's as far as I got, no working algorithms.  Feel free to delete and resubmit to the top of the stack.

Good Luck!

Eric.

Hold the question open a bit longer.  Let me take a crack at it. :p
Avatar of wehoit

ASKER

I've already asked Support to allow me to close and re-post it, but if they do, I'll let you know.

By all means, have at it!
Bleh, I know how to do it in my head, but I just don't have the math skill to code it.

Good luck to anyone who does get it. :)
Avatar of wehoit

ASKER

Why don't you try explaining it to me - perhaps I can write the math.

Just so you know, this is what I do now:

1) Calculate the angle from corner to corner
2) Calculate the length of the diagonal (hypotenuse) from corner to corner.
3) Calculate the font size based on using TextWidth until it equals or exceeds the hypotenuse.
4) Calculate the left position by getting the hypotenuse minus the TextWidth, then finding the length of the base of the triangle formed by the 'new' hypotenuse and a horizontal and vertical line. Add this to the left margin.
5) Calculate the vertical (y) position using the same method as step 4.

Unfortunately, I think that TextWidth/TextHeight are not returning correct values...
Arg. I've been working on this for about an hour now, but no success. I'm still a little unsure about what you are calculating at certain points, but I don't think the math is the problem. Here's what I've come up with, maybe it'll spark an idea in someone else's mind (these are referencing the setBanners Sub):

- Printer.FontSize is not being set to the proper values when you are calculating the length of the page's hypotenuse. It seems to always be off by a multiple of 0.08, and is also returning a non-integer when you set rotobj.Font.

- pageWidth and pageHeight are each approximately 0.5 less than the actual dimensions of the paper size (meaning, 0.25" margins all around are automatically being factored in). The actual values of the two variables are not exactly 8.0" and 10.5" for some reason -- they are both off by a few hundredths.

- An observation: The phrase in all capital letters (i.e. "NOT FOR PRODUCTION") is centered properly, but the phrases in lower-case or mixed letters (i.e. "watermark" or "Not For Production") are not centered properly.

It's a conundrum, it is... Lastly,

>> Unfortunately, I think that TextWidth/TextHeight are not returning correct values...

I don't understand what these are supposed to be returning. Is it the height and width of the text at 0 degrees, returned in pixels? Or is it the height and width of the rotated text (meaning, the lengths of the tall vertical leg and the short horizontal leg of the triangle, respectively)?
Sorry if that seems like an obvious question; I understand the actual mathematics involved (aka "triangle stuff"), but I don't understand very well how Visual Basic handles the Printer object in this regard... lol :)

-Burbble
Have you tried to set printer.scalemode, this field is important
Avatar of wehoit

ASKER

Burbble -

I've noticed the incorrect values that you  pointed out, but I think they are irrelevant. If they're always off by the same amount (which they are) then the math should still place the watermark at the same place every time - which, of course, it doesn't.

As for TextWidth/Height - it returns the width or height of the text based on 0 degrees. It doesn't take into account any rotation. Besides, I use it before the rotation.
Also, they return units based on the ScaleMode setting of the device. In this case, the printer is Twips by default.

Which also refers to the comment by EDDYKT - I don't change the ScaleMode - it's twips and I work with twips for all the other calculations.

Yes, it is incredibly frustrating!!! I've been tinkering with this for DAYS, which leads me to believe that the math is basically sound, but the TextWidth/Height is wrong.
I've also tried the API GetTextExtentPoint32 which returns different values but still doesn;t solve the problem...

I wanted to change the point value of this question to 750 but I'm not allowed to.

I really need a solution to this problem, and I know it can be solved...
Just a thought, instead of using arial can you use courier which is fixed size for all character


also use form to return textwidth instead of using printer object and see what is the different
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As a side note.  Please use Option Explicit as the first line in all modules.  This forces you to Dim all your variables.  I ran across a couple undeclared variables in your code ("half" for example which I believe should be called "halftwip").  This kind of error is very difficult to trace because the compiler does not complain, and just sets the value to 0.
Avatar of wehoit

ASKER

Erick -

Your code is consistent, but I'm looking to get the watermark centered. Yours is always in the lower-left corner.
Also, it seems as though the biggest change you make is when you calculate the font size - it now comes out smaller than mine - why did you this?

The actual code for positioning the text (setting CurrentX and CurrentY) is nearly identical to my original code.

I've been tinkering with it still, and have found that TextHeight includes not only the height of the actual text, but also the imposed line spacing. My testing came up with a value of 0.155 which can account for this spacing. (Your example shows this line spacing when you draw the box around the text area.)
When I incorporate this into my earlier code I get this for the CurrentY:
Printer.CurrentY=(pageHeight * twip) - (margs.bottom * twip) - (Printer.TextHeight(iso.wmark) * 0.155) - (Printer.TextHeight(iso.wmark) * half)
This is more accurate for the vertical centering although it is still not completely accurate.

Regarding the TextWidth, it seems to calculate additional space based on the text kerning and the fact that it's a proportional font (I can't use a fixed-width font like EDDYKT suggested 'cause I need to give the user the option of selecting any font...) So, for example, if you don't use the Rotate method ans simply try to left-align the text at 0, you'll see what I mean....

As for "half" - that's definitely my fault - I removed the lines defining and setting the var - it's supposed to be "half=0.5" - sorry.
Halftwip is something else. If you notice, when you changed "half" to "halftwip", the centered text around the edges disappeared...
Avatar of wehoit

ASKER

All -

I've been working on this and have come very close to solving it.
I've updated the sample code at this link:
http://whoit.home.comcast.net/TESTrotate.zip

I'd still like to get it working 100%.


wehoit: I do not have a printer available, so I cannot test the code, and hence am not looking at it, but can you offset the text vertically by [height/2] before rotating it? This will align it at the center, and make it seem more of in-the-same-spot each time.  
Also, if you change the font to a fixed-pitch font, and try two different strings of the same character length, do they show up in the same place?
Avatar of wehoit

ASKER

tonsofpcs -

First - be aware that I wanted to reduce the point value - mainly to Erick 200 pts since no one else really came close.

Also, be aware that I have mainly solved it my self and have uploaded newer test code.

I have offset the text before rotation, by several methods. You'll have to look at the code for the current solution as it's fairly complex.

As for Fixed -width fonts, they have a tendency to look worse and still not be positioned any better than variable-width fonts.

If someone wants to take my uploaded code and make it work 100%, I'll split the points with you and Erick.
Avatar of wehoit

ASKER

While not a complete answer, it did help point me in the right direction to *mostly* solve this myself.

Thanks for the effort.