Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new mathjax support, requires JS #418

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Compose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export compose, compose!, Context, UnitBox, AbsoluteBoundingBox,
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
boundingbox, Patchable,
rawsvg, mathjax

abstract type Backend end

Expand Down
82 changes: 82 additions & 0 deletions src/form.jl
Original file line number Diff line number Diff line change
Expand Up @@ -870,5 +870,87 @@ end

form_string(::Bezigon) = "BP"

# RawSVGPrimitive form
struct RawSVGPrimitive{VT<:AbstractString} <: FormPrimitive
value::VT
end


const RawSVG{P<:RawSVGPrimitive} = Form{P}

"""
rawsvg(str::AbstractString)

Print a raw string into the SVG image.
"""
function rawsvg(str::AbstractString)
Form([RawSVGPrimitive(str)])
end

form_string(::RawSVG) = "RAW"

function resolve(box::AbsoluteBox, units::UnitBox, t::Transform, p::RawSVGPrimitive{VT}) where {VT}
return p
end

# MathJaxPrimitive form
struct MathJaxPrimitive{P<:Vec, S<:Vec, R<:Rotation, O<:Vec} <: FormPrimitive
position::P
size::S
value::AbstractString

rot::R

offset::O
end

const MathJax{P<:MathJaxPrimitive} = Form{P}
form_string(::MathJax) = "MATH"

"""
mathjax(x, y, width, heigh, value [,rot::Rotation])

Draw the text `value` at the position (`x`,`y`) relative to the current context.
Parameters `width` and `height` specify the bounding box size.
"""
function mathjax(x, y, width, height, value::AbstractString,
rot=Rotation(), offset::Vec2=(0mm,0mm);
tag::Symbol=empty_tag)
moffset = (x_measure(offset[1]), y_measure(offset[2]))
prim = MathJaxPrimitive((x_measure(x), y_measure(y)), (x_measure(width), y_measure(height)), string(value), rot, moffset)
MathJax{typeof(prim)}([prim], tag)
end

"""
mathjax(xs::AbstractArray, ys::AbstractArray, ws::AbstractArray, hs::AbstractArray, values::AbstractArray [,rots::Rotation])

Arguments can be passed in arrays in order to perform multiple drawing operations at once.
"""
mathjax(xs::AbstractArray, ys::AbstractArray, ws::AbstractArray, hs::AbstractArray, values::AbstractArray{AbstractString},
rots::AbstractArray=[Rotation()], offsets::AbstractArray=[(0mm,0mm)];
tag::Symbol=empty_tag) =
@makeform (x in xs, y in ys, w in ws, h in hs, value in values, rot in rots, offset in offsets),
MathJaxPrimitive((x_measure(x), y_measure(y)), (x_measure(w), y_measure(h)), value, rot, (x_measure(offset[1]), y_measure(offset[2]))) tag

function resolve(box::AbsoluteBox, units::UnitBox, t::Transform, p::MathJaxPrimitive)
rot = resolve(box, units, t, p.rot)
return MathJaxPrimitive(
resolve(box, units, t, p.position),
resolve(box, units, t, p.size),
p.value, rot, p.offset)
end

function boundingbox(form::MathJaxPrimitive, linewidth::Measure,
font::AbstractString, fontsize::Measure)

width, height = form.size
x0 = form.position.x
y0 = form.position.y

return BoundingBox(x0 - linewidth + form.offset[1],
y0 - linewidth + form.offset[2],
width + linewidth,
height + linewidth)
end


62 changes: 62 additions & 0 deletions src/svg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,13 @@ function draw(img::SVG, prim::TextPrimitive, idx::Int)
print(img.out, "</g>\n")
end

function draw(img::SVG, prim::RawSVGPrimitive, idx::Int)
indent(img)
img.indentation += 1
print(img.out, prim.value)
img.indentation -= 1
end

