textWidth() .... what is being returned?

Just playing with text and think that textWidth() returns a length that includes an added space?

Test code:


String[] strArray = {"the", "quick", "brown", "fox"} ;
int xOffset = 0 ;

void setup() {
  size(400, 400) ;
  fill(0);
}

void draw() {
  background(255) ;
  for (int i=0; i<4; i++) {
    text(strArray[i], xOffset, 10);
    xOffset += textWidth(strArray[i]) ;
  }
  xOffset = 0 ; //reset
}

xOffset is used to calculate the x position of each string to be displayed. I would expect there to be no spaces between each word with this code but there is … textWidth() is returning a value that is the string itself plus an added space char ??/ Is this right?

Any thoughts?

Mark

1 Like

it helps just to print it:

String[] strArray = {"the", "quick", "brown", "fox"} ;
textSize(40);
for (int i=0; i<4; i++)    println("text: ["+strArray[i]+"] wide: "+textWidth(strArray[i])) ;

now play with the textSize number for test

1 Like

@kll

Thanks for replying…

Seems that if I don’t call textSize(), then I get what appears to be a space added to the end of each word OR the returned width value makes it appear as if there has been a space added. By calling texSIze(), the space disappears.

I can work with that … although it seems to me to be a minor bug?

Mark

hm, i can not follow:
https://processing.org/reference/textWidth_.html

void setup() {
  size(200, 200);
  background(200,200,0);
  stroke(200,0,0);
  String[] strArray = {"the", "quick", "brown", "fox"} ;
  //textSize(20);
  for (int i=0; i<4; i++) {
    float sw = textWidth(strArray[i]);
    text(strArray[i], 0, 20+i*20);
    line(sw, 20+i*20, sw, 20+i*20+5); 
    println("text: ["+strArray[i]+"] wide: "+textWidth(strArray[i])) ;
  }
}

1 Like

Did the answer from @kll resolve this for you? If not, consider sharing the test sketch you are describing here – it may have a bug.

@jeremydouglass

No it didn’t help … if you run the test code I put in my original post what happens for you? For me I get a space between each word … “the quick brown fox” , but if I add textSize(12) I get … “thequickbrownfox”

i.e. without the textSIze() command, there is a space between the word?

quickbrownfox

No, I do not get a space between each word. I get “thequickbrownfox”.

  1. What OS and version are you running?
  2. What Processing version are you running?
  3. Where do you add the line “textSize(12)” exactly?

@jeremydouglass
Right, so it maybe my laptop? It’s a Win10 64bit laptop. Processing 3.5.2 … but was on 3.4 and ot was the same.

Have just edited my previous post with a screenshot … textSize(12) is in setup … commented out in the screenshot … uncomment it and I get this …

quickbrownfox2

I just got the same result in another program - what seemed like added spaces. Adding textSize(12) fixed it.

That’s on Windows 10 Pro, 64-bit.

I also tried it on my laptop running Linux Mint Cinnamon 19. There the result was the opposite - textWidth was a bit too short. Adding textSize fixed that too.

Processing 3.5.4 on both OS’es.

After closer inspection thanks to kll’s test, it seems that the text size is slightly off unless fixed with textSize (from flicking back and forth between the pictures below). It is sligthly too small on Windows, and slightly too big on Linux, before fixing it with textSize. Then they are the same size on both OS’es (from what I can tell anyway). The calculated widths are the same regardless.

Comparison between Linux Mint 19 Cinnamon and Windows 10 Pro, each with side-by-side comparison with and without using “textSize”.

Linux:

Windows 10:

1 Like