def layout_bounding_boxes(canvas_x, canvas_y, canvas_width, line_height,
                          space_widths, y_space, sizes):
  """Layout token bounding boxes on canvas with greedy wrapping.

  Args:
    canvas_x: x coordinate of top left of canvas
    canvas_y: y coordinate of top left of canvas
    canvas_width: width of canvas
    line_height: height for each line
    space_widths: width for space between each word
    y_space: extra space between lines
    sizes: list of width,height tuples of sizes for each word

  Returns:
    boxes: 4-tuple bounding box for each word
    line_breaks: token index for each line break
    line_poss: x,y positions starting each line
  """

  cur_x, cur_y = canvas_x, canvas_y
  cur_size = 0
  cur_line = 0
  boxes = []
  line_breaks = []
  line_poss = []
  line_poss.append((cur_x, cur_y))
  while cur_size < len(sizes):
    sz = sizes[cur_size]
    if cur_x + sz[0] > canvas_width + canvas_x:
      cur_line += 1
      cur_y = canvas_y - cur_line * (y_space + line_height)
      cur_x = canvas_x
      line_poss.append((cur_x, cur_y))
      line_breaks.append(cur_size)
    else:
      boxes.append((cur_x, cur_y, sz[0], sz[1]))
      cur_x += sz[0]
      if cur_size < len(space_widths):
        cur_x += space_widths[cur_size]
      cur_size += 1
  return boxes, line_breaks, line_poss