How can I - get the width of a caracter

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
JSRoy
Junior Member
Posts: 11
Joined: Fri Sep 22, 2017 9:28 pm

How can I - get the width of a caracter

Post by JSRoy » Sat Mar 09, 2019 7:16 pm

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:

Code: Select all

include("scripts/library.js");
include("scripts/Draw/Draw.js");
include("scripts/WidgetFactory.js");
include("scripts/Draw/Text/TextDialog/TextDialog.js");

function FunnyText(inText, centreX, centreY){

    var doc = getTransactionDocument();
    if (isNull(doc)) {
        return undefined;
    }

    var height = 1.0;
    var angle = 0.0;
    var font = "Standard";
    var vAlign = RS.VAlignTop;
    var hAlign = RS.HAlignLeft;
    var bold = false;
    var italic = false;

    this.textData = new RTextData();
    textData.text = inText;

    this.textEntity = new RTextEntity(doc, textData);

//Get text width 
    var num = textData.text.length;
    var charWidths = [];
    var totalCharWidth = 0.0;

    for (var i = 0; i < num; i++) {
        var char = textData.text[i];
        // A space character is returned as zero width
        if (char === " ") {
            char = "-";
        }
        var td = getTextData(char);
        var wid = td.getTextWidth()
        charWidths.push(wid);
        totalCharWidth += wid;
    }

    writeTextFile('/home/jsroy/OUTPUT', num + " | " + this.textData.getTextWidth());
}

FunnyText.prototype = new Draw();


getTextData = function(txt) {
    var td = new RTextData();
    if (this.pos !== undefined) {
        td.setAlignmentPoint(this.pos);
    }
    td.setText(txt);
    td.setTextHeight(10);
    td.setTextWidth(this.textData.getTextWidth());

    return td;
}

FunnyText("Janvier", 0, 0);
Unfortunatly, when I read my OUTPUT file, I notice my width is zero.

Any help would be appreciated.

Thank you,

Jean

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

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

Post by andrew » Mon Mar 11, 2019 10:23 am

A few problems with that code:
  • this.textData and textData are two different variables
  • 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().

JSRoy
Junior Member
Posts: 11
Joined: Fri Sep 22, 2017 9:28 pm

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

Post by JSRoy » Tue Mar 19, 2019 2:16 am

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?

To get these results, I used the font "Default" and text height of 2.

Thanks again,

J-S

Code: Select all

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

JSRoy
Junior Member
Posts: 11
Joined: Fri Sep 22, 2017 9:28 pm

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

Post by JSRoy » Tue Mar 19, 2019 12:53 pm

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?

If so, any idea where I get this information from?

Thank you,

Jean

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

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

Post by andrew » Tue Mar 19, 2019 5:13 pm

Code: Select all

entity.getLineweight()
This can return the value RLineweight.WeightByLayer which means you'll have to look up the layer of the entity and query its lineweight:

Code: Select all

layer.getLineweight()

JSRoy
Junior Member
Posts: 11
Joined: Fri Sep 22, 2017 9:28 pm

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

Post by JSRoy » Wed Apr 10, 2019 5:39 pm

My code effectively tells me that the weight is in the layer. But I can't figure out how to proceed from here.

What confuses me is that I don't see a layer.getLineweight in the file Layer.js. So I have to use something else.

Also, since I need the current layer, the only function I found I could use is: RDocument.getCurrentLayerId().

But even with that, I can't figure out how I would use the returned RLayer::Id since I see no constructors of a Layer that uses RLayer::Id.




Again, thank you for your help!

J-S

User avatar
andrew
Site Admin
Posts: 9037
Joined: Fri Mar 30, 2007 6:07 am

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

Post by andrew » Wed Apr 10, 2019 9:52 pm

Code: Select all

var layerId = entity.getLayerId();
var layer = document.queryLayer(layerId);
var layerLineweight = layer.getLineweight();
Hope that helps.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”