|
1 | 1 | """ |
2 | 2 | Code written mostly by Julius Krumbiegel with some modifications by Benedikt Ehinger |
3 | 3 | """ |
4 | | - struct TextOnPath |
5 | | - text::String |
6 | | - path::Makie.BezierPath |
7 | | - end |
8 | | - |
9 | | - function Makie._get_glyphcollection_and_linesegments(top::TextOnPath, index, ts, f, fs, al, rot, jus, lh, col, scol, swi, www, offs) |
10 | | - gc = Makie.layout_text(top, ts, f, fs, al, rot, jus, lh, col, scol, swi) |
11 | | - gc, Point2f[], Float32[], RGBAf[], Int[] |
| 4 | +struct TextOnPath |
| 5 | + text::String |
| 6 | + path::Makie.BezierPath |
| 7 | +end |
| 8 | + |
| 9 | +function Makie._get_glyphcollection_and_linesegments( |
| 10 | + top::TextOnPath, |
| 11 | + index, |
| 12 | + ts, |
| 13 | + f, |
| 14 | + fs, |
| 15 | + al, |
| 16 | + rot, |
| 17 | + jus, |
| 18 | + lh, |
| 19 | + col, |
| 20 | + scol, |
| 21 | + swi, |
| 22 | + www, |
| 23 | + offs, |
| 24 | +) |
| 25 | + gc = Makie.layout_text(top, ts, f, fs, al, rot, jus, lh, col, scol, swi) |
| 26 | + gc, Point2f[], Float32[], RGBAf[], Int[] |
| 27 | +end |
| 28 | + |
| 29 | +""" |
| 30 | +returns the attribute per character - if the length of the attribute does not equal to the length of the string, AND it equals to the number of points from the BezierPath - we will use the attribute per segment (until a MoveTo / NaN segment) |
| 31 | +
|
| 32 | +Note: currently only works for LineTo and MoveTo - not Curve, as Curve interpolates 30points per curve! |
| 33 | +""" |
| 34 | +attribute_per_char_or_segment(string, attr, lengthPoints) = |
| 35 | + Makie.attribute_per_char(string, attr) |
| 36 | +function attribute_per_char_or_segment(string, attr::AbstractVector, lengthPoints) |
| 37 | + |
| 38 | + if (length(attr) !== length(string)) & (length(attr) == lengthPoints) |
| 39 | + @assert all(isa.([MoveTo(1, 2), LineTo(1, 2)], Union{MoveTo,LineTo})) "Currently no support for BezierSegments other than MoveTo and LineTo" |
| 40 | + return attr |
| 41 | + else |
| 42 | + return Makie.attribute_per_char(string, attr) |
| 43 | + |
12 | 44 | end |
13 | | - |
14 | | - """ |
15 | | - returns the attribute per character - if the length of the attribute does not equal to the length of the string, AND it equals to the number of points from the BezierPath - we will use the attribute per segment (until a MoveTo / NaN segment) |
16 | | - |
17 | | - Note: currently only works for LineTo and MoveTo - not Curve, as Curve interpolates 30points per curve! |
18 | | - """ |
19 | | - attribute_per_char_or_segment(string,attr,lengthPoints) = Makie.attribute_per_char(string,attr) |
20 | | - function attribute_per_char_or_segment(string,attr::AbstractVector,lengthPoints) |
21 | | - |
22 | | - if (length(attr) !== length(string) ) & (length(attr) == lengthPoints) |
23 | | - @assert all(isa.([MoveTo(1,2),LineTo(1,2)],Union{MoveTo,LineTo})) "Currently no support for BezierSegments other than MoveTo and LineTo" |
24 | | - return attr |
| 45 | +end |
| 46 | +function Makie.layout_text(top::TextOnPath, ts, f, fs, al, rot, jus, lh, col, scol, swi) |
| 47 | + points = only(Makie.convert_arguments(PointBased(), top.path)) |
| 48 | + |
| 49 | + ft_font = Makie.to_font(f) |
| 50 | + rscale = Makie.to_fontsize(ts) |
| 51 | + |
| 52 | + |
| 53 | + colors = attribute_per_char_or_segment(top.text, col, length(points)) |
| 54 | + strokecolors = attribute_per_char_or_segment(top.text, scol, length(points)) |
| 55 | + strokewidths = attribute_per_char_or_segment(top.text, swi, length(points)) |
| 56 | + fonts = attribute_per_char_or_segment(top.text, ft_font, length(points)) |
| 57 | + fontsizes = attribute_per_char_or_segment(top.text, rscale, length(points)) |
| 58 | + |
| 59 | + gc = glyph_collection_on_path( |
| 60 | + points, |
| 61 | + top.text, |
| 62 | + fonts, |
| 63 | + fontsizes, |
| 64 | + colors, |
| 65 | + strokecolors, |
| 66 | + strokewidths, |
| 67 | + ) |
| 68 | +end |
| 69 | + |
| 70 | +function glyph_collection_on_path( |
| 71 | + points, |
| 72 | + text, |
| 73 | + fonts, |
| 74 | + fontsizes, |
| 75 | + colors, |
| 76 | + strokecolors, |
| 77 | + strokewidths, |
| 78 | +) |
| 79 | + glyphinfos = Makie.GlyphInfo[] |
| 80 | + |
| 81 | + seg = 1 |
| 82 | + at_fraction::Float64 = 0.0 |
| 83 | + |
| 84 | + |
| 85 | + get_attr(attr::Base.Generator, ix_char, seg) = collect(attr)[1] |
| 86 | + get_attr(attr, ix_char, seg) = attr |
| 87 | + |
| 88 | + |
| 89 | + function get_attr(attr::AbstractVector, ix_char, seg) |
| 90 | + |
| 91 | + if length(attr) == length(text) |
| 92 | + return attr[ix_char] |
25 | 93 | else |
26 | | - return Makie.attribute_per_char(string,attr) |
27 | | - |
| 94 | + return attr[seg] |
28 | 95 | end |
29 | | - end |
30 | | - function Makie.layout_text(top::TextOnPath, ts, f, fs, al, rot, jus, lh, col, scol, swi) |
31 | | - points = only(Makie.convert_arguments(PointBased(), top.path)) |
32 | | - |
33 | | - ft_font = Makie.to_font(f) |
34 | | - rscale = Makie.to_fontsize(ts) |
35 | | - |
36 | | - |
37 | | - colors = attribute_per_char_or_segment(top.text,col,length(points)) |
38 | | - strokecolors = attribute_per_char_or_segment(top.text,scol,length(points)) |
39 | | - strokewidths = attribute_per_char_or_segment(top.text,swi,length(points)) |
40 | | - fonts = attribute_per_char_or_segment(top.text, ft_font,length(points)) |
41 | | - fontsizes = attribute_per_char_or_segment(top.text, rscale,length(points)) |
42 | | - |
43 | | - gc = glyph_collection_on_path(points, top.text, fonts, fontsizes,colors,strokecolors,strokewidths) |
44 | 96 | end |
45 | | - |
46 | | - function glyph_collection_on_path(points, text, fonts, fontsizes,colors,strokecolors,strokewidths) |
47 | | - glyphinfos = Makie.GlyphInfo[] |
48 | | - |
49 | | - seg = 1 |
50 | | - at_fraction::Float64 = 0.0 |
51 | | - |
52 | | - |
53 | | - get_attr(attr::Base.Generator,ix_char,seg) = collect(attr)[1] |
54 | | - get_attr(attr,ix_char,seg) = attr |
55 | | - |
56 | | - |
57 | | - function get_attr(attr::AbstractVector,ix_char,seg) |
58 | | - |
59 | | - if length(attr) == length(text) |
60 | | - return attr[ix_char] |
61 | | - else |
62 | | - return attr[seg] |
| 97 | + for (ix_char, char) in enumerate(collect(text)) |
| 98 | + |
| 99 | + font = get_attr(fonts, ix_char, seg) |
| 100 | + fontsize = get_attr(fontsizes, ix_char, seg) |
| 101 | + strokecolor = get_attr(strokecolors, ix_char, seg) |
| 102 | + strokewidth = get_attr(strokewidths, ix_char, seg) |
| 103 | + color = get_attr(colors, ix_char, seg) |
| 104 | + |
| 105 | + v = points[seg+1] - points[seg] |
| 106 | + p = points[seg] + v * at_fraction |
| 107 | + |
| 108 | + ext = Makie.GlyphExtent(font, char) |
| 109 | + sz = Vec2f(fontsize, fontsize) |
| 110 | + |
| 111 | + hadvance = ext.hadvance * sz[1] |
| 112 | + |
| 113 | + # find next point |
| 114 | + accumulated = 0.0 |
| 115 | + while true |
| 116 | + if isnan(points[seg+1]) | isnan(points[seg]) |
| 117 | + seg += 1 |
| 118 | + continue |
63 | 119 | end |
64 | | - end |
65 | | - for (ix_char,char) in enumerate(collect(text)) |
66 | | - |
67 | | - font = get_attr(fonts,ix_char,seg) |
68 | | - fontsize = get_attr(fontsizes,ix_char,seg) |
69 | | - strokecolor = get_attr(strokecolors,ix_char,seg) |
70 | | - strokewidth = get_attr(strokewidths,ix_char,seg) |
71 | | - color = get_attr(colors,ix_char,seg) |
72 | | - |
73 | | - v = points[seg+1] - points[seg] |
74 | | - p = points[seg] + v * at_fraction |
75 | | - |
76 | | - ext = Makie.GlyphExtent(font, char) |
77 | | - sz = Vec2f(fontsize, fontsize) |
78 | | - |
79 | | - hadvance = ext.hadvance * sz[1] |
80 | | - |
81 | | - # find next point |
82 | | - accumulated = 0.0 |
83 | | - while true |
84 | | - if isnan(points[seg+1]) | isnan(points[seg]) |
| 120 | + |
| 121 | + seglength = Makie.norm(points[seg+1] - points[seg]) |
| 122 | + remaining_this_seg = (1 - at_fraction) * seglength |
| 123 | + to_go = hadvance - (accumulated + remaining_this_seg) |
| 124 | + if to_go <= 0 |
| 125 | + at_fraction = 1 - (-to_go / seglength) |
| 126 | + break |
| 127 | + else |
| 128 | + accumulated += remaining_this_seg |
| 129 | + at_fraction = 0.0 |
| 130 | + if seg < length(points) - 1 |
85 | 131 | seg += 1 |
86 | | - continue |
87 | 132 | end |
88 | | - |
89 | | - seglength = Makie.norm(points[seg+1] - points[seg]) |
90 | | - remaining_this_seg = (1 - at_fraction) * seglength |
91 | | - to_go = hadvance - (accumulated + remaining_this_seg) |
92 | | - if to_go <= 0 |
93 | | - at_fraction = 1 - (-to_go / seglength) |
94 | | - break |
95 | | - else |
96 | | - accumulated += remaining_this_seg |
97 | | - at_fraction = 0.0 |
98 | | - if seg < length(points)-1 |
99 | | - seg += 1 |
100 | | - end |
101 | | - end |
102 | | - end |
103 | | - |
104 | | - gi = Makie.GlyphInfo( |
105 | | - Makie.FreeTypeAbstraction.glyph_index(font, char), |
106 | | - font, |
107 | | - p, |
108 | | - ext, |
109 | | - sz, |
110 | | - Makie.to_rotation(reverse(v) .* (-1, 1)), |
111 | | - color, |
112 | | - strokecolor, |
113 | | - strokewidth, |
114 | | - ) |
115 | | - push!(glyphinfos, gi) |
116 | | - if seg >= length(points)-1 |
117 | | - @warn "Could not fit all characters on path, you could reduce the fontsize" |
118 | | - break |
119 | 133 | end |
120 | 134 | end |
121 | | - #@show seg, size(glyphinfos) |
122 | | - return Makie.GlyphCollection(glyphinfos) |
| 135 | + |
| 136 | + gi = Makie.GlyphInfo( |
| 137 | + Makie.FreeTypeAbstraction.glyph_index(font, char), |
| 138 | + font, |
| 139 | + p, |
| 140 | + ext, |
| 141 | + sz, |
| 142 | + Makie.to_rotation(reverse(v) .* (-1, 1)), |
| 143 | + color, |
| 144 | + strokecolor, |
| 145 | + strokewidth, |
| 146 | + ) |
| 147 | + push!(glyphinfos, gi) |
| 148 | + if seg >= length(points) - 1 |
| 149 | + @warn "Could not fit all characters on path, you could reduce the fontsize" |
| 150 | + break |
| 151 | + end |
123 | 152 | end |
124 | | - |
125 | | - |
| 153 | + #@show seg, size(glyphinfos) |
| 154 | + return Makie.GlyphCollection(glyphinfos) |
| 155 | +end |
0 commit comments