Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to fix marker size #3073

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ReferenceTests/src/tests/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,17 @@ end
for (i, marker) in enumerate(markers)
scatter!(Point2f.(1:5, i), marker = marker, markersize = range(10, 30, length = 5), color = :black)
scatter!(Point2f.(1:5, i), markersize = 4, color = :white)

# # Debug - show bbox outline
# if !(marker isa Char)
# scene = Makie.get_scene(ax)
# bb = Makie.bbox(Makie.DEFAULT_MARKER_MAP[marker])
# w, h = widths(bb)
# ox, oy = origin(bb)
# xy = map(pv -> Makie.project(pv, Vec2f(widths(pixelarea(scene)[])), Point2f(5, i)), scene.camera.projectionview)
# bb = map(xy -> Rect2f(xy .+ 30 * Vec2f(ox, oy), 30 * Vec2f(w, h)), xy)
# lines!(bb, linewidth = 1, color = :orange, space = :pixel, linestyle = :dash)
# end
end

f
Expand Down
18 changes: 13 additions & 5 deletions src/bezier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -502,16 +502,24 @@ function render_path(path, bitmap_size_px = 256)
# freetype has no ClosePath and EllipticalArc, so those need to be replaced
path_replaced = replace_nonfreetype_commands(path)

aspect = widths(bbox(path)) / maximum(widths(bbox(path)))
path_unit_rect = fit_to_bbox(path_replaced, Rect2f(Point2f(0), aspect))
# Minimal size that becomes integer when mutliplying by 64 (target size for
# atlas). This adds padding to avoid blurring/scaling factors from rounding
# during sdf generation
path_size = widths(bbox(path)) / maximum(widths(bbox(path)))
w = ceil(Int, 64 * path_size[1])
h = ceil(Int, 64 * path_size[2])
path_size = Vec2f(w, h) / 64f0

path_unit_rect = fit_to_bbox(path_replaced, Rect2f(Point2f(0), path_size))

path_transformed = Makie.scale(path_unit_rect, scale_factor)

outline_ref = make_outline(path_transformed)

# Adjust bitmap size to match path aspect
w = ceil(Int, bitmap_size_px * aspect[1])
h = ceil(Int, bitmap_size_px * aspect[2])
# Adjust bitmap size to match path size
w = ceil(Int, bitmap_size_px * path_size[1])
h = ceil(Int, bitmap_size_px * path_size[2])

pitch = w * 1 # 8 bit gray
pixelbuffer = zeros(UInt8, h * pitch)
bitmap_ref = Ref{FT_Bitmap}()
Expand Down
34 changes: 28 additions & 6 deletions src/utilities/texture_atlas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function Base.show(io::IO, atlas::TextureAtlas)
println(io, " font_render_callback: ", length(atlas.font_render_callback))
end

const SERIALIZATION_FORMAT_VERSION = "v2"
const SERIALIZATION_FORMAT_VERSION = "v3"

# basically a singleton for the textureatlas
function get_cache_path(resolution::Int, pix_per_glyph::Int)
Expand Down Expand Up @@ -481,12 +481,15 @@ function bezierpath_pad_scale_factor(atlas::TextureAtlas, bp)
full_pixel_size_in_atlas = uv_width * Vec2f(size(atlas))
# left + right pad - cutoff from pixel centering
full_pad = 2f0 * atlas.glyph_padding - 1
return full_pad ./ (full_pixel_size_in_atlas .- full_pad)
# size without padding
unpadded_pixel_size = full_pixel_size_in_atlas .- full_pad
# See offset_bezierpath
return full_pixel_size_in_atlas ./ maximum(unpadded_pixel_size)
end

function marker_scale_factor(atlas::TextureAtlas, path::BezierPath)
# padded_width = (unpadded_target_width + unpadded_target_width * pad_per_unit)
return (1f0 .+ bezierpath_pad_scale_factor(atlas, path)) .* widths(Makie.bbox(path))
# See offset_bezierpath
return bezierpath_pad_scale_factor(atlas, path) * maximum(widths(bbox(path)))
end

function rescale_marker(atlas::TextureAtlas, pathmarker::BezierPath, font, markersize)
Expand All @@ -510,9 +513,28 @@ function rescale_marker(atlas::TextureAtlas, char::Char, font, markersize)
end

function offset_bezierpath(atlas::TextureAtlas, bp::BezierPath, markersize::Vec2, markeroffset::Vec2)
# - wh = widths(bbox(bp)) is the untouched size of the given bezierpath
# - full_pixel_size_in_atlas is the size of the signed distance field in the
# texture atlas. This includes glyph padding
# - px_size is the size of signed distance field without padding
# To correct scaling on glow, stroke and AA widths in GLMakie we need to
# keep the aspect ratio of the aspect ratio (somewhat) correct when
# generating the sdf. This results in direct proportionality only for the
# longer dimension of wh and px_size. The shorter side becomes inaccurate
# due to integer rounding issues.
# 1. To calculate the width we can use the ratio of the proportional sides
# scale = maximum(wh) / maximum(px_size)
# to scale the padded_size we need to display
# scale * full_pixel_size_in_atlas
# (Part of this is moved to bezierpath_pad_scale_factor)
# 2. To calculate the offset we can simple move to the center of the bezier
# path and consider that the center of the final marker. (From the center
# scaling should be equal in ±x and ±y direction respectively.)

bb = bbox(bp)
pad_offset = origin(bb) .- 0.5f0 .* bezierpath_pad_scale_factor(atlas, bp) .* widths(bb)
return markersize .* pad_offset
scaled_size = bezierpath_pad_scale_factor(atlas, bp) * maximum(widths(bb))
return markersize * (origin(bb) .+ 0.5f0 * widths(bb) .- 0.5f0 .* scaled_size)

end

function offset_bezierpath(atlas::TextureAtlas, bp, scale, offset)
Expand Down
Loading