Skip to content

Commit

Permalink
prelim work on adding optional runtime backend loading
Browse files Browse the repository at this point in the history
  • Loading branch information
tlnagy committed Jul 29, 2018
1 parent b96083d commit 4e20db6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 45 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ DataStructures
IterTools
JSON
Measures
Requires
71 changes: 26 additions & 45 deletions src/Compose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using IterTools
using DataStructures
using Compat
using Measures
using Requires
import JSON

import Base: length, start, next, done, isempty, getindex, setindex!,
Expand All @@ -15,7 +16,6 @@ import Base: length, start, next, done, isempty, getindex, setindex!,

using Compat.Dates
using Compat.Printf
using Compat.Pkg

import Measures: resolve, w, h

Expand All @@ -31,25 +31,6 @@ export compose, compose!, Context, UnitBox, AbsoluteBoundingBox, Rotation, Mirro
CAIROSURFACE, introspect, set_default_graphic_size, set_default_jsmode,
boundingbox, Patchable

function isinstalled(pkg, ge=v"0.0.0-")
try
# Pkg.installed might throw an error,
# we need to account for it to be able to precompile
ver = Pkg.installed(pkg)
ver == nothing && try
# Assume the version is new enough if the package is in LOAD_PATH
ex = Expr(:import, Symbol(pkg))
@eval $ex
return true
catch
return false
end
return ver >= ge
catch
return false
end
end

abstract type Backend end

"""
Expand Down Expand Up @@ -157,40 +138,40 @@ macro missing_cairo_error(backend)
string(msg1, msg2)
end

if isinstalled("Cairo")
include("cairo_backends.jl")
include("immerse_backend.jl")
else
global PNG
global PS
global PDF

PNG(args...) = error(@missing_cairo_error "PNG")
PS(args...) = error(@missing_cairo_error "PS")
PDF(args...) = error(@missing_cairo_error "PDF")
end
global PDF
PNG(args...) = error(@missing_cairo_error "PNG")
PS(args...) = error(@missing_cairo_error "PS")
PDF(args...) = error(@missing_cairo_error "PDF")

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.

if isinstalled("Fontconfig")
include("fontfallback.jl")

function link_fontconfig()
pango_cairo_ctx = C_NULL
include("pango.jl")

function __init__()
global pango_cairo_ctx
global pangolayout
ccall((:g_type_init, Cairo._jl_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
else
include("fontfallback.jl")
ccall((:g_type_init, Cairo._jl_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()
@info "Loading Cairo backend into Compose.jl"
include("cairo_backends.jl")
include("immerse_backend.jl")
end

function __init__()
@require Cairo="159f3aea-2a34-519c-b102-8c37f9878175" link_cairo()
@require Fontconfig="186bb1d3-e1f7-5a2c-a377-96d770f13627" link_fontconfig()
end

show(io::IO, m::MIME"text/html", ctx::Context) =
Expand Down

0 comments on commit 4e20db6

Please sign in to comment.