Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Font clipping of custom Italic fonts

Font clipping of custom Italic fonts

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 322 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mystique_
    wrote on last edited by
    #1

    I'm developing an application that will allow users to import new font to render some texts, I was also trying some fonts from Google Fonts but when I use an italic fonts I'm getting clipping issues either in capital letters or in other letters, for example:
    fe27298d-deb4-4876-8549-7b6b431e39ea-image.png

    What could I try to solve this issue?

    This is the font I was trying. but it happened also with other fonts:
    https://fonts.google.com/specimen/Inter

    Here is some basic code to reproduce this behavior:

    import sys
    import os
    from PySide6.QtWidgets import (
        QApplication,
        QWidget,
        QVBoxLayout,
        QTextEdit,
        QPushButton,
        QLabel 
    )
    from PySide6.QtGui import QFont, QFontDatabase
    from PySide6.QtCore import Qt
    
    class FontTestApp(QWidget):
        def __init__(self, font_path: str):
            super().__init__()
            self.font_path = font_path
            self.init_ui()
    
        def init_ui(self):
            self.setWindowTitle("PySide6 Custom Font Test")
            self.setGeometry(100, 100, 600, 400)
    
            layout = QVBoxLayout(self)
            font_family_name = self._load_custom_font()
            self.status_label = QLabel()
            layout.addWidget(self.status_label)
    
            if font_family_name:
                self.status_label.setText(f"Font '{font_family_name}' loaded successfully.")
                self.text_edit = QTextEdit()
                self.text_edit.setPlainText(
                     "This is a sample text displayed in the custom font.\n"
                     "CCCCCCCCDDDDDDDDDDDEEEEEEEEEEEEEOOOOOOOOOOOO"
                     "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
                     "Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
                     "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
                )
                custom_font = QFont(font_family_name, 12) 
                self.text_edit.setFont(custom_font)
                layout.addWidget(self.text_edit)
            self.close_button = QPushButton("Close")
            self.close_button.clicked.connect(QApplication.instance().quit)
            layout.addWidget(self.close_button)
    
        def _load_custom_font(self) -> str | None:
            if not os.path.exists(self.font_path):
                print(f"Error: Font file not found at '{self.font_path}'")
                return None
    
            font_id = QFontDatabase.addApplicationFont(self.font_path)
            if font_id == -1:
                print(f"Error: Could not load font from '{self.font_path}'. It might be corrupted or not a valid TTF.")
                return None
    
            font_families = QFontDatabase.applicationFontFamilies(font_id)
            if font_families:
                return font_families[0] # Return the first family name found in the font file
            else:
                print(f"Error: No font family names found in '{self.font_path}' after loading.")
                return None
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        FONT_FILE = r"Inter-Italic-VariableFont_opsz,wght.ttf"
        main_window = FontTestApp(FONT_FILE)
        main_window.adjustSize() 
        main_window.show()
        sys.exit(app.exec())
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Which version of PySide6 are you using ?
      On which OS ?
      I tried on macOS with PySide 6.9.1 and it worked correctly. I just had to use the addApplicationFontFromData method because for some reason it did not succeed when loading from the file path.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mystique_
        wrote on last edited by
        #3

        This was with PySide6 6.9.2, I also tried with 6.10.0
        The OS is Windows 10 22H2

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved