{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "oneOf": [
    {
      "$ref": "#/$defs/PdfTemplate"
    }
  ],
  "description": "Templo template, schemaVersion 1. Ids must be unique, a template holds at most 1000 elements and 10000 characters per text and language, languages are unique, the default language is available and every translated text has the default language and only available ones; these rules are checked by parseTemplate, not by this JSON Schema.",
  "$defs": {
    "PdfTemplate": {
      "type": "object",
      "properties": {
        "schemaVersion": {
          "type": "number",
          "const": 1,
          "description": "Version of the format; parseTemplate migrates older versions and refuses newer ones"
        },
        "kind": {
          "type": "string",
          "const": "pdf",
          "description": "What the template produces"
        },
        "locales": {
          "$ref": "#/$defs/Locales"
        },
        "page": {
          "$ref": "#/$defs/PageSetup"
        },
        "pages": {
          "minItems": 1,
          "maxItems": 50,
          "type": "array",
          "items": {
            "$ref": "#/$defs/Page"
          },
          "description": "The pages, in order"
        }
      },
      "required": [
        "schemaVersion",
        "kind",
        "locales",
        "page",
        "pages"
      ],
      "additionalProperties": false,
      "description": "A PDF template"
    },
    "Locales": {
      "type": "object",
      "properties": {
        "default": {
          "type": "string",
          "pattern": "^[a-z]{2,3}(-([A-Z]{2}|[0-9]{3}))?$",
          "description": "Used where a text has no translation; one of available"
        },
        "available": {
          "minItems": 1,
          "maxItems": 20,
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[a-z]{2,3}(-([A-Z]{2}|[0-9]{3}))?$",
            "description": "Language tag (BCP 47): a language with an optional region, e.g. de, de-AT, en-GB"
          },
          "description": "The languages of the template, without duplicates"
        }
      },
      "required": [
        "default",
        "available"
      ],
      "additionalProperties": false,
      "description": "Languages of the template"
    },
    "PageSetup": {
      "type": "object",
      "properties": {
        "size": {
          "$ref": "#/$defs/PageSize"
        },
        "orientation": {
          "type": "string",
          "enum": [
            "portrait",
            "landscape"
          ],
          "description": "Portrait puts the shorter side horizontally, landscape the longer one"
        },
        "margins": {
          "$ref": "#/$defs/Margins"
        }
      },
      "required": [
        "size",
        "orientation",
        "margins"
      ],
      "additionalProperties": false,
      "description": "Page setup shared by all pages"
    },
    "PageSize": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "format": {
              "type": "string",
              "enum": [
                "A3",
                "A4",
                "A5",
                "Letter",
                "Legal"
              ],
              "description": "A standard format (portrait width × height: A3 297 × 420 mm, A4 210 × 297 mm, A5 148 × 210 mm, Letter 215.9 × 279.4 mm, Legal 215.9 × 355.6 mm)"
            }
          },
          "required": [
            "format"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "format": {
              "type": "string",
              "const": "custom"
            },
            "width": {
              "type": "number",
              "minimum": 1,
              "maximum": 10000,
              "description": "Millimetres"
            },
            "height": {
              "type": "number",
              "minimum": 1,
              "maximum": 10000,
              "description": "Millimetres"
            }
          },
          "required": [
            "format",
            "width",
            "height"
          ],
          "additionalProperties": false
        }
      ],
      "description": "A standard format or a custom size; the orientation decides which side is the width"
    },
    "Margins": {
      "type": "object",
      "properties": {
        "top": {
          "type": "number",
          "minimum": 0,
          "maximum": 10000,
          "description": "Millimetres, not negative"
        },
        "right": {
          "type": "number",
          "minimum": 0,
          "maximum": 10000,
          "description": "Millimetres, not negative"
        },
        "bottom": {
          "type": "number",
          "minimum": 0,
          "maximum": 10000,
          "description": "Millimetres, not negative"
        },
        "left": {
          "type": "number",
          "minimum": 0,
          "maximum": 10000,
          "description": "Millimetres, not negative"
        }
      },
      "required": [
        "top",
        "right",
        "bottom",
        "left"
      ],
      "additionalProperties": false,
      "description": "Margins in millimetres; guides in the editor, the output does not clip to them"
    },
    "Page": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "maxLength": 64,
          "pattern": "^[A-Za-z0-9_-]+$",
          "description": "Unique within the template (pages and elements)"
        },
        "background": {
          "anyOf": [
            {
              "$ref": "#/$defs/Paint"
            },
            {
              "type": "null"
            }
          ],
          "description": "Fills the whole page; null = none"
        },
        "elements": {
          "type": "array",
          "items": {
            "oneOf": [
              {
                "$ref": "#/$defs/TextElement"
              },
              {
                "$ref": "#/$defs/RectangleElement"
              },
              {
                "$ref": "#/$defs/EllipseElement"
              },
              {
                "$ref": "#/$defs/LineElement"
              }
            ]
          },
          "description": "Stacking order: the first element is at the bottom, the last on top"
        }
      },
      "required": [
        "id",
        "background",
        "elements"
      ],
      "additionalProperties": false,
      "description": "One page and its elements"
    },
    "Paint": {
      "type": "object",
      "properties": {
        "color": {
          "type": "string",
          "pattern": "^#[0-9A-F]{6}$",
          "description": "#RRGGBB, upper case"
        },
        "opacity": {
          "type": "number",
          "minimum": 0,
          "maximum": 100,
          "description": "Opacity in %, 0–100"
        }
      },
      "required": [
        "color",
        "opacity"
      ],
      "additionalProperties": false,
      "description": "A colour with opacity"
    },
    "TextElement": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "const": "text"
        },
        "id": {
          "type": "string",
          "maxLength": 64,
          "pattern": "^[A-Za-z0-9_-]+$",
          "description": "Unique within the template (pages and elements)"
        },
        "name": {
          "type": "string",
          "maxLength": 200,
          "description": "Shown in the content list"
        },
        "hidden": {
          "type": "boolean",
          "description": "Not drawn; still listed in the editor"
        },
        "locked": {
          "type": "boolean",
          "description": "Locked in the editor; no effect on the output"
        },
        "x": {
          "type": "number",
          "minimum": -10000,
          "maximum": 10000,
          "description": "Left edge of the box before rotation, mm from the left edge of the page"
        },
        "y": {
          "type": "number",
          "minimum": -10000,
          "maximum": 10000,
          "description": "Top edge of the box before rotation, mm from the top edge of the page"
        },
        "width": {
          "type": "number",
          "minimum": 0,
          "maximum": 10000,
          "description": "Millimetres, not negative"
        },
        "height": {
          "type": "number",
          "minimum": 0,
          "maximum": 10000,
          "description": "Millimetres, not negative"
        },
        "rotation": {
          "type": "number",
          "minimum": -360,
          "maximum": 360,
          "description": "Degrees, clockwise, around the centre"
        },
        "fill": {
          "anyOf": [
            {
              "$ref": "#/$defs/Paint"
            },
            {
              "type": "null"
            }
          ],
          "description": "Fill of the box; null = none"
        },
        "stroke": {
          "anyOf": [
            {
              "$ref": "#/$defs/Stroke"
            },
            {
              "type": "null"
            }
          ],
          "description": "Outline of the box; null = none"
        },
        "content": {
          "anyOf": [
            {
              "minItems": 1,
              "maxItems": 1000,
              "type": "array",
              "items": {
                "$ref": "#/$defs/Paragraph"
              },
              "description": "The same paragraphs in every language"
            },
            {
              "$ref": "#/$defs/I18nText"
            }
          ],
          "description": "Paragraphs for every language, or paragraphs per language"
        },
        "fontFamily": {
          "type": "string",
          "minLength": 1,
          "maxLength": 100,
          "description": "Font family name; an unknown one falls back to the default font"
        },
        "fontWeight": {
          "type": "integer",
          "minimum": 100,
          "maximum": 900,
          "multipleOf": 100,
          "description": "100 (thin) to 900 (black) in steps of 100; 400 is regular, 700 bold"
        },
        "italic": {
          "type": "boolean",
          "description": "Italic style"
        },
        "fontSize": {
          "type": "number",
          "minimum": 1,
          "maximum": 1000,
          "description": "Font size in points"
        },
        "lineHeight": {
          "type": "number",
          "minimum": 0.5,
          "maximum": 5,
          "description": "Line height as a multiple of the font size"
        },
        "letterSpacing": {
          "type": "number",
          "minimum": -50,
          "maximum": 200,
          "description": "Letter spacing in % of the font size"
        },
        "color": {
          "type": "string",
          "pattern": "^#[0-9A-F]{6}$",
          "description": "Text colour, #RRGGBB, upper case"
        },
        "opacity": {
          "type": "number",
          "minimum": 0,
          "maximum": 100,
          "description": "Opacity in %, 0–100"
        },
        "align": {
          "type": "string",
          "enum": [
            "left",
            "center",
            "right",
            "justify"
          ],
          "description": "Horizontal alignment; justify leaves the last line of a paragraph left-aligned"
        }
      },
      "required": [
        "type",
        "id",
        "name",
        "hidden",
        "locked",
        "x",
        "y",
        "width",
        "height",
        "rotation",
        "fill",
        "stroke",
        "content",
        "fontFamily",
        "fontWeight",
        "italic",
        "fontSize",
        "lineHeight",
        "letterSpacing",
        "color",
        "opacity",
        "align"
      ],
      "additionalProperties": false,
      "description": "A text box: paragraphs of static text and placeholders, one style. Text that does not fit runs past the bottom of the box."
    },
    "Stroke": {
      "type": "object",
      "properties": {
        "color": {
          "type": "string",
          "pattern": "^#[0-9A-F]{6}$",
          "description": "#RRGGBB, upper case"
        },
        "opacity": {
          "type": "number",
          "minimum": 0,
          "maximum": 100,
          "description": "Opacity in %, 0–100"
        },
        "width": {
          "type": "number",
          "exclusiveMinimum": 0,
          "maximum": 100,
          "description": "Stroke width in points, above 0 (no stroke is null)"
        },
        "style": {
          "type": "string",
          "enum": [
            "solid",
            "dashed",
            "dotted"
          ],
          "description": "Dashes and dots scale with the stroke width"
        }
      },
      "required": [
        "color",
        "opacity",
        "width",
        "style"
      ],
      "additionalProperties": false,
      "description": "An outline or line stroke"
    },
    "Paragraph": {
      "type": "object",
      "properties": {
        "pieces": {
          "maxItems": 200,
          "type": "array",
          "items": {
            "oneOf": [
              {
                "$ref": "#/$defs/TextPiece"
              },
              {
                "$ref": "#/$defs/FieldPiece"
              }
            ]
          },
          "description": "Static text and placeholders, joined without spaces; empty = an empty line"
        }
      },
      "required": [
        "pieces"
      ],
      "additionalProperties": false,
      "description": "One paragraph; a line break in a text starts a new one"
    },
    "TextPiece": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "const": "text"
        },
        "text": {
          "type": "string",
          "pattern": "^[^\\r\\n]*$",
          "description": "Without line breaks; a new line is a new paragraph"
        }
      },
      "required": [
        "type",
        "text"
      ],
      "additionalProperties": false,
      "description": "Static text, printed as is (\"{{\" in it is just text)"
    },
    "FieldPiece": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "const": "field"
        },
        "path": {
          "type": "string",
          "maxLength": 200,
          "pattern": "^[A-Za-z0-9_]+(\\.[A-Za-z0-9_]+)*$",
          "description": "Data path, e.g. customer.name or items.0.price"
        }
      },
      "required": [
        "type",
        "path"
      ],
      "additionalProperties": false,
      "description": "A placeholder, filled from the data at the given path"
    },
    "I18nText": {
      "type": "object",
      "properties": {
        "i18n": {
          "type": "object",
          "propertyNames": {
            "type": "string",
            "pattern": "^[a-z]{2,3}(-([A-Z]{2}|[0-9]{3}))?$",
            "description": "Language tag (BCP 47): a language with an optional region, e.g. de, de-AT, en-GB"
          },
          "additionalProperties": {
            "minItems": 1,
            "maxItems": 1000,
            "type": "array",
            "items": {
              "$ref": "#/$defs/Paragraph"
            }
          },
          "description": "Paragraphs per language; the default language is required"
        }
      },
      "required": [
        "i18n"
      ],
      "additionalProperties": false,
      "description": "Text per language, each with its own placeholders and word order"
    },
    "RectangleElement": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "const": "rectangle"
        },
        "id": {
          "type": "string",
          "maxLength": 64,
          "pattern": "^[A-Za-z0-9_-]+$",
          "description": "Unique within the template (pages and elements)"
        },
        "name": {
          "type": "string",
          "maxLength": 200,
          "description": "Shown in the content list"
        },
        "hidden": {
          "type": "boolean",
          "description": "Not drawn; still listed in the editor"
        },
        "locked": {
          "type": "boolean",
          "description": "Locked in the editor; no effect on the output"
        },
        "x": {
          "type": "number",
          "minimum": -10000,
          "maximum": 10000,
          "description": "Left edge of the box before rotation, mm from the left edge of the page"
        },
        "y": {
          "type": "number",
          "minimum": -10000,
          "maximum": 10000,
          "description": "Top edge of the box before rotation, mm from the top edge of the page"
        },
        "width": {
          "type": "number",
          "minimum": 0,
          "maximum": 10000,
          "description": "Millimetres, not negative"
        },
        "height": {
          "type": "number",
          "minimum": 0,
          "maximum": 10000,
          "description": "Millimetres, not negative"
        },
        "rotation": {
          "type": "number",
          "minimum": -360,
          "maximum": 360,
          "description": "Degrees, clockwise, around the centre"
        },
        "fill": {
          "anyOf": [
            {
              "$ref": "#/$defs/Paint"
            },
            {
              "type": "null"
            }
          ],
          "description": "Fill of the box; null = none"
        },
        "stroke": {
          "anyOf": [
            {
              "$ref": "#/$defs/Stroke"
            },
            {
              "type": "null"
            }
          ],
          "description": "Outline of the box; null = none"
        },
        "cornerRadius": {
          "type": "number",
          "minimum": 0,
          "maximum": 5000,
          "description": "Millimetres"
        }
      },
      "required": [
        "type",
        "id",
        "name",
        "hidden",
        "locked",
        "x",
        "y",
        "width",
        "height",
        "rotation",
        "fill",
        "stroke",
        "cornerRadius"
      ],
      "additionalProperties": false,
      "description": "A rectangle"
    },
    "EllipseElement": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "const": "ellipse"
        },
        "id": {
          "type": "string",
          "maxLength": 64,
          "pattern": "^[A-Za-z0-9_-]+$",
          "description": "Unique within the template (pages and elements)"
        },
        "name": {
          "type": "string",
          "maxLength": 200,
          "description": "Shown in the content list"
        },
        "hidden": {
          "type": "boolean",
          "description": "Not drawn; still listed in the editor"
        },
        "locked": {
          "type": "boolean",
          "description": "Locked in the editor; no effect on the output"
        },
        "x": {
          "type": "number",
          "minimum": -10000,
          "maximum": 10000,
          "description": "Left edge of the box before rotation, mm from the left edge of the page"
        },
        "y": {
          "type": "number",
          "minimum": -10000,
          "maximum": 10000,
          "description": "Top edge of the box before rotation, mm from the top edge of the page"
        },
        "width": {
          "type": "number",
          "minimum": 0,
          "maximum": 10000,
          "description": "Millimetres, not negative"
        },
        "height": {
          "type": "number",
          "minimum": 0,
          "maximum": 10000,
          "description": "Millimetres, not negative"
        },
        "rotation": {
          "type": "number",
          "minimum": -360,
          "maximum": 360,
          "description": "Degrees, clockwise, around the centre"
        },
        "fill": {
          "anyOf": [
            {
              "$ref": "#/$defs/Paint"
            },
            {
              "type": "null"
            }
          ],
          "description": "Fill of the box; null = none"
        },
        "stroke": {
          "anyOf": [
            {
              "$ref": "#/$defs/Stroke"
            },
            {
              "type": "null"
            }
          ],
          "description": "Outline of the box; null = none"
        }
      },
      "required": [
        "type",
        "id",
        "name",
        "hidden",
        "locked",
        "x",
        "y",
        "width",
        "height",
        "rotation",
        "fill",
        "stroke"
      ],
      "additionalProperties": false,
      "description": "An ellipse filling its box"
    },
    "LineElement": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "const": "line"
        },
        "id": {
          "type": "string",
          "maxLength": 64,
          "pattern": "^[A-Za-z0-9_-]+$",
          "description": "Unique within the template (pages and elements)"
        },
        "name": {
          "type": "string",
          "maxLength": 200,
          "description": "Shown in the content list"
        },
        "hidden": {
          "type": "boolean",
          "description": "Not drawn; still listed in the editor"
        },
        "locked": {
          "type": "boolean",
          "description": "Locked in the editor; no effect on the output"
        },
        "x1": {
          "type": "number",
          "minimum": -10000,
          "maximum": 10000,
          "description": "Start point, mm from the left edge of the page"
        },
        "y1": {
          "type": "number",
          "minimum": -10000,
          "maximum": 10000,
          "description": "Start point, mm from the top edge of the page"
        },
        "x2": {
          "type": "number",
          "minimum": -10000,
          "maximum": 10000,
          "description": "End point, mm from the left edge of the page"
        },
        "y2": {
          "type": "number",
          "minimum": -10000,
          "maximum": 10000,
          "description": "End point, mm from the top edge of the page"
        },
        "stroke": {
          "description": "The line itself",
          "$ref": "#/$defs/Stroke"
        },
        "cap": {
          "type": "string",
          "enum": [
            "butt",
            "round",
            "square"
          ],
          "description": "Shape of the line ends: butt stops at the end point, round and square reach past it"
        }
      },
      "required": [
        "type",
        "id",
        "name",
        "hidden",
        "locked",
        "x1",
        "y1",
        "x2",
        "y2",
        "stroke",
        "cap"
      ],
      "additionalProperties": false,
      "description": "A straight line from (x1, y1) to (x2, y2)"
    }
  },
  "title": "Templo template"
}
