Skip to content

Commit 53ce1e1

Browse files
authored
Merge pull request #281 from PumasAI/jk/unpin-makie
Unpin Makie and fix tests
2 parents a2ce1e4 + ee30627 commit 53ce1e1

File tree

10 files changed

+109
-45
lines changed

10 files changed

+109
-45
lines changed

src/server.jl

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,7 @@ function process_results(dict::Dict{String,@NamedTuple{error::Bool, data::Vector
13381338
return (; data, metadata, errors)
13391339
end
13401340

1341-
function png_image_metadata(bytes::Vector{UInt8})
1341+
function png_image_metadata(bytes::Vector{UInt8}; phys_correction = true)
13421342
if @view(bytes[1:8]) != b"\x89PNG\r\n\x1a\n"
13431343
throw(ArgumentError("Not a png file"))
13441344
end
@@ -1371,25 +1371,27 @@ function png_image_metadata(bytes::Vector{UInt8})
13711371
width = Int(_load(UInt32, chunk.data, 1))
13721372
height = Int(_load(UInt32, chunk.data, 5))
13731373

1374-
# if the png reports a physical pixel size, i.e., it has a pHYs chunk
1375-
# with the pixels per meter unit flag set, correct the basic width and height
1376-
# by those physical pixel sizes so that quarto receives the intended size
1377-
# in CSS pixels
1378-
while true
1379-
chunk = read_chunk!()
1380-
chunk === nothing && break
1381-
chunk.type == b"IDAT" && break
1382-
if chunk.type == b"pHYs"
1383-
is_in_meters = Bool(_load(UInt8, chunk.data, 9))
1384-
is_in_meters || break
1385-
x_px_per_meter = _load(UInt32, chunk.data, 1)
1386-
y_px_per_meter = _load(UInt32, chunk.data, 5)
1387-
# it seems sensible to round the final image size to full CSS pixels,
1388-
# especially given that png doesn't store dpi but px per meter
1389-
# in an integer format, losing some precision
1390-
width = round(Int, width / x_px_per_meter * (96 / 0.0254))
1391-
height = round(Int, height / y_px_per_meter * (96 / 0.0254))
1392-
break
1374+
if phys_correction
1375+
# if the png reports a physical pixel size, i.e., it has a pHYs chunk
1376+
# with the pixels per meter unit flag set, correct the basic width and height
1377+
# by those physical pixel sizes so that quarto receives the intended size
1378+
# in CSS pixels
1379+
while true
1380+
chunk = read_chunk!()
1381+
chunk === nothing && break
1382+
chunk.type == b"IDAT" && break
1383+
if chunk.type == b"pHYs"
1384+
is_in_meters = Bool(_load(UInt8, chunk.data, 9))
1385+
is_in_meters || break
1386+
x_px_per_meter = _load(UInt32, chunk.data, 1)
1387+
y_px_per_meter = _load(UInt32, chunk.data, 5)
1388+
# it seems sensible to round the final image size to full CSS pixels,
1389+
# especially given that png doesn't store dpi but px per meter
1390+
# in an integer format, losing some precision
1391+
width = round(Int, width / x_px_per_meter * (96 / 0.0254))
1392+
height = round(Int, height / y_px_per_meter * (96 / 0.0254))
1393+
break
1394+
end
13931395
end
13941396
end
13951397

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[deps]
2+
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
23
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
34
JSONSchema = "7d188eb4-7ad8-530c-ae41-71a32a6d4692"
45
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"

test/examples/integrations/CairoMakie.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ import CairoMakie
1616

1717
```{julia}
1818
CairoMakie.scatter([1, 2, 3], [1, 2, 3])
19-
```
19+
```
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
[deps]
22
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
3-
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
4-
5-
[compat]
6-
Makie = "= 0.22.1"

test/examples/mimetypes/Project.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@
22
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
33
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
44
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
5-
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
65
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
7-
8-
[compat]
9-
Makie = "= 0.22.1"

test/testsets/integrations/CairoMakie.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ include("../../utilities/prelude.jl")
33
test_example(joinpath(@__DIR__, "../../examples/integrations/CairoMakie.qmd")) do json
44
cells = json["cells"]
55
cell = cells[6]
6+
7+
px_per_inch = 96
8+
9+
# the size metadata is just inches converted to CSS pixels, independent of pixel resolution
610
@test cell["outputs"][1]["metadata"]["image/png"] ==
7-
Dict("width" => 4 * 150, "height" => 3 * 150)
11+
Dict("width" => 4 * px_per_inch, "height" => 3 * px_per_inch)
12+
13+
pngbytes = Base64.base64decode(cell["outputs"][1]["data"]["image/png"])
14+
@test QuartoNotebookRunner.png_image_metadata(pngbytes; phys_correction = false) ==
15+
(; width = 4 * 150, height = 3 * 150)
816
end

test/testsets/package_integration_hooks.jl

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,48 @@ include("../utilities/prelude.jl")
2626
"CairoMakie.qmd";
2727
showprogress = false,
2828
)
29-
return json.cells[end-1].outputs[1].metadata["image/png"]
29+
logical_size = json.cells[end-1].outputs[1].metadata["image/png"]
30+
pngbytes =
31+
Base64.base64decode(json.cells[end-1].outputs[1].data["image/png"])
32+
px_size = QuartoNotebookRunner.png_image_metadata(
33+
pngbytes;
34+
phys_correction = false,
35+
)
36+
return (;
37+
width_px = px_size.width,
38+
height_px = px_size.height,
39+
logical_size...,
40+
)
3041
end
3142

3243
metadata = png_metadata()
33-
@test metadata.width == 4 * 150
34-
@test metadata.height == 3 * 150
44+
@test metadata.width_px == 4 * 150
45+
@test metadata.height_px == 3 * 150
3546

3647
metadata = png_metadata("""
3748
fig-width: 8
3849
fig-height: 6
3950
fig-dpi: 300""")
40-
@test metadata.width == 8 * 300
41-
@test metadata.height == 6 * 300
51+
@test metadata.width_px == 8 * 300
52+
@test metadata.height_px == 6 * 300
53+
@test metadata.width == 8 * 96
54+
@test metadata.height == 6 * 96
4255

4356
metadata = png_metadata("""
4457
fig-width: 5
4558
fig-dpi: 100""")
46-
@test metadata.width == 5 * 100
47-
@test metadata.height == round(5 / 4 * 3 * 100)
59+
@test metadata.width_px == 5 * 100
60+
@test metadata.height_px == round(5 / 4 * 3 * 100)
61+
@test metadata.width == 5 * 96
62+
@test metadata.height == round(5 / 4 * 3 * 96)
4863

4964
metadata = png_metadata("""
5065
fig-height: 5
5166
fig-dpi: 100""")
52-
@test metadata.height == 5 * 100
53-
@test metadata.width == round(5 / 3 * 4 * 100)
67+
@test metadata.height_px == 5 * 100
68+
@test metadata.width_px == round(5 / 3 * 4 * 100)
69+
@test metadata.height == 5 * 96
70+
@test metadata.width == round(5 / 3 * 4 * 96)
5471

5572
# we don't want to rely on hardcoding Makie's own default size for our tests
5673
# but for the dpi-only test we can check that doubling the
@@ -59,8 +76,10 @@ include("../utilities/prelude.jl")
5976
fig-dpi: 96""")
6077
metadata_200dpi = png_metadata("""
6178
fig-dpi: 192""")
62-
@test 2 * metadata_100dpi.height == metadata_200dpi.height
63-
@test 2 * metadata_100dpi.width == metadata_200dpi.width
79+
@test 2 * metadata_100dpi.height_px == metadata_200dpi.height_px
80+
@test 2 * metadata_100dpi.width_px == metadata_200dpi.width_px
81+
@test metadata_100dpi.height == metadata_200dpi.height
82+
@test metadata_100dpi.width == metadata_200dpi.width
6483

6584
# same logic for width and height only
6685
metadata_single = png_metadata("""
@@ -69,6 +88,8 @@ include("../utilities/prelude.jl")
6988
metadata_double = png_metadata("""
7089
fig-width: 6
7190
fig-height: 4""")
91+
@test 2 * metadata_single.height_px == metadata_double.height_px
92+
@test 2 * metadata_single.width_px == metadata_double.width_px
7293
@test 2 * metadata_single.height == metadata_double.height
7394
@test 2 * metadata_single.width == metadata_double.width
7495

test/testsets/png_metadata.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,48 @@ include("../utilities/prelude.jl")
44
@test QuartoNotebookRunner.png_image_metadata(
55
read(joinpath(@__DIR__, "..", "assets", "10x15.png")),
66
) == (; width = 10, height = 15)
7+
@test QuartoNotebookRunner.png_image_metadata(
8+
read(joinpath(@__DIR__, "..", "assets", "10x15.png"));
9+
phys_correction = false,
10+
) == (; width = 10, height = 15)
11+
712
@test QuartoNotebookRunner.png_image_metadata(
813
read(joinpath(@__DIR__, "..", "assets", "15x10.png")),
914
) == (; width = 15, height = 10)
15+
@test QuartoNotebookRunner.png_image_metadata(
16+
read(joinpath(@__DIR__, "..", "assets", "15x10.png"));
17+
phys_correction = false,
18+
) == (; width = 15, height = 10)
19+
1020
@test QuartoNotebookRunner.png_image_metadata(
1121
read(joinpath(@__DIR__, "..", "assets", "black_no_dpi.png")),
1222
) == (; width = 100, height = 100)
23+
@test QuartoNotebookRunner.png_image_metadata(
24+
read(joinpath(@__DIR__, "..", "assets", "black_no_dpi.png"));
25+
phys_correction = false,
26+
) == (; width = 100, height = 100)
27+
1328
@test QuartoNotebookRunner.png_image_metadata(
1429
read(joinpath(@__DIR__, "..", "assets", "black_96_dpi.png")),
1530
) == (; width = 100, height = 100)
31+
@test QuartoNotebookRunner.png_image_metadata(
32+
read(joinpath(@__DIR__, "..", "assets", "black_96_dpi.png"));
33+
phys_correction = false,
34+
) == (; width = 100, height = 100)
35+
1636
@test QuartoNotebookRunner.png_image_metadata(
1737
read(joinpath(@__DIR__, "..", "assets", "black_300_dpi.png")),
1838
) == (; width = 32, height = 32)
39+
@test QuartoNotebookRunner.png_image_metadata(
40+
read(joinpath(@__DIR__, "..", "assets", "black_300_dpi.png"));
41+
phys_correction = false,
42+
) == (; width = 100, height = 100)
43+
1944
@test QuartoNotebookRunner.png_image_metadata(
2045
read(joinpath(@__DIR__, "..", "assets", "black_600_dpi.png")),
2146
) == (; width = 16, height = 16)
47+
@test QuartoNotebookRunner.png_image_metadata(
48+
read(joinpath(@__DIR__, "..", "assets", "black_600_dpi.png"));
49+
phys_correction = false,
50+
) == (; width = 100, height = 100)
2251
end

test/testsets/project_frontmatter.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ include("../utilities/prelude.jl")
1919
)
2020
cell = json.cells[6]
2121
metadata = cell.outputs[1].metadata["image/png"]
22-
@test metadata.width == 625
23-
@test metadata.height == 469
22+
pngbytes = Base64.base64decode(cell.outputs[1].data["image/png"])
23+
px_size =
24+
QuartoNotebookRunner.png_image_metadata(pngbytes; phys_correction = false)
25+
@test metadata.width == 600
26+
@test metadata.height == 450
27+
@test px_size.width == 625
28+
@test px_size.height == 469
2429

2530
options_file = "temp_options.json"
2631
open(options_file, "w") do io
@@ -35,8 +40,13 @@ include("../utilities/prelude.jl")
3540
)
3641
cell = json.cells[6]
3742
metadata = cell.outputs[1].metadata["image/png"]
38-
@test metadata.width == 625
39-
@test metadata.height == 469
43+
pngbytes = Base64.base64decode(cell.outputs[1].data["image/png"])
44+
px_size =
45+
QuartoNotebookRunner.png_image_metadata(pngbytes; phys_correction = false)
46+
@test metadata.width == 600
47+
@test metadata.height == 450
48+
@test px_size.width == 625
49+
@test px_size.height == 469
4050

4151
close!(server)
4252
end

test/utilities/prelude.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using Test
22
using Logging
33
using QuartoNotebookRunner
44

5+
import Base64
56
import JSON3
67
import JSONSchema
78
import NodeJS_18_jll

0 commit comments

Comments
 (0)