Skip to content

Commit

Permalink
Improved output for text selection and copy/paste
Browse files Browse the repository at this point in the history
Replace text elements with a text element per line which consists of tspan elements and
remove all whitespace between tspan:s.

Replace figure space codepoint with normal whitespace and use xml:space="preserve" instead.
Otherwise copied text would include a confusing non-U+0020 whitespace. This also fixes an issue
with Inkscape not rendering underline for a text element with only whitespace.

With this combined copy/paste should work much better and even preserve new lines.

Note that tabs are not preserved at the moment, they get converted into spaces using 8 space tabulation.

Possibly also fixes issue in Safari with text baseline styling.

Inkescape issue has been reported here https://gitlab.com/inkscape/inbox/-/issues/9621

Fixes #5
  • Loading branch information
wader committed Dec 4, 2023
1 parent 3272d35 commit 1e0d3dc
Show file tree
Hide file tree
Showing 22 changed files with 323 additions and 4,399 deletions.
23 changes: 18 additions & 5 deletions ansitosvg/ansisvg.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ var DefaultOptions = Options{
// Convert reads ANSI input from r and writes SVG to w
func Convert(r io.Reader, w io.Writer, opts Options) error {
ad := ansidecoder.NewDecoder(r)
var chars []svgscreen.Char

lineNr := 0
var lines []svgscreen.Line
line := svgscreen.Line{
Y: lineNr,
}

for {
r, _, err := ad.ReadRune()
if err == io.EOF {
Expand All @@ -44,6 +50,11 @@ func Convert(r io.Reader, w io.Writer, opts Options) error {
}

if r == '\n' {
lines = append(lines, line)
lineNr++
line = svgscreen.Line{
Y: lineNr,
}
continue
}

Expand All @@ -54,17 +65,19 @@ func Convert(r io.Reader, w io.Writer, opts Options) error {
n = 8 - (ad.X % 8)
}
for i := 0; i < n; i++ {
chars = append(chars, svgscreen.Char{
line.Chars = append(line.Chars, svgscreen.Char{
Char: string([]rune{r}),
X: ad.X + i,
Y: ad.Y,
Foreground: ad.Foreground.String(),
Background: ad.Background.String(),
Underline: ad.Underline,
Intensity: ad.Intensity,
})
}
}
if len(line.Chars) > 0 {
lines = append(lines, line)
}
terminalWidth := ad.MaxX + 1
if opts.TerminalWidth != 0 {
terminalWidth = opts.TerminalWidth
Expand Down Expand Up @@ -125,8 +138,8 @@ func Convert(r io.Reader, w io.Writer, opts Options) error {
},
TerminalWidth: terminalWidth,
Columns: ad.MaxX + 1,
Lines: ad.MaxY + 1,
Chars: chars,
NrLines: ad.MaxY + 1,
Lines: lines,
},
)
}
17 changes: 5 additions & 12 deletions ansitosvg/testdata/charboxfontsize.ansi.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1e0d3dc

Please sign in to comment.