Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to print text when indexed.c.utf8() > 1 #8

Open
iamazy opened this issue Jan 6, 2025 · 2 comments · May be fixed by #9
Open

How to print text when indexed.c.utf8() > 1 #8

iamazy opened this issue Jan 6, 2025 · 2 comments · May be fixed by #9

Comments

@iamazy
Copy link
Contributor

iamazy commented Jan 6, 2025

Hello, egui_term is a nice crate but it only supports input ASCII, when I type Chinese, the existing code can not fully display Chinese, so I made the following modifications.

In view.rs

fn show(self, state: &mut TerminalViewState, layout: &Response, painter: &Painter) {
        let content = self.backend.sync_content();
        let layout_offset = layout.rect.min;
        let TerminalSize {
            cell_height,
            cell_width,
            ..
        } = content.term_size;

        let mut prev_len_utf8 = 1;
        for indexed in content.grid.display_iter() {
            let x =
                layout_offset.x + indexed.point.column.saturating_mul((cell_width * prev_len_utf8) as usize) as f32;
            let y = layout_offset.y
                + indexed
                    .point
                    .line
                    .saturating_add(content.grid.display_offset() as i32)
                    .saturating_mul(cell_height as i32) as f32;

            let mut fg = self.theme.get_color(indexed.fg);
            let mut bg = self.theme.get_color(indexed.bg);

            if indexed
                .cell
                .flags
                .intersects(cell::Flags::DIM | cell::Flags::DIM_BOLD)
            {
                fg = fg.linear_multiply(0.7);
            }

            if indexed.cell.flags.contains(cell::Flags::INVERSE)
                || content
                    .selection_range
                    .is_some_and(|r| r.contains(indexed.point))
            {
                std::mem::swap(&mut fg, &mut bg);
            }

            painter.rect(
                Rect::from_min_size(
                    Pos2::new(x, y),
                    Vec2::new((cell_width as usize * indexed.c.len_utf8()) as f32, cell_height as f32),
                ),
                Rounding::ZERO,
                bg,
                Stroke::NONE,
            );

            // Draw hovered hyperlink underline
            if content.hovered_hyperlink.as_ref().is_some_and(|range| {
                range.contains(&indexed.point) && range.contains(&state.mouse_position_on_grid)
            }) {
                layout.ctx.set_cursor_icon(CursorIcon::PointingHand);
                let underline_height = y + cell_height as f32;
                painter.line_segment(
                    [
                        Pos2::new(x, underline_height),
                        Pos2::new(x + cell_width as f32 * indexed.c.len_utf8() as f32, underline_height),
                    ],
                    Stroke::new(cell_height as f32 * 0.15, fg),
                );
            }

            // Handle cursor rendering
            if content.grid.cursor.point == indexed.point {
                let cursor_color = self.theme.get_color(content.cursor.fg);
                painter.rect(
                    Rect::from_min_size(
                        Pos2::new(x, y),
                        Vec2::new((cell_width as usize * indexed.c.len_utf8()) as f32, cell_height as f32),
                    ),
                    Rounding::default(),
                    cursor_color,
                    Stroke::NONE,
                );
            }

            // Draw text content
            if indexed.c != ' ' && indexed.c != '\t' {
                if content.grid.cursor.point == indexed.point
                    && content.term_mode.contains(TermMode::APP_CURSOR)
                {
                    fg = bg;
                }

                painter.text(
                    Pos2 {
                        x: x + cell_width as f32 / 2.,
                        y: y + cell_height as f32 / 2.,
                    },
                    Align2::CENTER_CENTER,
                    indexed.c,
                    self.font.font_type(),
                    fg,
                );
            }
            prev_len_utf8 = indexed.c.len_utf8() as u16;
        }
    }

but the effect is still not good, as shown below.

image

I tried it in alacritty and the effect is as follows(It's what I expected)

image

If you have some suggestions, I would be very appreciated.

Thanks in advance.

@Harzu Harzu linked a pull request Jan 6, 2025 that will close this issue
@Harzu
Copy link
Owner

Harzu commented Jan 6, 2025

Hey, I attached the PR with some fixes. Could you please check that it's fixed your problem

@iamazy
Copy link
Contributor Author

iamazy commented Jan 7, 2025

@Harzu Hi, Thanks a lot, the pr solved my problem, but it looks like the cursor didn't fully cover the text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants