Skip to content

Commit 9934b90

Browse files
committed
Add example code for library (lispkit draw).
1 parent 1bc148c commit 9934b90

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

LispKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
CC26263E20F5707800AC08E8 /* 112.sld in Copy pre-installed SRFI libraries */ = {isa = PBXBuildFile; fileRef = CC26263D20F56F9300AC08E8 /* 112.sld */; };
3737
CC26264020F8014900AC08E8 /* 113.sld in Copy pre-installed SRFI libraries */ = {isa = PBXBuildFile; fileRef = CC26263F20F7FB8100AC08E8 /* 113.sld */; };
3838
CC26264220F9491E00AC08E8 /* SRFI113.scm in Resources */ = {isa = PBXBuildFile; fileRef = CC26264120F9469100AC08E8 /* SRFI113.scm */; };
39+
CC26264420FA007800AC08E8 /* Plot.scm in Copy examples */ = {isa = PBXBuildFile; fileRef = CC26264320FA003700AC08E8 /* Plot.scm */; };
3940
CC2D124F1DB40E7F009BDC72 /* LibraryManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC2D124E1DB40E7F009BDC72 /* LibraryManager.swift */; };
4041
CC31A6981D3AC223008D7728 /* Hash.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC31A6971D3AC223008D7728 /* Hash.swift */; };
4142
CC35FD0D1C6FA4B700C8B992 /* VirtualMachine.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC35FD0C1C6FA4B700C8B992 /* VirtualMachine.swift */; };
@@ -267,6 +268,7 @@
267268
dstPath = LispKit/Resources/Examples;
268269
dstSubfolderSpec = 7;
269270
files = (
271+
CC26264420FA007800AC08E8 /* Plot.scm in Copy examples */,
270272
CC5E474320D45AE500F21B03 /* Coroutines.scm in Copy examples */,
271273
CC4385BE20BB5F3400055289 /* Compiler.scm in Copy examples */,
272274
CC4385BC20BABAA600055289 /* HTTP.scm in Copy examples */,
@@ -449,6 +451,7 @@
449451
CC26263D20F56F9300AC08E8 /* 112.sld */ = {isa = PBXFileReference; lastKnownFileType = text; path = 112.sld; sourceTree = "<group>"; };
450452
CC26263F20F7FB8100AC08E8 /* 113.sld */ = {isa = PBXFileReference; lastKnownFileType = text; path = 113.sld; sourceTree = "<group>"; };
451453
CC26264120F9469100AC08E8 /* SRFI113.scm */ = {isa = PBXFileReference; lastKnownFileType = text; path = SRFI113.scm; sourceTree = "<group>"; };
454+
CC26264320FA003700AC08E8 /* Plot.scm */ = {isa = PBXFileReference; lastKnownFileType = text; path = Plot.scm; sourceTree = "<group>"; };
452455
CC2D124E1DB40E7F009BDC72 /* LibraryManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LibraryManager.swift; sourceTree = "<group>"; };
453456
CC31A6971D3AC223008D7728 /* Hash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Hash.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
454457
CC35FD0C1C6FA4B700C8B992 /* VirtualMachine.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = VirtualMachine.swift; sourceTree = "<group>"; tabWidth = 2; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
@@ -1068,6 +1071,7 @@
10681071
CCBDB73A1EDB4AB3001606E2 /* Prolog.scm */,
10691072
CC4385BD20BB5EE200055289 /* Compiler.scm */,
10701073
CC14F4AE1F9385BF000FB1E0 /* AvlTrees.scm */,
1074+
CC26264320FA003700AC08E8 /* Plot.scm */,
10711075
CC750BD71F5AC68900CD82A2 /* PDF.scm */,
10721076
CC4385BB20BAB79500055289 /* HTTP.scm */,
10731077
);

