Skip to content

Commit 2d86de7

Browse files
authored
Merge pull request #377 from cogentcore/pdf
pdf: canvas updates to pdf branch for exporting pdf, and new setting api
2 parents cf20155 + cc211e0 commit 2d86de7

File tree

21 files changed

+227
-232
lines changed

21 files changed

+227
-232
lines changed

.github/workflows/core.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ jobs:
3737
- name: Install Go
3838
uses: actions/setup-go@v5
3939
with:
40-
go-version: '1.23.4'
40+
go-version: '1.25.6'
4141

4242
- name: Install Core
43-
run: go install cogentcore.org/core@main
43+
run: go install cogentcore.org/core@main && core setup
4444

4545
- name: Build Docs
4646
run: core build web -dir docs -o static -vanity-url cogentcore.org/cogent -github-vanity-repository cogentcore/cogent
@@ -76,4 +76,4 @@ jobs:
7676
steps:
7777
- name: Deploy to GitHub Pages
7878
id: deployment
79-
uses: actions/deploy-pages@v4
79+
uses: actions/deploy-pages@v4

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Go
1717
uses: actions/setup-go@v5
1818
with:
19-
go-version: '1.23.4'
19+
go-version: '1.25.6'
2020

2121
- name: Set up Core
2222
run: go install cogentcore.org/core@main && core setup
@@ -28,7 +28,7 @@ jobs:
2828
run: go test -v ./... -coverprofile cover.out -timeout 30s
2929

3030
- name: Update coverage report
31-
uses: ncruces/go-coverage-report@v0
31+
uses: cogentcore/goal-coverage-report@v0
3232
with:
3333
coverage-file: cover.out
3434
if: github.event_name == 'push'

canvas/TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ but is instead constraining to diagonal movement, which is not the same!
1515

1616
* grouping can change front / back order -- it is using the selection order instead of the existing order?
1717

18+
* align chooser -> switches, without Align prefix (first, last, etc)
19+
1820
* more subtle logic about duplicating groups:
1921
+ is it duplicating all the elements within the same group?
2022
+ duplicating single element within group puts it in the group.. not what you expect.

canvas/canvas.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"cogentcore.org/core/math32"
2727
"cogentcore.org/core/styles"
2828
"cogentcore.org/core/styles/abilities"
29+
"cogentcore.org/core/styles/units"
2930
"cogentcore.org/core/svg"
3031
"cogentcore.org/core/system"
3132
"cogentcore.org/core/tree"
@@ -347,11 +348,28 @@ func (cv *Canvas) ExportPNG(width, height float32) error { //types:add
347348
}
348349

