Skip to content
Andre R edited this page Mar 2, 2018 · 1 revision

Skeleton

To create a PDF in your project directory:

(defn mm-to-px
  [mm]
  (/ (* mm 72) 25.4))
(def a4-w (mm-to-px 210))
(def a4-h (mm-to-px 297))

(q/sketch
  :draw (fn []
          (q/do-record (q/create-graphics a4-w a4-h :pdf "out.pdf")
                       (draw!))
          (q/exit)))

Where draw! is your drawing function. Here you'll create an A4 page (210mm x 297mm).

Note: Use Evince (Linux) to view the PDF, it will automatically refresh the PDF as soon as you re-eval the namespace in your REPL.

Use fonts

By default, processing will render any text as "Shapes", i.e. your text will not be rendered as true "text" which could be selected. Option: If you want to change it use:

(q/text-mode :model)

This should be the very first command in your draw! function or you'll get errors. This command will also embed your used fonts in the PDF (so other people will see the PDF the same).

Example:

  (q/text-mode :model)
  (q/fill 0 0 0)
  (q/text-font (q/create-font "Roboto Light" 32))
  (q/text-align :left :top)
  (q/text "Hello there" 10 10)

To see which fonts are available on your system:

(let [xs (q/available-fonts)]
  (filter #(re-find #"(?i)" %) xs))

Change the regex to "search" for fonts.

Custom fonts

Let's include Font Awesome (FA) and use it.

  1. On the FA landing page, click "Download Free".
  2. In your project root dir create a directory data -- mkdir data.
  3. In the zip file go to advanced-options/metadata and extract the file icons.json to your data directory
  4. In the zip file go to web-fonts-with-css/webfonts
  5. Extract all files in that directory to your data dir.

Now you can use it:

  (q/text-mode :shape)
  ;(q/text-font (q/create-font "fa-solid-900.ttf" 20))
  (q/text-font (q/create-font "fa-regular-400.ttf" 20))
  (q/text-align :left :top)
  (q/text (str \uf2bb) 50 400) ;; Address card
  (q/text-mode :model) ;; reset back to :model if you wanted.

This is the basics, you can of course make this more robust/flexible with

  • Reading the JSON file we extracted an then map from a name (like address-card) to the unicode (your homework)
  • Read out what the current text-mode is and reset it back to the previous one. (.textMode (q/current-graphics)) will give you the current selected text-mode.