QCAD.org Forum

How can I - get the width of a caracter

How can I - get the width of a caracter

Posted: Sat Mar 09, 2019 7:16 pm

by JSRoy

Hi,

I'm trying to figure out the width a character would have before I draw it. For example, if I want to draw "Hello", I would like to know the width of the letters "H", "e", "l" and "o" and finally put that information in an array for usage further along

I'm new at JS/ECMA and find it a bit hard to use. What I have so far, after having looked at TextAlong and other scripts, is:

Re: How can I - get the width of a caracter

getTextData is declared as a global function. this in the context of a global function is the function, not the class. Use FunnyText.prototype.getTextData = function(...) {...}

getTextWidth refers to a static property of the text (textWidth). To get the actual, calculated width (of a text with angle 0) use this.textData.getBoundingBox().getWidth().

Re: How can I - get the width of a caracter

Posted: Tue Mar 19, 2019 2:16 am

by JSRoy

Thanks for the help Andrew.

With your help I managed to get further along, but I still have some problems.

I created a RTextData for each letter of the alphabet, and extracted the width of each using functions RTextData.getWidth() and RTextData.getBoundingBox().getWidth(), but got funny results for the letter "i". As you can see from my log file, "i" has a width of 0. Do you have any idea why that is? Or better yet, what would you recommend doing to get the actual painted width of a letter?

RTextData.getWidth(a) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(a) is 0.8888888888888888
RTextData.getWidth(b) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(b) is 0.8888888888888888
RTextData.getWidth(c) is 0.6666666666666666 RTextData.getBoundingBox().getWidth(c) is 0.6666666666666666
RTextData.getWidth(d) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(d) is 0.8888888888888888
RTextData.getWidth(e) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(e) is 0.8888888888888888
RTextData.getWidth(f) is 0.6666666666666666 RTextData.getBoundingBox().getWidth(f) is 0.6666666666666666
RTextData.getWidth(g) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(g) is 0.8888888888888888
RTextData.getWidth(h) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(h) is 0.8888888888888888
RTextData.getWidth(i) is 0 RTextData.getBoundingBox().getWidth(i) is 0
RTextData.getWidth(j) is 0.4444444444444444 RTextData.getBoundingBox().getWidth(j) is 0.4444444444444444
RTextData.getWidth(k) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(k) is 0.8888888888888888
RTextData.getWidth(l) is 0.22222222222222215 RTextData.getBoundingBox().getWidth(l) is 0.22222222222222215
RTextData.getWidth(m) is 1.3333333333333333 RTextData.getBoundingBox().getWidth(m) is 1.3333333333333333
RTextData.getWidth(n) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(n) is 0.8888888888888888
RTextData.getWidth(o) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(o) is 0.8888888888888888
RTextData.getWidth(p) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(p) is 0.8888888888888888
RTextData.getWidth(q) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(q) is 0.8888888888888888
RTextData.getWidth(r) is 0.6666666666666666 RTextData.getBoundingBox().getWidth(r) is 0.6666666666666666
RTextData.getWidth(s) is 0.8887839145950894 RTextData.getBoundingBox().getWidth(s) is 0.8886789403012909
RTextData.getWidth(t) is 0.6666666666666666 RTextData.getBoundingBox().getWidth(t) is 0.6666666666666666
RTextData.getWidth(u) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(u) is 0.8888888888888888
RTextData.getWidth(v) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(v) is 0.8888888888888888
RTextData.getWidth(w) is 1.3333333333333333 RTextData.getBoundingBox().getWidth(w) is 1.3333333333333333
RTextData.getWidth(x) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(x) is 0.8888888888888888
RTextData.getWidth(y) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(y) is 0.8888888888888888
RTextData.getWidth(z) is 0.8888888888888888 RTextData.getBoundingBox().getWidth(z) is 0.8888888888888888

Re: How can I - get the width of a caracter

Posted: Tue Mar 19, 2019 12:53 pm

by JSRoy

I think I understand now. Is it possible that the getWidth() and getBoundingBox().getWidth() will get the X-axis travel distance the pen needs to take to draw a letter only? It does not take into account the pen width (or line width)?

So, since the letter "i" is a vertical line, the getWidth() will return 0. If I want to get the real width of "i" drawn on the screen, I need to add the width of the pen?