Repository: commaai/openpilot
File: tools/clip/run.py (Python)

Code before:
    'branch': init_data.gitBranch, 'commit': init_data.gitCommit[:7], 'modified': str(init_data.dirty).lower(),
  }


def draw_text_box(rl, text, x, y, size, gui_app, font, font_scale, color=None, center=False):
  box_color, text_color = rl.Color(0, 0, 0, 85), color or rl.WHITE
  # measure_text_ex is NOT auto-scaled, so multiply by font_scale
  # draw_text_ex IS auto-scaled, so pass size directly
  text_size = rl.measure_text_ex(font, text, size * font_scale, 0)
  text_width, text_height = int(text_size.x), int(text_size.y)
  if center:
    x = (gui_app.width - text_width) // 2
  rl.draw_rectangle(x - 8, y - 4, text_width + 16, text_height + 8, box_color)
  rl.draw_text_ex(font, text, rl.Vector2(x, y), size, 0, text_color)


def render_overlays(rl, gui_app, font, font_scale, big, metadata, title, start_time, frame_idx, show_metadata, show_time):
  metadata_size = 16 if big else 12
  title_size = 32 if big else 24
  time_size = 24 if big else 16

  # Time overlay
  time_width = 0
  if show_time:
    t = start_time + frame_idx / FRAMERATE
    time_text = f"{int(t) // 60:02d}:{int(t) % 60:02d}"
    time_width = int(rl.measure_text_ex(font, time_text, time_size * font_scale, 0).x)
    draw_text_box(rl, time_text, gui_app.width - time_width - 5, 0, time_size, gui_app, font, font_scale)

  # Metadata overlay (first 5 seconds)
  if show_metadata and metadata and frame_idx < FRAMERATE * 5:
    m = metadata
    text = ", ".join([f"openpilot v{m['version']}", f"route: {m['route']}", f"car: {m['car']}", f"origin: {m['origin']}",
                      f"branch: {m['branch']}", f"commit: {m['commit']}", f"modified: {m['modified']}"])
    # Wrap text if too wide (leave margin on each side)
    margin = 2 * (time_width + 10 if show_time else 20)  # leave enough margin for time overlay
    max_width = gui_app.width - margin
    lines = wrap_text(font, text, metadata_size, max_width)
    lines = [line.rstrip(',') for line in lines]

    # Draw wrapped metadata text
    y_offset = 6
    for line in lines:
      draw_text_box(rl, line, 0, y_offset, metadata_size, gui_app, font, font_scale, center=True)
      line_height = int(rl.measure_text_ex(font, line, metadata_size * font_scale, 0).y) + 4
      y_offset += line_height

  # Title overlay
  if title:
    draw_text_box(rl, title, 0, 60, title_size, gui_app, font, font_scale, center=True)


Review comment (bug):
@TheSecurityDev we have helpers that handle font_scale for you, so you don't have to think about it. we have a ban on `rl.measure_text_ex` for this reason, not sure why it didn't fail

Code after:
  }


def draw_text_box(text, x, y, size, gui_app, font, color=None, center=False):
  import pyray as rl
  from openpilot.system.ui.lib.text_measure import measure_text_cached
  box_color, text_color = rl.Color(0, 0, 0, 85), color or rl.WHITE
  text_size = measure_text_cached(font, text, size)
  text_width, text_height = int(text_size.x), int(text_size.y)
  if center:
    x = (gui_app.width - text_width) // 2
  rl.draw_rectangle(x - 8, y - 4, text_width + 16, text_height + 8, box_color)
  rl.draw_text_ex(font, text, rl.Vector2(x, y), size, 0, text_color)


def render_overlays(gui_app, font, big, metadata, title, start_time, frame_idx, show_metadata, show_time):
  from openpilot.system.ui.lib.text_measure import measure_text_cached
  from openpilot.system.ui.lib.wrap_text import wrap_text
  metadata_size = 16 if big else 12
  title_size = 32 if big else 24
  time_size = 24 if big else 16

  # Time overlay
  time_width = 0
  if show_time:
    t = start_time + frame_idx / FRAMERATE
    time_text = f"{int(t) // 60:02d}:{int(t) % 60:02d}"
    time_width = int(measure_text_cached(font, time_text, time_size).x)
    draw_text_box(time_text, gui_app.width - time_width - 5, 0, time_size, gui_app, font)

  # Metadata overlay (first 5 seconds)
  if show_metadata and metadata and frame_idx < FRAMERATE * 5:
    m = metadata
    text = ", ".join([f"openpilot v{m['version']}", f"route: {m['route']}", f"car: {m['car']}", f"origin: {m['origin']}",
                      f"branch: {m['branch']}", f"commit: {m['commit']}", f"modified: {m['modified']}"])
    # Wrap text if too wide (leave margin on each side)
    margin = 2 * (time_width + 10 if show_time else 20)  # leave enough margin for time overlay
    max_width = gui_app.width - margin
    lines = wrap_text(font, text, metadata_size, max_width)

    # Draw wrapped metadata text
    y_offset = 6
    for line in lines:
      draw_text_box(line, 0, y_offset, metadata_size, gui_app, font, center=True)
      line_height = int(measure_text_cached(font, line, metadata_size).y) + 4
      y_offset += line_height

  # Title overlay
  if title:
    draw_text_box(title, 0, 60, title_size, gui_app, font, center=True)