Support for changing fonts per syntax group (#178)

This commit is contained in:
liquidev 2021-05-05 23:08:10 +02:00 committed by GitHub
parent 135dfa6f03
commit b76905e78a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 15 deletions

View File

@ -2,6 +2,20 @@ Lite XL is following closely [rxi/lite](https://github.com/rxi/lite) but with so
This files document the changes done in Lite XL for each release. This files document the changes done in Lite XL for each release.
### 1.16.9
[#126](https://github.com/franko/lite-xl/issues/126): Implemented changing fonts per syntax group.
Example user module snippet that makes all comments italic:
```lua
local style = require "core.style"
-- italic.ttf must be provided by the user
local italic = renderer.font.load("italic.ttf", 14)
style.syntax_fonts["comment"] = italic
```
### 1.16.8
### 1.16.7 ### 1.16.7
Add support for retina displays on Mac OS X. Add support for retina displays on Mac OS X.

View File

@ -141,20 +141,35 @@ end
function DocView:get_col_x_offset(line, col) function DocView:get_col_x_offset(line, col)
local text = self.doc.lines[line] local default_font = self:get_font()
if not text then return 0 end local column = 1
return self:get_font():get_width(text:sub(1, col - 1)) local xoffset = 0
for _, type, text in self.doc.highlighter:each_token(line) do
local font = style.syntax_fonts[type] or default_font
for char in common.utf8_chars(text) do
if column == col then
return xoffset / font:subpixel_scale()
end
xoffset = xoffset + font:get_width_subpixel(char)
column = column + #char
end
end
return xoffset / default_font:subpixel_scale()
end end
function DocView:get_x_offset_col(line, x) function DocView:get_x_offset_col(line, x)
local text = self.doc.lines[line] local line_text = self.doc.lines[line]
local xoffset, last_i, i = 0, 1, 1 local xoffset, last_i, i = 0, 1, 1
local subpixel_scale = self:get_font():subpixel_scale(); local default_font = self:get_font()
local subpixel_scale = default_font:subpixel_scale()
local x_subpixel = subpixel_scale * x + subpixel_scale / 2 local x_subpixel = subpixel_scale * x + subpixel_scale / 2
for _, type, text in self.doc.highlighter:each_token(line) do
local font = style.syntax_fonts[type] or default_font
for char in common.utf8_chars(text) do for char in common.utf8_chars(text) do
local w = self:get_font():get_width_subpixel(char) local w = font:get_width_subpixel(char)
if xoffset >= subpixel_scale * x then if xoffset >= subpixel_scale * x then
return (xoffset - x_subpixel > w / 2) and last_i or i return (xoffset - x_subpixel > w / 2) and last_i or i
end end
@ -162,8 +177,9 @@ function DocView:get_x_offset_col(line, x)
last_i = i last_i = i
i = i + #char i = i + #char
end end
end
return #text return #line_text
end end
@ -308,11 +324,12 @@ end
function DocView:draw_line_text(idx, x, y) function DocView:draw_line_text(idx, x, y)
local font = self:get_font() local default_font = self:get_font()
local subpixel_scale = font:subpixel_scale() local subpixel_scale = default_font:subpixel_scale()
local tx, ty = subpixel_scale * x, y + self:get_line_text_y_offset() local tx, ty = subpixel_scale * x, y + self:get_line_text_y_offset()
for _, type, text in self.doc.highlighter:each_token(idx) do for _, type, text in self.doc.highlighter:each_token(idx) do
local color = style.syntax[type] local color = style.syntax[type]
local font = style.syntax_fonts[type] or default_font
if config.draw_whitespace then if config.draw_whitespace then
tx = renderer.draw_text_subpixel(font, text, tx, ty, color, core.replacements, style.syntax.comment) tx = renderer.draw_text_subpixel(font, text, tx, ty, color, core.replacements, style.syntax.comment)
else else

View File

@ -57,4 +57,11 @@ style.syntax["string"] = { common.color "#f7c95c" }
style.syntax["operator"] = { common.color "#93DDFA" } style.syntax["operator"] = { common.color "#93DDFA" }
style.syntax["function"] = { common.color "#93DDFA" } style.syntax["function"] = { common.color "#93DDFA" }
-- This can be used to override fonts per syntax group.
-- The syntax highlighter will take existing values from this table and
-- override style.code_font on a per-token basis, so you can choose to eg.
-- render comments in an italic font if you want to.
style.syntax_fonts = {}
-- style.syntax_fonts["comment"] = renderer.font.load(path_to_font, size_of_font, rendering_options)
return style return style