From ee8d89c1121a6d68721d3632541b11d8f42fe6c1 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Tue, 28 Feb 2023 12:57:26 -0800 Subject: [PATCH 1/4] update background rect width and height for appendix --- d2renderers/d2svg/appendix/appendix.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/d2renderers/d2svg/appendix/appendix.go b/d2renderers/d2svg/appendix/appendix.go index 3cb6a48a1..4dd4fa430 100644 --- a/d2renderers/d2svg/appendix/appendix.go +++ b/d2renderers/d2svg/appendix/appendix.go @@ -91,14 +91,15 @@ func Append(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []by newOuterViewbox := fmt.Sprintf(`viewBox="0 0 %d %d"`, viewboxWidth, viewboxHeight) newViewbox := fmt.Sprintf(`viewBox="%s %s %s %s"`, viewboxSlice[0], viewboxSlice[1], strconv.Itoa(viewboxWidth), strconv.Itoa(viewboxHeight)) - widthMatches := widthRegex.FindAllStringSubmatch(svg, 2) - heightMatches := heightRegex.FindAllStringSubmatch(svg, 2) + // update 1st 3 matches of width and height 1st is outer svg, 2nd inner svg, 3rd is background color rect + widthMatches := widthRegex.FindAllStringSubmatch(svg, 3) + heightMatches := heightRegex.FindAllStringSubmatch(svg, 3) newWidth := fmt.Sprintf(`width="%s"`, strconv.Itoa(viewboxWidth)) newHeight := fmt.Sprintf(`height="%s"`, strconv.Itoa(viewboxHeight)) svg = strings.Replace(svg, viewboxMatches[0][0], newOuterViewbox, 1) svg = strings.Replace(svg, viewboxMatch[0], newViewbox, 1) - for i := 0; i < 2; i++ { + for i := 0; i < 3; i++ { svg = strings.Replace(svg, widthMatches[i][0], newWidth, 1) svg = strings.Replace(svg, heightMatches[i][0], newHeight, 1) } From 08bcc7477a68272da0298f774f7c4cd0be4bbbec Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Tue, 28 Feb 2023 13:17:06 -0800 Subject: [PATCH 2/4] there are only 3 dimensions to update if rendered with SetDimensions --- d2renderers/d2svg/appendix/appendix.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/d2renderers/d2svg/appendix/appendix.go b/d2renderers/d2svg/appendix/appendix.go index 4dd4fa430..2575edcd0 100644 --- a/d2renderers/d2svg/appendix/appendix.go +++ b/d2renderers/d2svg/appendix/appendix.go @@ -52,6 +52,7 @@ const ( var viewboxRegex = regexp.MustCompile(`viewBox=\"([0-9\- ]+)\"`) var widthRegex = regexp.MustCompile(`width=\"([.0-9]+)\"`) var heightRegex = regexp.MustCompile(`height=\"([.0-9]+)\"`) +var svgRegex = regexp.MustCompile(``) func Append(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []byte { svg := string(in) @@ -91,15 +92,22 @@ func Append(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []by newOuterViewbox := fmt.Sprintf(`viewBox="0 0 %d %d"`, viewboxWidth, viewboxHeight) newViewbox := fmt.Sprintf(`viewBox="%s %s %s %s"`, viewboxSlice[0], viewboxSlice[1], strconv.Itoa(viewboxWidth), strconv.Itoa(viewboxHeight)) - // update 1st 3 matches of width and height 1st is outer svg, 2nd inner svg, 3rd is background color rect - widthMatches := widthRegex.FindAllStringSubmatch(svg, 3) - heightMatches := heightRegex.FindAllStringSubmatch(svg, 3) + // check if outer svg has dimensions set + svgMatches := svgRegex.FindAllStringSubmatch(svg, 2) + dimensionsToUpdate := 2 + if widthRegex.FindString(svgMatches[0][0]) != "" { + dimensionsToUpdate++ + } + + // update 1st 3 matches of width and height 1st is outer svg (if dimensions are set), 2nd inner svg, 3rd is background color rect + widthMatches := widthRegex.FindAllStringSubmatch(svg, dimensionsToUpdate) + heightMatches := heightRegex.FindAllStringSubmatch(svg, dimensionsToUpdate) newWidth := fmt.Sprintf(`width="%s"`, strconv.Itoa(viewboxWidth)) newHeight := fmt.Sprintf(`height="%s"`, strconv.Itoa(viewboxHeight)) svg = strings.Replace(svg, viewboxMatches[0][0], newOuterViewbox, 1) svg = strings.Replace(svg, viewboxMatch[0], newViewbox, 1) - for i := 0; i < 3; i++ { + for i := 0; i < dimensionsToUpdate; i++ { svg = strings.Replace(svg, widthMatches[i][0], newWidth, 1) svg = strings.Replace(svg, heightMatches[i][0], newHeight, 1) } From afb70dd5261d4434b93169386445e7bfe0d00cad Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Tue, 28 Feb 2023 13:22:05 -0800 Subject: [PATCH 3/4] cleanup --- d2renderers/d2svg/appendix/appendix.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/d2renderers/d2svg/appendix/appendix.go b/d2renderers/d2svg/appendix/appendix.go index 2575edcd0..4d2b61ca1 100644 --- a/d2renderers/d2svg/appendix/appendix.go +++ b/d2renderers/d2svg/appendix/appendix.go @@ -93,9 +93,8 @@ func Append(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []by newViewbox := fmt.Sprintf(`viewBox="%s %s %s %s"`, viewboxSlice[0], viewboxSlice[1], strconv.Itoa(viewboxWidth), strconv.Itoa(viewboxHeight)) // check if outer svg has dimensions set - svgMatches := svgRegex.FindAllStringSubmatch(svg, 2) dimensionsToUpdate := 2 - if widthRegex.FindString(svgMatches[0][0]) != "" { + if widthRegex.FindString(svgRegex.FindString(svg)) != "" { dimensionsToUpdate++ } From a910a4aaa4b14d8b494dfc2e09349f2ef2759c4b Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Tue, 28 Feb 2023 13:32:56 -0800 Subject: [PATCH 4/4] cleanup --- d2renderers/d2svg/appendix/appendix.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/d2renderers/d2svg/appendix/appendix.go b/d2renderers/d2svg/appendix/appendix.go index 4d2b61ca1..df756e26c 100644 --- a/d2renderers/d2svg/appendix/appendix.go +++ b/d2renderers/d2svg/appendix/appendix.go @@ -92,9 +92,10 @@ func Append(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []by newOuterViewbox := fmt.Sprintf(`viewBox="0 0 %d %d"`, viewboxWidth, viewboxHeight) newViewbox := fmt.Sprintf(`viewBox="%s %s %s %s"`, viewboxSlice[0], viewboxSlice[1], strconv.Itoa(viewboxWidth), strconv.Itoa(viewboxHeight)) - // check if outer svg has dimensions set dimensionsToUpdate := 2 - if widthRegex.FindString(svgRegex.FindString(svg)) != "" { + outerSVG := svgRegex.FindString(svg) + // if outer svg has dimensions set we also need to update it + if widthRegex.FindString(outerSVG) != "" { dimensionsToUpdate++ }