Closed tkf closed 3 years ago
Currently, Compose is based on 3 type of units:
cx
, cy
w
, h
mm
, inch
The issue that occurs above is because Compose needs a new unit that describes "size relative to context units".
sx
, sy
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"))
))
I'll open a PR sometime soon.
It looks like
ngon
andxgon
are transformed withUnitBox
differently to other shapes likecircle
andrectangle
. Here is a script that tries to put these shapes at the center of sub-contexts:It generates:
As you can see,
circle
,rectangle
,xgon
andngon
in the first (blue) sub-grid are perfectly centered (i.e., at the correct location). However, if I useUnitBox
(to add padding) as in the second (red) sub-grid,xgon
andngon
are not centered whilecircle
andrectangle
are centered.Is the behavior I'm seeing a bug? Or is it expected?
Version of the packages I'm using: