Project

General

Profile

WPainteDevice, WFontmetrics and measureText

Added by Paul T... almost 9 years ago

Hello all,

I'm trying to determine the length of a text which I draw in a WPaintedWidget.

In my paintEvent function, I try to do a pPaintDevice->measureText(testString); But it crashed...

I discovered that pPaintDevice->features was returning 0, indicating that the PaintDevice has not the FontMetrics option...

How can I enable this FontMetric option ???

Thanks for any help.

Paul


Replies (4)

RE: WPainteDevice, WFontmetrics and measureText - Added by Wim Dumon almost 9 years ago

Paul,

It is not possible to obtain font metrics for client-side rendered paint devices. The font metrics are only available for WRasterImage, which renders an image server-side, where Wt knows exactly what fonts and what font renderer will be used to render the text.

So the work around is to create a raster image, query font metrics for the server-side fonts, and assume this is a good enough approximation for the client-side font.

Wim.

RE: WPainteDevice, WFontmetrics and measureText - Added by Paul T... almost 9 years ago

Hi Wim,

Thanks for your answer, I discovered that late yesterday, no way to measureText...

It's kind of annoying because I want to drawText and according to the size that it takes, dynamically adapt the rest of the display...

I saw a Javascript code that was measuring text... I don't know nothing about Javascript... do you think there is a way to make it work in wt ?

$.fn.textWidth = function(text, font) {

if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('').hide().appendTo(document.body);

$.fn.textWidth.fakeEl.html(text || this.val() || this.text()).css('font', font || this.css('font'));

return $.fn.textWidth.fakeEl.width();

};

RE: WPainteDevice, WFontmetrics and measureText - Added by Wim Dumon almost 9 years ago

Paul,

I'm afraid that obtaining client-side font metris is quite hard with the server-side rendering approach that Wt has chosen. If the approximation that I proposed (using WRasterImage) is not good enough for your use case, it may be necessary to implement the rendering in a JavaScript widget.

I wouldn't use that method to measure the text size for a canvas; a canvas seems to have a built-in method of determining text sizes:

https://html.spec.whatwg.org/multipage/scripting.html#dom-context-2d-measuretext

wim.

RE: WPainteDevice, WFontmetrics and measureText - Added by Paul T... almost 9 years ago

WRasterImage needs ImageMagick isn't it ? I did not compiled IM that is why I was trying something else. (I'm on an embedded platform under QNX)

I must admit that Javascript is chinese to me... and I'm a begginer on Wt...so the combination of the 2 is pretty....messy...

Can you help me with that ?

Regards,

Paul

    (1-4/4)