Font collection files contain multiple faces, which can have different
variable data, such as different named styles. Both the C font selection
code and the Python variable data cache were keyed on the file path alone,
so once the data for one face of a collection was cached, other faces in
the same file got that data. For example, selecting a named style that
only exists in the second face of a collection failed if the first face
was looked at first, falling back to a different family.
Key the caches on the face index for fontconfig and the postscript name
for CoreText, which does not expose the face index. Do not share cache
entries for fonts without a path in the C code, matching the Python code.
Expose the face index on the FreeType Face object for the Python cache.
- Convert OpenType axis tags to Python as four Latin-1 characters instead
of UTF-8, so that fonts with axis tags that are not valid UTF-8 no longer
cause an exception. On macOS this made font selection fail entirely if
the selected variable font had such a tag. Convert axis_map keys back to
tags losslessly, validating them, instead of reading four bytes from the
UTF-8 encoding, which could read past the end of short keys.
- variable_data_from_font_file() now raises ValueError for files that are
not valid fonts, instead of an OSError with a stale or zero errno.
- Avoid quadratic work when reading a crafted font with many axes, named
instances and name records: de-duplicate axis tags once rather than per
instance, and binary search name records by name id. A 64MB crafted font
that took over a minute to parse now takes about a second.
Cells containing a codepoint from the registration block that had a
registered glyph when they were printed are rendered with that glyph,
into the area the character occupies: its cell, the cells given to it
by the text sizing protocol, or two cells when it is followed by a space
and the width of its viewBox when fit to the cell height is more than
one cell.
The glyph is bound to the text when it is printed, by storing a
reference to it in the text cache entry of the cell. Changing or
clearing a registration affects only text printed afterwards, so
programs using the same codepoints do not change each other's output
and registering never requires re-rendering lines. Bound entries are
kept in a separate map so that other text is not slowed down. Glyphs
are interned by a seeded hash of their content.
Glyphs that use only the foreground color are alpha masks and glyphs
that use only fixed colors are colored sprites. Glyphs that mix them are
a colored sprite followed in the sprite map by a sprite with the weight
of the foreground color, which the cell shader combines with the
foreground color, so that selection, cursor and other foreground color
changes apply to them exactly.
Sprites of a glyph are freed when the glyph is, which happens only when
no cell uses it, and are reused. Each window can keep alive a limited
number and size of glyphs, beyond which registration fails with
no_space, so a program that keeps registering and printing new glyphs
cannot exhaust memory or sprites for other windows.
A dedicated renderer for vg glyphs. Paths are flattened to polygons in
device space and rasterized by a scanline rasterizer that computes exact
winding numbers on 16 sub-scanlines per pixel row with exact horizontal
coverage, so both fill rules and unions of overlapping polygons are
correct. Strokes are generated in layer space, where the pen is round,
as a union of quadrilaterals, joins and caps, giving the elliptical pen
of SVG under non-uniform matrices. Gradients are linear and two point
conical with pad, reflect and repeat.
The output is decomposed into a premultiplied color, an alpha and a
foreground weight, so that the foreground color can be applied when
drawing, which is exact as compositing is linear in the opaque
foreground color.
Parse vg payloads into a VGGlyph structure, normalizing path data to
absolute moves, lines, quadratic and cubic Béziers and closes, with
arcs converted to cubics as the spec requires. All limits of the spec
are enforced while parsing.
Registrations are stored per screen in a GlyphRegistry owned by the
text cache of the screen. r and c are implemented and RIS clears all
registrations.
Parse the APC escape codes of the glyph protocol (kitty#10872) into a
GlyphCommand structure and reply to the s and q verbs. The s verb
advertises no payload formats until rendering is implemented. q for
codepoints in the registration block is answered from the
registrations; for all other codepoints it is answered with
reason=unknown, as kitty does not perform font lookups while parsing
input. Grapheme queries are not supported.
While rendering is paused the cursor is sized and colored from the cell
under it in the paused snapshot, but the snapshot row was taken to be the
cursor's screen row. With pixel scroll the snapshot has an extra leading
row, so the cursor took the colors of the cell above it.
Map linebuf rows to snapshot rows in one place, accounting for both the
extra pixel scroll row and the scroll position captured at pause time, and
use it for the cursor, selections and the cursor row check when rendering
the snapshot, which was off by the scroll position when scrolled back.
The choice of the line under the cursor, including the IME overlay, moves
from the renderer to screen_line_for_cursor() so that it can be tested.
Closes#10892
HarfBuzz returns the glyphs of a backward (RTL) buffer in visual order,
with decreasing clusters, but cells were consumed from the logical front
of the run, so pointed Hebrew and Arabic lam-alef ligatures were grouped
with the wrong cells.
Replace the per codepoint cell cursor with a table, built while loading
the HarfBuzz buffer, mapping codepoint offsets to cells. The clusters
grouped so far always cover a prefix (LTR) or a suffix (RTL) of the text,
so the cells they complete come straight from the table, with no
direction specific code. The codepoint used to detect special glyphs is
now that of the glyph's own cluster.
Add DejaVu Sans Mono to the test fonts for Arabic shaping tests.
Closes#10891
Mode 5523, while mouse tracking is on, makes kitty report each scroll event
as a single CSI > ... M escape code instead of as button 4-7 presses. It
carries integer deltas in v120 units or pixels, with fractions carried
over, the user's scroll multiplier as a hint, the kind of device, the
gesture phase, and flags for momentum and inverted (natural) scrolling,
and the full set of modifiers. Only the sign of the multipliers is applied.
To support this GLFW scroll events now carry the source device, the
gesture phase and whether the direction is inverted, on Wayland, X11 and
macOS. The momentum scroller now forwards the gesture end before starting
momentum and no longer reports a cancel for momentum that never began.
The snapshot of the screen taken for a synchronized update is kept after
the update ends, so its cells prevented the style table from being
emptied on hard resets and clearing of the scrollback, and kept their
styles alive through garbage collection. It is unused when rendering is
not paused and is overwritten entirely when rendering is next paused, so
only consider it while rendering is paused.
Also detect use of the formatting of stale line copies when comparing
lines and in test_shape() and test_render_line().
Lines scrolled onto the screen no longer have their render data cleared,
instead they are marked dirty so that rendering recomputes it, except when
there is a marker, as marks are render data used without rendering. This
reduces the memory written to clear a line by 40%.
The ASCII fast path in draw_text no longer checks for multicell cells in
a line that was just cleared by a line feed scrolling the screen, until
something that can create multicell cells happens.
Together these make the ascii benchmark ~14% and the scrollback and
render benchmarks ~8% and ~14% faster. Also update the throughput
numbers in the performance docs, using the median of several runs.
Emptying it whenever the whole screen was erased meant that full screen
programs, which erase the screen on every redraw, had all their styles
interned again and the style texture uploaded again every frame. Also do
not empty tables with few styles, as they are not worth it. Empty it when
the scrollback is cleared by the clear_terminal action as well.
Improves the truecolor text and half blocks benchmarks by 50% and 33%.
Remove the temp_flag bit from CPUCell, which was used only to mark the
cells whose text is extracted for a selection, and use a temporary bitmap
for that instead. The freed bit is used for the style index, doubling
the maximum number of styles to 262144.
Copies of lines made with LineBuf.create_line_copy() are not remapped
when the style table of their screen is garbage collected, so their
style indices become invalid. Record the generation of the style table
in copies and raise ValueError when the formatting of a copy made
before a collection is used. Copying a whole line into a copy makes
its formatting valid again.
On a hard reset or when erasing the whole display, if the scrollback is
empty and no cell on any screen buffer has a style, empty the style
table, freeing the memory used by styles from, for example, a burst of
output with many colors. Checking is cheap as only the screen buffers
need to be scanned.
The formatting of cells (colors and SGR attributes) is now interned per
screen in a StyleTable and cells store a 17 bit index into it in the
previously unused bits of CPUCell. Render data (sprite and mark) is kept
in a separate RenderCell array for screen lines and in a small render
cache for history lines being displayed, so the scrollback stores only
CPUCells, 12 bytes per cell instead of 32. GPUCell is now only the upload
format, built from the CPUCell, its style and its RenderCell.
When the table fills up, it is compacted at the start of drawing text,
reducing the color precision of cells in the scrollback if needed, cells
on screen always keep their exact styles. The map from styles to indices
is a verstable set of indices with a small cache of recent styles in
front of it.
benchmark.py throughput: ascii +48%, unicode +29%, csi +10%, truecolor
text -22%, truecolor half blocks -9%.
Errors during startup that are not caused by invalid config lines, such
as failing to listen on the remote control socket, were shown under the
title "Errors parsing configuration" with the heading "In final effective
configuration". The title now reflects whether the errors are config
parse errors, startup errors or both.
If the socket file for --listen-on was left behind by a kitty instance
that was killed, binding to it failed, so remote control was disabled
and an error was shown over the program. Now, if no process is listening
on the socket, it is removed and binding is retried. A socket that a
process is listening on and files that are not sockets are never
removed, and the error shown includes the reason for the failure.
The new render_interval option either syncs redraws to the monitor
(the default), optionally with a minimum interval between frames, or
redraws on a schedule with a minimum interval between frames. The
interval is now enforced per OS window, including when output is being
parsed, which the old repaint_delay was not.
On X11, render frames are now provided by the Present extension, with
libxcb-present loaded at runtime. Frames are requested lazily on X11,
since a pending NotifyMSC request delays buffer presentation by the GPU
driver under Xwayland. Xwayland completing requests early and failing to
report vertical blanks for long periods are handled. Without Present,
kitty falls back to blocking buffer swaps as before.
Render frames now only wake up the main loop when something is waiting
for them, so animations such as the eased cursor blink are no longer
sampled at the monitor refresh rate. On Wayland, frame callbacks are
also used when not syncing to the monitor, to stop rendering hidden
windows.
The removed options are converted to render_interval if it is not
specified anywhere, including in other config files and overrides,
otherwise they are ignored with a warning.
Also fix gen config failing on options whose type is a builtin.
- Add runs of plain combining chars to a cell with a single lookup in
the text cache, rather than one for each char, which also added the
intermediate texts of the cell to the cache.
- Also collect garbage in the text cache when starting a new line, not
just when starting to draw text, as a single draw can add a great many
entries, making the cache slower to use the larger it grows.
- Size the hash map of the text cache for the expected number of entries
after garbage collection, to avoid repeatedly rehashing it as it grows.
This makes the unique multi-codepoint cells benchmark ~80% faster.
The differential test of batched drawing now compares cells by content,
since cells with multiple codepoints store indices into the text cache,
which change as intermediate texts are no longer added to it.
Decode 64 byte blocks of valid UTF-8 containing no ESC using the
approach of simdutf: blocks are validated with the lookup tables of
Keiser and Lemire and converted twelve bytes at a time using shuffles
looked up from a mask of the bytes that end code points, with fast
paths for runs of ASCII, two byte and three byte sequences. Anything
else, ESC, invalid UTF-8 and the end of the input, is decoded as
before.
The shuffle tables are generated by gen/utf8_decode_tables.py.
This decodes the text of the unicode benchmark 2.6 times faster, faster
than simdutf's own conversion with error checking, and speeds up the
unicode benchmark by ~8%.
The new AVX2 decoder is faster than the AVX-512 one, so remove the
AVX-512 decoder and use the AVX2 one on CPUs with AVX-512 as well.
Also add a test comparing the SIMD decoders with the scalar one on long
inputs, which exercise the decoding of blocks, as the existing tests use
short inputs.
Decoding the packed bitfields of CharProps took about half the
instructions of the batched non-ASCII draw loop. Precompute, for each
distinct CharProps value, the number of cells a char occupies and its
grapheme segmentation property, and use that in the loop instead.
This speeds up the unicode benchmark by ~8%.
Also add test cases for a batched run that stops at an existing
multicell char after an unpaired regional indicator, which the random
test cases did not cover.
- Check for multicell chars in the erased range before looking for them
cell by cell, with a branch free check that reads only the word of each
cell containing the multicell flag. The common case is a line with no
multicell chars.
- Add a fast path that parses CSI codes of the common form, an optional
private marker, numeric params separated by ; or : and the final byte,
in a single pass, falling back to the generic CSI parser for anything
else.
- Do not call the UTF-8 decoder for no text when an escape code directly
follows another one, and start parsing CSI codes right after ESC [
rather than in the next iteration of the parse loop.
This speeds up the CSI benchmark by 25% to 30% and the truecolor
benchmarks by ~10%.
A differential test compares the fast path and the generic CSI parser on
random input, and a new test covers erasing parts of multicell chars,
which no existing test did.
Draw runs of non-ASCII chars that each start a new cell of width one or
two in a tight loop, with the run invariant checks done once and state
kept in locals, falling back to the scalar path for anything that needs
special handling: combining chars, zero width chars, the image
placeholder, existing multicell cells, wrapping, insert mode and charset
translation.
Cells are written as 64 bit values built in registers, since assembling
them as structs makes compilers go through the stack with partial stores
that stall on store forwarding. The grapheme segmentation step is cached
as it depends only on the state and the segmentation property of the
char, which repeat in a run of text, breaking the chain of dependent
table lookups between consecutive chars.
This speeds up the unicode benchmark by ~30%, drawing CJK text by ~70%.
A differential test compares the batched and scalar paths on random
operations.
Loading the builtin NERD font, which includes a fontconfig match for its
hinting settings, was done on the main thread when the results of font
selection were first used. Do the slow parts of it in the font selection
thread instead. On Linux the PostScript name is read from the name table
of the font file and the fontconfig match is done in the thread, and the
FreeType face, which is only used for fallback, is created when first
needed. On macOS the font descriptor is created in the thread.
This saves ~0.6ms on the main thread during startup on Linux.
Font selection was done in Python on the main thread, taking ~15ms at
startup on Linux and much more on macOS, where all installed fonts were
listed from CoreText and converted to Python, taking ~165ms.
Port the selection algorithm to pure C in font-select.c, with platform
backends in fontconfig.c and core_text.m, so that it can run in a thread
started by set_font_family(). The results are used when the first font
group is created, typically when the first OS Window is created. The
data for variable fonts is read from the font tables by C code that does
not use Python or FreeType.
The fontconfig backend lists all monospace fonts once and filters them
in C, as listing fonts is dominated by scanning all fonts. The CoreText
backend queries only the needed font families and PostScript names
instead of all installed fonts.
On Linux, the selected fonts are identical to those selected by the
previous Python implementation for ~1400 font specifications, including
variable fonts. The CoreText backend has not yet been compiled or tested.
Creating the first window imported kitty.launch, which is slow to
import, just to find out there are no watchers to load. This delayed
the launch of the first child by a few ms.
On Linux and BSD, kitty now falls back to rendering on the CPU with the
Mesa lavapipe (Vulkan) or llvmpipe (OpenGL) software renderers when no
usable GPU is found, such as in virtual machines or with broken GPU
drivers. This is the new software value of the gpu_driver option, last
in the default list.
Drivers are now told which kind of renderer they may use. The software
candidates use only software renderers: Vulkan selects CPU devices and
OpenGL sets LIBGL_ALWAYS_SOFTWARE and __GLX_VENDOR_LIBRARY_NAME while
the renderer is chosen, restoring them afterwards so they are not passed
on to child programs. The other candidates reject software renderers,
unless they are last, so that a working GPU, even one usable only by a
later driver, is always preferred to the CPU. Previously, on systems
with a GPU usable only with OpenGL, kitty would use lavapipe. Drivers
report whether they render in software in their capabilities.
Failing to initialize the GPU driver for the first window is no longer
fatal: the window is recreated with the next driver, as is already done
for failures creating surfaces. OpenGL can only detect that it is using
llvmpipe once a context is current.
When software rendering is used, a message saying so is shown at the
top of every OS window for five seconds after it is first rendered.
On macOS, kitty now tries to render using Metal, falling back to OpenGL,
see the gpu_driver option. The driver has not yet been compiled or run
on macOS, it was written on Linux and only syntax checked against stub
headers.
Shader targets of the form msl-NN (for MSL N.N) compile each program to
a single MSL source file containing the entry points for all stages,
compiled by the driver at runtime, for the built-in and custom shaders.
slangc ignores [[vk::binding]] for Metal, so the program metadata keeps
the kitty binding slots and passes the argument table indices assigned
by slangc as native names. The push constant block, emitted as an
ordinary constant buffer, is recognized by the name of its type. As
Metal has y=1 at the top in NDC, the generated code is fixed up to
negate the y coordinate of the position output by vertex entry points.
Layouts that would differ because bool is a single byte in MSL are
rejected. The MSL is validated with the Metal compiler when available.
The MSL shaders are temporarily built on all platforms, for development.
The driver follows the design of the Vulkan driver: command buffers with
serials and at most three in flight, copy on write of buffer storage the
GPU may still use, private textures uploaded with blits from staging
memory, pipeline variants created when first used and a CAMetalLayer
per window. Vertex inputs whose offset or stride is not a multiple of
four bytes, as Metal requires, are repacked into a cached copy.
On Linux and BSD, kitty now renders using Vulkan 1.3, falling back to
OpenGL if Vulkan is not available, see the gpu_driver option. The driver
is built when the Vulkan headers are present, libvulkan is loaded at
runtime.
The driver implements the immediate mode GPU API on top of a small ring
of command buffers, submitted when windows are presented or when the CPU
needs results, such as when reading back pixels. It uses dynamic
rendering, synchronization2, push descriptors for textures and uniform
buffers and push constants for the uniform parameters of pipelines.
Buffers are host visible and written only by the CPU, with copy on write
of storage still in use by the GPU, which preserves the OpenGL semantics
the rest of kitty relies on without the CPU ever waiting for the GPU.
Pipelines have variants for each render target format, blend mode and
set of vertex input formats, created when first used. Vertex strides are
dynamic state.
Rendering is pixel identical to OpenGL in the screenshot comparisons,
with the validation layers reporting no errors.
Integer textures are now declared in the shaders with their actual
number of channels, as Vulkan requires the format of the image in the
SPIR-V to match the texture. Drivers that cannot determine the size of
uniform blocks, such as Vulkan without SPIR-V reflection, can now report
it as zero.
Shader targets of the form spirv-NN (for SPIR-V N.N) compile each
program to a single SPIR-V module containing the entry points for all
stages, along with the same backend neutral program metadata as for
GLSL. The compiled modules are validated with spirv-val, when it is
available. Custom shaders are compiled at runtime for SPIR-V targets as
well, and the tests compile and validate all bundled custom shaders for
SPIR-V in addition to the target of the loaded driver.
The program metadata now includes the names of the entry points, which
are passed to drivers in GPUShaderSource, since a module can contain
the entry points of several stages. compile_program() accepts binary
shaders as bytes.
This replaces the unused, untargeted .spv files that were built before.
Pixel coordinates of viewports, scissor rectangles and read back pixels
now have their origin at the top left of the render target, rows are
read back top to bottom, NDC y=-1 is the top of the viewport and render
targets have their top row at texture coordinate t=0, the same as
uploaded images. Vulkan and Metal drivers can then render with no flips
at all.
The OpenGL driver is now the special case. Framebuffer objects already
behave this way, so it converts only when rendering to a window: vertex
shaders negate gl_Position.y via a kitty_flip_y uniform that the GLSL
fixup adds, viewport and scissor y coordinates are converted, rows of
read back pixels are reversed and copying to a texture uses a flipping
blit.
kitty's code gets simpler: viewports and scissor rectangles no longer
need the framebuffer height, screenshots no longer need their rows
flipped and textures produced by rendering are sampled the same way as
uploaded ones. The rounded rect shader no longer uses SV_Position, whose
origin differs between backends.
User facing custom shader UV coordinates are unchanged, still with
their origin at the bottom left.
Rendering is pixel identical to before.
The gpu_driver option is an ordered list of the GPU drivers to try,
defaulting to metal, vulkan, opengl. Drivers that are not part of the
build are skipped.
Add connect() to the GPU driver API. It is called once the windowing
system has been initialized, with the connection to the display, so
that drivers can check that they are able to render to windows on it,
before any windows are created. The driver is now loaded after
glfwInit() for this reason. If a driver cannot connect, the next one is
tried. If the driver fails to choose a visual or create a surface for
the first window, before any GPU resources exist, kitty switches to the
next usable driver and creates the window again.
--debug-rendering reports the driver used and why others were rejected.
Declare the uniform parameters of every program as a single Params
struct, shared by all stages of the program, in a push constant block,
instead of as uniform parameters of the entry points. Slang lays out
the uniform parameters of each entry point starting at offset zero, so
the vertex and fragment parameters overlap, which Vulkan does not allow
in push constants. A single block shared by all stages is also what
Metal wants, and it is still emitted as loose uniforms for OpenGL.
The build now fails if a program has entry point uniform parameters or
more than 128 bytes of them, the push constant size guaranteed by Vulkan.
Since a program now has at most one block of uniform parameters, drop
the stage from GPUParamBlock, bump the GPU API version, and simplify
the lookup of parameters by name in kitty, as each name is now present
in only one parameter block.
Each GPU driver now declares the shader target it consumes (glsl-140 for
the OpenGL driver), a fixed property available as soon as the driver is
loaded. setup.py compiles the built-in shaders for the targets of the
drivers it builds, into a directory per target, and custom shaders are
compiled at runtime for the target of the loaded driver. The GLSL_VERSION
constant is no longer exported to Python.
The program metadata passed to the C code is now generated from the
reflection data produced by slangc and is backend neutral: vertex input
locations, uniform names, uniform block sizes and array layouts, all
identified by their names in the Slang source. Block sizes are verified
against the driver. The only target specific part is a map from Slang
names to the identifiers in the compiled code, which the OpenGL driver
needs to look up parameters by name and which is passed to the driver
when creating a pipeline. The driver no longer needs to be queried for
uniform array layouts.
The debug config output now uses the name reported by the driver.
A new panel created via single instance now runs in the working
directory of the invocation, rather than that of the first instance, as
commands run in existing panels and other single instance commands
already do. The environment of the invocation is cleaned up with
process_env() as for other single instance commands, so that variables
such as XDG_ACTIVATION_TOKEN and VTE_VERSION are not passed to the
commands.
Commands sent to an already running panel or quick access terminal, both
in existing panels and in newly created ones, are now user launches
passed to the launch wrapper. They have no source window, since they are
not run from any window in the panel. A new panel without a command runs
the shell instead of the run-shell kitten, so the wrapper sees the actual
shell, as for a panel started without single instance.
Closes#10900
The layout did not reserve space for the border of a single window, so
the border overlapped the margin and, when the margin was narrower than
the border, extended past the edges of the OS window. The negative
coordinates were then parsed as unsigned, which with rounded borders made
the whole window be covered by the corner fill, hiding its contents.
Reserve the border in layout_single_window_group whenever a full border
is drawn around the window, including when a visible dock causes borders
to be drawn, account for it in the initial OS window size in cells, and
parse border rects as signed integers, clipping them to the OS window.
Fixes#10899
The HarfBuzz font was created before the named instance or axis
coordinates were applied to the FreeType face and was only resynced if
the face had a transform matrix. So shaping used the default variation
coordinates, which broke fonts that substitute glyphs based on axis
values via GSUB FeatureVariations, such as the cursive (CRSV) axis of
Annotation Mono.
Adds a test using a small subset of Annotation Mono (SIL OFL 1.1).
Fixes#10897
On macOS the default shell is run via /usr/bin/login and the run-shell
kitten, with the user wrapper placed inside login, so the expected argv
differs from Linux.