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

Add margin/border support #43

Merged
merged 6 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Example usage:
--grid Grid mode (sets position for each character)
--help, -h Show help
--listcolorschemes List color schemes
--marginsize WxH Margin size (in either pixel or font units)
--transparent Transparent background
--version, -v Show version
--width, -w NUMBER Terminal width (auto if not set)
Expand Down Expand Up @@ -75,6 +76,11 @@ By default, `ansisvg` uses font-relative `ch`/`em` coordinates. This should make

Inkscape currently [cannot deal with SVG size expressed in font-relative units](https://gitlab.com/inkscape/inkscape/-/issues/4737), a quick workaround is Ctrl-Shift-R (resize page to content).

## Margin size

With `--marginsize` a margin can be defined, so there is a bit of empty space (or "border") around the image. Default is zero margin size, i.e. the terminal characters are touching the edge of the image.
`--marginsize` is interpreted as X/Y in the currently selected units, i.e. `ch`/`em` by default, and `px` if `--charboxsize` is used.

## Consolidated text vs. grid mode

By default, `ansisvg` consolidates text to `<tspan>` chunks, leaving the X positioning of characters to the SVG renderer. This usually works well for monospace fonts. However if not all glyphs involved are monospace (e.g. when exotic characters are used, making the SVG renderer fall back to a different font for those characters) then the alignment will be off; this can be worked around with `-grid` mode which will make `ansisvg` put each character to explicit positions, making the SVG bigger and less readable but ensuring proper positioning/alignment for all characters.
Expand Down
23 changes: 12 additions & 11 deletions ansitosvg/ansisvg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/wader/ansisvg/ansidecoder"
"github.com/wader/ansisvg/colorscheme/schemes"
"github.com/wader/ansisvg/svgscreen"
"github.com/wader/ansisvg/svgscreen/xydim"
)

type Options struct {
Expand All @@ -15,7 +16,8 @@ type Options struct {
FontRef string
FontSize int
TerminalWidth int
CharBoxSize svgscreen.BoxSize
CharBoxSize xydim.XyDimInt
MarginSize xydim.XyDimFloat
ColorScheme string
Transparent bool
GridMode bool
Expand All @@ -24,7 +26,8 @@ type Options struct {
var DefaultOptions = Options{
FontName: "Courier",
FontSize: 14,
CharBoxSize: svgscreen.BoxSize{Width: 0, Height: 0},
CharBoxSize: xydim.XyDimInt{X: 0, Y: 0},
MarginSize: xydim.XyDimFloat{X: 0, Y: 0},
ColorScheme: "Builtin Dark",
Transparent: false,
}
Expand Down Expand Up @@ -129,15 +132,13 @@ func Convert(r io.Reader, w io.Writer, opts Options) error {
FontRef: opts.FontRef,
FontSize: opts.FontSize,
},
CharacterBoxSize: svgscreen.BoxSize{
Width: opts.CharBoxSize.Width,
Height: opts.CharBoxSize.Height,
},
TerminalWidth: terminalWidth,
Columns: ad.MaxX + 1,
NrLines: ad.MaxY + 1,
Lines: lines,
GridMode: opts.GridMode,
CharacterBoxSize: opts.CharBoxSize,
MarginSize: opts.MarginSize,
TerminalWidth: terminalWidth,
Columns: ad.MaxX + 1,
NrLines: ad.MaxY + 1,
Lines: lines,
GridMode: opts.GridMode,
}
return s.Render(w)
}
40 changes: 8 additions & 32 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,12 @@ import (
"flag"
"fmt"
"io"
"strconv"
"strings"

"github.com/wader/ansisvg/ansitosvg"
"github.com/wader/ansisvg/colorscheme/schemes"
"github.com/wader/ansisvg/svgscreen"
)

type boxSize struct {
Width int
Height int
}

func (d *boxSize) String() string {
return fmt.Sprintf("%dx%d", d.Width, d.Height)
}

func (d *boxSize) Set(s string) error {
parts := strings.Split(s, "x")
if len(parts) != 2 {
return fmt.Errorf("must be WxH")
}
d.Width, _ = strconv.Atoi(parts[0])
d.Height, _ = strconv.Atoi(parts[1])
return nil
}

type Env struct {
Version string
ReadFile func(string) ([]byte, error)
Expand Down Expand Up @@ -59,11 +38,10 @@ func Main(env Env) error {
var helpFlag bool
fs.BoolVar(&helpFlag, "h", false, "")
fs.BoolVar(&helpFlag, "help", false, "Show help")
var charBoxSize = boxSize{
Width: ansitosvg.DefaultOptions.CharBoxSize.Width,
Height: ansitosvg.DefaultOptions.CharBoxSize.Height,
}
charBoxSize := ansitosvg.DefaultOptions.CharBoxSize
fs.Var(&charBoxSize, "charboxsize", "WxH|Character box size (use pixel units instead of font units)")
marginSize := ansitosvg.DefaultOptions.MarginSize
fs.Var(&marginSize, "marginsize", "WxH|Margin size (in either pixel or font units)")
// handle error and usage output ourself
fs.Usage = func() {}
fs.SetOutput(io.Discard)
Expand Down Expand Up @@ -159,13 +137,11 @@ Example usage:
FontRef: *fontRefFlag,
FontSize: *fontSizeFlag,
TerminalWidth: terminalWidthFlag,
CharBoxSize: svgscreen.BoxSize{
Width: charBoxSize.Width,
Height: charBoxSize.Height,
},
ColorScheme: *colorSchemeFlag,
Transparent: *transparentFlag,
GridMode: *gridModeFlag,
CharBoxSize: charBoxSize,
MarginSize: marginSize,
ColorScheme: *colorSchemeFlag,
Transparent: *transparentFlag,
GridMode: *gridModeFlag,
},
)
}
14 changes: 6 additions & 8 deletions cli/testdata/b-it-ul-st.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions cli/testdata/charboxfontsize.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 19 additions & 21 deletions cli/testdata/colortest.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 19 additions & 21 deletions cli/testdata/colortest_slate.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading