processing/processing

Text rotation, placement and font metrics incorrect when scaled in 2.0.3 and 2.1b1

Open

#2167 opened on Oct 23, 2013

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Java (6,431 stars) (1,540 forks)batch import
help wanted

Description

There appear to be a number of related bugs with text placement and font metrics when subjected to scaling. If the display is scaled by, say 10x with scale(10) and then text size is set to 1/10th of its display size, characters are misaligned or poorly rotated. This scaling does not affect other graphical placement such as via line(). It may possibly be related to bug #731

Bug is severe in 2.0.3 but still evident in 2.1b1. Tested on Macbook Pro Retina.

The code below demonstrates the problem

void setup()
{
  size(500,200);

  // Different behaviour depending on whether default font or created font is used
  // (try commenting/uncommenting the line below).
  //textFont(createFont("Arial",20),20);

  fill(0);
  noLoop();
}

void draw()
{
  background(255);

  float textSize = 10;      // Smaller fonts poorly positioned.
  float scaleFactor = 16;   // Try changing the scale factor.
  String msg = "This text should slope dowards by 5 degrees";

  // Centred cross-hairs
  strokeWeight(0.5);
  stroke(150,0,0,100);
  line(0,height/2,width,height/2);
  line(width/2,0,width/2,height);

  // Rotated text.
  translate(width/2,height/2);
  rotate(radians(5));
  scale(scaleFactor);
  textSize(textSize/scaleFactor);
  text(msg,0,0);

  // Use font metrics to underline text.
  float msgWidth = textWidth(msg);
  strokeWeight(1/scaleFactor);
  stroke(0);
  line(0,0,msgWidth,0);
}

The problems in detail (using code above as an example):

  1. Under 2.1b1, but not 2.0.3, with the default rasterised font, angle of a line of text is correct, but individual characters vary slightly in their orientation. The textSize() font metric overestimates the true length of the text by ~10%.
  2. Under 2.1b1 with a native vector font via createFont(), the angle of the text line is significantly altered and individual characters vary slightly in their orientation. The textSize() font metric rounds to 0.
  3. Under 2.0.3.with a native vector font via createFont(), font placement is very poor, with characters placed on top of each other. The textSize() font metric rounds to 0.
  4. Under 2.1b1 and 2.0.3, text sizes below 0.5 using default rasterised font appear to be rounded to 0 and generate a fatal error ['Problem createFont(Lucida Sans). java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0']. This does not occur if a native vector font is used via createFont().

Contributor guide