Sources/LispKit/Compiler/EvalError.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public enum EvalError: Int, Hashable {
8383
case invalidPoint
8484
case invalidSize
8585
case invalidRect
86-
case unknownFont
8786
case cannotLoadImage
8887
case cannotCreateBitmap
8988
case invalidImageFileType
@@ -210,8 +209,6 @@ public enum EvalError: Int, Hashable {
210209
return "invalid size: $0"
211210
case .invalidRect:
212211
return "invalid rect: $0"
213-
case .unknownFont:
214-
return "unknown font $0 of size $1"
215212
case .cannotLoadImage:
216213
return "cannot load image from file $0"
217214
case .cannotCreateBitmap:

Sources/LispKit/Primitives/DrawingLibrary.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import Foundation
2222
import Cocoa
2323

24+
///
25+
/// This class implements the library `(lispkit draw)`.
26+
///
2427
public final class DrawingLibrary: NativeLibrary {
2528

2629
/// Imported native library
@@ -1046,7 +1049,7 @@ public final class DrawingLibrary: NativeLibrary {
10461049
private func font(font: Expr, size: Expr) throws -> Expr {
10471050
guard let nsfont = NSFont(name: try font.asString(),
10481051
size: CGFloat(try size.asDouble(coerce: true))) else {
1049-
throw RuntimeError.eval(.unknownFont, font, size)
1052+
return .false
10501053
}
10511054
return .object(ImmutableBox(nsfont))
10521055
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
;;; Plot graphs into a PDF file
2+
;;;
3+
;;; This is a demo of library (lispkit draw). Function `plot` draws a function over
4+
;;; a given range using a number of interpolation points into a given rectangle.
5+
;;; It is used a number of times in function `plot-demo-page` which explains how to
6+
;;; compose drawings and save them in a PDF file.
7+
;;;
8+
;;; Usage: (plot-demo-page "graph-demo.pdf")
9+
;;;
10+
;;; Author: Matthias Zenger
11+
;;; Copyright © 2018 Matthias Zenger. All rights reserved.
12+
;;;
13+
;;; Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
14+
;;; except in compliance with the License. You may obtain a copy of the License at
15+
;;;
16+
;;; http://www.apache.org/licenses/LICENSE-2.0
17+
;;;
18+
;;; Unless required by applicable law or agreed to in writing, software distributed under the
19+
;;; License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
20+
;;; either express or implied. See the License for the specific language governing permissions
21+
;;; and limitations under the License.
22+
23+
24+
(import (lispkit draw))
25+
26+
;; Plots a function `f` over range `[xmin; xmax]` using `n` interpolation points
27+
;; within rectangle `rect`. Prints `label` at the bottom of the graph.
28+
(define (plot f xmin xmax n rect label)
29+
(let* ((dx (/ (- xmax xmin) n))
30+
(xs (tabulate (fx1+ n) (lambda (i) (+ xmin (* i dx)))))
31+
(ys (map f xs))
32+
(ymin (apply min ys))
33+
(ymax (apply max ys))
34+
(xfac (/ (car (rect-size rect)) (- xmax xmin)))
35+
(yfac (/ (cdr (rect-size rect)) (- ymax ymin)))
36+
(ps (map (lambda (x y) (point (* xfac (- x xmin)) (* yfac (- y ymin)))) xs ys))
37+
(shift (translate (car (rect-point rect)) (cdr (rect-point rect))))
38+
(d (make-drawing)))
39+
; Interpolate the points and flip the shape
40+
(define s (flip-shape (interpolate ps)))
41+
; Draw a bounding box
42+
(define box (shape-bounds s))
43+
(draw-shape d (make-rect (rect-point rect) (rect-size rect)) 0.5)
44+
; Draw the graph and coordinate axis
45+
(enable-transformation d shift)
46+
(if (and (<= xmin 0.0) (>= xmax 0.0))
47+
(draw-shape d (make-polygon (point (* xfac (- xmin)) 0)
48+
(point (* xfac (- xmin)) (cdr (rect-size rect))))
49+
0.3))
50+
(if (and (<= ymin 0.0) (>= ymax 0.0))
51+
(draw-shape d (make-polygon (point 0
52+
(+ (cdr (rect-size rect)) (* yfac ymin)))
53+
(point (car (rect-size rect))
54+
(+ (cdr (rect-size rect)) (* yfac ymin))))
55+
0.3))
56+
(set-color d (make-color 0.0 0.0 1.0 1.0))
57+
(draw-shape d s)
58+
; Draw interpolation points
59+
(set-color d (make-color 0.0 0.0 0.0))
60+
(set-fill-color d (make-color 0.0 0.0 0.0))
61+
(for-each (lambda (p) (fill-shape d (flip-shape (make-arc p 1 0) box))) ps)
62+
; Draw the label
63+
(draw-text d label (point 30 (- (cdr (rect-size rect)) 12))
64+
(make-color 0.3 0.3 0.3) (font "Times-Italic" 7))
65+
(disable-transformation d shift)
66+
d))
67+
68+
;; Creates a demo page consisting of a header and four graphs
69+
(define (plot-demo-page path)
70+
; Create a new drawing
71+
(define page (make-drawing))
72+
; Draw a header in font "Helvetica" of size 8
73+
(draw-text page "Demo of library (lispkit draw)"
74+
(point 160 8) (make-color 0.0 0.0 0.0) (font "Helvetica" 8))
75+
; Plot four graphs
76+
(draw-drawing page (plot sin -1 6.3 50 (rect 10 30 200 100) "sin(x)"))
77+
(draw-drawing page (plot cos -1 6.3 50 (rect 220 30 200 100) "cos(x)"))
78+
(draw-drawing page (plot (lambda (x) (* (sin (* x 2)) (cos (/ x 4))))
79+
-1 6.3 50 (rect 10 140 200 100) "sin(x*2)*cos(x/4)"))
80+
(draw-drawing page (plot (lambda (x) (/ (* x x) 40)) -1 6.3 50 (rect 220 140 200 100) "x*x/40"))
81+
; Save drawing in a PDF file
82+
(save-drawing page path (size 430 250)))
83+

0 commit comments

Comments
 (0)