+ if c.type is TYPE_TEXT
+ # click is within bounding box that contains all text.
+ return [c, 0] if c.text.length is 0
+ before_i = 0
+ before = cursor_to_xyh c, before_i
+ after_i = c.text.length
+ after = cursor_to_xyh c, after_i
+ if loc.y < before.y + before.h and loc.x < before.x
+ # console.log 'before first char on first line'
+ continue
+ if loc.y > after.y and loc.x > after.x
+ # console.log 'after last char on last line'
+ continue
+ if loc.y < before.y
+ console.log "Warning: click in bounding box but above first line"
+ continue # above first line (runaround?)
+ if loc.y > after.y + after.h
+ console.log "Warning: click in bounding box but below last line", loc.y, after.y, after.h
+ continue # below last line (shouldn't happen?)
+ while after_i - before_i > 1
+ cur_i = Math.round((before_i + after_i) / 2)
+ cur = cursor_to_xyh c, cur_i
+ if loc.y < cur.y or (loc.y <= cur.y + cur.h and loc.x < cur.x)
+ after_i = cur_i
+ after = cur
+ else
+ before_i = cur_i
+ before = cur
+ # which one is closest?
+ if Math.abs(before.x - loc.x) < Math.abs(after.x - loc.x)
+ return [c, before_i]
+ else
+ return [c, after_i]