diff options
author | ThaCuber <70547062+thacuber2a03@users.noreply.github.com> | 2024-03-28 14:32:03 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-28 19:32:03 +0100 |
commit | 6506a5336d39911c12cc1b647cb0a0c1bfd39e30 (patch) | |
tree | 38100d3d2d132aa6e4cb8c8fe85295a0f99b0f36 | |
parent | 4077626507f857bfccadcd83bcb08810aa3a35a0 (diff) | |
download | lite-xl-plugins-6506a5336d39911c12cc1b647cb0a0c1bfd39e30.tar.gz lite-xl-plugins-6506a5336d39911c12cc1b647cb0a0c1bfd39e30.zip |
add font preview plugin (#392)
* add font preview plugin
* remove get_scrollable_size and unused variable
* add a list of supported font types
I also realized I haven't been returning the view that `open_doc`
returns, so I fixed that too
* add rows for every printable character in ASCII
* deactivate vertical scrolling
* properly mark patterns in supported_types list
Co-authored-by: Guldoman <giulio.lettieri@gmail.com>
---------
Co-authored-by: Guldoman <giulio.lettieri@gmail.com>
-rw-r--r-- | manifest.json | 7 | ||||
-rw-r--r-- | plugins/fontpreview.lua | 75 |
2 files changed, 82 insertions, 0 deletions
diff --git a/manifest.json b/manifest.json index 64819a3..fa0a2d2 100644 --- a/manifest.json +++ b/manifest.json @@ -301,6 +301,13 @@ "version": "0.1" }, { + "description": "Automatically displays a font with increasing sizes in a new view.", + "version": "0.1", + "path": "plugins/fontpreview.lua", + "id": "fontpreview", + "mod_version": "3" + }, + { "description": "Debug Lite-XL's Lua VM interactively, if you're running it from a terminal. Warning: Will significantly slow down Lite-XL if installed.", "id": "lite-debugger", "mod_version": "3", diff --git a/plugins/fontpreview.lua b/plugins/fontpreview.lua new file mode 100644 index 0000000..0ce8fc0 --- /dev/null +++ b/plugins/fontpreview.lua @@ -0,0 +1,75 @@ +--mod-version:3 --priority:110 + +local common = require 'core.common' +local style = require 'core.style' +local View = require 'core.view' + +local FontView = View:extend() + +local font_text = "The quick brown fox jumps over the lazy dog." + +function FontView:new(path) + FontView.super.new(self) + self.path = path + self.fonts = {} + for i=1, 8 do self.fonts[i] = renderer.font.load(path, (12+i*7)*SCALE) end + self.scrollable = true +end + +function FontView:get_h_scrollable_size() + return self.fonts[#self.fonts]:get_width(font_text) + style.padding.x +end + +function FontView:get_scrollable_size() return 0 end + +function FontView:get_name() + return "Font: " .. self.path +end + +local function draw_next_row(fv, y, text, font) + local _ + _, y = common.draw_text( + font, style.text, text, "left", + fv.position.x - fv.scroll.x + style.padding.x, y - fv.scroll.y, + 0, style.padding.y + font:get_height() / 2 + ) + return y +end + +function FontView:draw() + self:draw_background(style.background) + + local y = self.position.y + self.fonts[1]:get_height() / 2 + style.padding.y + + for i=1, #self.fonts do + y = draw_next_row(self, y, font_text, self.fonts[i]) + end + + local font = self.fonts[1] + y = draw_next_row(self, y, "abcdefghijklmnopqrstuvwxyz", font) + y = draw_next_row(self, y, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", font) + y = draw_next_row(self, y, "0123456789", font) + y = draw_next_row(self, y, "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", font) + + self:draw_scrollbar() +end + +local supported_types = { "%.ttf$", "%.otf$" } + +local RootView = require 'core.rootview' +local open_doc = RootView.open_doc +function RootView:open_doc(doc) + local path = doc.filename or doc.abs_filename or "" + + for _, v in ipairs(supported_types) do + if path:find(v) then + local node = self:get_active_node_default() + local view = FontView(path) + node:add_view(view) + self.root_node:update_layout() + return view + end + end + + return open_doc(self, doc) +end |