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

**URL:** <https://discourse.processing.org/t/textwidth-what-is-being-returned/7864>\
**Category:** Coding Questions\
**Created:** [January 26, 2019, 7:28pm UTC](https://discourse.processing.org/t/textwidth-what-is-being-returned/7864 "2019-01-26T19:28:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)\
**Post date:** [January 26, 2019, 7:28pm UTC](https://discourse.processing.org/t/textwidth-what-is-being-returned/7864/1 "2019-01-26T19:28:11Z")

</div>

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

Test code:

```auto

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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 27, 2019, 2:42am UTC](https://discourse.processing.org/t/textwidth-what-is-being-returned/7864/2 "2019-01-27T02:42:39Z")

</div>

it helps just to print it:

```auto
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

---

<div class="post-metadata">

**Author:** ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)\
**Post date:** [January 27, 2019, 9:30am UTC](https://discourse.processing.org/t/textwidth-what-is-being-returned/7864/3 "2019-01-27T09:30:51Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 27, 2019, 1:17pm UTC](https://discourse.processing.org/t/textwidth-what-is-being-returned/7864/4 "2019-01-27T13:17:49Z")

</div>

> [@shedMusic](#):
>
> returned width value makes it appear as if there has been a space added

hm, i can not follow:

> **[textWidth() / Reference](https://processing.org/reference/textWidth_.html)**
>
> Calculates and returns the width of any character or text string.

```auto
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])) ;
  }
}

```

 ![2019-01-27_20-15-56_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/ae7ef1f9ec2f5ff9cb2910a6c4603bc987f988a0.png)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 28, 2019, 7:05pm UTC](https://discourse.processing.org/t/textwidth-what-is-being-returned/7864/5 "2019-01-28T19:05:30Z")

</div>

> [@shedMusic](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)\
**Post date:** [January 28, 2019, 7:56pm UTC](https://discourse.processing.org/t/textwidth-what-is-being-returned/7864/6 "2019-01-28T19:56:48Z")

</div>

@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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f6cb5624e25d96a60c6c4593979cfeb70dd54538.png)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [January 28, 2019, 7:59pm UTC](https://discourse.processing.org/t/textwidth-what-is-being-returned/7864/7 "2019-01-28T19:59:06Z")

</div>

> [@shedMusic](#):
>
> 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

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?

---

<div class="post-metadata">

**Author:** ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)\
**Post date:** [January 28, 2019, 8:04pm UTC](https://discourse.processing.org/t/textwidth-what-is-being-returned/7864/8 "2019-01-28T20:04:48Z")

</div>

@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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/850c8ae901f18fae77c2f0b1255a1368db9a278d.png)

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [August 24, 2020, 5:30am UTC](https://discourse.processing.org/t/textwidth-what-is-being-returned/7864/9 "2020-08-24T05:30:43Z")

</div>

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:

 ![textWidthtest2-kll-LinuxMint19](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/772703720329388935fe7bd756047e709d851f0e.png)

Windows 10:

 ![textWidthtest2-win10Pro](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c3d360fbb1d66a3e534f2165e69623f526253196.png)
