<commit_before>#lang racket/base
(require racket/class
         racket/file
         racket/path
         racket/math
         "syntax.rkt"
         racket/gui/dynamic
         ffi/unsafe
         ffi/unsafe/alloc
         "../unsafe/cairo.ss"
         "../unsafe/bstr.ss"
	 "dc.ss"
         "font.ss"
         "local.ss"
         "ps-setup.ss")

(provide post-script-dc%
         pdf-dc%)

(define (make-dc-backend pdf?)
  (class default-dc-backend%
    (init [interactive #t]
          [parent #f]
          [use-paper-bbox #f]
          [as-eps #t])

    (define-values (s port-box width height landscape?)
      (let ([su (if interactive
                    ((gui-dynamic-require 'get-ps-setup-from-user) #f parent)
                    (current-ps-setup))])
        (cond
         [su 
          (unless (eq? su (current-ps-setup))
            (send (current-ps-setup) copy-from su))
          (let* ([pss (current-ps-setup)]
                 [to-file? (eq? (send pss get-mode) 'file)]
                 [get-file (lambda (fn)
                             ((gui-dynamic-require 'put-file)
                              (if pdf?
                                  "Save PDF As"
                                  "Save PostScript As")
                              parent
                              (and fn (path-only fn))
                              (and fn (file-name-from-path fn))
                              (if pdf? "pdf" "ps")))]
                 [fn (if to-file?
                         (if interactive
                             (get-file (send pss get-file))
                             (let ([fn (send pss get-file)])
                               (or fn (get-file #f))))
                         #f)])
            (if (and to-file?
                     (not fn))
                (values #f #f #f #f #f)
                (let* ([paper (assoc (send pss get-paper-name) paper-sizes)]
                       [w (cadr paper)]
                       [h (caddr paper)]
                       [landscape? (eq? (send pss get-orientation) 'landscape)]
                       [file (open-output-file
                              (or fn (make-temporary-file (if pdf?
                                                              "draw~a.pdf"
                                                              "draw~a.ps")))
                              #:exists 'truncate/replace)]
                       [port-box (make-immobile file)])
                  (let-values ([(w h) (if (and pdf? landscape?)
                                          (values h w)
                                          (values w h))])
                    (values
                     ((if pdf?
                          cairo_pdf_surface_create_for_stream 
                          cairo_ps_surface_create_for_stream)
                      write_port_bytes
                      port-box
                      w
                      h)
                     port-box ; needs to be accessible as long as `s'
                     w
                     h
                     landscape?)))))]
         [else
          (values #f #f #f #f)])))

    (define-values (margin-x margin-y)
      (let ([xb (box 0)] [yb (box 0.0)])
        (send (current-ps-setup) get-margin xb yb)
        (values (unbox xb) (unbox yb))))
    (define-values (scale-x scale-y)
      (let ([xb (box 0)] [yb (box 0.0)])
        (send (current-ps-setup) get-scaling xb yb)
        (values (unbox xb) (unbox yb))))
    (define-values (trans-x trans-y)
      (let ([xb (box 0)] [yb (box 0.0)])
        (send (current-ps-setup) get-translation xb yb)
        (values (unbox xb) (unbox yb))))
<commit_msg>fix interactive cancel in post-script-dc%<commit_after>#lang racket/base
(require racket/class
         racket/file
         racket/path
         racket/math
         "syntax.rkt"
         racket/gui/dynamic
         ffi/unsafe
         ffi/unsafe/alloc
         "../unsafe/cairo.ss"
         "../unsafe/bstr.ss"
	 "dc.ss"
         "font.ss"
         "local.ss"
         "ps-setup.ss")

(provide post-script-dc%
         pdf-dc%)

(define (make-dc-backend pdf?)
  (class default-dc-backend%
    (init [interactive #t]
          [parent #f]
          [use-paper-bbox #f]
          [as-eps #t])

    (define-values (s port-box width height landscape?)
      (let ([su (if interactive
                    ((gui-dynamic-require 'get-ps-setup-from-user) #f parent)
                    (current-ps-setup))])
        (cond
         [su 
          (unless (eq? su (current-ps-setup))
            (send (current-ps-setup) copy-from su))
          (let* ([pss (current-ps-setup)]
                 [to-file? (eq? (send pss get-mode) 'file)]
                 [get-file (lambda (fn)
                             ((gui-dynamic-require 'put-file)
                              (if pdf?
                                  "Save PDF As"
                                  "Save PostScript As")
                              parent
                              (and fn (path-only fn))
                              (and fn (file-name-from-path fn))
                              (if pdf? "pdf" "ps")))]
                 [fn (if to-file?
                         (if interactive
                             (get-file (send pss get-file))
                             (let ([fn (send pss get-file)])
                               (or fn (get-file #f))))
                         #f)])
            (if (and to-file?
                     (not fn))
                (values #f #f #f #f #f)
                (let* ([paper (assoc (send pss get-paper-name) paper-sizes)]
                       [w (cadr paper)]
                       [h (caddr paper)]
                       [landscape? (eq? (send pss get-orientation) 'landscape)]
                       [file (open-output-file
                              (or fn (make-temporary-file (if pdf?
                                                              "draw~a.pdf"
                                                              "draw~a.ps")))
                              #:exists 'truncate/replace)]
                       [port-box (make-immobile file)])
                  (let-values ([(w h) (if (and pdf? landscape?)
                                          (values h w)
                                          (values w h))])
                    (values
                     ((if pdf?
                          cairo_pdf_surface_create_for_stream 
                          cairo_ps_surface_create_for_stream)
                      write_port_bytes
                      port-box
                      w
                      h)
                     port-box ; needs to be accessible as long as `s'
                     w
                     h
                     landscape?)))))]
         [else
          (values #f #f #f #f #f)])))

    (define-values (margin-x margin-y)
      (let ([xb (box 0)] [yb (box 0.0)])
        (send (current-ps-setup) get-margin xb yb)
        (values (unbox xb) (unbox yb))))
    (define-values (scale-x scale-y)
      (let ([xb (box 0)] [yb (box 0.0)])
        (send (current-ps-setup) get-scaling xb yb)
        (values (unbox xb) (unbox yb))))
    (define-values (trans-x trans-y)
      (let ([xb (box 0)] [yb (box 0.0)])
        (send (current-ps-setup) get-translation xb yb)
        (values (unbox xb) (unbox yb))))

{'fraction_non_alphanumeric': 0.1210390316877465, 'fraction_numerical': 0.0024479804161566705, 'mean_word_length': 0.942419440042261, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7383597', 'n_tokens_mistral': 2112, 'n_tokens_neox': 2005, 'n_words': 519}