GiovineItalia / Compose.jl

Declarative vector graphics
http://giovineitalia.github.io/Compose.jl/latest/
Other
248 stars 81 forks source link

ngon and xgon are transformed incorrectly in UnitBox? #406

Closed tkf closed 3 years ago

tkf commented 3 years ago

It looks like ngon and xgon are transformed with UnitBox differently to other shapes like circle and rectangle. Here is a script that tries to put these shapes at the center of sub-contexts:

using Compose, Colors
import Cairo

set_default_graphic_size(10cm, 10cm)

function cmpgrid(args...)
    properties = (args..., stroke("gray"), linewidth(0.1mm))
    compose(
        context(),
        (context(), line([[(x, 0), (x, 1)] for x in 0:0.1:1]), properties...),
        (context(), line([[(0, y), (1, y)] for y in 0:0.1:1]), properties...),
    )
end

objects = compose(
    context(),
    (context(), circle(0.5, 0.5, 0.1), stroke("orange"), fill("transparent")),
    (context(), xgon(0.5, 0.5, 0.3, 4), stroke("blue"), fill("transparent")),
    (context(), ngon(0.5, 0.5, 0.3, 4), stroke("green"), fill("transparent")),
    (context(), rectangle(0.4, 0.4, 0.2, 0.2), stroke("purple"), fill("transparent")),
)
img = compose(
    context(),
    cmpgrid(),
    (
        # (1)
        context(0.1, 0.1, 0.4, 0.4),
        cmpgrid(stroke("blue")),
        objects,
    ),
    (
        # (2)
        context(0.5, 0.5, 0.4, 0.4; units = UnitBox(-0.1, -0.1, 1.2, 1.2)),
        cmpgrid(stroke("red")),
        objects,
    ),
    text(0.1, 0.1, "(1)"),
    text(0.9, 0.9, "(2)"),
)

It generates:

output

As you can see, circle, rectangle, xgon and ngon in the first (blue) sub-grid are perfectly centered (i.e., at the correct location). However, if I use UnitBox (to add padding) as in the second (red) sub-grid, xgon and ngon are not centered while circle and rectangle are centered.

Is the behavior I'm seeing a bug? Or is it expected?

Version of the packages I'm using:

  [159f3aea] Cairo v1.0.5
  [5ae59095] Colors v0.12.4
  [a81c6b42] Compose v0.9.1
Mattriks commented 3 years ago

Currently, Compose is based on 3 type of units:

  1. Context (position) units: cx, cy
  2. Relative units: w, h
  3. Absolute units: mm, inch

The issue that occurs above is because Compose needs a new unit that describes "size relative to context units".

  1. Size wrt Context: sx, sy

Design example:

module t

using Compose

import Compose: Length, AbsoluteBox, Transform, width, resolve
import Base.(*)

size_x_measure(a::Measure) = a
size_x_measure(a::T) where T = Length{:sx, T}(a)

cmpgrid2(args...) = (context(), 
    line([[(x, 0), (x, 1)] for x in 0:0.1:1]), line([[(0, y), (1, y)] for y in 0:0.1:1]), args..., linewidth(0.1mm))

resolve(box::AbsoluteBox, units::UnitBox, t::Transform, a::Length{:sx}) = 
    a.value / width(units) * box.a[1]

end

Here I use a different red grid to better illustrate the current issues (green polygon). The new (blue) polygon is correct:

s = t.size_x_measure(0.3)
newpoly = polygon([(0.5cx, 0.5cy-s), (0.5cx+s, 0.5cy), (0.5cx, 0.5cy+s), (0.5cx-s, 0.5cy), (0.5cx, 0.5cy-s)])

set_default_graphic_size(10cm, 10cm)

img = compose(context(), t.cmpgrid2(stroke("gray")), fill("transparent"),
    (context(0.5, 0.5, 0.4, 0.4; units = UnitBox(-0.1, 0., 1.1, 1.)), t.cmpgrid2(stroke("red")),
        (context(), ngon(0.5, 0.5, 0.3, 4), stroke("green")),
        (context(), newpoly, stroke("blue"))
))

size_x_units

I'll open a PR sometime soon.