Introduction

Collagen is a program that takes as input a folder containing zero or more image files (.jpeg, .png, etc.) and a JSON or Jsonnet manifest file describing the layout of these images along with SVG components such as shapes and text, and produces as output a single SVG file with all assets embedded.

This allows a user to combine several graphics into a single file that can be displayed as an image without compromising on visual quality or file size.[1]

Using Collagen

The input to Collagen is a folder containing at the very least a collagen.json or collagen.jsonnet manifest file describing the layout of the resulting SVG. If the manifest specifies any image files (by their path relative to the folder), then those image files must also be present at the expected path in the folder. An input folder satisfying these criteria will be referred to as a skeleton.

If collagen.jsonnet is provided, it is first expanded into JSON, then evaluated as if it were a collagen.json file. While collagen.json is supported, even the least bit of complexity will quickly become overwhelming, in which case you should switch from JSON to Jsonnet. Jsonnet was designed to look like JSON (and, when evaluated, produces JSON) while supporting common programmatic features such as variables, functions, string interpolation, and array concatenation and object merging. These features make building the desired SVG a breeze.

A Basic Example

An example of a simple input-output pair is below. Suppose you have the following simple skeleton at directory example-01:

example-01
├── collagen.jsonnet
└── images
    └── smiley.jpg

Where images/smiley.jpg is the following image (whose native size is 380×380 pixels):

smiley

And where collagen.jsonnet contains the following:

local bubble_text = 'Collagen!!'; (1)
local nose_color = '#f00';
local text_color = '#000';

{
  attrs: { viewBox: '0 0 500 400' },
  children: [
    {
      image_path: 'images/smiley.jpg', (2)
      attrs: { transform: 'translate(0 100) scale(1.3)' },
    },
    {
      tag: 'circle', (3)
      attrs: {
        cx: 123,
        cy: 240,
        r: 15,
        fill: nose_color,
        stroke: '#000',
        'stroke-width': 3,
      },
    },
    {
      tag: 'path',
      attrs: {
        d: 'M 230 140 L 265 120 A 100 40 0 1 0 235 110 Z',
        stroke: '#000',
        'stroke-width': 3,
        fill: '#fff',
      },
    },
    {
      tag: 'text',
      attrs: {
        x: 250,
        y: 97,
        'text-anchor': 'start',
        'dominant-baseline': 'top',
        'font-family': 'Impact',
        'font-size': 30,
        fill: text_color,
      },
      children: [bubble_text], (4)
    },
  ],
}
1 Some variable declarations we’ll make use of later.
2 To include an image, just give its relative path.
3 Most other tags are specified with the tag field, which contains the name of the SVG tag to use.
4 A string like 'Collagen!!' gets turned into a text node: in SVG, <text>Collagen!!</text>.

Then, running the following command:

clgn -i example-01 -o example-01.svg

Will produce the following file, examples-01.svg:

out

If you zoom in, you’ll see the smiley face’s pixels — this is unavoidable, as the original smiley was just a jpeg. But because the nose and speech bubble are SVG elements (i.e. vector graphics, not raster) they look nice and smooth and crisp even when zoomed in. That’s the whole point! Perfectly precise vector graphics can coexist alongside raster graphics.

A More Complicated Example

As we’ve seen, we can include raster images in skeletons; it would be silly if we couldn’t also include other skeletons. Nested skeletons can be included by adding a child of the form {"clgn_path": <path>}. (Whereas a standalone skeleton gets turned into a <svg> tag, a nested skeleton will reside in a <g> tag.) Let’s include the above skeleton in another (and just for fun, let’s add a photo of a kitten (source) too, because why not):

example-02
├── collagen.jsonnet
├── example-01
│   ├── collagen.jsonnet
│   └── images
│       └── smiley.jpg
└── kitty.jpg

Where example-02/collagen.jsonnet is below:

{
  attrs: { viewBox: '0 0 300 250' },
  children: [
    {
      tag: 'rect',
      attrs: {
        x: '10',
        y: '10',
        width: '275',
        height: '225',
        fill: '#ddd',
        stroke: '#00f',
        'stroke-width': '10',
        'stroke-dasharray': '10 10',
      },
    },
    {
      tag: 'g',
      attrs: { transform: 'translate(50 25) scale(.5)' },
      children: [
        {
          clgn_path: './smiley/skeleton',
        },
      ],
    },
    {
      image_path: './kitty.jpg',
      attrs: { transform: 'translate(180 150) scale(.15)' },
    },
  ],
}

Here’s the result when you run clgn -i example-02 -o example-02.svg:

out

So, as far as Collagen is concerned, skeletons act more or less the same as raster images, in the sense that the path is sufficient to include them. The only difference is that the path to a skeleton child is given by the key clgn_path instead of image_path.

Memes

A format that makes it easy to place text on images? Sounds like it would be perfect for memes. But it’s not a meme unless it uses the “Impact” font.

{
  attrs: {
    viewBox: "0 0 800 650",
  },
  children: [
    {
      fonts: [
        {
          name: "Impact",
          path: "./impact.woff2", (1)
        },
      ],
    },
    {
      image_path: "./drake-small.jpg",
      attrs: {
        width: 800,
      },
    },
    {
      local x = 550,
      local dy = 50,
      tag: "text",
      attrs: {
        "font-family": "Impact",
        "font-size": 50,
        color: "black",
        "text-anchor": "middle",
        "vertical-align": "top",
        x: x,
        y: 420,
      },
      children: [
        {
          tag: "tspan",
          attrs: {
            x: x,
            dy: if i == 0 then 0 else dy, (3)
          },
          children: ["Using SVG-based text,", "which is infinitely", "zoomable and has", "no artifacts"][i], (4)
        }
        for i in std.range(0, 3) (2)
      ],
    },
  ],
}
1 We can include a path to the font file to embed it directly in the SVG.
2 We can use list comprehensions…​
3 And use the variable in conditional statements…​
4 And as a list index (0-indexed).

1. Technically base64 encoding data does increase its size by about a third. However, you don’t need to pay this cost when transmitting the file; you can transmit the raw components and then use Collagen to encode them into an SVG on the receiving end. In other words, Collagen is akin to compression such as gunzip: it allows a smaller payload to be transmitted as long as the receiving end can turn it back into something useful.