349350
// ExportPDF exports drawing to a PDF file (auto-names to same name
350-
// with .pdf suffix). Calls inkscape -- needs to be on the PATH.
351+
// with .pdf suffix). Uses native PDF rendering or optionally
352+
// calls inkscape from the command line -- needs to be on the PATH.
351353
// specify DPI of resulting image for effects rendering.
352354
// Renders full current page -- do ResizeToContents
353355
// to render just current contents.
354-
func (cv *Canvas) ExportPDF(dpi float32) error { //types:add
356+
func (cv *Canvas) ExportPDF(dpi float32, inkscape bool) error { //types:add
357+
if inkscape {
358+
return cv.ExportPDFInkscape(dpi)
359+
}
360+
fext := filepath.Ext(string(cv.Filename))
361+
onm := strings.TrimSuffix(string(cv.Filename), fext) + ".pdf"
362+
363+
sv := cv.SSVG()
364+
ctx := units.NewContext()
365+
sz := math32.Vector2{}
366+
sz.X = ctx.ToDots(sv.PhysicalWidth.Value, sv.PhysicalWidth.Unit)
367+
sz.Y = ctx.ToDots(sv.PhysicalHeight.Value, sv.PhysicalHeight.Unit)
368+
nsv := sv.CloneSVG(sz)
369+
return nsv.SavePDF(onm)
370+
}
371+
372+
func (cv *Canvas) ExportPDFInkscape(dpi float32) error {
355373
path, _ := filepath.Split(string(cv.Filename))
356374
fnm := filepath.Join(path, "export_pdf.svg")
357375
sv := cv.SVG

canvas/cmd/canvas/canvas.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"cogentcore.org/cogent/canvas"
1111
"cogentcore.org/core/core"
12+
"cogentcore.org/core/system"
1213
)
1314

1415
func main() {
@@ -18,7 +19,7 @@ func main() {
1819
var fnms []string
1920
if len(ofs) > 0 {
2021
fnms = ofs
21-
} else if len(os.Args) > 1 {
22+
} else if !system.GenerateHTMLArg() && len(os.Args) > 1 {
2223
fnms = os.Args[1:]
2324
}
2425

canvas/path.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func (sv *SVG) PathNodes(path *svg.Path) []*PathNode {
5858
cmd := scan.Cmd()
5959
end := scan.End()
6060
start := scan.Start()
61-
tend := xf.MulVector2AsPoint(end)
62-
tstart := xf.MulVector2AsPoint(start)
61+
tend := xf.MulPoint(end)
62+
tstart := xf.MulPoint(start)
6363

6464
pn := &PathNode{CmdPath: cmd, Index: scan.Index(), PtIndex: pti, Start: start, End: end, TStart: tstart, TEnd: tend}
6565
pns = append(pns, pn)
@@ -73,13 +73,13 @@ func (sv *SVG) PathNodes(path *svg.Path) []*PathNode {
7373
case ppath.QuadTo:
7474
pn.Cmd = SpQuadTo
7575
pn.Cp1 = scan.CP1()
76-
pn.TCp1 = xf.MulVector2AsPoint(pn.Cp1)
76+
pn.TCp1 = xf.MulPoint(pn.Cp1)
7777
case ppath.CubeTo:
7878
pn.Cmd = SpCubeTo
7979
pn.Cp1 = scan.CP1()
8080
pn.Cp2 = scan.CP2()
81-
pn.TCp1 = xf.MulVector2AsPoint(pn.Cp1)
82-
pn.TCp2 = xf.MulVector2AsPoint(pn.Cp2)
81+
pn.TCp1 = xf.MulPoint(pn.Cp1)
82+
pn.TCp2 = xf.MulPoint(pn.Cp2)
8383
// todo: arc
8484
case ppath.Close:
8585
pn.Cmd = SpClose
@@ -368,7 +368,7 @@ func (sv *SVG) PathNodeMove(pidx int, pointOnly bool, dv math32.Vector2, dxf mat
368368
path := es.ActivePath
369369
pn := es.PathNodesOrig[pidx]
370370
_, isSel := es.NodeSelect[pidx]
371-
end := dxf.MulVector2AsPoint(pn.End)
371+
end := dxf.MulPoint(pn.End)
372372
switch pn.Cmd {
373373
case SpMoveTo, SpLineTo, SpClose:
374374
path.Data[pn.Index+1] = end.X
@@ -377,7 +377,7 @@ func (sv *SVG) PathNodeMove(pidx int, pointOnly bool, dv math32.Vector2, dxf mat
377377
path.Data[pn.Index+3] = end.X
378378
path.Data[pn.Index+4] = end.Y
379379
if !pointOnly && isSel {
380-
cp1 := dxf.MulVector2AsPoint(pn.Cp1)
380+
cp1 := dxf.MulPoint(pn.Cp1)
381381
path.Data[pn.Index+1] = cp1.X
382382
path.Data[pn.Index+2] = cp1.Y
383383
if sp1, ok := sprites.SpriteByNameNoLock(SpriteName(SpNodeCtrl, SpQuad1, pidx)); ok {
@@ -388,7 +388,7 @@ func (sv *SVG) PathNodeMove(pidx int, pointOnly bool, dv math32.Vector2, dxf mat
388388
path.Data[pn.Index+5] = end.X
389389
path.Data[pn.Index+6] = end.Y
390390
if !pointOnly && isSel {
391-
cp2 := dxf.MulVector2AsPoint(pn.Cp2)
391+
cp2 := dxf.MulPoint(pn.Cp2)
392392
path.Data[pn.Index+3] = cp2.X
393393
path.Data[pn.Index+4] = cp2.Y
394394
if sp2, ok := sprites.SpriteByNameNoLock(SpriteName(SpNodeCtrl, SpCube2, pidx)); ok {
@@ -406,7 +406,7 @@ func (sv *SVG) PathNodeMove(pidx int, pointOnly bool, dv math32.Vector2, dxf mat
406406
if pn.Cmd != SpCubeTo {
407407
return
408408
}
409-
cp1 := dxf.MulVector2AsPoint(pn.Cp1)
409+
cp1 := dxf.MulPoint(pn.Cp1)
410410
path.Data[pn.Index+1] = cp1.X
411411
path.Data[pn.Index+2] = cp1.Y
412412
sp1, ok := sprites.SpriteByNameNoLock(SpriteName(SpNodeCtrl, SpCube1, pidx))
@@ -423,12 +423,12 @@ func (sv *SVG) PathCtrlMove(pidx int, ctyp Sprites, dxf math32.Matrix2) math32.V
423423
pn := es.PathNodesOrig[pidx]
424424
switch ctyp {
425425
case SpQuad1, SpCube1:
426-
cp1 := dxf.MulVector2AsPoint(pn.Cp1)
426+
cp1 := dxf.MulPoint(pn.Cp1)
427427
path.Data[pn.Index+1] = cp1.X
428428
path.Data[pn.Index+2] = cp1.Y
429429
return pn.TCp1
430430
case SpCube2:
431-
cp2 := dxf.MulVector2AsPoint(pn.Cp2)
431+
cp2 := dxf.MulPoint(pn.Cp2)
432432
path.Data[pn.Index+3] = cp2.X
433433
path.Data[pn.Index+4] = cp2.Y
434434
return pn.TCp2
@@ -571,8 +571,8 @@ func (sv *SVG) NodeAdd(ntyp Sprites, p ppath.Path, end, start math32.Vector2) pp
571571
es := sv.EditState()
572572
path := es.ActivePath
573573
xf := path.ParentTransform(true).Inverse()
574-
lend := xf.MulVector2AsPoint(end)
575-
lstart := xf.MulVector2AsPoint(start)
574+
lend := xf.MulPoint(end)
575+
lstart := xf.MulPoint(start)
576576
del := lend.Sub(lstart)
577577
switch ntyp {
578578
case SpClose:

canvas/select.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ func (sv *SVG) SelectContainsPoint(pt image.Point, leavesOnly, excludeSel bool)
640640
return tree.Break
641641
}
642642
xf := p.ParentTransform(true).Inverse()
643-
pxf := xf.MulVector2AsPoint(ptv)
643+
pxf := xf.MulPoint(ptv)
644644
if intersect.Contains(p.Data, pxf.X, pxf.Y, p.Paint.Fill.Rule) {
645645
rval = n
646646
return tree.Break

canvas/settings.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package canvas
66

77
import (
88
"path/filepath"
9-
"slices"
109

1110
"cogentcore.org/core/base/errors"
1211
"cogentcore.org/core/base/fsx"
@@ -17,7 +16,7 @@ import (
1716

1817
func init() {
1918
core.TheApp.SetName("Cogent Canvas")
20-
core.AllSettings = slices.Insert(core.AllSettings, 1, core.Settings(Settings))
19+
core.AddAppSettings(Settings)
2120
}
2221

2322
// Settings are the overall Code settings

canvas/shapes.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ func NewSVGElementDrag[T tree.NodeValue](sv *SVG, start, end image.Point) *T {
8080
snb := sn.AsNodeBase()
8181

8282
xfi := sv.Root().Paint.Transform.Inverse()
83-
pos := xfi.MulVector2AsPoint(st)
83+
pos := xfi.MulPoint(st)
8484
sn.SetNodePos(pos)
8585
sz := dv.Abs().Max(math32.Vector2Scalar(minsz / 2))
86-
sz = xfi.MulVector2AsVector(sz)
86+
sz = xfi.MulVector(sz)
8787
sn.SetNodeSize(sz)
8888
sn.BBoxes(sv.SVG, snb.ParentTransform(false))
8989

@@ -111,11 +111,11 @@ func (sv *SVG) NewText(start, end image.Point) svg.Node {
111111
pos := math32.FromPoint(start)
112112
// minsz := float32(20)
113113
pos.Y += 20 // todo: need the font size..
114-
pos = xfi.MulVector2AsPoint(pos)
114+
pos = xfi.MulPoint(pos)
115115
tspan.Properties = maps.Clone(es.Text.TextProperties())
116116
tspan.Pos = pos
117117
sz := dv.Abs().Max(math32.Vector2Scalar(minsz / 2))
118-
sz = xfi.MulVector2AsVector(sz)
118+
sz = xfi.MulVector(sz)
119119
tspan.SetNodeSize(sz)
120120
tspan.BBoxes(sv.SVG, tspan.ParentTransform(false))
121121

@@ -136,8 +136,8 @@ func (sv *SVG) NewPath(start, end image.Point) *svg.Path {
136136
sv.ManipStart(NewPath, "")
137137
n := NewSVGElement[svg.Path](sv, false)
138138
xfi := sv.Root().Paint.Transform.Inverse()
139-
pos := xfi.MulVector2AsPoint(math32.FromPoint(start))
140-
sz := xfi.MulVector2AsVector(dv)
139+
pos := xfi.MulPoint(math32.FromPoint(start))
140+
sz := xfi.MulVector(dv)
141141
fmt.Println(n.Data)
142142
n.Data = nil
143143
n.Data.MoveTo(pos.X, pos.Y)

canvas/text.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (ts *TextStyle) TextProperties() map[string]any {
8989
delete(tps, "select-color")
9090
delete(tps, "highlight-color")
9191
if ts.Stroke == colors.Transparent { // need to explicitly set to none
92-
tps["stroke-color"] = "none"
92+
tps["stroke"] = "none"
9393
}
9494
return tps
9595
}

0 commit comments

Comments
 (0)