Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to render icon content with d2 API #1853

Closed
zrcoder opened this issue Mar 3, 2024 · 3 comments
Closed

How to render icon content with d2 API #1853

zrcoder opened this issue Mar 3, 2024 · 3 comments

Comments

@zrcoder
Copy link

zrcoder commented Mar 3, 2024

I want to use d2 API to render icons, as the test below:

func TestTmp(t *testing.T) {
	d2 := `github: {icon: https://icons.terrastruct.com/dev/github.svg}
	gg: {
	  icon: https://icons.terrastruct.com/dev/github.svg
	  shape: image
	}`

	var ruler *textmeasure.Ruler
	var err error
	if ruler, err = textmeasure.NewRuler(); err != nil {
		t.Error(err)
	}
	layoutResolver := func(engine string) (d2graph.LayoutGraph, error) {
		return d2elklayout.DefaultLayout, nil
	}
	compileOpts := &d2lib.CompileOptions{
		LayoutResolver: layoutResolver,
		Ruler:          ruler,
	}

	renderOpt := &d2svg.RenderOpts{}

	diagram, _, _ := d2lib.Compile(context.Background(), d2, compileOpts, renderOpt)
	data, err := d2svg.Render(diagram, renderOpt)
	if err != nil {
		t.Error(err)
	}

	err = os.WriteFile("ttt.svg", data, 0600)
	if err != nil {
		t.Error(err)
	}
}

the saved ttt.svg just contains only href=https://icons.terrastruct.com/dev/github.svg but not the image content, how should I do?

@zachcp
Copy link

zachcp commented Mar 12, 2024

Related to #1815

@alixander
Copy link
Collaborator

alixander commented Mar 14, 2024

@zrcoder You can use this code

https://github.com/alixander/d2/blob/808f5c3aaca6b9dbfb0f085c71cc09381da37bbd/d2cli/main.go#L868

It downloads the images and encodes them as base64 in the SVG. I assume that's what you want

@zrcoder
Copy link
Author

zrcoder commented Mar 15, 2024

@zrcoder You can use this code

https://github.com/alixander/d2/blob/808f5c3aaca6b9dbfb0f085c71cc09381da37bbd/d2cli/main.go#L868

It downloads the images and encodes them as base64 in the SVG. I assume that's what you want

That's it, thanks a lot.

@zrcoder zrcoder closed this as completed Mar 15, 2024
@github-project-automation github-project-automation bot moved this to Done in D2 Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

3 participants