diff --git a/docs/Project.toml b/docs/Project.toml index 3b20544..56b4933 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,5 +1,7 @@ [deps] +CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" +HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3" LiveServer = "16fef848-5104-11e9-1b77-fb7a48bbb589" ThesisArt = "6184abd9-1539-47ca-b742-6ec6f8d46641" diff --git a/docs/src/01-example.md b/docs/src/01-example.md index ee376b7..0ad2a20 100644 --- a/docs/src/01-example.md +++ b/docs/src/01-example.md @@ -2,16 +2,16 @@ ## Create a Figure -by default the Figure is created to be A3 format 420x297 px +by default the Figure is created to be A2 format 420 x 594 "px" -```@example +```@example main using ThesisArt using CairoMakie nothing #hide ``` -```@example +```@example main backgroundcolor = "#333" f,ax_main = ThesisArt.newfigure(;backgroundcolor) @@ -22,7 +22,7 @@ nothing #hide I cant share the pdf right now, so we'll use a dummy text -```@example +```@example main # t_text = import_pdf("file.pdf",pages=[2 4:24...]); t_text = text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." nothing #hide @@ -30,8 +30,9 @@ nothing #hide ## Create the title -```@example - (h_title,h_nameyear),ax_title = add_title!(f,"My Awesome Thesis \n Title","Benedikt Ehinger","August 2024") +```@example main + (h_title,h_nameyear),ax_title = add_title!(f,"My Awesome \n Thesis-Title","Benedikt Ehinger","August 2024") + h_title.color= :white nothing #hide ``` @@ -39,21 +40,35 @@ nothing #hide For this example we'll use a simple BezierPath / Svg String -```@example +```@example main -path_bat = CairoMakie.BezierPath("M96.84 141.998c-4.947-23.457-20.359-32.211-25.862-13.887-11.822-22.963-37.961-16.135-22.041 6.289-3.005-1.295-5.872-2.682-8.538-4.191-8.646-5.318-15.259-11.314-19.774-17.586-3.237-5.07-4.994-10.541-4.994-16.229 0-19.774 21.115-36.758 50.861-43.694.446-.078.909-.154 1.372-.231-22.657 30.039 9.386 50.985 15.258 24.645l2.528-24.367 5.086 6.52H103.205l5.07-6.52 2.543 24.367c5.842 26.278 37.746 5.502 15.414-24.429 29.777 6.951 50.891 23.936 50.891 43.709 0 15.136-12.406 28.651-31.609 37.267 14.842-21.822-10.867-28.266-22.549-5.549-5.502-18.325-21.147-9.341-26.125 13.886") +path_bat = CairoMakie.BezierPath("M96.84 141.998c-4.947-23.457-20.359-32.211-25.862-13.887-11.822-22.963-37.961-16.135-22.041 6.289-3.005-1.295-5.872-2.682-8.538-4.191-8.646-5.318-15.259-11.314-19.774-17.586-3.237-5.07-4.994-10.541-4.994-16.229 0-19.774 21.115-36.758 50.861-43.694.446-.078.909-.154 1.372-.231-22.657 30.039 9.386 50.985 15.258 24.645l2.528-24.367 5.086 6.52H103.205l5.07-6.52 2.543 24.367c5.842 26.278 37.746 5.502 15.414-24.429 29.777 6.951 50.891 23.936 50.891 43.709 0 15.136-12.406 28.651-31.609 37.267 14.842-21.822-10.867-28.266-22.549-5.549-5.502-18.325-21.147-9.341-26.125 13.886 M0 0") nothing #hide ``` ## Plot the text on path -```@example +```@example main textpath = ThesisArt.TextOnPath(t_text,path_bat) -text!(ax_main,0.0,0,text=textpath,fontcolor = :white,fontsize=10) +text!(ax_main,0,0,text=textpath,color = :white,fontsize=10) + +# and let's add some others to make it look cooler ;-) +text!(ax_main,-0.1,+0.1,text=textpath,color = :white,fontsize=10) +text!(ax_main,0.1,+0.1,text=textpath,color = :white,fontsize=10) +xlims!(ax_main,[-0.2,0.5]) +ylims!(ax_main,[-0.5,1]) ``` +Note: The text is repeated 3 times, not continued. Thus the words repeat as well. In the second tutorial, you'll see how we could combine paths/points. + ## The final thesis art -```@example +```@example main f ``` + +In case you want to print it use: + +```julia +save("thesisart.png",f,px_per_unit=15) +``` diff --git a/docs/src/02-textonpath.md b/docs/src/02-textonpath.md new file mode 100644 index 0000000..6805cc4 --- /dev/null +++ b/docs/src/02-textonpath.md @@ -0,0 +1,66 @@ +# [TextOnPath](@id init_example) + +## TextOnPath + +```@example main +using ThesisArt +using CairoMakie +using Random +nothing #hide +``` + +Let's define a (random) shape first + +```@example main +my_points = 400 .* (rand(MersenneTwister(1),Point2f,20).-Point2f(0.5,0.5)) +my_path = ThesisArt.points_to_bezierpath(my_points) +nothing #hide +``` + +And some random text + +```@example main +using HTTP +my_text = String(HTTP.get("http://loripsum.net/api/verylong/plaintext").body) +my_text = replace(my_text,"\n"=>"") +``` + +```@example main + +t = ThesisArt.TextOnPath(my_text,my_path) +CairoMakie.text(0,0;text=t) +``` + +## Color by glyph + +if a color per glyph is provided, use that color + +```@example main +c = CairoMakie.cgrad(:RdBu,500)|>collect|> x->x[1:500] +t = ThesisArt.TextOnPath(my_text[1:500],my_path) + +CairoMakie.text(0,0;text=t,color=c) +``` + +## Color by segment + +sometimes it is better to color by segment, where segments are divisions by `LineTo` and `MoveTo` in the path. + +First we show how to make a break in the `BezierPath` + +```@example main +my_points_split = Vector{Any}(deepcopy(my_points)) +my_points_split[end÷2] = NaN +my_path_split = ThesisArt.points_to_bezierpath(my_points_split) +``` + +See how we have a `MoveTo` in the path? We introduced it by adding a `NaN` into our pointlist + +Next, we introduce a color per segment + +```@example main +t = ThesisArt.TextOnPath(my_text,my_path_split) + +CairoMakie.text(0,0;text=t,color=repeat([:red,:blue],8+1),fontsize=5) + +``` diff --git a/src/ThesisArt.jl b/src/ThesisArt.jl index f1d7692..894ed6a 100644 --- a/src/ThesisArt.jl +++ b/src/ThesisArt.jl @@ -15,4 +15,5 @@ export import_pdf export get_rotation export rolling_inter2d export add_spacer!, add_title!, newfigure +export TextOnPath end diff --git a/src/fontOnCurve.jl b/src/fontOnCurve.jl index 92cc7a8..4f37303 100644 --- a/src/fontOnCurve.jl +++ b/src/fontOnCurve.jl @@ -36,7 +36,7 @@ Note: currently only works for LineTo and MoveTo - not Curve, as Curve interpola attribute_per_char_or_segment(string, attr, lengthPoints) = Makie.attribute_per_char(string, attr) function attribute_per_char_or_segment(string, attr::AbstractVector, lengthPoints) - + @debug lengthPoints, length(attr), length(string) if (length(attr) !== length(string)) & (length(attr) == lengthPoints) @assert all(isa.([MoveTo(1, 2), LineTo(1, 2)], Union{MoveTo,LineTo})) "Currently no support for BezierSegments other than MoveTo and LineTo" return attr diff --git a/src/interpolateCurve.jl b/src/interpolateCurve.jl index fabf597..91b8e7e 100644 --- a/src/interpolateCurve.jl +++ b/src/interpolateCurve.jl @@ -47,3 +47,16 @@ end function dist2(x, y) return sqrt((x[1] - x[2])^2 + (y[1] - y[2])^2) end + + + +#---- +function points_to_bezierpath(points) + _point_to_svg = + (prev, p, pnext) -> isnan(prev) ? nothing : isnan(p) ? MoveTo(pnext) : LineTo(p) + ix = length(points) + #points = vcat(NaN, NaN, points) + vec_lineto = _point_to_svg.(points[1:ix-2], points[2:ix-1], points[3:ix]) + vec_lineto = vec_lineto[.!isnothing.(vec_lineto)] + return BezierPath(vec_lineto) +end