Skip to content

Commit

Permalink
Merge pull request #276 from hustf/iss275
Browse files Browse the repository at this point in the history
Move from @require to v1.9 Ext
  • Loading branch information
cormullion authored Oct 13, 2023
2 parents b508156 + ba5f199 commit 06a6d02
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: false
matrix:
julia-version:
- "1.6"
- "1.9"
- "1"
- "nightly"
os:
Expand Down
18 changes: 12 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ version = "3.9.0"
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Cairo = "159f3aea-2a34-519c-b102-8c37f9878175"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
FFMPEG = "c87230d0-a227-11e9-1b43-d7ebe4e7570a"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Juno = "e5e0dc1b-0480-54bc-9374-aad01c23163d"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
MathTeXEngine = "0a4f8689-d25c-4efe-a92b-7142dfc1aa53"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Rsvg = "c4c386cf-5103-5370-be45-f3a111cca3b8"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

[weakdeps]
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
MathTeXEngine = "0a4f8689-d25c-4efe-a92b-7142dfc1aa53"

[extensions]
LuxorExtLatex = ["LaTeXStrings", "MathTeXEngine"]

[compat]
Cairo = "0.7, 0.8, 1.0"
Expand All @@ -26,10 +33,9 @@ FFMPEG = "0.4"
FileIO = "1"
Juno = "0.7, 0.8"
LaTeXStrings = "1.1, 1.2, 1.3"
Requires = "1, 1.1, 1.2, 1.3"
Rsvg = "1.0"
PrecompileTools = "1"
julia = "1.6"
Rsvg = "1.0"
julia = "1.9"

[extras]
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
environment:
matrix:
- julia_version: 1.7
- julia_version: 1.9
- julia_version: latest

platform:
Expand Down
10 changes: 10 additions & 0 deletions ext/LuxorExtLatex.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
This extension is loaded and used by Luxor if both LaTeXStrings and MathTeXEngine is loaded.
The functions are accessed through Luxor as usual.
"""
module LuxorExtLatex

include("latex.jl")

end # module
11 changes: 9 additions & 2 deletions src/latex.jl → ext/latex.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import .MathTeXEngine:
import MathTeXEngine
# We don't add new methods to functions from MathTeXEngine. Hence, we 'use' these:
using MathTeXEngine:
generate_tex_elements, inkwidth, inkheight, bottominkbound, TeXChar, HLine
using .LaTeXStrings
using LaTeXStrings
import Luxor
import Luxor: text, latextextsize, latexboundingbox, rawlatexboundingbox
using Luxor: Point, @layer, translate, rotate, move,
fontface, fontsize, get_fontsize, setfont,
setline, line, poly, closepath, newsubpath

"""
texalign(halign, valign, bottom_pt, top_pt, font_size)
Expand Down
11 changes: 2 additions & 9 deletions src/Luxor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module Luxor
using FileIO
using Base64
using Cairo
using Requires
using Colors
using Dates
using FFMPEG
Expand Down Expand Up @@ -73,16 +72,10 @@ include("randompoints.jl")
include("Path.jl")
include("precompile.jl")
include("placeeps.jl")
include("placeholders_for_extensions.jl")
# include("play.jl") # will require MiniFB
# include("shapefile.jl") # don't load unless you've loaded Shapefile.jl

function __init__()
@require MathTeXEngine = "0a4f8689-d25c-4efe-a92b-7142dfc1aa53" begin
@require LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" include("latex.jl")
end
_reset_all_drawings()
end

export Drawing,
cm, inch, mm,
paper_sizes,
Expand Down Expand Up @@ -181,7 +174,7 @@ export Drawing,
# experimental
tidysvg,

# latex
# latex. These always throw errors if Base.get_extension(Luxor, :LuxorExtLatex) isa Nothing
latextextsize, latexboundingbox, rawlatexboundingbox

