Pikchr

repository·master·Indexed 41 days ago

https://github.com/drhsqlite/pikchr

A PIC-inspired markup language for generating diagrams in technical documentation, designed for embedding within Markdown fenced code blocks. It provides a text-based way to render SVG diagrams with support for box and circle objects, boundary points, and a C-API for integration into larger applications.

Tokens
28K
Snippets
116
Records
161
Agent score
100%

What's inside Pikchr

  1. What is Pikchr?

    master
    Pikchr is a PIC-like markup language specifically designed for creating diagrams within technical documentation. It is intended to be embedded directly into fenced code blocks of Markdown (or other markup languages) to provide a convenient, text-based way to render diagrams.
  2. Explore Pikchr integrations and ecosystem

    master

    Pikchr is supported across various platforms and tools:

    • Web/Online: Use the online editor to enter Pikchr text and see results immediately.
    • Documentation Tools:
      • mdbook-pikchr: A pikchr-aware preprocessor for mdBook.
      • pandoc: A Lua-based pikchr filter is available.
      • kroki.io: Integrated into the Kroki diagram-rendering service.
    • IDEs and Editors:
    • Runtimes:
    • CLI: pikchr-cmd is a command-line preprocessor tool for use with any Markdown formatter.
  3. Understand the purpose and scope of Pikchr

    master

    Pikchr is a specialized tool for generating diagrams for technical documentation (such as Markdown). It uses a text-based, enduring language inspired by the [PIC] language from the 1980s, making it easy for humans to read and maintain in generic text editors.

    Key Characteristics

    • Cross-platform: Implemented in generic C code using only the standard C library.
    • Simple Syntax: Designed to be well-defined and easy to learn.
    • Enduring: The source text is intended to be readable and editable for the long term.

    Use Cases and Limitations

    • Use for: Technical diagrams that convey mathematical or logical truth in a dry, clear style.
    • Do NOT use for:
      • Marketing graphics (it is not designed for emotional or aesthetic appeal).
      • Charts and graphs (it lacks specific extensions for this purpose).
      • CAD/CAM images (it lacks pixel-perfect precision).
      • Point-and-click diagramming (it is a text-based system, similar to how Markdown compares to MS-Word).
  4. What is a place in Pikchr

    master

    A place is a specific, named point on an object. While a position refers to any arbitrary X,Y coordinate, a place is a predefined anchor point on an existing object (like a box, ellipse, or line).

    You can refer to a place using several syntaxes:

    • object (refers to the center of the object)
    • object.dot-edgename (e.g., B.n for the north point of object B)
    • edgename of object (e.g., north of B)
    • ORDINAL vertex of object (e.g., 1st vertex of B, used for line objects)

    Most places lie on the object's boundary, but special places like .center or .c are located in the middle. For line objects, .start and .end may reside in the interior.

  5. Position text relative to specific line segments

    master

    By default, text annotations are anchored to the center of an object's bounding box. For multi-segment lines (like complex arrows), this center might be far from the actual path.

    To position text beside a specific segment of a multi-segment line, create an invisible line (line invis) that overlays that specific segment and attach the text to that invisible line instead.

    # Overlaying an invisible line to anchor text to a specific segment
    line invis from 2nd vertex of last arrow to 3rd vertex of last arrow \
        "text" below aligned
  6. Understand the four core numeric properties in Pikchr

    master

    Pikchr uses four primary numeric properties to control the size and appearance of objects. Most other property names are simply aliases for these four:

    • width (alias: wid): The horizontal size of most objects.
    • height (alias: ht): The vertical size of most objects.
    • radius (alias: rad): Used to set the size of circles, cylinders, files, and corner rounding.
    • thickness: The width of lines used to draw objects.

    Note: diameter is an alias for twice the radius.

  7. Understand automatic layout and anchor points

    master

    If you omit the at attribute, Pikchr automatically positions the object using the rule: with .start at previous.end. (The first object in a script is positioned with .c at (0,0)).

    Every object has 9 standard anchor points:

    • Compass directions: .n, .ne, .e, .se, .s, .sw, .w, .nw
    • Center: .c

    Additionally, every object has .start and .end anchor points. The physical location of .start and .end depends on the current layout direction:

    Layout Direction.start.end
    right.w.e
    down.n.s
    left.e.w
    up.s.n
  8. How nested macros work in pikchr

    master

    Pikchr supports nested macros up to a maximum depth (currently 10).

    When passing arguments to a nested macro, you must follow one of two rules:

    1. Pass arbitrary text as arguments.
    2. Pass a single $N parameter.

    You cannot mix arbitrary text and a $N parameter in the same argument list for a nested macro call.

  9. Use the `previous` qualifier for relative positioning

    master

    The previous qualifier allows you to refer to the most recently created object of the same type. This is useful for aligning objects or calculating dimensions relative to the last element added.

    Note: If you insert a new object of a different type between two objects, previous still refers to the last object of the same type. If you insert a new object of the same type, previous will point to the new one. To avoid broken references when inserting objects, assign an explicit name to your objects and use that name instead of previous.

    Example: box same width previous.e.x - C2.w.x uses the eastern edge (.e.x) of the previous box to calculate the new width.

  10. How .start and .end places behave with layout direction

    master

    The specific locations of .start and .end depend on the object's layout direction. For block objects, the mapping is as follows:

    Layout Direction.start.end
    right.w.e
    down.n.s
    left.e.w
    up.s.n

    For line objects:

    • .start always refers to the first vertex of the line.
    • .end is usually the last vertex. However, if the close keyword is used, .end behaves like a block object's end point (e.g., .e in 'right' direction) rather than the final vertex.
  11. Use the 'behind' attribute to control drawing order

    master

    By default, Pikchr draws objects in the order they appear in the input script. The behind object attribute allows you to override this behavior, causing the object currently being constructed to be drawn before the referenced object.

    This is particularly useful for creating background effects. For example, if you define your primary content boxes first and then define large background-colored boxes later in the script, you can use behind [content_box] to ensure the background box is rendered underneath the content rather than covering it up.

    # Example pattern:
    box "Content" wid 100 ht 100
    
    # This box will be drawn first because of the 'behind' attribute,
    # appearing underneath the "Content" box.
    box wid 150 ht 150 fill 0xcccccc behind "Content"
  12. Use different units for numeric literals

    master

    Unlike legacy PIC which uses only inches, Pikchr allows you to attach unit designators to numeric literals. This makes expressing distances more intuitive. Note that units must be attached directly to the number without whitespace (e.g., 2.3cm, not 2.3 cm). Internally, Pikchr still performs all calculations in inches.

    Supported units:

    • cm: centimeters
    • in: inches (default)
    • mm: millimeters
    • pc: picas
    • pt: points
    • px: pixels
    2.3cm