-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfontdialog.lua
343 lines (298 loc) · 9.48 KB
/
fontdialog.lua
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
--
-- Font Dialog Widget.
-- @copyright Jefferson Gonzalez
-- @license MIT
--
local core = require "core"
local style = require "core.style"
local Button = require "widget.button"
local CheckBox = require "widget.checkbox"
local NumberBox = require "widget.numberbox"
local Dialog = require "widget.dialog"
local Label = require "widget.label"
local Line = require "widget.line"
local SelectBox = require "widget.selectbox"
local MessageBox = require "widget.messagebox"
local Fonts = require "widget.fonts"
---@class widget.fontdialog.fontoptions : renderer.fontoptions
---@field size number
---@class widget.fontdialog : widget.dialog
---@overload fun(font?:widget.fontslist.font, options?:widget.fontdialog.fontoptions):widget.fontdialog
---@field super widget.dialog
---@field fontdata widget.fontslist.font
---@field preview widget.label
---@field font_size widget.numberbox
---@field choose widget.button
---@field choose_mono widget.button
---@field line widget.line
---@field antialiasing widget.selectbox
---@field hinting widget.selectbox
---@field bold widget.checkbox
---@field italic widget.checkbox
---@field underline widget.checkbox
---@field smoothing widget.checkbox
---@field strikethrough widget.checkbox
---@field save widget.button
---@field cancel widget.button
local FontDialog = Dialog:extend()
---Constructor
---@param font? widget.fontslist.font
---@param options? widget.fontdialog.fontoptions
function FontDialog:new(font, options)
FontDialog.super.new(self, "Font Selector")
self.selected = nil
local this = self
self.type_name = "widget.fontdialog"
self.preview = Label(self.panel, "No Font Selected")
self.preview.border.width = 1
self.preview.clickable = true
function self.preview:on_mouse_enter(...)
Label.super.on_mouse_enter(self, ...)
self.border.color = style.caret
end
function self.preview:on_mouse_leave(...)
Label.super.on_mouse_leave(self, ...)
self.border.color = style.text
end
self.font_size = NumberBox(self.panel, 15, 5)
function self.font_size:on_change()
this:update_preview()
end
self.choose = Button(self.panel, "All")
self.choose:set_icon("D")
self.choose:set_tooltip("Choose a Font")
function self.choose:on_click()
Fonts.show_picker(function(name, path)
local fontdata = {name = name, path = path}
this:set_font(fontdata)
this:update_preview()
end, false)
core.status_view:remove_tooltip()
end
self.choose_mono = Button(self.panel, "Mono")
self.choose_mono:set_icon("D")
self.choose_mono:set_tooltip("Choose a Monospace Font")
function self.choose_mono:on_click()
Fonts.show_picker(function(name, path)
local fontdata = {name = name, path = path}
this:set_font(fontdata)
this:update_preview()
end, true)
core.status_view:remove_tooltip()
end
self.line = Line(self.panel)
self.antialiasing = SelectBox(self.panel, "antialiasing")
self.antialiasing:add_option("None", "none")
self.antialiasing:add_option("Grayscale", "grayscale")
self.antialiasing:add_option("Subpixel", "subpixel")
function self.antialiasing:on_selected()
this:update_preview()
end
self.hinting = SelectBox(self.panel, "hinting")
self.hinting:add_option("None", "none")
self.hinting:add_option("Slight", "slight")
self.hinting:add_option("full", "full")
function self.hinting:on_selected()
this:update_preview()
end
self.bold = CheckBox(self.panel, "Bold")
function self.bold:on_checked()
this:update_preview()
end
self.italic = CheckBox(self.panel, "Italic")
function self.italic:on_checked()
this:update_preview()
end
self.underline = CheckBox(self.panel, "Underline")
function self.underline:on_checked()
this:update_preview()
end
self.smoothing = CheckBox(self.panel, "Smooth")
function self.smoothing:on_checked()
this:update_preview()
end
self.strikethrough = CheckBox(self.panel, "Strike")
function self.strikethrough:on_checked()
this:update_preview()
end
self.save = Button(self.panel, "Save")
self.save:set_icon("S")
function self.save:on_click()
if this.fontdata and this.fontdata.name then
this:on_save(this:get_font(), this:get_options())
this:on_close()
else
MessageBox.error("No font selected", "Please select a font")
end
end
self.cancel = Button(self.panel, "Cancel")
self.cancel:set_icon("C")
function self.cancel:on_click()
this:on_close()
end
if font then
self:set_font(font)
if not options then
self:update_preview()
end
end
if options then
self:set_options(options)
end
end
function FontDialog:update_preview()
local options = self:get_options()
if self.fontdata and self.fontdata.path then
self.preview.font = renderer.font.load(
self.fontdata.path, options.size * SCALE, options
)
local fontmeta = renderer.font.get_metadata(self.fontdata.path) or {}
local preview = fontmeta.sampletext or self.fontdata.name
if preview then
self.preview:set_label(preview)
end
else
self.preview.font = renderer.font.load(
DATADIR .. "/fonts/FiraSans-Regular.ttf",
options.size * SCALE,
options
)
end
collectgarbage "step"
end
---@param font widget.fontslist.font
function FontDialog:set_font(font)
self.fontdata = font
if self.fontdata.name then
self.preview:set_label(self.fontdata.name)
end
end
---@return widget.fontslist.font
function FontDialog:get_font()
return self.fontdata
end
---@param options widget.fontdialog.fontoptions
function FontDialog:set_options(options)
if options.size then
self.font_size:set_value(tonumber(options.size) or 15)
end
if options.antialiasing then
if options.antialiasing == "none" then
self.antialiasing:set_selected(1)
elseif options.antialiasing == "grayscale" then
self.antialiasing:set_selected(2)
elseif options.antialiasing == "subpixel" then
self.antialiasing:set_selected(3)
end
end
if options.hinting then
if options.hinting == "none" then
self.hinting:set_selected(1)
elseif options.hinting == "slight"then
self.hinting:set_selected(2)
elseif options.hinting == "full" then
self.hinting:set_selected(3)
end
end
if options.bold ~= nil then
self.bold:set_checked(options.bold)
end
if options.italic ~= nil then
self.italic:set_checked(options.italic)
end
if options.underline ~= nil then
self.underline:set_checked(options.underline)
end
if options.smoothing ~= nil then
self.smoothing:set_checked(options.smoothing)
end
if options.strikethrough ~= nil then
self.strikethrough:set_checked(options.strikethrough)
end
end
---@return widget.fontdialog.fontoptions
function FontDialog:get_options()
return {
size = self.font_size:get_value(),
antialiasing = self.antialiasing:get_selected_data() or "none",
hinting = self.hinting:get_selected_data() or "none",
bold = self.bold:is_checked(),
italic = self.italic:is_checked(),
underline = self.underline:is_checked(),
smoothing = self.smoothing:is_checked(),
strikethrough = self.strikethrough:is_checked()
}
end
---Called when the user clicks on save
---@param font widget.fontslist.font
---@param options widget.fontdialog.fontoptions
function FontDialog:on_save(font, options) end
function FontDialog:update_size_position()
FontDialog.super.update_size_position(self)
self.preview:set_position(style.padding.x/2, style.padding.y/2)
self.preview:set_size(100, 75 * SCALE)
self.font_size:set_position(
style.padding.x/2,
self.preview:get_bottom() + style.padding.y
)
self.choose:set_position(
self.font_size:get_right() + (style.padding.x/2),
self.preview:get_bottom() + style.padding.y
)
self.choose_mono:set_position(
self.choose:get_right() + (style.padding.x/2),
self.preview:get_bottom() + style.padding.y
)
self.line:set_position(
0,
self.font_size:get_bottom() + style.padding.y
)
self.antialiasing:set_position(
style.padding.x/2,
self.line:get_bottom() + style.padding.y
)
self.hinting:set_position(
self.antialiasing:get_right() + (style.padding.x/2),
self.line:get_bottom() + style.padding.y
)
self.bold:set_position(
style.padding.x/2,
self.hinting:get_bottom() + style.padding.y
)
self.italic:set_position(
self.bold:get_right() + style.padding.x,
self.hinting:get_bottom() + style.padding.y
)
self.underline:set_position(
self.italic:get_right() + style.padding.x,
self.hinting:get_bottom() + style.padding.y
)
self.smoothing:set_position(
self.underline:get_right() + style.padding.x,
self.hinting:get_bottom() + style.padding.y
)
self.strikethrough:set_position(
self.smoothing:get_right() + style.padding.x,
self.hinting:get_bottom() + style.padding.y
)
self.save:set_position(
style.padding.x/2,
self.underline:get_bottom() + style.padding.y
)
self.cancel:set_position(
self.save:get_right() + style.padding.x,
self.underline:get_bottom() + style.padding.y
)
self.panel.size.x = self.panel:get_real_width() + style.padding.x
self.panel.size.y = self.panel:get_real_height() + style.padding.y
self.size.x = self:get_real_width() - (style.padding.x / 2)
self.size.y = self:get_real_height()
self.line:set_width(self.size.x - style.padding.x)
self.preview:set_size(self.hinting:get_right() - style.padding.x / 2)
self.size.x = self.preview:get_right() + style.padding.x / 2
self.close:set_position(
self.size.x - self.close.size.x - (style.padding.x / 2),
style.padding.y / 2
)
end
return FontDialog