How to set font family when adding scale bar

Hi,

I am using the following example script on ovito website to add scale bar on my plots. However, I came across a problem:

DirectWrite: CreateFontFaceFromHDC() failed for QFontDef(Family=“MS Sans Serif”, pointsize=20, pixelsize=29, styleHint=5, weight=400, stretch=100, hintingPreference=0) LOGFONT(“MS Sans Serif”, lfWidth=0, lfHeight=-29) dpi=96

It looks like a font error, and I am writing to ask how to change the font style in this case.

Thanks in advance.

BR,
Fan

class ScaleBarOverlay(ViewportOverlayInterface):

    # Adjustable user parameters:

    # World-space length of the scale bar:
    length = Range(value=4.0, low=0.0, label='Length (nm)')

    # Screen-space height of the scale bar:
    height = Range(value=0.05, low=0.0, high=0.2, label='Height')

    # Bar color:
    bar_color = Color(default=(0.0, 0.0, 0.0), label='Bar color')

    # Text color:
    text_color = Color(default=(1.0, 1.0, 1.0), label='Text color')

    def render(self, canvas: ViewportOverlayInterface.Canvas, data: DataCollection, **kwargs):

        # Compute the center coordinates of the simulation cell.
        center = data.cell @ (0.5, 0.5, 0.5, 1.0)

        # Compute length of bar in screen space - as a fraction of the canvas height.
        screen_length = canvas.project_length(center, self.length)

        # Convert from nanometers to simulation units of length (Angstroms) and
        # convert from vertical to horizontal canvas coordinates by multiplying with the h/w aspect ratio.
        screen_length *= 10 * canvas.logical_size[1] / canvas.logical_size[0]

        # Create a 1-by-1 pixel image for drawing the bar rectangle.
        image = QtGui.QImage(1, 1, canvas.preferred_qimage_format)
        image.fill(QtGui.QColor.fromRgbF(*self.bar_color))

        # Draw the bar rectangle.
        canvas.draw_image(image, pos=(0.01, 0.01), size=(screen_length, self.height), anchor="south west")

        # Draw the text label.
        canvas.draw_text(f"{self.length:.3} nm",
                         pos=(0.01 + 0.5*screen_length, 0.01 + 0.5*self.height),
                         font_size=self.height,
                         anchor="center",
                         color=self.text_color)

Hi,
I’d like to clarify a few things:

  • Which operating system are you using?
  • Which OVITO version do you have installed?
  • Did you install OVITO using pip, conda, or as a standalone application?

Hi,

Thanks for your kind reply and I am sorry for the foggy description.

  1. The operating system is Win11 24H2 26100.3194 (Chinese Version).
  2. The OVITO python version is 3.11.3.
  3. OVITO python was installed using conda under miniconda3.

Hope it will be helpful and if any other necessary information is required, pls let me know.

Thanks!

BR,
Fan

Hi,

I just want to present some additional information:

After I turned to Ubuntu 22.04 ( the host is wsl2 on Windows), the error disappeared and it works.

BR,
Fan

Thank you for the additional information. I can’t reproduce this error on my Windows 11 machine, so it might be related to the default fonts installed on the Chinese version of Windows compared to the European one.

In any case, you can change the font using HTML commands in the draw_text command, like this:

canvas.draw_text(f"<span style='font-family: Palatino;'>{self.length:.3} nm</span>", ...)

Here, Palatino would be the font used for this label.

Hi,

Thank you very much for the instructions. I really appreciate it.

Have a nice rest of the week!

BR,
Fan