Skip to content

Commit

Permalink
fix: viewBox attr when using pixel units
Browse files Browse the repository at this point in the history
fixes SVG output for tools that rely on viewBox, such as asciidoctor
  • Loading branch information
patrislav1 committed Apr 10, 2024
1 parent 2a87c3c commit 29c992c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ go build -o ansisvg .

By default, `ansisvg` uses font-relative `ch`/`em` coordinates. This should make SVG dimensions and line/character spacing consistent with font family/size. When SVG dimensions and/or text coordinates are off, it is possible to force explicit pixel units for coordinates by specifying `-charboxsize` in X/Y pixel units, e.g. `8x16`.

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).
* 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).

* Some SVG processing tools like [asciidoctor](https://docs.asciidoctor.org/pdf-converter/latest/image-paths-and-formats/#image-formats) require the presence of the `viewBox` attribute. Use `-charboxsize` option to enable this attribute (it only works with pixel dimensions).

## Margin size

Expand Down
2 changes: 1 addition & 1 deletion 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.
2 changes: 1 addition & 1 deletion cli/testdata/margintest_gridmode.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions svgscreen/svgscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type bgRect struct {
type SvgDom struct {
Width string
Height string
ViewBox string
FontName string
FontEmbedded []byte
FontRef string
Expand Down Expand Up @@ -308,10 +309,14 @@ func (s *Screen) Render(w io.Writer) error {
// Font-relative coordinates
s.Dom.Width = s.columnCoordinate(float32(s.TerminalWidth)+2*s.MarginSize.X, false)
s.Dom.Height = s.rowCoordinate(float32(s.NrLines)+2*s.MarginSize.Y, false)
s.Dom.ViewBox = ""
} else {
// Pixel coordinates
s.Dom.Width = fmt.Sprintf("%gpx", float32(s.CharacterBoxSize.X*s.TerminalWidth)+2*s.MarginSize.X)
s.Dom.Height = fmt.Sprintf("%gpx", float32(s.CharacterBoxSize.Y*s.NrLines)+2*s.MarginSize.Y)
w := float32(s.CharacterBoxSize.X*s.TerminalWidth) + 2*s.MarginSize.X
h := float32(s.CharacterBoxSize.Y*s.NrLines) + 2*s.MarginSize.Y
s.Dom.Width = fmt.Sprintf("%gpx", w)
s.Dom.Height = fmt.Sprintf("%gpx", h)
s.Dom.ViewBox = fmt.Sprintf("0 0 %g %g", w, h)
}

s.handleColorInversion()
Expand Down
2 changes: 1 addition & 1 deletion svgscreen/template.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 29c992c

Please sign in to comment.