8

I am writing text on an image . I am using DrawString(x,y,string) method and I set font size as below

Font font = new Font(fontName, fontWeight, fontSize);

enter image description here

As you can see left side text written on image with 12pt size. Right side you can see 12pt size in HTML . Is there any way to map this so that I get same size in output as user sees in HTML ?

Ross
  • 2,079
  • 2
  • 22
  • 26
Pit Digger
  • 9,618
  • 23
  • 78
  • 122
  • 2
    It looks like the Java version doesn't use pt as unit, but pixels. – Rekin Jun 06 '11 at 20:46
  • It does use pt cause drawstring method has size as point . Kensen's solution fixed it. – Pit Digger Jun 07 '11 at 00:31
  • related to https://stackoverflow.com/questions/6202225/java-fontmetrics-ascent-incorrect . Java font size seems to take into accounts letters with diacritics but HTML not. – Robert Jun 13 '17 at 13:02

2 Answers2

6

I found this link. Maybe useful. Try it out.

Basically it says that

Java assumes 72 dpi screen resolution Windows uses 96 dpi or 120 dpi depending 
on your font size setting in the display properties.

The site suggests

instead of using getNormalizingTransform() you have to use getScreenResolution()


From the website again.

int screenRes = Toolkit.getDefaultToolkit().getScreenResolution();
int fontSize = (int)Math.round(12.0 * screenRes / 72.0);
Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
kensen john
  • 5,439
  • 5
  • 28
  • 36
2

I believe this is because java assumes 72 dpi display, where most everything else uses 96dpi now?

see: http://download.oracle.com/javase/tutorial/2d/text/fonts.html

John Gardner
  • 24,225
  • 5
  • 58
  • 76