mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-15 16:26:08 +03:00
Merge pull request #753 from mitchellh/update-undercurl
feat: update undercurl with antialiasing
This commit is contained in:
@ -140,6 +140,12 @@ const WebCanvasImpl = struct {
|
|||||||
self.* = undefined;
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn pixel(self: *WebCanvasImpl, x: u32, y: u32, color: Color) void {
|
||||||
|
const ctx = self.context(color) catch return;
|
||||||
|
defer ctx.deinit();
|
||||||
|
ctx.call(void, "fillRect", .{ x, y, 1, 1 }) catch return;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn rect(self: *WebCanvasImpl, v: Rect, color: Color) void {
|
pub fn rect(self: *WebCanvasImpl, v: Rect, color: Color) void {
|
||||||
const ctx = self.context(color) catch return;
|
const ctx = self.context(color) catch return;
|
||||||
defer ctx.deinit();
|
defer ctx.deinit();
|
||||||
@ -401,6 +407,20 @@ const PixmanImpl = struct {
|
|||||||
return region;
|
return region;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Draw and fill a single pixel
|
||||||
|
pub fn pixel(self: *Canvas, x: u32, y: u32, color: Color) void {
|
||||||
|
const boxes = &[_]pixman.Box32{
|
||||||
|
.{
|
||||||
|
.x1 = @intCast(x),
|
||||||
|
.y1 = @intCast(y),
|
||||||
|
.x2 = @intCast(x + 1),
|
||||||
|
.y2 = @intCast(y + 1),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
self.image.fillBoxes(.src, color.pixmanColor(), boxes) catch {};
|
||||||
|
}
|
||||||
|
|
||||||
/// Draw and fill a rectangle. This is the main primitive for drawing
|
/// Draw and fill a rectangle. This is the main primitive for drawing
|
||||||
/// lines as well (which are just generally skinny rectangles...)
|
/// lines as well (which are just generally skinny rectangles...)
|
||||||
pub fn rect(self: *Canvas, v: Rect, color: Color) void {
|
pub fn rect(self: *Canvas, v: Rect, color: Color) void {
|
||||||
|
@ -166,40 +166,43 @@ const Draw = struct {
|
|||||||
// This is the lowest that the curl can go.
|
// This is the lowest that the curl can go.
|
||||||
const y_max = self.height - 1;
|
const y_max = self.height - 1;
|
||||||
|
|
||||||
|
// Determines the density of the waves.
|
||||||
|
// `2 * pi...` = 1 peak per character
|
||||||
|
// `4 * pi...` = 2 peaks per character
|
||||||
|
const x_factor = 2 * std.math.pi / @as(f64, @floatFromInt(self.width - 1));
|
||||||
|
|
||||||
// Some fonts put the underline too close to the bottom of the
|
// Some fonts put the underline too close to the bottom of the
|
||||||
// cell height and this doesn't allow us to make a high enough
|
// cell height and this doesn't allow us to make a high enough
|
||||||
// wave. This constant is arbitrary, change it for aesthetics.
|
// wave. This constant is arbitrary, change it for aesthetics.
|
||||||
const pos = pos: {
|
const pos: u32 = pos: {
|
||||||
const MIN_HEIGHT = 7;
|
const MIN_HEIGHT = 5;
|
||||||
const clamped_pos = @min(y_max, self.pos);
|
const clamped_pos = @min(y_max, self.pos);
|
||||||
const height = y_max - clamped_pos;
|
const height = y_max - clamped_pos;
|
||||||
break :pos if (height < MIN_HEIGHT) clamped_pos -| MIN_HEIGHT else clamped_pos;
|
break :pos if (height < MIN_HEIGHT) clamped_pos -| MIN_HEIGHT else clamped_pos;
|
||||||
};
|
};
|
||||||
|
|
||||||
// The full heightof the wave can be from the bottom to the
|
// The full height of the wave can be from the bottom to the
|
||||||
// underline position. We also calculate our starting y which is
|
// underline position. We also calculate our mid y point of the wave
|
||||||
// slightly below our descender since our wave will move about that.
|
const wave_height: f64 = @floatFromInt(y_max - pos);
|
||||||
const wave_height = @as(f64, @floatFromInt(y_max - pos));
|
const half_height: f64 = @max(1, wave_height / 4);
|
||||||
const half_height = wave_height / 4;
|
const y_mid: u32 = pos + @as(u32, @intFromFloat(2 * half_height));
|
||||||
const y = pos + @as(u32, @intFromFloat(half_height));
|
|
||||||
|
|
||||||
const x_factor = (2 * std.math.pi) / @as(f64, @floatFromInt(self.width));
|
// follow Xiaolin Wu's antialias algorithm to draw the curve
|
||||||
var x: u32 = 0;
|
var x: u32 = 0;
|
||||||
while (x < self.width) : (x += 1) {
|
while (x < self.width) : (x += 1) {
|
||||||
const vertical = @as(
|
const y: f64 = @as(f64, @floatFromInt(y_mid)) + (half_height * @cos(@as(f64, @floatFromInt(x)) * x_factor));
|
||||||
u32,
|
const y_upper: u32 = @intFromFloat(@floor(y));
|
||||||
@intFromFloat((-1 * half_height) * @sin(@as(f64, @floatFromInt(x)) * x_factor) + half_height),
|
const y_lower: u32 = y_upper - 1 + self.thickness;
|
||||||
);
|
const alpha: u8 = @intFromFloat(255 * @abs(y - @floor(y)));
|
||||||
|
|
||||||
var row: u32 = 0;
|
// upper and lower bounds
|
||||||
while (row < self.thickness) : (row += 1) {
|
canvas.pixel(x, @min(y_upper, y_max), @enumFromInt(255 - alpha));
|
||||||
const y1 = @min(row + y + vertical, y_max);
|
canvas.pixel(x, @min(y_lower, y_max), @enumFromInt(alpha));
|
||||||
canvas.rect(.{
|
|
||||||
.x = @intCast(x),
|
// fill between upper and lower bound
|
||||||
.y = @intCast(y1),
|
var y_fill: u32 = y_upper + 1;
|
||||||
.width = 1,
|
while (y_fill < y_lower) : (y_fill += 1) {
|
||||||
.height = 1,
|
canvas.pixel(x, @min(y_fill, y_max), .on);
|
||||||
}, .on);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user