Skip to content

Commit

Permalink
added wikipedia test
Browse files Browse the repository at this point in the history
added Cairo source

working on Cairo C source

ignore user state

reverted to OS X only version

working on unit tests

wiki example crashes
  • Loading branch information
colemancda committed Jun 3, 2017
1 parent cace3d0 commit 90683a4
Show file tree
Hide file tree
Showing 29 changed files with 1,235 additions and 46 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

Xcode/Cairo.xcodeproj/project.xcworkspace/xcuserdata/
Xcode/Cairo.xcodeproj/xcuserdata
libcairo/autom4te.cache
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified Sources/Cairo/Content.swift
100644 → 100755
Empty file.
35 changes: 30 additions & 5 deletions Sources/Cairo/Context.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,19 @@ public final class Context {
cairo_pop_group_to_source(internalPointer)
}

public func setSourceColor(red: Double, green: Double, blue: Double) {
public func setSource(color: (red: Double, green: Double, blue: Double)) {

cairo_set_source_rgb(internalPointer, red, green, blue)
cairo_set_source_rgb(internalPointer, color.red, color.green, color.blue)
}

public func setSourceColor(red: Double, green: Double, blue: Double, alpha: Double) {
public func setSource(color: (red: Double, green: Double, blue: Double, alpha: Double)) {

cairo_set_source_rgba(internalPointer, red, green, blue, alpha)
cairo_set_source_rgba(internalPointer, color.red, color.green, color.blue, color.alpha)
}

public func setSource(pattern: Pattern) {

cairo_set_source(internalPointer, pattern.internalPointer)
}

public func stroke() {
Expand All @@ -127,7 +132,7 @@ public final class Context {
}
}

/// Adds a closed sub-path rectangle of the given size to the current path at position (x , y ) in user-space coordinates.
/// Adds a closed sub-path rectangle of the given size to the current path at position `(x , y)` in user-space coordinates.
public func addRectangle(x: Double, y: Double, width: Double, height: Double) {

cairo_rectangle(internalPointer, x, y, width, height)
Expand Down Expand Up @@ -178,6 +183,26 @@ public final class Context {
return Path(pathPointer)
}

public func setFont(size: Double) {

cairo_set_font_size(internalPointer, size)
}

public func setFont(face: (family: String, slant: FontSlant, weight: FontWeight)) {

cairo_select_font_face(internalPointer, face.family, cairo_font_slant_t(face.slant.rawValue), cairo_font_weight_t(face.weight.rawValue))
}

public func move(to coordinate: (x: Double, y: Double)) {

cairo_move_to(internalPointer, coordinate.x, coordinate.y)
}

public func show(text: String) {

cairo_show_text(internalPointer, text)
}

// MARK: - Accessors

/// Gets the current destination surface for the context.
Expand Down
19 changes: 19 additions & 0 deletions Sources/Cairo/Font.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Font.swift
// Cairo
//
// Created by Alsey Coleman Miller on 5/7/16.
// Copyright © 2016 PureSwift. All rights reserved.
//

import CCairo

public enum FontSlant: cairo_font_slant_t.RawValue {

case normal, italic, oblique
}

public enum FontWeight: cairo_font_weight_t.RawValue {

case normal, bold
}
Empty file modified Sources/Cairo/ImageFormat.swift
100644 → 100755
Empty file.
Empty file modified Sources/Cairo/Path.swift
100644 → 100755
Empty file.
29 changes: 22 additions & 7 deletions Sources/Cairo/Pattern.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ public final class Pattern {
self.internalPointer = internalPointer
}

public convenience init(surface: Surface) {
public init(surface: Surface) {

let internalPointer = cairo_pattern_create_for_surface(surface.internalPointer)

self.init(internalPointer)
self.internalPointer = cairo_pattern_create_for_surface(surface.internalPointer)
}

public convenience init(linear: ((Double, Double), (Double, Double))) {
public init(linear: ((Double, Double), (Double, Double))) {

let internalPointer = cairo_pattern_create_linear(linear.0.0, linear.0.1, linear.1.0, linear.1.1)
self.internalPointer = cairo_pattern_create_linear(linear.0.0, linear.0.1, linear.1.0, linear.1.1)
}

public init(radial: (start: (center: (x: Double, y: Double), radius: Double), end: (center: (x: Double, y: Double), radius: Double))) {

self.init(internalPointer)
self.internalPointer = cairo_pattern_create_radial(radial.start.center.x, radial.start.center.y, radial.start.radius, radial.end.center.x, radial.end.center.y, radial.end.radius)
}

public static var mesh: Pattern {
Expand All @@ -60,6 +61,20 @@ public final class Pattern {

return pattern
}

// MARK: - Methods

/// Adds an opaque color stop to a gradient pattern.
public func addColorStop(offset: Double, red: Double, green: Double, blue: Double) {

cairo_pattern_add_color_stop_rgb(internalPointer, offset, red, green, blue)
}

/// Adds an opaque color stop to a gradient pattern.
public func addColorStop(offset: Double, red: Double, green: Double, blue: Double, alpha: Double) {

cairo_pattern_add_color_stop_rgba(internalPointer, offset, red, green, blue, alpha)
}
}


Expand Down
16 changes: 12 additions & 4 deletions Sources/Cairo/Surface.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,25 @@ public final class Surface {
}

internal init(_ internalPointer: OpaquePointer) {

self.internalPointer = internalPointer
}

public convenience init(format: ImageFormat, width: Int, height: Int) {
public init(format: ImageFormat, width: Int, height: Int) {

let internalFormat = cairo_format_t(rawValue: format.rawValue)

let pointer = cairo_image_surface_create(internalFormat, Int32(width), Int32(height))
self.internalPointer = cairo_image_surface_create(internalFormat, Int32(width), Int32(height))
}

public init(svg filename: String, width: Double, height: Double) {

self.internalPointer = cairo_svg_surface_create(filename, width, height)
}

public init(pdf filename: String, width: Double, height: Double) {

self.init(pointer)
self.internalPointer = cairo_pdf_surface_create(filename, width, height)
}

// MARK: - Methods
Expand Down
Empty file modified Sources/Cairo/SurfaceType.swift
100644 → 100755
Empty file.
15 changes: 15 additions & 0 deletions Sources/UnitTests/CairoTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// CairoTest.h
// Cairo
//
// Created by Alsey Coleman Miller on 5/8/16.
// Copyright © 2016 PureSwift. All rights reserved.
//

@import Foundation;

@interface CairoTest : NSObject

+ (void)helloWikipedia:(NSString *)filename;

@end
55 changes: 55 additions & 0 deletions Sources/UnitTests/CairoTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// CairoTest.m
// Cairo
//
// Created by Alsey Coleman Miller on 5/8/16.
// Copyright © 2016 PureSwift. All rights reserved.
//

#import "CairoTest.h"
#import <cairo/cairo.h>

@implementation CairoTest

+ (void)helloWikipedia:(NSString *)filename
{
cairo_t *cr;
cairo_surface_t *surface;
cairo_pattern_t *pattern;
int x,y;

const char *cString = [filename cStringUsingEncoding: NSUTF8StringEncoding];

surface =
(cairo_surface_t *)cairo_svg_surface_create(cString, 100.0, 100.0);
cr = cairo_create(surface);

/* Draw the squares in the background */
for (x=0; x<10; x++)
for (y=0; y<10; y++)
cairo_rectangle(cr, x*10.0, y*10.0, 5, 5);

pattern = cairo_pattern_create_radial(50, 50, 5, 50, 50, 50);
cairo_pattern_add_color_stop_rgb(pattern, 0, 0.75, 0.15, 0.99);
cairo_pattern_add_color_stop_rgb(pattern, 0.9, 1, 1, 1);

cairo_set_source(cr, pattern);
cairo_fill(cr);

/* Writing in the foreground */
cairo_set_font_size (cr, 15);
cairo_select_font_face (cr, "Georgia",
CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_source_rgb (cr, 0, 0, 0);

cairo_move_to(cr, 10, 25);
cairo_show_text(cr, "Hallo");

cairo_move_to(cr, 10, 75);
cairo_show_text(cr, "Wikipedia!");

cairo_destroy (cr);
cairo_surface_destroy (surface);
}

@end
5 changes: 5 additions & 0 deletions Sources/UnitTests/CairoTests-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "CairoTest.h"
72 changes: 72 additions & 0 deletions Sources/UnitTests/CairoTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// CairoTests.swift
// Cairo
//
// Created by Alsey Coleman Miller on 5/7/16.
// Copyright © 2016 PureSwift. All rights reserved.
//

import XCTest
import Cairo

final class CairoTests: XCTestCase {

func testHelloWikipedia() {

let filename = outputDirectory + "helloWiki.svg"

print("Writing to \(filename)")

//let surface = Surface
let surface = Surface(pdf: filename, width: 100, height: 100)
let context = Context(surface: surface)

/// Draw the squares in the background
for x in 0 ..< 10 {

for y in 0 ..< 10 {

context.addRectangle(x: Double(x) * 10.0, y: Double(y) * 10.0, width: 5.0, height: 5.0)
}
}

let pattern = Cairo.Pattern(radial: (start: (center: (x: 50, y: 50), radius: 5), end: (center: (x: 50, y: 50), radius: 5)))

pattern.addColorStop(offset: 0.0, red: 0.75, green: 0.15, blue: 0.99)

pattern.addColorStop(offset: 0.9, red: 1.00, green: 1.00, blue: 1.00)

context.setSource(pattern: pattern)

context.fill()

// Writing in the foreground

context.setFont(size: 15.0)

context.setFont(face: (family: "Georgia", slant: .normal, weight: .bold))

context.setSource(color: (red: 0, green: 0, blue: 0))

context.move(to: (x: 10, y: 25))
context.show(text: "Hallo!")

context.move(to: (x: 10, y: 75))
context.show(text: "Wikipedia!")

let originalFile = outputDirectory + "helloWiki2.svg"

CairoTest.helloWikipedia(originalFile)
}

func testCairoSourceX() {


}
}

#if os(OSX)
let outputDirectory = NSTemporaryDirectory()
#elseif os(Linux)
let outputDirectory = "/tmp/"
#endif
Loading

0 comments on commit 90683a4

Please sign in to comment.