[vtkusers] Using fontconfig with vtkTextMapper/vtkTextProperty?

Elvis Stansvik elvis.stansvik at orexplore.com
Fri Oct 21 04:22:42 EDT 2016


2016-10-20 18:29 GMT+02:00 David Lonie <david.lonie at kitware.com>:
> On Thu, Oct 20, 2016 at 12:25 PM, Elvis Stansvik
> <elvis.stansvik at orexplore.com> wrote:
>>
>> Den 20 okt. 2016 5:30 em skrev "David Lonie" <david.lonie at kitware.com>:
>> >
>> > On Thu, Oct 20, 2016 at 11:27 AM, Elvis Stansvik
>> > <elvis.stansvik at orexplore.com> wrote:
>> >>
>> >> Using built-in fonts I also get text rendered smaller than expected.
>> >>
>> >> However, I think it's a matter of DPI. I guess VTK assumes a DPI of 72
>> >> somewhere?
>> >>
>> >> With the following, the text is rendered in the expected size:
>> >>
>> >>     auto logicalDpiY =
>> >> QApplication::screens().at(0)->logicalDotsPerInchY();
>> >>     ...
>> >>     textProperty->SetFontSize(qRound(10 * (logicalDpiY / 72.0)));
>> >>     textProperty->SetFontFamilyAsString("Noto Sans");
>> >>
>> >> So I'm compensating for VTK's assumption about 72 DPI by increasing
>> >> the font size (on my system Qt's logicalDotsPerInchY() reports
>> >> 96.2526).
>> >>
>> >> I guess I should also figure out how to be notified when the widget is
>> >> moved to another screen and update the size, since the DPI of the
>> >> other screen might be different.
>> >
>> >
>> > Aha, yes, VTK does assume 72 DPI by default. We very recently added the
>> > ability to change that for text rendering purposes. Call
>> > vtkRenderWindow::SetDPI to update it to what Qt is using, and the text
>> > rendering should adjust accordingly. Let me know if that fixes it.
>>
>> Aha, thanks for the tip. On my way home now but will test tomorrow. Do you
>> know if it's in 7.1rc1? I'm building my Debian package from that tag

SetDPI(...) worked great.

So in summary, in a VTK/Qt application, you can set the DPI to the one
Qt detects with

    GetRenderWindow()->SetDPI(qRound(windowHandle()->screen()->logicalDotsPerInch()));

in your QVTKWidget, and then configure the vtkTextProperty to
approximate the general system font with

    auto font = QFontDatabase::systemFont(QFontDatabase::GeneralFont);
    textProperty->SetFontSize(font.pointSize());
    textProperty->SetFontFamilyAsString(font.family().toUtf8().constData());
    textProperty->SetBold(font.bold());
    textProperty->SetItalic(font.italic());

Thanks for the pointers David!

Elvis

>
>
> It is. We added this about a year ago, but like the fontconfig stuff, it
> hasn't seen widespread use yet so any feedback would be great :)
>
> Dave


More information about the vtkusers mailing list