function draw(img::SVG, prim::CurvePrimitive, idx::Int)
x0, y0 = prim.anchor0[1], prim.anchor0[2]
x0, y0 += prim.ctrl0[1], prim.ctrl0[2]
Expand Down Expand Up @@ -1305,3 +1312,58 @@ function draw(img::SVG, prim::BezierPolygonPrimitive, idx::Int)
indent(img)
print(img.out, "</g>\n")
end

function draw(img::SVG, prim::MathJaxPrimitive, idx::Int)
indent(img)

img.indentation += 1
print(img.out, "<g transform=\"translate(")
svg_print_float(img.out, prim.position[1].value)
print(img.out, ",")
svg_print_float(img.out, prim.position[2].value)
print(img.out, ")\"")
print_vector_properties(img, idx, true)
print(img.out, ">\n")
indent(img)

print(img.out, "<g class=\"primitive\">\n")
img.indentation += 1
indent(img)
print(img.out, """<foreignObject x="0" y="0" """)

if abs(prim.rot.theta) > 1e-4 || sum(abs.(prim.offset)) > 1e-4mm
print(img.out, " transform=\"")
if abs(prim.rot.theta) > 1e-4
print(img.out, "rotate(")
svg_print_float(img.out, rad2deg(prim.rot.theta))
print(img.out, ",")
svg_print_float(img.out, prim.rot.offset[1].value-prim.position[1].value)
print(img.out, ", ")
svg_print_float(img.out, prim.rot.offset[2].value-prim.position[2].value)
print(img.out, ")")
end
if sum(abs.(prim.offset)) > 1e-4mm
print(img.out, "translate(")
svg_print_float(img.out, prim.offset[1].value)
print(img.out, ",")
svg_print_float(img.out, prim.offset[2].value)
print(img.out, ")")
end
print(img.out, "\"")
end

print(img.out, """ width="$(prim.size[1])" height="$(prim.size[2])">
<div xmlns="http://www.w3.org/1999/xhtml">
\\(\\displaystyle{$(prim.value)}\\)
</div>
</foreignObject>""")

img.indentation -= 1
indent(img)
print(img.out, "</g>\n")
img.indentation -= 1
indent(img)
print(img.out, "</g>\n")
end


7 changes: 7 additions & 0 deletions test/examples/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ compose(context(),
line([(0.1,0.3),(0.2,0.3)]),
curve((0.25,0.35),(0.25,0.25),(0.35,0.25),(0.35,0.35))),
bitmap("image/png",rawimg,0.4,0.25,0.1,0.1),
mathjax(0.2, 0.4, 0.1, 0.1, "x+1\\over y-1"),
rawsvg(raw"""<g xmlns="http://www.w3.org/2000/svg" transform="translate(84.85,30)">
<g class="primitive">
<text dy="-1em">hello</text>
</g>
</g>"""),
bitmap("image/png",rawimg,0.4,0.25,0.1,0.1),
text(0.6,0.3,"hello")) |> SVG("primitives.svg")
9 changes: 9 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ bm = bitmap("fake", rand(UInt8,10), 0, 1, 0.8, 0.7, :image)
@test isa(Compose.text(0.5,0.4,"hello"), Compose.Text)
@test isa(Compose.text(rand(5),rand(5),["hello","there"]), Compose.Text)

@test isa(Compose.mathjax(0.5,0.4,0.1,0.2,raw"\frac{x}{2}"), Compose.MathJax)
@test isa(Compose.mathjax(rand(2),rand(2),rand(2),rand(2),[raw"\frac{x}{2}",raw"\frac{x}{3}"]), Compose.MathJax)

@test isa(Compose.rawsvg(raw"""<foreignObject x="50" y="50" width="100" height="100">
<div xmlns="http://www.w3.org/1999/xhtml" style="font-family:Times; font-size:15px">
\(\displaystyle{x+1\over y-1}\)
</div>
</foreignObject>"""), Compose.RawSVG)

@test isa(Compose.line(), Compose.Line)
@test isa(Compose.line([(1,2),(3,5),(4,2)]), Compose.Line)

Expand Down