Skip to content

Commit 1fa6b3c

Browse files
authored
Merge pull request #3 from s-ccs/fix
fix
2 parents 8f8f0a6 + 22a0917 commit 1fa6b3c

File tree

7 files changed

+223
-188
lines changed

7 files changed

+223
-188
lines changed

.github/workflows/Copier.yml_deactivate

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Copier Update
22

33
on:
44
schedule:
5-
- cron: 0 7 1/7 * * # Every 7 days at 7:00 UTC
5+
- cron: 0 7 1/7 * * # Every 7 days at 7:00 UTC
66
workflow_dispatch:
77

88
jobs:
@@ -24,7 +24,7 @@ jobs:
2424
- name: Setup Python
2525
uses: actions/setup-python@v5
2626
with:
27-
cache: 'pip'
27+
cache: "pip"
2828
python-version: "3.11"
2929
- name: Hack for setup-python cache # https://github.com/actions/setup-python/issues/807
3030
run: rm requirements.txt

src/fontOnCurve.jl

Lines changed: 142 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,155 @@
11
"""
22
Code written mostly by Julius Krumbiegel with some modifications by Benedikt Ehinger
33
"""
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+
1244
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]
2593
else
26-
return Makie.attribute_per_char(string,attr)
27-
94+
return attr[seg]
2895
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)
4496
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
63119
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
85131
seg += 1
86-
continue
87132
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
119133
end
120134
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
123152
end
124-
125-
153+
#@show seg, size(glyphinfos)
154+
return Makie.GlyphCollection(glyphinfos)
155+
end

src/importpdf.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
function getPDFText(src; pages=nothing)
1+
function getPDFText(src; pages = nothing)
22
# handle that can be used for subsequence operations on the document.
33
doc = pdDocOpen(src)
44

5-
# Metadata extracted from the PDF document.
6-
# This value is retained and returned as the return from the function.
5+
# Metadata extracted from the PDF document.
6+
# This value is retained and returned as the return from the function.
77
docinfo = pdDocGetInfo(doc)
88
io = IOBuffer()
99

10-
# Returns number of pages in ithe document
10+
# Returns number of pages in ithe document
1111
#
1212
if isnothing(pages)
1313

1414
pages = 1:pdDocGetPageCount(doc)
1515
end
16-
for i = pages
16+
for i in pages
1717
@info "processing page $i"
1818
try
19-
# handle to the specific page given the number index.
19+
# handle to the specific page given the number index.
2020
page = pdDocGetPage(doc, i)
2121

2222
# Extract text from the page and write it to the output file.
@@ -26,7 +26,7 @@ function getPDFText(src; pages=nothing)
2626
end
2727
end
2828

29-
# Close the document handle.
29+
# Close the document handle.
3030
# The doc handle should not be used after this call
3131
pdDocClose(doc)
3232
return io

0 commit comments

Comments
 (0)