Skip to content

Commit

Permalink
Fix font finder not working on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
naghim committed Nov 10, 2024
1 parent abf618d commit cba85fe
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 32 deletions.
65 changes: 56 additions & 9 deletions subassistant/gui/tab.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from PySide6.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QFileDialog, QHBoxLayout, QLineEdit, QScrollArea, QMessageBox, QTableWidget, QHeaderView, QTableWidgetItem, QCheckBox
from PySide6.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QFileDialog, QHBoxLayout, QLineEdit, QScrollArea, QMessageBox, QTableWidget, QHeaderView, QTableWidgetItem, QCheckBox, QAbstractItemView
from PySide6.QtCore import Qt
from PySide6.QtGui import QColor
from subassistant.logic.logic import RemoveComments, CommentDialogue
from subassistant.gui import util
from subassistant.logic.fonts import copy, font, subtitle, windows
from subassistant.logic.fonts import copy, font, fontfinder, subtitle
import os

class BaseSubtitleUI(QWidget):
Expand Down Expand Up @@ -116,7 +116,6 @@ class RemoveCommentTab(BaseSubtitleUI):
ACTION_CLASS = RemoveComments



class FontCheckerTab(QWidget):
TAB_TITLE = "Font Checker"

Expand Down Expand Up @@ -145,7 +144,6 @@ def __init__(self):
self.subtitle_layout.addWidget(self.input_btn)

self.layout.addLayout(self.subtitle_layout)


self.table_widget = QTableWidget()
self.table_widget.setColumnCount(3)
Expand All @@ -154,10 +152,59 @@ def __init__(self):
self.table_widget.horizontalHeader().setSectionResizeMode(2, QHeaderView.Stretch)
self.table_widget.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeToContents)
self.table_widget.setObjectName("TableWidget")
self.table_widget.setStyleSheet("QTableWidget#TableWidget {"
"border: 1px solid #dcdcdc;"
"border-radius: 5px;"
"}")

# No selection, no focus, no editing
self.table_widget.setSelectionMode(QAbstractItemView.NoSelection)
self.table_widget.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.table_widget.setFocusPolicy(Qt.NoFocus)

self.table_widget.setStyleSheet("""
/* Main table styling */
QTableWidget {
border: 1px solid #dcdcdc;
border-radius: 8px;
background-color: #f9f9f9;
color: #333;
font-size: 16px;
gridline-color: #ddd;
}
/* Header styling */
QHeaderView::section {
background-color: #333; /* Header background color */
color: white;
padding: 8px;
border: none;
font-weight: bold;
font-size: 12px;
border-right: 1px solid #e0e0e0; /* Optional divider */
}
/* Body row styling */
QTableWidget::item {
padding: 12px;
border-bottom: 1px solid #ddd; /* Subtle border between rows */
}
/* Vertical and horizontal scrollbars */
QScrollBar:vertical {
border: none;
background: #f1f1f1;
width: 10px;
margin: 22px 0 22px 0;
}
QScrollBar::handle:vertical {
background: #333;
min-height: 20px;
border-radius: 5px;
}
/* Absolutely no pink lines!! */
QTableWidget::item:focus {
outline: none;
}
""")

self.layout.addWidget(self.table_widget)

self.button_widget = QWidget()
Expand Down Expand Up @@ -225,7 +272,7 @@ def check_fonts(self):
self.check_button.setEnabled(False)

# First, find all installed fonts...
installed_font_ttfs = windows.find_installed_ttfs()
installed_font_ttfs = fontfinder.find_installed_ttfs()

# Second, find the font names of all installed fonts...
installed_fonts = {}
Expand Down
39 changes: 39 additions & 0 deletions subassistant/logic/fonts/fontfinder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os

def find_installed_ttfs():
installed_font_ttfs = []

if os.name == 'nt': # Windows
# Find all installed font TTFs
# There are two places to check:
# - System fonts (Windows)
# - User installed fonts (AppData)

import winreg
for registry_hive in [winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER]:
reg = winreg.ConnectRegistry(None, registry_hive)
key = winreg.OpenKey(reg, r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts', 0, winreg.KEY_READ)

for i in range(0, winreg.QueryInfoKey(key)[1]):
ttf = winreg.EnumValue(key, i)[1]

if '\\' not in ttf:
ttf = os.path.join(os.environ['WINDIR'], 'Fonts', ttf)

installed_font_ttfs.append(ttf)
elif os.name == 'posix': # Linux
# Common directories where fonts are installed on Linux
font_dirs = [
'/usr/share/fonts',
'/usr/local/share/fonts',
os.path.expanduser('~/.fonts'),
os.path.expanduser('~/.local/share/fonts')
]

for font_dir in font_dirs:
for root, _, files in os.walk(font_dir):
for file in files:
if file.lower().endswith('.ttf'):
installed_font_ttfs.append(os.path.join(root, file))

return installed_font_ttfs
23 changes: 0 additions & 23 deletions subassistant/logic/fonts/windows.py

This file was deleted.

0 comments on commit cba85fe

Please sign in to comment.