From 31299b8e6814af14312ab07a045e6e859cf5317c Mon Sep 17 00:00:00 2001 From: Fahmi Akbar Wildana Date: Tue, 25 Feb 2025 15:35:27 +0700 Subject: [PATCH] preserve the arrow head when ReScale() --- d2renderers/d2ascii/d2ascii.go | 64 +++++++++++++++--- docs/examples/chess/dia.txt | 24 +++---- docs/examples/flipt/output.txt | 14 ++-- docs/examples/twitter/out.txt | 82 +++++++++++------------ docs/examples/vector-grid/vector-grid.txt | 28 ++++---- docs/flow.txt | 28 ++++---- 6 files changed, 141 insertions(+), 99 deletions(-) diff --git a/d2renderers/d2ascii/d2ascii.go b/d2renderers/d2ascii/d2ascii.go index cc131287f..f02de8a49 100644 --- a/d2renderers/d2ascii/d2ascii.go +++ b/d2renderers/d2ascii/d2ascii.go @@ -62,8 +62,7 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) { } } - // TODO: preserve arrow head - // canvas.ReScale(canvas.w, canvas.AutoHeight()) + canvas.ReScale(canvas.w, canvas.AutoHeight()) return canvas.TrimBytes(), nil } @@ -383,6 +382,10 @@ func (c *Canvas) AutoHeight() int { return maxH } +func isArrowHead(ch rune) bool { + return ch == '>' || ch == '<' || ch == '^' || ch == 'v' +} + // ReScale reduces the size of ASCII art using a pixel-like sampling technique func (c *Canvas) ReScale(targetWidth, targetHeight int) { scaleX := float64(targetWidth) / float64(c.w) @@ -397,27 +400,67 @@ func (c *Canvas) ReScale(targetWidth, targetHeight int) { } } - // First scale the borders and lines (source -> target mapping) - for y := 0; y < c.h; y++ { + // First pass: scale borders and lines, but skip arrow heads + for y := range c.h { targetY := int(float64(y) * scaleY) if targetY >= targetHeight { continue } - for x := 0; x < c.w; x++ { + for x := range c.w { targetX := int(float64(x) * scaleX) if targetX >= targetWidth { continue } ch := c.grid[y][x] - if ch == '+' || ch == '-' || ch == '|' || ch == '/' || ch == '\\' || ch == '.' { + if !isArrowHead(ch) && (ch == '+' || ch == '-' || ch == '|' || ch == '/' || ch == '\\') { newGrid[targetY][targetX] = ch } } } - // Then redraw text at scaled positions + // Second pass: copy arrow heads with position adjustment + for y := range c.h { + targetY := int(float64(y) * scaleY) + if targetY >= targetHeight { + continue + } + + for x := range c.w { + targetX := int(float64(x) * scaleX) + if targetX >= targetWidth { + continue + } + + ch := c.grid[y][x] + if isArrowHead(ch) { + // Determine offset based on arrow direction + var dx, dy int + switch ch { + case '>': + dx = -1 + case '<': + dx = 1 + case 'v': + dy = -1 + case '^': + dy = 1 + } + + // Apply offset and ensure we stay within bounds + finalX := min(max(0, targetX+dx), targetWidth-1) + finalY := min(max(0, targetY+dy), targetHeight-1) + + // Only place arrow if target position is empty or has a line character + if newGrid[finalY][finalX] == ' ' || newGrid[finalY][finalX] == '-' || newGrid[finalY][finalX] == '|' { + newGrid[finalY][finalX] = ch + } + } + } + } + + // Third pass: redraw text at scaled positions for _, label := range c.textPositions { // Get box dimensions in source coordinates first srcBoxCenterY := label.y + label.h/2 @@ -455,11 +498,10 @@ func (c *Canvas) ReScale(targetWidth, targetHeight int) { continue } - // Only overwrite space or existing text + // Only overwrite if not an arrow head and not a border existing := newGrid[targetY][targetX] - if existing == ' ' || (existing != '+' && existing != '-' && - existing != '|' && existing != '/' && existing != '\\' && - existing != '.') { + if !isArrowHead(existing) && existing != '+' && existing != '-' && + existing != '|' && existing != '/' && existing != '\\' { newGrid[targetY][targetX] = ch } } diff --git a/docs/examples/chess/dia.txt b/docs/examples/chess/dia.txt index 4014dcbfa..6c1e003ef 100644 --- a/docs/examples/chess/dia.txt +++ b/docs/examples/chess/dia.txt @@ -1,29 +1,29 @@ |-----------------| - / Hans Niemann | - ///////////+---|||------|||--/////// - ||////////// |||| ||| ///////| + | Hans Niemann | + ///////////+---||--------||--+////// + ||////////// |||| ^ ^ ||| ///////| ||||| || || || - | || | || -|-|---------------------||---------------------|| || -| |--------------|---| || | + v || | || +|-----------------------|----------------------|| || +| |------------------| || | | | Magnus Carlsen | || | | +---------|--------+ || | | | || | | | || | -| | || | -| |----------|----------| || | +| v || | +| |---------------------| || | | | Play Magnus Group | || | | +----------|----------+ || | | | || | | || defendants |||| | -| |||| //////|| | | -| |---|----------|///// | | +| |||| v //////|| | | +| |--------------|///// | | | | Chess.com | | | | +-------|------+ | | | | | | | || | |||| -| ||| | /////////| -| |-------|------------|///|/// +| ||| v | /////////| +| |--------------------|///|/// | | Hikaru Nakamura | | | +--------------------+ | +----------------------------------------------+ diff --git a/docs/examples/flipt/output.txt b/docs/examples/flipt/output.txt index a56cb3d78..a73482d17 100644 --- a/docs/examples/flipt/output.txt +++ b/docs/examples/flipt/output.txt @@ -7,15 +7,15 @@ | | | | | | | | | | | | | | | | +----+ +----+ +----+ +----+ | | | |-------| | |------------------------------------------------------------------| | | | | | | | | -| +----------+ | | | |----| |----| |----| |----| | | | ||//-----| | | +| +----------+ | | | |----| |----| |----| |----| | | | ||//---> | | | | | | | | | | | | | | | | | | | | ||| +-------+ | -| ///-----| linked |----///| | | | +----+ +----+ +----+ +----+ | | | |----|| | -| |||| | | |||||-----------| | | | Test |--|-----------------|---| | Release | -| |----------| +----------+ | | | | | |----| |----| |----| |----| | | | +----||| +-------+ | -| | source | Build | |--|-----------------|---| | | | | | | | | | | | ||| | | | -| | | | | | | | +----+ +----+ +----+ +----+ | | | ///-----| | | +| ///---> | linked |----///| | | | +----+ +----+ +----+ +----+ | | | |----|| | +| |||| | | |||||-----------| | | | Test |--|-----------------|-> | | Release | +| |----------| +----------+ | | | | | |----| |----| |----| |----| | | | +----+|| +-------+ | +| | source | Build | |--|-----------------|-> | | | | | | | | | | | | ||| | | | +| | | | | | | | +----+ +----+ +----+ +----+ | | | ///---> | | | | +----------+||| |----------| ||| | | | | | | | | | | -| |///------ assets -----///||| +-----------+ | | | |----| |----| |----| |----| | | | +-------+ | +| |///---> | assets |----///||| +-----------+ | | | |----| |----| |----| |----| | | | +-------+ | | | | | | | | | | | | | | | | | +--------------------------------+ | +----------+ | | | +----+ +----+ +----+ +----+ | | +------------------------------------------------------------------+ | | | | diff --git a/docs/examples/twitter/out.txt b/docs/examples/twitter/out.txt index ee47d9815..122fd9ece 100644 --- a/docs/examples/twitter/out.txt +++ b/docs/examples/twitter/out.txt @@ -17,8 +17,8 @@ || | || || | || || | || - ||| | ||| - || +-------|-------+ || + ||| v ||| + || +---------------+ || ||| | | ||| ||| ||| | | @@ -43,13 +43,13 @@ || | | | |-----------------|---------------------------------------------------------------------------------------------------------------------------|----------| + | | v | | | | | - | | | | - | | |-------|-------| | - | | | | | - | +-------------|--------------+ | | | + | | |---------------| | + | v | | | + | +----------------------------+ | | | | | | | | | - | | | |-------------------------------------------------------------| | | | + | | | |-------------------------------------v-----------------------| | | | | | TLS-API (being deprecated) | | ## Tweet/user content hydration, visibility filtering | | GraphQL | | | | | | | ////|rated Strato Co|um| | | | +-------------------------------------------------------------+ //////// | |//| @@ -60,7 +60,7 @@ | | ///////// | ///// | | /////// | ///| | || ////// | || - +-------------------||---------------------------------------------///-----------------------------------------------------------------------------------+ || + +--------------------|---------------------------------------------//------------------------------------------------------------------------------------+ || ||| |//// || || | || || | || @@ -69,22 +69,22 @@ |/////////// | /////// /////// | ////////// /////////// | - ////////// //////////// | + ////////// //////////// v ////////// /////////// | - |///// |-----------------------------------------//----------------------------------------------------------------------------------------------------|--| + |///// |--------------------------------------------------------------------------------------------------------------------------------------------------| | | | | | | | | |----------------------------------| | | | | | | | | | | | + v | | | | | | | | | - | | | | | - |-------|-------| | | | | + |---------------| | | | | | | | | | | - | | | -| ## **Time-in- mixer** | | - | Home mixer | | -|Inject-ads, who-to-follow, onboard|ng | + | | | -| ## **vime-in- mixer** | | + | Home mixer | | -|Inject-ads,vwho-to-follow, onboard|ng | ///| | | | - Conversation module | | - /////////+---------------/ | | - Cursoring,pagination | | + /////////+---------------+ | | - Cursoring,pagination | | ////// //// || | || || /// | | - Tweat deduplication | | //// // || || | || // | | - Served data logging | | ////// //// || | || ||| /// | | | | @@ -105,15 +105,15 @@ | | | | || ||| /////// || || | | | | | | | || ||| //// /// ||| || | | | | | | | || ||| //// //// || || | | | - | | | | | /|| ///|| || | | | +--------------+ - | | | | || //// ||| ||/// +------------+ | +---------------+ +-------------------+ | | -|--------------| |---------------| |--------------| |--------------| |-------------------| |----------------| ////| | |------------| | | | | | | -| | | | | | | | | | | | | | | | | | | | | | -| | | | | | | | | | | | | Timeline | | | | Onboarding | | People discovery | | | -| Tweety Pie | | Social graph | | Gizmoduck | | Manhattan | | Timeline Service | | Home Ranker | | Scorer | | Ad mixer | | service | | service | /////Fetch | -| | | | | | | | | | | |//// | | | | | | | //////////| | -| | | | | | | | | | | | //// | | | | | | ////// | | | -+--------------+ +---------------+ +--------------+ +--------------+ +-------------------+ +------|---------+ +-----//-----+ +------------+ +---------------+ +-///---------------+ | | + v v v v | v /|| v ///|| v|| v v v +--------------+ + | | | | | //// ||| ||/// +------------+ | +---------------+ +-------------------+ | | +|--------------| |---------------| |--------------| |--------------| |-------------------| < |----------------| ////| | |------------| | | | | | | +| | | | | | | | | | | | > | | | | | | | v | | | +| | | | | | | | | v | | | | Timeline | | | | Onboavding | | People dvscovery | | | +| Tweety Pie | | Social graph | | Gizmoduck | | Manhattan | | Timeline Service | | Home Ranker | | Scorer | | Ad mixer | | service | | service | |////Fetch | +| | | | | | | | | | | |//// | | | | | | | //|///////| | +| | | | | | | | | | | | ///| | | | | | | ////// | | | ++--------------+ +---------------+ +--------------+ +--------------+ +-------------------+ +------|---------+ +------------+ +------------+ +---------------+ +-------------------+ | | || /////// ///////// +--------|-----+ | /////// ////////// || | /////// ////////// | @@ -127,16 +127,16 @@ ////////// || /// | /////// | /// | |///// | //// | - | || //////// | - | |-------|--------------------------------------/////---------------------------------------------------------------------| | - | | || //////// | |----------------| + | || //////// v + | |-------|--------------------------------------//------------------------------------------------------------------------| | + v | |v //////// | |----------------| | | | //////// | | | |----------------| | |-----------| ///// |------------| |--------| |---------| |----------------| | | | | | | | | //////// | | | | | | | | | | | - | | | | |/ | | | | | | | | | | | + | | | | | < | | | | | | | | | | | | Home Scorer | | | CrMixer | | EarlyBird | | Utag | | Space | | Communities | | | Feature | - | | | | | | | | | | | | | | ///// | - +---|--||---|----+ | +-----------+ +------------+ +--------+ +---------+ +----------------+ | ///// |// | + | | | | | | | | | | | | | | /|/// | + +----------------+ | +-----------+ +------------+ +--------+ +---------+ +----------------+ | ///// |// | || || || | | //// //| | || | | | | | //// // +--|--------|----+ || ||| || +------------------------------------------------------------------------------------------------------------------------+ ///// /// || || @@ -152,18 +152,18 @@ | /// /// //// || | | | | /////// //////// //////// | | | | | ///////////// ////////////// //////////////// || || | | - | ////////////// ////////////// //////////////// || || | | - |-------|-------| ///////////// ////////////// /////////////// ||| || | | - | | ///////////// ////////////// //////////////// ||| || | +----------------+ - | | ///////////// /////////////// //////////////// ||| |--------------|--| | | | - | | ////////////// //////////////---------||/////|/////// | |--------|-----| | | - | | //////// |/////////| | /////////// | | | | - | | //////////// |//////| |///////| | | | + v ////////////// ////////////// //////////////// || || | v + |---------------| ///////////// ////////////// /////////////// ||| v || | | + | | ///////////// ////////////// //////////////// ||| || v| +----------------+ + | | ///////////// /////////////// //////////////// ||| |-----------------| | | | + | | ////////////// /////////////|---------||/////|/////// | |--------------| | | + | | //////// |/////////| | //////////| | | | | + | v | ////////// | |///// | |////// | | | | |rediction Servi|e | ...etc | | Memcache | | Manhattan | | Scoring | | | | | | | | | | | | | +---------+ | | +--------------+ | | | | | | | | - | | +-----------------+ +-------|--------+ + | | +-----------------+ +----------------+ +---------------+ | | | @@ -172,13 +172,13 @@ | | | - | - |-------|-------| - | | + v + |---------------| | | | | | | | | + | v | |rediction Servi|e | | | | diff --git a/docs/examples/vector-grid/vector-grid.txt b/docs/examples/vector-grid/vector-grid.txt index 913ff4d14..582c319ed 100644 --- a/docs/examples/vector-grid/vector-grid.txt +++ b/docs/examples/vector-grid/vector-grid.txt @@ -58,7 +58,7 @@ | | | | | | | | | | | | | | | | | | -| +----------------------------------------------------------------------------------------------------------------------------------------+ +-----------------|------------------+ | +| +----------------------------------------------------------------------------------------------------------------------------------------+ +------------------------------------+ | | | | | | | | | | @@ -74,7 +74,7 @@ | | | | | | | | | -+----------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------+ ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | | | @@ -85,7 +85,7 @@ | | | -+----------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------+ ++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | | | | | | | | | @@ -101,7 +101,7 @@ | | | | | | -| +-----------------------------------------------------------------------------------+ +---------------------------+ +-----------------------------------|----------------------------+ +-----------+ +-----------------------------------------------------------------------------------+ | +| +-----------------------------------------------------------------------------------+ +---------------------------+ +----------------------------------------------------------------+ +-----------+ +-----------------------------------------------------------------------------------+ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | @@ -114,8 +114,8 @@ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | -| | | | | | | | | | | | | -| | | | | | | | | | | | | +| | | | | | v | | | | | | +| | | | | | | | | | | | | | | | +------------+ | | | | | | | | | | | | | | | | | | | | | | | @@ -138,7 +138,7 @@ | | | | || | || | || | | | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | | | || | || | | | | | | | || | | || | | | | -| | | . | . |------------- |------------- . || . | | | | | | | | . || . | . || . | | | | | | . | . |------------- -------------- . | . | | | +| | | | ||-------- - | || ------> | || | | | | | | | | || | || | | | | | | | ||-------- - | | --------|| | | | | | | | 0.62 | 0.15 || n-s|zen V||tor | 0.91 || 0.48 | | | | | | | | 0.41 || 0.32 | 0.92 || 0.13 | | | | | | 0.62 | 0.15 || m-s|zem Ve|tor || 0.91 | 0.48 | | | | | | | || | || | || | | | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | | | || | || | | | | | | | || | | || | | | | @@ -164,7 +164,7 @@ | | | | | | | | | || | || | | | | | | | | | | | | +-+ | | | | || | || | | | | | | | | | | | | | | | | | | || | || | | | | | | | -| | | | Dot |r|duct | | | . || . | . || . | | | | | | | +| | | | Dot |r|duct | | | || | || | | | | | | | | | | | | | | | | | | 0.74 || 0.15 | 0.53 || 0.21 | | | | | | | | | | | | | | | | | | || | || | | | | | | | | | | | | +-+ | | | | || | || | | | | | | | @@ -179,18 +179,18 @@ | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | +-----------++-----------+-----------++-----------+ | | | | | | || | | || | | | | -| | | | || | || | || | | | | | | || | || | ----------------------- | | || | | || | | | | +| | | | || | || | || | | | | | | || | || | |----|-----------|--> | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | -| | | . | . |------------- |------------- . || . | | | | | | || | || | | | | | | . | . |------------- -------------- . | . | | | +| | | | ||-------- - | || ------> | || | | | | | | || | || | | | | | | | ||-------- - | | --------|| | | | | | | | 0.62 | 0.15 || n-s|zen V||tor | 0.91 || 0.48 | | | | | | || | || | | | | | | 0.62 | 0.15 || m-s|zem Ve|tor || 0.91 | 0.48 | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | -| | | | || | || | || | | | | | | . || . | . || . | | | | | | | || | | || | | | | +| | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | 0.97 || 0.45 | 0.11 || 0.05 | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | @@ -217,7 +217,7 @@ | | | | | | | || | || | | | | | | | | | +--------+--------++-----------+-----++-----------+--------++--------+ | | | | | || | || | | | | | +--------+--------++-----------+------+-----------++--------+--------+ | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | -| | | | || | || | || | | | | | | . || . | . || . | | | | | | | || | | || | | | | +| | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | 0.61 || 0.77 | 0.59 || 0.83 | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | @@ -228,8 +228,8 @@ | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | -| | | . | . || - | || | . || . | | | | | | || | || | | | | | | . | . || - | | || . | . | | | -| | | 0.62 | 0.15 |-------------zen V|------------- 0.91 || 0.48 | | | | | | || | || | | | | | | 0.62 | 0.15 |-------------zem Ve-------------- 0.91 | 0.48 | | | +| | | | || - | || | || | | | | | | || | || | | | | | | | || - | | || | | | | +| | | 0.62 | 0.15 ||-----------|zen V||---------> | 0.91 || 0.48 | | | | | | || | || | | | | | | 0.62 | 0.15 ||-----------|zem Ve|-----------|| 0.91 | 0.48 | | | | | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | | | | | | || | || | || | | | | | +-----------++-----------+-----------++-----------+ | | | | | | || | | || | | | | | | | | || | || | || | | | | | | | | | | | || | | || | | | | diff --git a/docs/flow.txt b/docs/flow.txt index 10b73cf24..4c0b8ff5d 100644 --- a/docs/flow.txt +++ b/docs/flow.txt @@ -2,40 +2,40 @@ | inputFile | +------|-----+ | - | - |------|-----| + v + |------------| | d2parser | +------|-----+ | | - | - |----|----| + v + |---------| | d2ast | +----|----+ | - | - |-------|------| + v + |--------------| | d2compiler | +-------|------+ | + v | - | - |-----|------| + |------------| | d2graph | +-----|------+ | - | -|-------------|-------------| + v +|---------------------------| | d2layouts/d2dagrelayout | +-------------|-------------+ | - | - +-------|------+ + v + +--------------+ | | | d2exporter | +-------|------+ | - | - |-----|------| + v + |------------| | d2target | +------------+