Loading...
Logo
Processing Forum
Suppose you wished to display some small text inside a rectangle, as below:



The code to do this might be something like this:

Copy code
  1. String message = "This is a simple line of text";
  2. float visualTextSize = 6;

  3. void setup()
  4. {
  5.   size(400,120);
  6.   smooth();
  7.   textAlign(CENTER,CENTER);
  8.   rectMode(CENTER);
  9.   textFont(createFont("Arial",6),6);
  10. }

  11. void draw()
  12. {
  13.   background(255,220,230);
  14.   noFill();
  15.   textSize(visualTextSize);
  16.   rect(width/2,height/2,textWidth(message),visualTextSize);
  17.   fill(255,0,0);
  18.   text(message,width/2,height/2);
  19. }
If in the sketch, you wished to enlarge the text/rectangle (for example in response to interactive zooming), applying a simple scaling results in blurred text as we might expect because a 6pt font is being displayed at a much larger size:



I would assume that the way to overcome the blurring would be to create a larger font before it is displayed:

Copy code
  1. String message = "This is a simple line of text";
  2. float visualTextSize = 6;

  3. void setup()
  4. {
  5.   size(400,120);
  6.   smooth();
  7.   textAlign(CENTER,CENTER);
  8.   rectMode(CENTER);
  9.   
  10.   textFont(createFont("Arial",30),30);
  11. }

  12. void draw()
  13. {
  14.   background(255,220,230);
  15.   noFill();
  16.   textSize(visualTextSize);
  17.   
  18.   translate(width/2,height/2);
  19.   scale(5);
  20.   translate(-width/2, -height/2);
  21.    
  22.   rect(width/2,height/2,textWidth(message),visualTextSize);
  23.   fill(255,0,0);
  24.   text(message,width/2,height/2);
  25. }
Unfortunately, when text is enlarged through scale(), even if it is stored at a high resolution, some rounding occurs in text placement (this is a
 
known bug  that I reported a couple of years ago):



This has been bugging me for a while now as I create lots of sketches with text that can be interactively zoomed.

Because I am not sufficiently familiar with the workings of the Processing source code to deal with the problem at source, I think I have a workaround. Whenever some scaled text needs to be displayed, temporarily reset the transformation matrix and display the text at a scale closer to its natural resolution. This is all done in the text2() method:
Copy code
  1. String message = "This is a simple line of text";
  2. float visualTextSize = 6;

  3. void setup()
  4. {
  5.   size(400,120);
  6.   smooth();
  7.   textAlign(CENTER,CENTER);
  8.   rectMode(CENTER);
  9.   textFont(createFont("Arial",30),30);
  10. }

  11. void draw()
  12. {
  13.   background(255,220,230);
  14.   noFill();
  15.   textSize(visualTextSize);
  16.   
  17.   translate(width/2,height/2);
  18.   scale(5);
  19.   translate(-width/2, -height/2);
  20.    
  21.   rect(width/2,height/2,textWidth(message),visualTextSize);
  22.   fill(255,0,0);
  23.   text2(message,width/2,height/2);
  24. }

  25. void text2(String textToDisplay, float xPos, float yPos)
  26. {
  27.    pushMatrix();
  28.    float matrixScale = g.getMatrix().get(null)[0];
  29.    PVector newTextPos = new PVector(0,0);
  30.    g.getMatrix().mult(new PVector(xPos,yPos),newTextPos);
  31.    resetMatrix();
  32.    
  33.    float origTextSize = g.textSize;
  34.    textSize(matrixScale*origTextSize);
  35.    text(textToDisplay,newTextPos.x,newTextPos.y);
  36.    textSize(origTextSize);
  37.    
  38.    popMatrix();
  39. }
It appears to give much better results and is about 3 times faster as a downscale-upscale transformation is avoided:



I don't like messing about with the 'g' object and this solution feels a bit hacky to me although it does appear to work. Are there any better ways of dealing with the text rounding problem?

Thanks,

Jo.