-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChooseFontsPlugin.py
165 lines (152 loc) · 7.9 KB
/
ChooseFontsPlugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import dearpygui.dearpygui as dpg
import os
#import ctypes
# Include the following code before showing the viewport/calling `dearpygui.dearpygui.show_viewport`.
#ctypes.windll.shcore.SetProcessDpiAwareness(2)
class ChooseFontsPlugin():
def __init__(self):
self.ignore = 'NO CUSTOM FONT, IGNORE'
self.font_registry = dpg.add_font_registry()
self.fontDict = dict()
self.create_folders()
self.create_font_library()
self.create_font_window()
self.create_font_menu()
def create_folders(self):
if not os.path.exists('Fonts'):
os.mkdir('Fonts')
if not os.path.exists('Fonts/USERFONT'):
with open('Fonts/USERFONT', 'w') as f:
f.write(self.ignore)
if not os.path.exists('Fonts/USERSIZE'):
with open('Fonts/USERSIZE', 'w') as f:
f.write("16")
if not os.path.exists('Fonts/USERSCALE'):
with open('Fonts/USERSCALE', 'w') as f:
f.write("1")
def create_font_library(self):
with open('Fonts/USERFONT', 'r') as f:
self.userFont = f.read()
if not self.userFont: self.userFont = self.ignore
with open('Fonts/USERSIZE', 'r') as f:
try: self.userSize = int(f.read())
except: self.userSize = 16
if not self.userSize: self.userSize = 16
with open('Fonts/USERSCALE', 'r') as f:
try: self.userScale = float(f.read())
except: self.userScale = 1
if not self.userScale: self.userScale = 1
self.fontDict["YOUR FONTS"] = None
for filename in os.listdir('Fonts'):
if filename.endswith((".ttf","otf")):
self.fontDict[filename] = f"Fonts/{filename}"
with dpg.font(self.fontDict[filename], 16, parent=self.font_registry) as f:
dpg.add_font_range_hint(dpg.mvFontRangeHint_Default)
dpg.add_font_range_hint(dpg.mvFontRangeHint_Cyrillic)
dpg.add_font_range(0x0, 0xF)
dpg.add_font_range(0x00, 0xFF)
dpg.add_font_range(0x000, 0xFFF)
dpg.add_font_range(0x0000, 0xFFFF)
dpg.bind_font(f)
self.fontDict["WINDOWS FONTS"] = None
try:
for filename in os.listdir('C:\\Windows\\Fonts'):
if filename.endswith((".ttf","otf")):
self.fontDict[filename] = f"C:\\Windows\\Fonts\\{filename}"
except:
self.fontDict.pop("WINDOWS FONTS")
if len(self.fontDict) > 0:
if self.userFont in self.fontDict:
dpg.add_font(self.fontDict[self.userFont], size=self.userSize, parent=self.font_registry, tag="fntCFPNewFont")
dpg.add_font_chars([0x2013,0x2014,0x2015,0x2017,0x2018,0x2019,0x201A,0x201B,0x201C,0x201D,0x201E,0x2020,0x2021,0x2022,0x2026,0x2030,0x2032,0x2033,0x2039,0x203A,0x203C,0x203E,0x2044,0x204A], parent="fntCFPNewFont")
dpg.bind_font("fntCFPNewFont")
def create_font_window(self):
with dpg.window(label="Font Menu", width=400, height=400, show=False, tag="winCFPFontWindow", pos=(int(dpg.get_viewport_width()/2 - 200), int(dpg.get_viewport_height() / 2 - 200))):
with dpg.child_window(autosize_x=True, height=-120):
dpg.add_text("This is the font window. To use it, find your Fonts folder, and put in any .ttf or .otf font, and they will show up here.", wrap=0)
dpg.add_text("Whatever font you choose will be applied to the whole application. Sometimes it's best to close and reopen the application after setting a font, in case things get weird.", wrap=0)
dpg.add_combo(list(self.fontDict.keys()), label="Font Type", callback=lambda:self.build_fonts(), tag="cmbCFPFontType")
dpg.add_input_int(label="Size", default_value=self.userSize, callback=lambda:self.build_fonts(), tag="intCFPSize")
dpg.add_slider_float(default_value=self.userScale, label="Scale", callback=lambda:self.build_fonts(), tag="slideCFPFontScale", min_value=0.0, max_value=2.0, clamped=True)
with dpg.group(horizontal=True):
dpg.add_checkbox(label="Autosize?", tag="chkbxCFPAutosize", default_value=True)
dpg.add_button(label="Change Font", callback=lambda: self.btnBuild())
with dpg.child_window(autosize_x=True, height=100):
dpg.add_text("the quick brown fox jumped over the lazy dog", wrap=0)
dpg.add_text("THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG", wrap=0)
with dpg.group(horizontal=True):
dpg.add_button(label="Close", callback=lambda: dpg.configure_item("winCFPFontWindow", show=False))
dpg.add_button(label="Save", callback=lambda: self.save_fonts())
dpg.add_button(label="Reset to Default (Your Saves Won't Be Changed)", callback=lambda: dpg.bind_font("DEFAULT") and dpg.set_global_font_scale(1.0))
def create_font_menu(self):
self.font_menu = dpg.add_menu(label="Font")
dpg.add_menu_item(label="Edit", callback=lambda: dpg.configure_item("winCFPFontWindow", show=True), parent=self.font_menu)
def btnBuild(self):
dpg.set_value("chkbxCFPAutosize", True)
self.build_fonts()
dpg.set_value("chkbxCFPAutosize", False)
def build_fonts(self):
if not dpg.get_value("chkbxCFPAutosize"): return
self.userFont = dpg.get_value("cmbCFPFontType")
self.userSize= dpg.get_value("intCFPSize")
if self.userFont not in self.fontDict: return
if not self.fontDict[self.userFont]: return
try:
if dpg.does_item_exist("fntCFPNewFont"): dpg.delete_item("fntCFPNewFont")
newFont = dpg.add_font(self.fontDict[self.userFont], size=self.userSize, parent=self.font_registry, tag="fntCFPNewFont")
dpg.add_font_range_hint(dpg.mvFontRangeHint_Default, parent="fntCFPNewFont")
dpg.add_font_range_hint(dpg.mvFontRangeHint_Cyrillic, parent="fntCFPNewFont")
dpg.add_font_chars([0x2013,0x2014,0x2015,0x2017,0x2018,0x2019,0x201A,0x201B,0x201C,0x201D,0x201E,0x2020,0x2021,0x2022,0x2026,0x2030,0x2032,0x2033,0x2039,0x203A,0x203C,0x203E,0x2044,0x204A], parent="fntCFPNewFont")
dpg.add_font_range(0x0, 0xF, parent="fntCFPNewFont")
dpg.add_font_range(0x00, 0xFF, parent="fntCFPNewFont")
dpg.add_font_range(0x000, 0xFFF, parent="fntCFPNewFont")
dpg.add_font_range(0x0000, 0xFFFF, parent="fntCFPNewFont")
dpg.bind_font(newFont)
self.userScale = float(dpg.get_value("slideCFPFontScale"))
dpg.set_global_font_scale(self.userScale)
except Exception as e:
print(e)
def save_fonts(self):
with open('Fonts/USERFONT', 'w') as f:
f.write(str(self.userFont))
with open('Fonts/USERSIZE', 'w') as f:
f.write(str(self.userSize))
with open('Fonts/USERSCALE', 'w') as f:
f.write(str(self.userScale))
dpg.configure_item("winCFPFontWindow", show=False)
if __name__ == "__main__":
dpg.create_context()
dpg.create_viewport(title='Custom Title', width=1200, height=800)
#from EditThemePlugin import EditThemePlugin
dpg.show_debug()
try:
with dpg.window(tag="main2"):
with dpg.child_window():
dpg.add_text("This is text")
dpg.add_button(tag="This is a button", label="THIS IS A BUTTON")
dpg.add_checkbox(label="Check Box")
with dpg.child_window(autosize_x=True, autosize_y=True):
with dpg.tab_bar():
with dpg.tab(label="THIS IS A TAB"):
with dpg.tree_node(label="THIS IS A TREE NODE"):
randListOfStuff = ['THIS', 'IS', 'A', 'LIST']
dpg.add_combo(randListOfStuff)
dpg.add_listbox(randListOfStuff)
with dpg.viewport_menu_bar():
with dpg.menu(label="Tools"):
dpg.add_menu_item(label="Show About", callback=lambda:dpg.show_tool(dpg.mvTool_About))
dpg.add_menu_item(label="Show Metrics", callback=lambda:dpg.show_tool(dpg.mvTool_Metrics))
dpg.add_menu_item(label="Show Documentation", callback=lambda:dpg.show_tool(dpg.mvTool_Doc))
dpg.add_menu_item(label="Show Debug", callback=lambda:dpg.show_tool(dpg.mvTool_Debug))
dpg.add_menu_item(label="Show Style Editor", callback=lambda:dpg.show_tool(dpg.mvTool_Style))
dpg.add_menu_item(label="Show Font Manager", callback=lambda:dpg.show_tool(dpg.mvTool_Font))
dpg.add_menu_item(label="Show Item Registry", callback=lambda:dpg.show_tool(dpg.mvTool_ItemRegistry))
#myEditTheme = EditThemePlugin()
myFonts = ChooseFontsPlugin()
except:
pass
dpg.set_primary_window("main2", True)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()