aspose file tools
The moose likes Java in General and the fly likes Fontmetrics, Graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Fontmetrics, Graphics" Watch "Fontmetrics, Graphics" New topic
Author

Fontmetrics, Graphics

Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 564
Hi All,
I want to measure the length of my string variable as it will appear on a page, but I don't have a window to paint it to.
I am writing HTML - I know what the font is going to be (on the browser), but I don't have any context or canvas or such like.
I want to run this on the server from a servlet so that I can work out how many blank spaces to add after the text to make it appear the same length every time.
I'm making a dropdown box which has to have 2 or 3 columns of data in it, and HTML options do not allow me to embed any other formatting, and unfortunately a fixed-space font like Courier is out of the question.
Thanks for the help
Adam


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
I think you can't do this.
Even if you are requesting to the browser that it use a specific font (using CSS or the FONT tag, for example), you still can't be sure that
(a) the browser will have that particular font - think about the fonts available on other operating systems, "console" browsers such as Lynx etc.) or
(b) The user hasn't overridden the settings because he/she likes another font better, or finds another font easier to read, or
(c) the user's browser will choose to display the text padded in the same way as you own browser.

Can we step back from trying to find out about browser fonts and find out what actual user benefits you are trying to achieve? There may be a way of achieving something just as useful which will work the way browsers do.


A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 564
(a) the browser will have that particular font - think about the fonts available on other operating systems, "console" browsers such as Lynx etc.) or
(b) The user hasn't overridden the settings because he/she likes another font better, or finds another font easier to read, or
(c) the user's browser will choose to display the text padded in the same way as you own browser.
Can we step back from trying to find out about browser fonts and find out what actual user benefits you are trying to achieve? There may be a way of achieving something just as useful which will work the way browsers do.

Well, the problem is that we are trying to keep the look and feel of the application as close as possible to the MS Access application we are replacing, in order to 'aid user acceptance'. (Made me squirm at first too).
Access has these multi-column dropdowns which we are trying to reproduce. So for example in one scenario, a monkey record can be edited by changing the banana it owns in the dropdown.
Instead of having a list with banana1, banana2 etc, the Access app has multi-columns with
banana1 - ripe - yellow - sweet
banana2 - ripe - yellow/black - mushy
etc etc.
My first impulse would be to replace it with a button that opens a new window with the list of bananas and all fields required, and a click on a radio button causes the window to shut and the banana ID in the record to be set using javascript.
Other possibilities include having javascript synch'd dropdown boxes, one for each column (yuck!).
What would you suggest? This application is littered with these things.
Are you sure it technically can't be done? Since it's an application on an intranet, all users can be instructed about fonts etc. to have on their browsers.

Adam
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
Are you sure it technically can't be done?
I don't think second-guessing how the fonts will render is the right way to go about it. You'll be much better off looking at the powerful way you can arrange content in the window using CSS. That way the browser can handle it for you. When you get "into" it, CSS is a lot more than just changing the colors of links.
If you haven't already seen it, try and get a look at Microsoft's "outlook in a browser" - it looks astonishingly like the real thing. Take their page apart and see how they do it.
Since it's an application on an intranet, all users can be instructed about fonts etc. to have on their browsers.
I've been round this loop, believe me. The trouble is that every department providing a service to the intranet has this attitude and you can't really avoid ending up with conflicting requirements. It all gets bogged down in politics as to which internal system is "more important", and real users just carry on using what they feel like.
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 564
second-guessing how the fonts will render

It's not really a great problem since all I am concerned about is the relative length of the options in the box. The font is after all Arial as well.
But that aside, are there actually any other solutions other than those I mentioned?
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6913
To reiterate, I think CSS is the way to go with this example. WIth CSS you should be able to position all your text items on the page, regardless of their length.
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 564
Problem is, it's not my decision. I need to present this as an option along with its disadvantages.
That is of course, presuming it is an option. Is it technically possible to get a Graphics object without actually having anywhere to paint anything?

Adam
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Fontmetrics, Graphics
 
Similar Threads
Top 10 Reasons to code Java for us...
Specify range of Fonts to use.
fonts in xslt are not coming through in JSTL
Japanse font (Urgent! Please help)
formatting a string