Skip to content

Commit 84be7a1

Browse files
committed
weak deps
1 parent e221cee commit 84be7a1

File tree

9 files changed

+146
-74
lines changed

9 files changed

+146
-74
lines changed

.github/workflows/format-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Install JuliaFormatter and format
2121
run: |
2222
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
23-
julia -e 'using JuliaFormatter; format(["src", "test"], verbose=true)'
23+
julia -e 'using JuliaFormatter; format(["src", "ext", "test"], verbose=true)'
2424
- name: Format check
2525
run: |
2626
julia -e '

Project.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Contour = "d38c429a-6771-53c6-b99e-75d170b6e991"
1212
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
1313
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1414
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
15-
FreeType = "b38be410-82b0-50bf-ab77-7b57e271db43"
1615
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1716
MarchingCubes = "299715c1-40a9-479a-aaf9-4a633d36f717"
1817
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
@@ -40,9 +39,18 @@ StatsBase = "0.33"
4039
Unitful = "1"
4140
julia = "1.6"
4241

42+
[weakdeps]
43+
FreeType = "b38be410-82b0-50bf-ab77-7b57e271db43"
44+
ImageInTerminal = "d8c32880-2388-543b-8c61-d9f865259254"
45+
46+
[extensions]
47+
FreeTypeExt = "FreeType"
48+
ImageInTerminalExt = "ImageInTerminal"
49+
4350
[extras]
4451
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
4552
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
53+
FreeType = "b38be410-82b0-50bf-ab77-7b57e271db43"
4654
ImageInTerminal = "d8c32880-2388-543b-8c61-d9f865259254"
4755
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
4856
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
@@ -53,4 +61,4 @@ TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
5361
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
5462

5563
[targets]
56-
test = ["Aqua", "DataFrames", "ImageInTerminal", "ImageMagick", "Random", "ReferenceTests", "StableRNGs", "Test", "TestImages", "TimerOutputs"]
64+
test = ["Aqua", "DataFrames", "FreeType", "ImageInTerminal", "ImageMagick", "Random", "ReferenceTests", "StableRNGs", "Test", "TestImages", "TimerOutputs"]

src/freetype.jl renamed to ext/FreeTypeExt.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# adapted from github.com/JuliaGraphics/FreeTypeAbstraction.jl/blob/master/src/rendering.jl
22
# credits to the `FreeTypeAbstraction` authors
33

4-
module FreeTypeRendering
4+
module FreeTypeExt
55

6-
using ..StaticArrays
7-
using ..ColorTypes
8-
using FreeType
9-
10-
export get_font_face, render_string!
6+
import UnicodePlots
7+
isdefined(Base, :get_extension) ? (using FreeType) : (using ..FreeType)
8+
using StaticArrays
9+
using ColorTypes
1110

1211
const REGULAR_STYLES = "regular", "normal", "medium", "standard", "roman", "book"
1312
const FT_LIB = FT_Library[C_NULL]
@@ -85,7 +84,7 @@ fallback_fonts() =
8584

8685
const FT_FONTS = Dict{String,FTFont}()
8786

