[racket] FIXED Re: Setting font for drawing to dc%

From: Jordan Johnson (jmj at fellowhuman.com)
Date: Fri Dec 7 16:14:39 EST 2012

Hi Robby, Matthew,

Thanks for the replies.  Robby, that was exactly what I needed, since I hadn't thought to look at the text% hierarchy recently for style changes.  (Even knowing that, it took a while to work it out; owing to my not having a full understanding of what triggers a reload of files the checker depends on, I didn't get the version below to work until I manually restarted the server. But it's all good now.  Also of interest: I learned that even if I set the style correctly as below, if I subsequently set the font to fixed-width in the dc as I'd erroneously been doing, the text reverted to the non-fixed sans serif.  That was surprising!)

Here is my corrected, working code:

#lang s-exp handin-server/checker

(require racket/gui/base racket/class  ;; for PDF rendering
         racket/draw)


(define fixed-width
  (send (new style-delta%) set-delta 'change-family 'modern))

;; make-pdf : String text% -> any
;; Generates a PDF of the given Definitions window, saved in
;; /tmp/test-pdf-writing.pdf:
(define (make-pdf file defs)
  (define dc (new pdf-dc%
                  [interactive #f]
                  [output "/tmp/test-pdf-writing.pdf"]))
  (send defs select-all)
  (send defs change-style fixed-width)
  (send dc start-doc "")
  (send defs print-to-dc dc -1)
  (send dc end-doc))

(check: :language '(special beginner)
        :textualize? #t
        (define user-string
          (if (list? users) (apply string-append users) users))
        (define-values (defs ints) (unpack-submission submission))
        (make-pdf user-string defs))

;; end

I suppose there are two remaining questions: does freeze-colorer need to be called even if the text% in question is not displayed, and where is the latest it can be called in the sequence above?

Best,
Jordan

On Dec 7, 2012, at 4:21 AM, Robby Findler <robby at eecs.northwestern.edu> wrote:

> Code would help understand, but setting the font in the dc isn't the
> way to go: you want the styles to be right (which is what racket:text%
> does). And you'd need to call freeze-colorer to wait for the coloring
> to finish, too, but that would affect only the colors, not the fonts.
> 
> Robby
> 
> On Fri, Dec 7, 2012 at 5:56 AM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
>> I'm not sure. Can you provide a small program that illustrates the problem?
>> 
>> At Fri, 7 Dec 2012 00:26:47 -0800, Jordan Johnson wrote:
>>> Hi all,
>>> 
>>> I was delighted to find awhile back that it's pretty easy to obtain a PDF of
>>> syntax-colored code that looks more or less like what I see in my DrRacket
>>> window, by drawing the text% to a PDF-dc%.  I am now using that in conjunction
>>> with the handin server to grade and mark up student work on my iPad (annotating
>>> the PDFs via GoodReader and emailing them straight back to students), and it
>>> works beautifully. (I am happy to share the code for this with anyone who is
>>> interested; email me if so.)
>>> 
>>> One glitch: I can't get Racket to give me a fixed-width font in the PDF.  I
>>> have tried inserting
>>>  (send dc set-font (make-object font% 12 'modern))
>>> where dc is my pdf-dc%, and alternately
>>>  (send dc set-font (send the-font-list find-or-create-font 12 'modern 'normal
>>> 'normal))
>>> but in both cases the generated PDF is still in a sans-serif non-fixed-width
>>> font.  It does not seem to matter whether I put the above lines before or after
>>> the
>>>  (send dc start-doc "")
>>> that begins the drawing process.
>>> 
>>> Interestingly, in a test I ran just now, if I generate a PDF by directly
>>> creating a racket:text% object, putting some Racket program text in it, and
>>> calling the same make-PDF function I use in the handin-server (calling set-font
>>> using the-font-list, as described above), it gives me fixed-width text as I'd
>>> expect.  That's regardless of whether I set the font before or after the
>>> start-doc command. (racket:text% is just a guess at the text% subclass that the
>>> handin server is providing, based on it being a representation of the
>>> Definitions window.)
>>> 
>>> Any ideas how to solve this, or further tests I could run to determine the
>>> cause?
>>> 
>>> Thanks for your consideration,
>>> Jordan
>>> ____________________
>>>  Racket Users list:
>>>  http://lists.racket-lang.org/users
>> ____________________
>>  Racket Users list:
>>  http://lists.racket-lang.org/users


Posted on the users mailing list.