Skip to content

Commit

Permalink
fixup! wip: margin/border around image
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav1 committed Feb 22, 2024
1 parent de79c19 commit 70b78cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
42 changes: 25 additions & 17 deletions svgscreen/svgscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type bgRect struct {
type SvgDom struct {
Width string
Height string
MarginTop string
MarginLeft string
MarginX string
MarginY string
FontName string
FontEmbedded []byte
FontRef string
Expand Down Expand Up @@ -98,13 +98,13 @@ type Screen struct {
Dom SvgDom
}

func (s *Screen) columnCoordinate(col int) string {
func (s *Screen) columnCoordinate(col float32) string {
unit := "ch"
if s.CharacterBoxSize.Width > 0 {
unit = "px"
col *= s.CharacterBoxSize.Width
col *= float32(s.CharacterBoxSize.Width)
}
return strconv.Itoa(col) + unit
return fmt.Sprintf("%g%s", col, unit)
}

func (s *Screen) rowCoordinate(row float32) string {
Expand Down Expand Up @@ -187,7 +187,7 @@ func (s *Screen) lineToTextElement(l Line) textElement {
newSpan := s.charToFgText(c)
if s.GridMode {
// In grid mode, set X coordinate for each text span
newSpan.X = s.columnCoordinate(col)
newSpan.X = s.columnCoordinate(float32(col))
// In grid mode, we never consolidate
appendSpan()
currentSpan = newSpan
Expand Down Expand Up @@ -248,9 +248,9 @@ func (s *Screen) setupBgRects() {
return
}
s.Dom.BgRects = append(s.Dom.BgRects, bgRect{
X: s.columnCoordinate(currentRect.x),
X: s.columnCoordinate(float32(currentRect.x)),
Y: s.rowCoordinate(float32(y)),
Width: s.columnCoordinate(currentRect.w),
Width: s.columnCoordinate(float32(currentRect.w)),
Height: s.rowCoordinate(1),
Color: currentRect.color,
})
Expand Down Expand Up @@ -300,18 +300,26 @@ func (s *Screen) Render(w io.Writer) error {
s.Foreground.Custom = map[string]int{}
s.Background.Custom = map[string]int{}

marginPx := 10
marginX := float32(5.0)
marginY := float32(5.0)

// Set SVG size
if s.CharacterBoxSize.Width == 0 {
s.Dom.Width = s.columnCoordinate(s.TerminalWidth)
s.Dom.Height = s.rowCoordinate(float32(s.NrLines))
s.Dom.MarginLeft = "0px"
s.Dom.MarginTop = "0px"
// Font-relative coordinates
s.Dom.Width = s.columnCoordinate(float32(s.TerminalWidth) + 2*marginX)
s.Dom.Height = s.rowCoordinate(float32(s.NrLines) + 2*marginY)
if marginX > 0 || marginY > 0 {
s.Dom.MarginX = fmt.Sprintf("%gch", marginX)
s.Dom.MarginY = fmt.Sprintf("%gem", marginY)
}
} else {
s.Dom.Width = strconv.Itoa(s.CharacterBoxSize.Width*s.TerminalWidth+2*marginPx) + "px"
s.Dom.Height = strconv.Itoa(s.CharacterBoxSize.Height*s.NrLines+2*marginPx) + "px"
s.Dom.MarginLeft = strconv.Itoa(marginPx) + "px"
s.Dom.MarginTop = strconv.Itoa(marginPx) + "px"
// Pixel coordinates
s.Dom.Width = fmt.Sprintf("%gpx", float32(s.CharacterBoxSize.Width*s.TerminalWidth)+2*marginX)
s.Dom.Height = fmt.Sprintf("%gpx", float32(s.CharacterBoxSize.Height*s.NrLines)+2*marginY)
if marginX > 0 || marginY > 0 {
s.Dom.MarginX = fmt.Sprintf("%gpx", marginX)
s.Dom.MarginY = fmt.Sprintf("%gpx", marginY)
}
}

s.handleColorInversion()
Expand Down
4 changes: 3 additions & 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 70b78cf

Please sign in to comment.