preserve the arrow head when ReScale()

This commit is contained in:
Fahmi Akbar Wildana 2025-02-25 15:35:27 +07:00
parent a4260057ce
commit 31299b8e68
No known key found for this signature in database
GPG key ID: 7A0CD14F54006AAE
6 changed files with 141 additions and 99 deletions

View file

@ -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
}
}

View file

@ -1,29 +1,29 @@
|-----------------|
/ Hans Niemann |
///////////+---|||------|||--///////
||////////// |||| ||| ///////|
| Hans Niemann |
///////////+---||--------||--+//////
||////////// |||| ^ ^ ||| ///////|
||||| || || ||
| || | ||
|-|---------------------||---------------------|| ||
| |--------------|---| || |
v || | ||
|-----------------------|----------------------|| ||
| |------------------| || |
| | Magnus Carlsen | || |
| +---------|--------+ || |
| | || |
| | || |
| | || |
| |----------|----------| || |
| v || |
| |---------------------| || |
| | Play Magnus Group | || |
| +----------|----------+ || |
| | || |
| || defendants |||| |
| |||| //////|| | |
| |---|----------|///// | |
| |||| v //////|| | |
| |--------------|///// | |
| | Chess.com | | |
| +-------|------+ | |
| | | |
| || | ||||
| ||| | /////////|
| |-------|------------|///|///
| ||| v | /////////|
| |--------------------|///|///
| | Hikaru Nakamura | |
| +--------------------+ |
+----------------------------------------------+

View file

@ -7,15 +7,15 @@
| | | | | | | | | | | | | |
| | +----+ +----+ +----+ +----+ | | | |-------| |
|------------------------------------------------------------------| | | | | | | | |
| +----------+ | | | |----| |----| |----| |----| | | | ||//-----| | |
| +----------+ | | | |----| |----| |----| |----| | | | ||//---> | | |
| | | | | | | | | | | | | | | | | ||| +-------+ |
| ///-----| linked |----///| | | | +----+ +----+ +----+ +----+ | | | |----|| |
| |||| | | |||||-----------| | | | Test |--|-----------------|---| | Release |
| |----------| +----------+ | | | | | |----| |----| |----| |----| | | | +----||| +-------+ |
| | source | Build | |--|-----------------|---| | | | | | | | | | | | ||| | | |
| | | | | | | | +----+ +----+ +----+ +----+ | | | ///-----| | |
| ///---> | linked |----///| | | | +----+ +----+ +----+ +----+ | | | |----|| |
| |||| | | |||||-----------| | | | Test |--|-----------------|-> | | Release |
| |----------| +----------+ | | | | | |----| |----| |----| |----| | | | +----+|| +-------+ |
| | source | Build | |--|-----------------|-> | | | | | | | | | | | | ||| | | |
| | | | | | | | +----+ +----+ +----+ +----+ | | | ///---> | | |
| +----------+||| |----------| ||| | | | | | | | | | |
| |///------ assets -----///||| +-----------+ | | | |----| |----| |----| |----| | | | +-------+ |
| |///---> | assets |----///||| +-----------+ | | | |----| |----| |----| |----| | | | +-------+ |
| | | | | | | | | | | | | | | | +--------------------------------+
| +----------+ | | | +----+ +----+ +----+ +----+ | |
+------------------------------------------------------------------+ | | | |

View file

@ -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
| |
| |

View file

@ -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 | | |
| | | | || | || | || | | | | | | || | || | | | | | | | || | | || | | | |
| | | | || | || | || | | | | | +-----------++-----------+-----------++-----------+ | | | | | | || | | || | | | |
| | | | || | || | || | | | | | | | | | | | || | | || | | | |

View file

@ -2,40 +2,40 @@
| inputFile |
+------|-----+
|
|
|------|-----|
v
|------------|
| d2parser |
+------|-----+
|
|
|
|----|----|
v
|---------|
| d2ast |
+----|----+
|
|
|-------|------|
v
|--------------|
| d2compiler |
+-------|------+
|
v
|
|
|-----|------|
|------------|
| d2graph |
+-----|------+
|
|
|-------------|-------------|
v
|---------------------------|
| d2layouts/d2dagrelayout |
+-------------|-------------+
|
|
+-------|------+
v
+--------------+
| |
| d2exporter |
+-------|------+
|
|
|-----|------|
v
|------------|
| d2target |
+------------+