# basic unit conversion to Cairo/PostScript points
Expand Down
25 changes: 25 additions & 0 deletions src/placeholders_for_extensions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Extensions are modules that depend on Luxor and other modules.
# They are loaded when Luxor and these modules are loaded.
# Extensions (LuxorExt...) cannot however reliably introduce new symbols
# into Luxor. Hence, placeholder functions with catchall argument types:
function latextextsize(catch_all)
if Base.get_extension(Luxor, :LuxorExtLatex) isa Module
throw(MethodError(latextextsize, catch_all))
else
throw(ErrorException("Modules LaTeXStrings and MathTeXEngine are not loaded."))
end
end
function latexboundingbox(catch_all; kwargs...)
if Base.get_extension(Luxor, :LuxorExtLatex) isa Module
throw(MethodError(latexboundingbox, catch_all))
else
throw(ErrorException("Modules LaTeXStrings and MathTeXEngine are not loaded."))
end
end
function rawlatexboundingbox(catch_all)
if Base.get_extension(Luxor, :LuxorExtLatex) isa Module
throw(MethodError(rawlatexboundingbox, catch_all))
else
throw(ErrorException("Modules LaTeXStrings and MathTeXEngine are not loaded."))
end
end
50 changes: 37 additions & 13 deletions test/latex.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
using Luxor
using Test

@testset "nolatexextension" begin
@test ! @isdefined LaTeXStrings
@test Base.get_extension(Luxor, :LuxorExtLatex) isa Nothing
@test length(methods(text)) == 3
@test length(methods(latextextsize)) == 1
@test length(methods(latexboundingbox)) == 1
@test length(methods(rawlatexboundingbox)) == 1
@test_throws ErrorException latextextsize(14.0)
@test_throws ErrorException latexboundingbox("14.0")
@test_throws ErrorException rawlatexboundingbox(:fourteen)
end


using LaTeXStrings
using MathTeXEngine
@testset "latexextension" begin
@test Base.get_extension(Luxor, :LuxorExtLatex) isa Module
@test length(methods(text)) == 4
@test length(methods(latextextsize)) == 2
@test length(methods(latexboundingbox)) == 3
@test length(methods(rawlatexboundingbox)) == 2
@test_throws MethodError latextextsize(14.0)
@test_throws MethodError latexboundingbox("14.0")
@test_throws MethodError rawlatexboundingbox(:fourteen)
end

@testset "latex" begin
d = Drawing(200, 200, :svg)
Expand All @@ -14,28 +38,28 @@ using MathTeXEngine
for halign in [:left, :right, :center], valign in [:baseline, :bottom, :top, :middle]
text(t, O, halign=halign, valign=valign)

@test round.(Luxor.latextextsize(t); digits=2) == (36.99, 46.69)
@test round.(latextextsize(t); digits=2) == (36.99, 46.69)

if halign === :left
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[1][1]; digits=2) == 0.0
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[2][1]; digits=2) == 36.99
@test round(latexboundingbox(t, halign=halign, valign=valign)[1][1]; digits=2) == 0.0
@test round(latexboundingbox(t, halign=halign, valign=valign)[2][1]; digits=2) == 36.99
elseif halign === :right
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[1][1]; digits=2) == -36.99
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[2][1]; digits=2) == 0.0
@test round(latexboundingbox(t, halign=halign, valign=valign)[1][1]; digits=2) == -36.99
@test round(latexboundingbox(t, halign=halign, valign=valign)[2][1]; digits=2) == 0.0
elseif halign === :center
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[1][1]; digits=2) == -36.98 / 2
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[2][1]; digits=2) == 36.98 / 2
@test round(latexboundingbox(t, halign=halign, valign=valign)[1][1]; digits=2) == -36.98 / 2
@test round(latexboundingbox(t, halign=halign, valign=valign)[2][1]; digits=2) == 36.98 / 2
end

if valign === :baseline || valign === :bottom
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[1][2]; digits=2) == 18.72
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[2][2]; digits=2) == -27.97
@test round(latexboundingbox(t, halign=halign, valign=valign)[1][2]; digits=2) == 18.72
@test round(latexboundingbox(t, halign=halign, valign=valign)[2][2]; digits=2) == -27.97
elseif valign === :top
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[1][2]; digits=2) == 46.69
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[2][2]; digits=2) == 0.0
@test round(latexboundingbox(t, halign=halign, valign=valign)[1][2]; digits=2) == 46.69
@test round(latexboundingbox(t, halign=halign, valign=valign)[2][2]; digits=2) == 0.0
elseif valign === :middle
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[1][2]; digits=2) == 46.70 / 2
@test round(Luxor.latexboundingbox(t, halign=halign, valign=valign)[2][2]; digits=2) == -46.70 / 2
@test round(latexboundingbox(t, halign=halign, valign=valign)[1][2]; digits=2) == 46.70 / 2
@test round(latexboundingbox(t, halign=halign, valign=valign)[2][2]; digits=2) == -46.70 / 2
end
end

Expand Down

0 comments on commit 06a6d02

Please sign in to comment.