# Calculate text length

**URL:** <https://discourse.processing.org/t/calculate-text-length/32885>\
**Category:** Coding Questions\
**Created:** [October 17, 2021, 2:52pm UTC](https://discourse.processing.org/t/calculate-text-length/32885 "2021-10-17T14:52:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [October 17, 2021, 2:52pm UTC](https://discourse.processing.org/t/calculate-text-length/32885/1 "2021-10-17T14:52:28Z")

</div>

Update:  
I found the command textAlign, but it doesn’t do what I need

I want to create a loop that runs for example 50 times,  
And each time it write word in the center of the screen, in smaller font,

Here is my code:

```auto
{
  
  int x1, y1;
  x1=width/2;
  y1=height/2;

for (int j=100; j>0;j-=2)
{
  
  int c = int(random(255));
  
  fill(c,c,c);
  textAlign(CENTER, CENTER);
  textSize(j);
  text("TODAY", x1, y1);
  
}

```

But the alignment to the horizontal center is not really refers to the center of the screen,  
because the result is this:

 ![2021-10-17 18_47_26-sketch_211017a](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/ab8906d0873d67b7b0fc2f393a7d3781dbe8950c.jpeg)

The question is - how do I calculate how much should I move the text?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 17, 2021, 3:07pm UTC](https://discourse.processing.org/t/calculate-text-length/32885/2 "2021-10-17T15:07:04Z")

</div>

Hello,

> [@yalaniv](#):
>
> Now, I want to write the text at the center of the screen,

See the _related items_:  
[https://processing.org/reference/text\_.html](https://processing.org/reference/text_.html)

Also this and the related items:  
[https://processing.org/reference/width.html](https://processing.org/reference/width.html)

Lots of resources here:

> **[Welcome to Processing!](https://processing.org/)**
>
> Processing is a flexible software sketchbook and a language for learning how to code. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology…

`:)`

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [October 17, 2021, 3:27pm UTC](https://discourse.processing.org/t/calculate-text-length/32885/3 "2021-10-17T15:27:54Z")

</div>

Hi

I think this is useful

[https://processing.org/tutorials/data](https://processing.org/tutorials/data)

And this is how text array …could guide from an idea

> [@How to text array](https://discourse.processing.org/t/how-to-text-array/30192):
>
> hi all how to text array elements i have try but i could not do it looks random and wrong code any help thanks in advance void setup () { size ( 800 , 800 ); } void draw () { fill(0); int[] x = { 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 50, 61, 83, 69, 71, 50, 29, 31, 17, 39, 6…

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 17, 2021, 3:54pm UTC](https://discourse.processing.org/t/calculate-text-length/32885/4 "2021-10-17T15:54:53Z")

</div>

@yalaniv,

Take a look at this topic:

> [@Unexpected Character Movement](https://discourse.processing.org/t/unexpected-character-movement/16716):
>
> I am developing a sketch to graphically display GPS data coming from an Arduino. The sketch paints the background and “gauge” bezel and then calls various functions to fill in the face with appropriate data, . The compass function paint dots for 5 degree gradations in a circle inside the bezel. Following the dots the directional characters are displayed in a circle around the dots. Finally a pointer is painted. The first time through the function, all is well. The second time through the fun…

It will help you get to this:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1a73e49c2e3bdd1e8ab3340079de4e44e214f678.png)

`:)`

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [October 18, 2021, 6:56am UTC](https://discourse.processing.org/t/calculate-text-length/32885/5 "2021-10-18T06:56:08Z")

</div>

if i understand correctly i think what you need to do is draw the letters individually. you can see an example of what i mean below

 ![example](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1cb162ae550eb4d3ba1d210387708411932a13c6.png)

and here’s the code i used (note this is really rough and i didn’t bother too much so no doubt it can be improved and you will need to mess with the inner character scaling)

```auto
String letters;
float defaultWidth, spread;
int layers, fontSize, scaleFactor;
int[] shadePalette;

void setup() {
  size(800, 600, P2D);
  fontSize = 100;
  scaleFactor = 10;
  textSize(fontSize);
  textAlign(CENTER, CENTER);
  letters = "TODAY";
  defaultWidth = textWidth(letters);
  spread = defaultWidth / letters.length();
  layers = 10;
  shadePalette = new int[layers];
  for(int i = 0; i < layers; i++) {
    //shadePalette[i] = 255 - (255 / (i + 1));
    shadePalette[i] = int(random(255));
  }
}

void draw() {
  background(0);
  int i, j;
  
  for(i = 0; i < layers; i++) {
    fill(shadePalette[i]);
    textSize(fontSize - i * 4);
    for(j = 0; j < letters.length(); j++) {
      char c = letters.charAt(j);
      text(c, (width / 2 - defaultWidth / 2) + (j * (fontSize - j * 4)), height / 2 + i);      
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 18, 2021, 10:28am UTC](https://discourse.processing.org/t/calculate-text-length/32885/6 "2021-10-18T10:28:47Z")

</div>

@yalaniv,

Tutorials related to text:

- [Strings and Drawing Text / Processing.org](https://processing.org/tutorials/text)
- [Typography / Processing.org](https://processing.org/tutorials/typography)

References:

- [Reference / Processing.org](https://processing.org/reference/String.html)

`:)`

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [October 20, 2021, 9:36am UTC](https://discourse.processing.org/t/calculate-text-length/32885/7 "2021-10-20T09:36:34Z")

</div>

I tried to copy all this code and run it, and got an error:  
gluegen-rt.dll: Can’t find dependent libraries  
A library relies on native code that’s not available.  
Or only works properly when the sketch is run as a 32-bit application.

I usually work with the 64-bit app

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [October 20, 2021, 2:42pm UTC](https://discourse.processing.org/t/calculate-text-length/32885/8 "2021-10-20T14:42:52Z")

</div>

this is raw processing 4 code. that error is unrelated to the code i posted.

---

<div class="post-metadata">

**Author:** ![yalaniv](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@yalaniv](https://discourse.processing.org/u/yalaniv)\
**Post date:** [October 20, 2021, 3:46pm UTC](https://discourse.processing.org/t/calculate-text-length/32885/9 "2021-10-20T15:46:38Z")

</div>

I just copy it and paste…  
What can be the error?
