pr comments

This commit is contained in:
Júlio César Batista 2023-04-11 10:25:04 -03:00
parent 989c3f88ac
commit cbd1afeaef
No known key found for this signature in database
GPG key ID: 10C4B861BF314878
2 changed files with 15 additions and 14 deletions

View file

@ -351,7 +351,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, rende
switch filepath.Ext(outputPath) { switch filepath.Ext(outputPath) {
case ".pdf": case ".pdf":
pageMap := buildBoardIdToIndex(diagram, nil, nil) pageMap := buildBoardIDToIndex(diagram, nil, nil)
pdf, err := renderPDF(ctx, ms, plugin, renderOpts, outputPath, page, ruler, diagram, nil, nil, pageMap) pdf, err := renderPDF(ctx, ms, plugin, renderOpts, outputPath, page, ruler, diagram, nil, nil, pageMap)
if err != nil { if err != nil {
return pdf, false, err return pdf, false, err
@ -369,7 +369,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, rende
// version must be only numbers to avoid issues with PowerPoint // version must be only numbers to avoid issues with PowerPoint
p := pptx.NewPresentation(rootName, description, rootName, username, version.OnlyNumbers()) p := pptx.NewPresentation(rootName, description, rootName, username, version.OnlyNumbers())
boardIdToIndex := buildBoardIdToIndex(diagram, nil, nil) boardIdToIndex := buildBoardIDToIndex(diagram, nil, nil)
svg, err := renderPPTX(ctx, ms, p, plugin, renderOpts, ruler, outputPath, page, diagram, nil, boardIdToIndex) svg, err := renderPPTX(ctx, ms, p, plugin, renderOpts, ruler, outputPath, page, diagram, nil, boardIdToIndex)
if err != nil { if err != nil {
return nil, false, err return nil, false, err
@ -799,8 +799,6 @@ func renderPPTX(ctx context.Context, ms *xmain.State, presentation *pptx.Present
svg = appendix.Append(diagram, ruler, svg) svg = appendix.Append(diagram, ruler, svg)
// png.ConvertSVG scales the image by 2x
pngScale := 2.
pngImg, err := png.ConvertSVG(ms, page, svg) pngImg, err := png.ConvertSVG(ms, page, svg)
if err != nil { if err != nil {
return nil, err return nil, err
@ -827,10 +825,10 @@ func renderPPTX(ctx context.Context, ms *xmain.State, presentation *pptx.Present
continue continue
} }
linkX := pngScale * (float64(shape.Pos.X) - viewboxX - float64(shape.StrokeWidth)) linkX := png.SCALE * (float64(shape.Pos.X) - viewboxX - float64(shape.StrokeWidth))
linkY := pngScale * (float64(shape.Pos.Y) - viewboxY - float64(shape.StrokeWidth)) linkY := png.SCALE * (float64(shape.Pos.Y) - viewboxY - float64(shape.StrokeWidth))
linkWidth := pngScale * (float64(shape.Width) + float64(shape.StrokeWidth*2)) linkWidth := png.SCALE * (float64(shape.Width) + float64(shape.StrokeWidth*2))
linkHeight := pngScale * (float64(shape.Height) + float64(shape.StrokeWidth*2)) linkHeight := png.SCALE * (float64(shape.Height) + float64(shape.StrokeWidth*2))
link := &pptx.Link{ link := &pptx.Link{
Left: int(linkX), Left: int(linkX),
Top: int(linkY), Top: int(linkY),
@ -959,9 +957,9 @@ func loadFonts(ms *xmain.State, pathToRegular, pathToItalic, pathToBold string)
return d2fonts.AddFontFamily("custom", regularTTF, italicTTF, boldTTF) return d2fonts.AddFontFamily("custom", regularTTF, italicTTF, boldTTF)
} }
// buildBoardIdToIndex returns a map from board path to page int // buildBoardIDToIndex returns a map from board path to page int
// To map correctly, it must follow the same traversal of PDF building // To map correctly, it must follow the same traversal of pdf/pptx building
func buildBoardIdToIndex(diagram *d2target.Diagram, dictionary map[string]int, path []string) map[string]int { func buildBoardIDToIndex(diagram *d2target.Diagram, dictionary map[string]int, path []string) map[string]int {
newPath := append(path, diagram.Name) newPath := append(path, diagram.Name)
if dictionary == nil { if dictionary == nil {
dictionary = map[string]int{} dictionary = map[string]int{}
@ -972,13 +970,13 @@ func buildBoardIdToIndex(diagram *d2target.Diagram, dictionary map[string]int, p
dictionary[key] = len(dictionary) dictionary[key] = len(dictionary)
for _, dl := range diagram.Layers { for _, dl := range diagram.Layers {
buildBoardIdToIndex(dl, dictionary, append(newPath, "layers")) buildBoardIDToIndex(dl, dictionary, append(newPath, "layers"))
} }
for _, dl := range diagram.Scenarios { for _, dl := range diagram.Scenarios {
buildBoardIdToIndex(dl, dictionary, append(newPath, "scenarios")) buildBoardIDToIndex(dl, dictionary, append(newPath, "scenarios"))
} }
for _, dl := range diagram.Steps { for _, dl := range diagram.Steps {
buildBoardIdToIndex(dl, dictionary, append(newPath, "steps")) buildBoardIDToIndex(dl, dictionary, append(newPath, "steps"))
} }
return dictionary return dictionary

View file

@ -19,6 +19,9 @@ import (
"oss.terrastruct.com/util-go/xmain" "oss.terrastruct.com/util-go/xmain"
) )
// ConvertSVG scales the image by 2x
const SCALE = 2.
type Playwright struct { type Playwright struct {
PW *playwright.Playwright PW *playwright.Playwright
Browser playwright.Browser Browser playwright.Browser