Skip to content

Commit baba136

Browse files
committed
line_fragments now handles 0-width ranges
- comes in handy when using self-closed tags to locate an x/y point within the Text flow
1 parent 553383c commit baba136

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

plotdevice/lib/foundry.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,18 @@ def trace_text(txt_obj, rng=None):
279279
def line_fragments(txt_obj, rng=None):
280280
"""Returns a list of dictionaries describing the line fragments in the entire Text object
281281
or a sub-range of it based on character indices"""
282+
flatten = None # flag ranges with a zero-width
283+
282284
if rng is None:
283285
rng = (0, len(txt_obj.text))
286+
elif rng[1]==0:
287+
# expand zero-width ranges to 1 char before measuring
288+
flatten = max;
289+
start = rng[0]
290+
if start == len(txt_obj.text)-1:
291+
start -= 1
292+
flatten = min
293+
rng = (start, 1)
284294

285295
lines = []
286296
for frag in Vandercook.lineFragmentsInRange_withLayout_(rng, txt_obj._engine):
@@ -299,13 +309,29 @@ def line_fragments(txt_obj, rng=None):
299309
info['baseline'] += offset
300310
info['used'].origin += offset
301311
info['bounds'].origin += offset
312+
313+
# re-contract ranges that were expanded from zero
314+
if flatten:
315+
loc = txt_range.location
316+
if flatten is min:
317+
info['baseline'].x += info['used'].width
318+
loc += 1
319+
info['text'] = u''
320+
info['used'].width = 0
321+
info['span'] = (loc, 0)
322+
302323
lines.append(LineFragment(**info))
303324

304325
return lines
305326

306327
def text_frames(txt_obj, rng=None):
307328
if rng is None:
308329
rng = (0, len(txt_obj.text))
330+
elif rng[1]==0:
331+
start = rng[0]
332+
if start == len(txt_obj.text)-1:
333+
start -= 1
334+
rng = (start, 1)
309335
containers = Vandercook.textContainersInRange_withLayout_(rng, txt_obj._engine)
310336
return [txt_obj._frames[i] for i in containers]
311337

0 commit comments

Comments
 (0)