Skip to content

Commit

Permalink
#2: Implement Arc Demo, fixes for path construction
Browse files Browse the repository at this point in the history
- fixed the bug where appending an arc wouldn't have changed the path's current position
- changed the assert condition for path construction (decreased accuracy)
- .black, .white and .clear colors are now in .deviceGray color space
  • Loading branch information
broadwaylamb committed Nov 5, 2016
1 parent ea8af3f commit 3d2df3f
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 7 deletions.
90 changes: 90 additions & 0 deletions Demo/ArcDemo.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import SwiftyHaru

let document = PDFDocument()
let page = document.addPage(width: 200, height: 220)

// Draw the grid

let horizontalLabels = stride(from: 50, through: 150, by: 50).map(String.init)
let verticalLabels = stride(from: 10, through: 210, by: 10).map(String.init)

let labels = Grid.Labels(top: Grid.LabelParameters(sequence: "" + horizontalLabels,
offset: Vector(x: 0, y: -6)),
bottom: Grid.LabelParameters(sequence: "" + horizontalLabels,
offset: Vector(x: 0, y: 6)),
left: Grid.LabelParameters(sequence: "" + verticalLabels,
frequency: 1,
offset: Vector(x: 6, y: 0)))

let serifs = Grid.Serifs(top: .default,
bottom: .default,
left: Grid.SerifParameters(frequency: 1),
right: nil)

let grid = Grid(width: page.width, height: page.height, labels: labels, serifs: serifs)
page.draw(object: grid, position: .zero)

/* Draw pie chart
*
* a: 45% Red
* b: 25% Blue
* c: 15% green
* d: other yellow
*/

let center = Point(x: 100, y: 100)

var temporaryPosition: Point

var a = Path()
.moving(to: center)
.appendingLine(toX: 100, y: 180)
.appendingArc(center: center,
radius: 80,
beginningAngle: 0,
endAngle: 360 * 0.45)
temporaryPosition = a.currentPosition
a.appendLine(to: center)

var b = Path()
.moving(to: center)
.appendingLine(to: temporaryPosition)
.appendingArc(center: center, radius: 80, beginningAngle: 360 * 0.45, endAngle: 360 * 0.7)
temporaryPosition = b.currentPosition
b.appendLine(to: center)

var c = Path()
.moving(to: center)
.appendingLine(to: temporaryPosition)
.appendingArc(center: center, radius: 80, beginningAngle: 360 * 0.7, endAngle: 360 * 0.85)
temporaryPosition = c.currentPosition
c.appendLine(to: center)

var d = Path()
.moving(to: center)
.appendingLine(to: temporaryPosition)
.appendingArc(center: center, radius: 80, beginningAngle: 360 * 0.85, endAngle: 360)
.appendingLine(to: center)

// Draw center circle
let centerCircle = Path().appendingCircle(center: center, radius: 30)

page.draw { context in

context.fillColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
context.fill(a)

context.fillColor = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)
context.fill(b)

context.fillColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
context.fill(c)

context.fillColor = #colorLiteral(red: 0.9686274529, green: 0.78039217, blue: 0.3450980484, alpha: 1)
context.fill(d)

context.fillColor = .white
context.fill(centerCircle)
}

document.display()
17 changes: 17 additions & 0 deletions Demo/ArcDemo.playground/Sources/ViewConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Quartz
import PlaygroundSupport
import SwiftyHaru

public extension SwiftyHaru.PDFDocument {

public func display() {

let view = PDFView(frame: NSRect(x: 0, y: 0, width: 480, height: 640))

view.document = PDFDocument(data: getData())

view.scaleFactor = 0.75

PlaygroundPage.current.liveView = view
}
}
4 changes: 4 additions & 0 deletions Demo/ArcDemo.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos'>
<timeline fileName='timeline.xctimeline'/>
</playground>
3 changes: 3 additions & 0 deletions Demo/SwiftyHaru.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Sources/SwiftyHaru/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ public struct Color {
/// Returns the blue color in `PDFColorSpace.deviceRGB` space
public static let blue = Color(red: 0, green: 0, blue: 1)!

/// Returns the black color in `PDFColorSpace.deviceRGB` space
public static let black = Color(red: 0, green: 0, blue: 0)!
/// Returns the black color in `PDFColorSpace.deviceGray` space
public static let black = Color(gray: 0)!

/// Returns the white color in `PDFColorSpace.deviceRGB` space
public static let white = Color(red: 1, green: 1, blue: 1)!
/// Returns the white color in `PDFColorSpace.deviceGray` space
public static let white = Color(gray: 1)!

/// Returns the transparent white color in `PDFColorSpace.deviceRGB` space
public static let clear = Color(red: 1, green: 1, blue: 1, alpha: 0)!
/// Returns the transparent white color in `PDFColorSpace.deviceGray` space
public static let clear = Color(gray: 1, alpha: 0)!

internal enum _ColorSpaceWrapper {
case rgb(red: Float, green: Float, blue: Float)
Expand Down
6 changes: 5 additions & 1 deletion Sources/SwiftyHaru/DrawingContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ public final class DrawingContext {
}
}

assert(path.currentPosition == Point(HPDF_Page_GetCurrentPos(_page)))
assert(path.currentPosition.x - HPDF_Page_GetCurrentPos(_page).x < 0.001 &&
path.currentPosition.y - HPDF_Page_GetCurrentPos(_page).y < 0.001,
"The value of property `currentPosition` (\(path.currentPosition)) is not equal to " +
"the value returned from the function " +
"`HPDF_Page_GetCurrentPos` (\(HPDF_Page_GetCurrentPos(_page)))")

HPDF_Page_MoveTo(_page, 0, 0)
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/SwiftyHaru/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
//

import CoreFoundation

/// A type for graphics paths: mathematical descriptions of shapes or lines to be drawn in a `DrawingContext`.
public struct Path {

Expand Down Expand Up @@ -162,6 +164,15 @@ public struct Path {

guard endAngle > beginningAngle else { return }

let deltaAngle = (90 - (beginningAngle + endAngle) / 2) / 180 * Float.pi
let newAngle = (endAngle - beginningAngle) / 2 / 180 * Float.pi

let rx3 = radius * cos(newAngle)
let ry3 = -radius * sin(newAngle)

_currentPosition = Point(x: rx3 * cos(deltaAngle) - ry3 * sin(deltaAngle) + center.x,
y: rx3 * sin(deltaAngle) + ry3 * cos(deltaAngle) + center.y)

_pathConstructionSequence.append(.arc(center: center,
radius: radius,
beginningAngle: beginningAngle,
Expand Down

0 comments on commit 3d2df3f

Please sign in to comment.