···7171 let add_text text =
7272 let trimmed = String.trim text in
7373 if trimmed <> "" then begin
7474- (* Add space if needed and last char isn't already whitespace *)
7575- if !need_space then begin
7474+ (* Check if text starts with punctuation that shouldn't have space before it *)
7575+ let starts_with_punctuation =
7676+ String.length trimmed > 0 &&
7777+ (match trimmed.[0] with
7878+ | ',' | '.' | ';' | ':' | '!' | '?' | ')' | ']' | '}' -> true
7979+ | _ -> false)
8080+ in
8181+8282+ (* Add space if needed, unless we're before punctuation *)
8383+ if !need_space && not starts_with_punctuation then begin
7684 match last_char () with
7785 | Some (' ' | '\n') -> ()
7886 | _ -> Buffer.add_char buffer ' '