Repository: alacritty/alacritty
File: alacritty/src/renderer/text/builtin_font.rs (Rust)

Code before:

    // Start with offset `1` and draw until the intersection of the f(x) = x + 1 and
    // g(x) = H - x - 1 lines. The intersection happens when f(x) = g(x), which is at
    // x = H/2 - 1.
    let intersection = (height as i32 + 1) / 2 - 1;

    let f_x = LineEquation::new(slope, f_y0, 0, intersection);
    let g_x = LineEquation::new(-slope, g_y0, 0, intersection);

    // Inner functions to make arrows thicker.
    let mut f_x_inner =
        LineEquation::new(slope, f_y0 + extra_thickness, 0, intersection - extra_thickness);
    let mut g_x_inner =
        LineEquation::new(-slope, g_y0 - extra_thickness, 0, intersection - extra_thickness);

    // NOTE f_x and g_x have the same amount of iterations.
    for (p1, p2) in iter::zip(f_x, g_x) {
        if character == POWERLINE_TRIANGLE || character == POWERLINE_TRIANGLE_FLIPPED {
            canvas.draw_rect(0., p1.1, p1.0 + 1., 1., COLOR_FILL);
            canvas.draw_rect(0., p2.1, p2.0 + 1., 1., COLOR_FILL);
        } else if character == POWERLINE_ARROW || character == POWERLINE_ARROW_FLIPPED {
            let p3 = f_x_inner.next().unwrap_or(p2);
            let p4 = g_x_inner.next().unwrap_or(p1);

            // Once we reach the canvas end fill the area between f(x) and g(x) to make a proper
            // cut.
            if p1.0 as usize + 1 == width {
                canvas.draw_rect(p1.0, p1.1, 1., p2.1 - p1.1 + 1., COLOR_FILL);
                break;
            } else {
                canvas.draw_rect(p1.0, p1.1, 1., p3.1 - p1.1 + 1., COLOR_FILL);
                canvas.draw_rect(p4.0, p4.1, 1., p2.1 - p4.1 + 1., COLOR_FILL);
            }
        }
    }

    if character == POWERLINE_TRIANGLE_FLIPPED || character == POWERLINE_ARROW_FLIPPED {
        canvas.flip_horizontal();
    }

    let top = height as i32 + metrics.descent as i32;
    let buffer = BitmapBuffer::Rgb(canvas.into_raw());
    RasterizedGlyph {
        character,
        top,
        left: 0,
        height: height as i32,
        width: width as i32,
        buffer,
        advance: (width as i32, height as i32),
    }

Review comment (suggestion):
This took me a second to figure out, I think it might be nice to be a little more verbose:

```
// If we can't fit the entire arrow in the cell, we cut off the tip of the arrow by drawing a rectangle between the two lines.
```

Code after:

    // Start with offset `1` and draw until the intersection of the f(x) = slope * x + 1 and
    // g(x) = H - slope * x - 1 lines. The intersection happens when f(x) = g(x), which is at
    // x = (H - 2) / (2 * slope).
    let x_intersection = (height as i32 + 1) / 2 - 1;

    let top_line = (0..x_intersection).map(|x| line_equation(slope, x, top_y));
    let bottom_line = (0..x_intersection).map(|x| line_equation(-slope, x, bottom_y));

    // Inner lines to make arrows thicker.
    let mut top_inner_line = (0..x_intersection - extra_thickness)
        .map(|x| line_equation(slope, x, top_y + extra_thickness));
    let mut bottom_inner_line = (0..x_intersection - extra_thickness)
        .map(|x| line_equation(-slope, x, bottom_y - extra_thickness));

    // NOTE: top_line and bottom_line have the same amount of iterations.
    for (p1, p2) in top_line.zip(bottom_line) {
        if character == POWERLINE_TRIANGLE_LTR || character == POWERLINE_TRIANGLE_RTL {
            canvas.draw_rect(0., p1.1, p1.0 + 1., 1., COLOR_FILL);
            canvas.draw_rect(0., p2.1, p2.0 + 1., 1., COLOR_FILL);
        } else if character == POWERLINE_ARROW_LTR || character == POWERLINE_ARROW_RTL {
            let p3 = top_inner_line.next().unwrap_or(p2);
            let p4 = bottom_inner_line.next().unwrap_or(p1);

            // If we can't fit the entire arrow in the cell, we cut off the tip of the arrow by
            // drawing a rectangle between the two lines.
            if p1.0 as usize + 1 == width {
                canvas.draw_rect(p1.0, p1.1, 1., p2.1 - p1.1 + 1., COLOR_FILL);
                break;
            } else {
                canvas.draw_rect(p1.0, p1.1, 1., p3.1 - p1.1 + 1., COLOR_FILL);
                canvas.draw_rect(p4.0, p4.1, 1., p2.1 - p4.1 + 1., COLOR_FILL);
            }
        }
    }

    if character == POWERLINE_TRIANGLE_RTL || character == POWERLINE_ARROW_RTL {
        canvas.flip_horizontal();
    }

    let top = height as i32 + metrics.descent as i32;
    let buffer = BitmapBuffer::Rgb(canvas.into_raw());
    RasterizedGlyph {
        character,
        top,
        left: 0,
        height: height as i32,
        width: width as i32,
        buffer,
        advance: (width as i32, height as i32),
    }