Skip to content

Commit

Permalink
Merge pull request #1 from legend-exp/watermarks
Browse files Browse the repository at this point in the history
Add functions for watermarks
  • Loading branch information
fhagemann authored Feb 20, 2025
2 parents 7bbbae9 + 39c2cd5 commit 8233962
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 4 deletions.
10 changes: 9 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ version = "0.1.0"
MakieCore = "20f20a25-4f0e-4fdf-b5d1-57303727442b"

[weakdeps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Format = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
MathTeXEngine = "0a4f8689-d25c-4efe-a92b-7142dfc1aa53"
RadiationDetectorSignals = "bf2c0563-65cf-5db2-a620-ceb7de82658c"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[extensions]
LegendMakieExt = ["Makie", "LaTeXStrings", "MathTeXEngine"]
LegendMakieExt = ["Makie", "Dates", "FileIO", "Format", "LaTeXStrings", "MathTeXEngine", "Unitful"]
LegendMakieRadiationDetectorSignalsExt = "RadiationDetectorSignals"

[compat]
Dates = "1"
FileIO = "1"
Format = "1"
LaTeXStrings = "1.2"
Makie = "0.22"
MakieCore = "0.9"
MathTeXEngine = "0.6.1"
RadiationDetectorSignals = "0.3.8"
Unitful = "1.6"
julia = "1.10"
8 changes: 8 additions & 0 deletions ext/LegendMakieExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ module LegendMakieExt

import LegendMakie

import Dates
import FileIO
import Format
import LaTeXStrings
import Makie
import MathTeXEngine

import LegendMakie: pt
import Unitful: @u_str

include("recipes/watermarks.jl")

function __init__()
# maybe just use with_theme() in every plot recipe?
Expand Down
108 changes: 108 additions & 0 deletions ext/recipes/watermarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# This file is a part of LegendMakie.jl, licensed under the MIT License (MIT).

function LegendMakie.add_legend_logo!(args...; kwargs...)
LegendMakie.add_logo!(args...; logofile = LegendMakie.LegendLogo, textcolor = LegendMakie.DeepCove, kwargs...)
end

function LegendMakie.add_juleana_logo!(args...; kwargs...)
LegendMakie.add_logo!(args...; logofile = LegendMakie.JuleanaSimple, textcolor = :black, kwargs...)
end

function LegendMakie.add_logo!(; fontsize = 18, position = "outer right", textcolor = :black, logofile = LegendMakie.JuleanaSimple)

fig = Makie.current_figure()
ax = Makie.current_axis()

# Optimized for 13.5pt
refsize = 13.5

# modify size using fontsize
font_scale = fontsize/refsize * 0.024pt

figwidth, figheight = fig.scene.viewport[].widths
axleft, axbot = ax.scene.viewport[].origin
axright, axtop = ax.scene.viewport[].origin .+ ax.scene.viewport[].widths

logo = FileIO.load(logofile)
logowidth, logoheight = size(logo) .* font_scale
legend_suffix = (logofile == LegendMakie.LegendLogo ? "-200" : "") * "" *
Format.format("{:02d}-{:04d}", Dates.month(Dates.today()), Dates.year(Dates.today()))

if position == "outer right"
img = Makie.image!(fig.scene, Makie.rot180(logo))
Makie.scale!(img, font_scale, font_scale)
Makie.translate!(img, (axright, axtop - logoheight))

Makie.text!(fig.scene, legend_suffix,
position = ((axright + fontsize / refsize), (axtop - logoheight)),
color = textcolor, fontsize = fontsize, font = :regular, rotation = 270u"°"
)
elseif position == "outer top"
img = Makie.image!(fig.scene, Makie.rotr90(logo))
Makie.scale!(img, font_scale, font_scale)
Makie.translate!(img, (axleft, axtop))

Makie.text!(fig.scene, legend_suffix,
position = ((axleft + logoheight), (axtop + fontsize / refsize)),
color = textcolor, fontsize = fontsize, font = :regular
)
else
throw(ArgumentError("Position $(position) is invalid. Please choose `outer top` or `outer right`."))
end
fig
end

function LegendMakie.add_text!(text::AbstractString)
fig = Makie.current_figure()
ax = Makie.current_axis()
axright, axtop = ax.scene.viewport[].origin .+ ax.scene.viewport[].widths
Makie.text!(fig.scene, text, font = :bold,
color = :red, position = (axright, axtop), align = (:right, :bottom))
fig
end


function LegendMakie.add_watermarks!(;
legend_logo::Bool = false, juleana_logo::Bool = true,
preliminary::Bool = true, approved::Bool = false, final::Bool = false,
kwargs...
)
if legend_logo
LegendMakie.add_legend_logo!()
elseif juleana_logo
LegendMakie.add_juleana_logo!()
end

if !final && preliminary
LegendMakie.add_text!("PRELIMINARY")
elseif !final && !approved
LegendMakie.add_text!("INTERNAL USE ONLY")
end

Makie.current_figure()
end

# function LegendMakie.add_juleana_watermark!(; logo_scale = 0.2, position = :rt)

# fig = Makie.current_figure()
# ax = Makie.current_axis()

# figwidth, figheight = fig.scene.viewport[].widths
# axleft, axbot = ax.scene.viewport[].origin
# axwidth, axheight = ax.scene.viewport[].widths
# axright, axtop = ax.scene.viewport[].origin .+ ax.scene.viewport[].widths

# juleana = FileIO.load(LegendMakie.JuleanaLogo)
# _logo_scale = logo_scale * axheight / size(juleana,1)
# juleanaheight, juleanawidth = size(juleana) .* _logo_scale
# img = Makie.image!(fig.scene, Makie.rotr90(juleana))
# Makie.scale!(img, _logo_scale, _logo_scale)
# space = min(0.03 * axwidth, 0.03 * axheight)

# (; halign, valign) = Makie.legend_position_to_aligns(position)
# juleanax = halign == :left ? axleft + space : axright - juleanawidth - space
# juleanay = valign == :bottom ? axbot + space : axtop - juleanaheight - space
# Makie.translate!(img, (juleanax, juleanay))

# fig
# end
7 changes: 7 additions & 0 deletions src/lplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ Plots `objs` via Makie in a LEGEND-specific form/style into a new plot.
function lplot end
function lplot! end
export lplot, lplot!

# watermark functions
function add_logo! end
function add_legend_logo! end
function add_juleana_logo! end
function add_text! end
function add_watermarks! end
19 changes: 16 additions & 3 deletions test/test_lplot.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
# This file is a part of LegendMakie.jl, licensed under the MIT License (MIT).

using LegendMakie
using Test

# load Makie extension
using Makie

using Test

@testset "lplot" begin
@testset "Test watermarks" begin

fig = Figure()

# test default watermark
ax = Axis(fig[1,1])
hist!(ax, randn(10000))
@test_nowarn LegendMakie.add_watermarks!()

# test alternative watermark
ax2 = Axis(fig[1,2])
hist!(ax2, randn(10000))
@test_nowarn LegendMakie.add_watermarks!(legend_logo = true, position = "outer top", preliminary = false)
end
end

0 comments on commit 8233962

Please sign in to comment.