88-
function get_font_face(font = nothing, fallback = fallback_fonts())
87+
function UnicodePlots.get_font_face(font = nothing, fallback = fallback_fonts())
8988
face = nothing
9089
for name filter(!isnothing, (font, "JuliaMono", fallback...))
9190
if (face = get(FT_FONTS, name, nothing)) nothing
@@ -287,7 +286,7 @@ Uses the conventions of freetype.org/freetype2/docs/glyphs/glyphs-3.html
287286
* `gstr`: background string or array of chars (for background sizing)
288287
* `incx`: extra x spacing
289288
"""
290-
function render_string!(
289+
function UnicodePlots.render_string!(
291290
img::AbstractMatrix{T},
292291
fstr::Union{AbstractVector{Char},String},
293292
face::FTFont,

ext/ImageInTerminalExt.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module ImageInTerminalExt
2+
3+
import UnicodePlots
4+
isdefined(Base, :get_extension) ? (import ImageInTerminal) : (import ..ImageInTerminal)
5+
using ColorTypes
6+
7+
UnicodePlots.sixel_encode(args...; kw...) = ImageInTerminal.sixel_encode(args...; kw...)
8+
UnicodePlots.imshow(args...; kw...) = ImageInTerminal.imshow(args...; kw...)
9+
10+
function UnicodePlots.terminal_specs(img)
11+
if ImageInTerminal.choose_sixel(img)
12+
ans = ImageInTerminal.Sixel.TerminalTools.query_terminal("\e[16t", stdout)
13+
if ans isa String && (m = match(r"\e\[6;(\d+);(\d+)t", ans)) nothing
14+
char_h, char_w = tryparse.(Int, m.captures)
15+
return char_h nothing && char_w nothing, char_h, char_w
16+
end
17+
end
18+
false, nothing, nothing
19+
end
20+
21+
UnicodePlots.imageplot(img::AbstractArray{<:Colorant}; kw...) =
22+
UnicodePlots.Plot(UnicodePlots.ImageGraphics(img); border = :corners, kw...)
23+
24+
end

src/UnicodePlots.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ include("canvas/heatmapcanvas.jl")
9595
include("description.jl")
9696
include("volume.jl")
9797

98-
include("freetype.jl")
99-
using .FreeTypeRendering
10098
include("plot.jl")
10199
include("show.jl")
102100

@@ -115,6 +113,8 @@ include("interface/boxplot.jl")
115113
include("interface/polarplot.jl")
116114
include("interface/imageplot.jl")
117115

116+
isdefined(Base, :get_extension) || import Requires
117+
118118
function __init__()
119119
if (terminal_24bit() || forced_24bit()) && !forced_8bit()
120120
truecolors!()
@@ -123,9 +123,13 @@ function __init__()
123123
colors256!()
124124
faintcolors!()
125125
end
126-
Requires.@require ImageInTerminal = "d8c32880-2388-543b-8c61-d9f865259254" begin
127-
imageplot(img::AbstractArray{<:Colorant}; kw...) =
128-
Plot(ImageGraphics(img); border = :corners, kw...)
126+
@static if !isdefined(Base, :get_extension)
127+
Requires.@require ImageInTerminal = "d8c32880-2388-543b-8c61-d9f865259254" include(
128+
"../ext/ImageInTerminalExt.jl",
129+
)
130+
Requires.@require FreeType = "b38be410-82b0-50bf-ab77-7b57e271db43" include(
131+
"../ext/FreeTypeExt.jl",
132+
)
129133
end
130134
nothing
131135
end

src/graphics/imagegraphics.jl

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,14 @@ end
2424
@inline nrows(c::ImageGraphics) = first(c.encoded_size)
2525
@inline ncols(c::ImageGraphics) = last(c.encoded_size)
2626

27+
# `ImageInTerminalExt` placeholders
28+
function terminal_specs end
29+
function sixel_encode end
30+
function imshow end
31+
2732
function preprocess!(io::IO, c::ImageGraphics)
2833
ctx = IOContext(PipeBuffer(), :displaysize => displaysize(io))
29-
c.sixel[] = false
30-
char_h = char_w = nothing # determine the terminal caret size, in pixels
31-
# COV_EXCL_START
32-
if ImageInTerminal.choose_sixel(c.img)
33-
ans = ImageInTerminal.Sixel.TerminalTools.query_terminal("\e[16t", stdout)
34-
if ans isa String && (m = match(r"\e\[6;(\d+);(\d+)t", ans)) nothing
35-
char_h, char_w = tryparse.(Int, m.captures)
36-
c.sixel[] = char_h nothing && char_w nothing
37-
end
38-
end
39-
# COV_EXCL_STOP
34+
c.sixel[], char_h, char_w = terminal_specs(c.img)
4035
postprocess = c -> begin
4136
c.encoded_size .= (0, 0)
4237
empty!(c.chars)
@@ -49,7 +44,7 @@ function preprocess!(io::IO, c::ImageGraphics)
4944
# COV_EXCL_START
5045
# it is better to encode the whole image in a single pass
5146
# otherwise, issues with the last line (see previous implementation)
52-
ImageInTerminal.sixel_encode(ctx, c.img)
47+
sixel_encode(ctx, c.img)
5348
push!(c.chars, read(ctx, String) |> collect)
5449
length(1:char_h:img_h), ceil(Int, img_w / char_w)
5550
# COV_EXCL_STOP
@@ -69,7 +64,7 @@ function preprocess!(io::IO, c::ImageGraphics)
6964
end
7065
nothing
7166
end
72-
ImageInTerminal.imshow(ctx, c.img; callback)
67+
imshow(ctx, c.img; callback)
7368
length(c.chars), length(c.chars |> first)
7469
end
7570
postprocess

src/show.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ function _show(end_io::IO, print_nocol, print_color, p::Plot)
398398
)
399399
end
400400

401+
# `FreeTypeExt` placeholders
402+
function get_font_face end
403+
function render_string! end
404+
401405
"""
402406
png_image(p::Plot, font = nothing, pixelsize = 32, transparent = true, foreground = nothing, background = nothing, bounding_box = nothing, bounding_box_glyph = nothing)
403407

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ImageInTerminal
1+
using ImageInTerminal, FreeType
22
using UnicodePlots, Test
33

44
import UnicodePlots: lines!, points!, pixel!, nrows, ncols

0 commit comments

Comments
 (0)