/
Compose.jl
270 lines (224 loc) · 8.99 KB
/
Compose.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
module Compose
using Colors
using IterTools
using DataStructures
using Measures
using Requires
using Dates
using Printf
using Base.Iterators
import JSON
import Base: length, isempty, getindex, setindex!,
display, show, convert, zero, isless, max, fill, size, copy,
min, max, abs, +, -, *, /, ==
import Measures: resolve, w, h
export compose, compose!, Context, UnitBox, AbsoluteBoundingBox,
Rotation, Mirror, Shear,
ParentDrawContext, context, ctxpromise, table, set_units!, minwidth, minheight,
text_extents, max_text_extents, polygon, ngon, star, xgon,
line, rectangle, circle, arc, sector, ellipse, text, curve, bitmap,
stroke, fill, strokedash, strokelinecap, arrow,
strokelinejoin, linewidth, visible, fillopacity, strokeopacity, clip,
font, fontsize, svgid, svgclass, svgattribute, jsinclude, jscall, Measure,
inch, mm, cm, pt, px, cx, cy, w, h, hleft, hcenter, hright, vtop, vcenter,
vbottom, SVG, SVGJS, PGF, PNG, PS, PDF, draw, pad, pad_inner, pad_outer,
hstack, vstack, gridstack, LineCapButt, LineCapSquare, LineCapRound,
CAIROSURFACE, introspect, set_default_graphic_size, set_default_jsmode,
boundingbox, Patchable
abstract type Backend end
"""
Some backends can more efficiently draw forms by batching. If so, they
shuld define a similar method that returns true.
"""
canbatch(::Backend) = false
# Allow users to supply strings without deprecation warnings
parse_colorant(c::Colorant) = c
parse_colorant(str::AbstractString) = parse(Colorant, str)
parse_colorant(c::Union{Tuple,Vector}) = [parse_colorant(x) for x in c]
parse_colorant(c...) = parse_colorant(c)
@deprecate parse_colorant_vec(c...) parse_colorant(c)
include("misc.jl")
include("measure.jl")
include("list.jl")
# Every graphic in Compose consists of a tree.
abstract type ComposeNode end
# Used to mark null child pointers
struct NullNode <: ComposeNode end
nullnode = NullNode()
include("form.jl")
include("property.jl")
include("container.jl")
include("batch.jl")
include("table.jl")
include("stack.jl")
# How large to draw graphics when not explicitly drawing to a backend
default_graphic_width = sqrt(2)*10*cm
default_graphic_height = 10cm
function set_default_graphic_size(width::MeasureOrNumber,
height::MeasureOrNumber)
global default_graphic_width
global default_graphic_height
default_graphic_width = x_measure(width)
default_graphic_height = y_measure(height)
nothing
end
default_graphic_format = :html
function set_default_graphic_format(fmt::Symbol)
fmt in [:html, :png, :svg, :pdf, :ps, :pgf] || error("$(fmt) is not a supported plot format")
global default_graphic_format
default_graphic_format = fmt
nothing
end
# Default means to include javascript dependencies in the SVGJS backend.
default_jsmode = :embed
function set_default_jsmode(mode::Symbol)
global default_jsmode
if mode in [:none, :exclude, :embed, :linkabs, :linkrel]
default_jsmode = mode
else
error("$(mode) is not a valid jsmode")
end
nothing
end
function default_mime()
if default_graphic_format == :png
"image/png"
elseif default_graphic_format == :svg
"image/svg+xml"
elseif default_graphic_format == :html
"text/html"
elseif default_graphic_format == :ps
"application/postscript"
elseif default_graphic_format == :pdf
"application/pdf"
elseif default_graphic_format == :pgf
"application/x-tex"
else
""
end
end
# Default property values
default_font_family = "Helvetica Neue,Helvetica,Arial,sans"
default_font_size = 11pt
default_line_width = 0.3mm
default_stroke_color = nothing
default_fill_color = colorant"black"
# If Cairo is not available, throw an error when trying to save with a Cairo backend
missing_cairo_error(backend::String, invocation::String=backend) =
"""
The Cairo and Fontconfig packages are necessary for saving as $backend.
Add them with the package manager if necessary, then run `import Cairo,
Fontconfig` before invoking `$invocation`.
"""
missing_cairo_error(m::MIME) =
missing_cairo_error(string(m), "show(::IO, ::MIME\"$m\", ::Context)")
for backend in [:PNG, :PS, :PDF]
docstr = missing_cairo_error(string(backend))
@eval @doc $docstr $backend(args...; kwargs...) = error(missing_cairo_error(string($backend)))
end
CairoMIME = Union{MIME"image/png", MIME"application/ps", MIME"application/pdf"}
show(io::IO, m::CairoMIME, ctx::Context) = error(missing_cairo_error(m))
include("svg.jl")
include("pgf_backend.jl")
# If available, pango and fontconfig are used to compute text extents and match
# fonts. Otherwise a simplistic pure-julia fallback is used.
include("fontfallback.jl")
const pango_cairo_ctx = Ref{Ptr}(C_NULL)
const pango_cairo_fm = Ref{Ptr}(C_NULL)
const pangolayout = Ref{Any}(nothing)
function link_fontconfig()
@debug "Loading Fontconfig backend into Compose.jl"
pango_cairo_ctx[] = C_NULL
ccall((:g_type_init, libgobject), Cvoid, ())
pango_cairo_fm[] = ccall((:pango_cairo_font_map_new, libpangocairo),
Ptr{Cvoid}, ())
pango_cairo_ctx[] = ccall((:pango_font_map_create_context, libpango),
Ptr{Cvoid}, (Ptr{Cvoid},), pango_cairo_fm[])
pangolayout[] = PangoLayout()
end
function link_cairo()
@debug "Loading Cairo backend into Compose.jl"
include(joinpath(@__DIR__,"cairo_backends.jl"))
include(joinpath(@__DIR__,"immerse_backend.jl"))
end
function __init__()
@require Cairo="159f3aea-2a34-519c-b102-8c37f9878175" link_cairo()
@require Fontconfig="186bb1d3-e1f7-5a2c-a377-96d770f13627" begin
include("pango.jl")
link_fontconfig()
end
end
show(io::IO, m::MIME"text/html", ctx::Context) =
draw(SVGJS(io, default_graphic_width, default_graphic_height, false,
jsmode=default_jsmode), ctx)
show(io::IO, m::MIME"image/svg+xml", ctx::Context) =
draw(SVG(io, default_graphic_width, default_graphic_height, false), ctx)
function pad_outer(c::Context,
left_padding::MeasureOrNumber,
right_padding::MeasureOrNumber,
top_padding::MeasureOrNumber,
bottom_padding::MeasureOrNumber)
left_padding = size_measure(left_padding)
right_padding = size_measure(right_padding)
top_padding = size_measure(top_padding)
bottom_padding = size_measure(bottom_padding)
root = context(c.box.x0[1], c.box.x0[1],
c.box.a[1] + left_padding + right_padding,
c.box.a[2] + top_padding + bottom_padding,
minwidth=c.minwidth,
minheight=c.minheight)
c = copy(c)
c.box = BoundingBox(left_padding, top_padding,
1w - left_padding - right_padding,
1h - top_padding - bottom_padding)
return compose!(root, c)
end
pad_outer(c::Context, padding::MeasureOrNumber) =
pad_outer(c, padding, padding, padding, padding)
pad_outer(cs::Vector{Context},
left_padding::MeasureOrNumber,
right_padding::MeasureOrNumber,
top_padding::MeasureOrNumber,
bottom_padding::MeasureOrNumber) =
map(c -> pad_outer(c, left_padding, right_padding, top_padding, bottom_padding), cs)
pad_outer(cs::Vector{Context}, padding::MeasureOrNumber) =
pad_outer(cs, padding, padding, padding, padding)
function pad_inner(c::Context,
left_padding::MeasureOrNumber,
right_padding::MeasureOrNumber,
top_padding::MeasureOrNumber,
bottom_padding::MeasureOrNumber)
left_padding = size_measure(left_padding)
right_padding = size_measure(right_padding)
top_padding = size_measure(top_padding)
bottom_padding = size_measure(bottom_padding)
root = context(c.box.x0[1], c.box.x0[2],
c.box.a[1], c.box.a[2],
minwidth=c.minwidth,
minheight=c.minheight)
c = copy(c)
c.box = BoundingBox(left_padding, top_padding,
1w - left_padding - right_padding,
1h - top_padding - bottom_padding)
return compose!(root, c)
end
pad_inner(c::Context, padding::MeasureOrNumber) =
pad_inner(c, padding, padding, padding, padding)
pad_inner(cs::Vector{Context},
left_padding::MeasureOrNumber,
right_padding::MeasureOrNumber,
top_padding::MeasureOrNumber,
bottom_padding::MeasureOrNumber) =
map(c -> pad_inner(c, left_padding, right_padding, top_padding, bottom_padding), cs)
pad_inner(cs::Vector{Context}, padding::MeasureOrNumber) =
pad_inner(cs, padding, padding, padding, padding)
function gridstack(cs::Matrix{Context})
m, n = size(cs)
t = Table(m, n, 1:m, 1:n, x_prop=ones(n), y_prop=ones(m))
for i in 1:m, j in 1:n
t[i, j] = [cs[i, j]]
end
return compose!(context(), t)
end
const pad = pad_outer
end # module Compose