Skip to content

Commit

Permalink
Updated for Swift 5
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed May 3, 2019
1 parent bc207ab commit 4dab21c
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 142 deletions.
24 changes: 0 additions & 24 deletions Package.pins

This file was deleted.

34 changes: 0 additions & 34 deletions Package.resolved

This file was deleted.

50 changes: 42 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
// swift-tools-version:3.0.2
// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Cairo",
products: [
.library(
name: "Cairo",
targets: ["Cairo"]),
],
targets: [
Target(
name: "Cairo")
.target(
name: "Cairo",
dependencies: [
"CCairo",
"CFontConfig",
"CFreeType"
]
),
.testTarget(
name: "CairoTests",
dependencies: [
"Cairo"
]
),
.systemLibrary(
name: "CCairo",
pkgConfig: "cairo",
providers: [
.brew(["cairo"]),
.apt(["libcairo2-dev"])
]),
.systemLibrary(
name: "CFontConfig",
pkgConfig: "fontconfig",
providers: [
.brew(["fontconfig"]),
.apt(["libfontconfig-dev"])
]),
.systemLibrary(
name: "CFreeType",
pkgConfig: "freetype",
providers: [
.brew(["freetype2"]),
.apt(["libfreetype6-dev"])
])
],
dependencies: [
.Package(url: "https://github.com/PureSwift/CCairo.git", majorVersion: 1),
.Package(url: "https://github.com/PureSwift/CFontConfig.git", majorVersion: 1),
.Package(url: "https://github.com/PureSwift/CFreeType.git", majorVersion: 1)
]
swiftLanguageVersions: [.v5]
)
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Cairo
Cairo library for Swift

## OS X Installation
## macOS Installation

`brew install cairo`

## Ubuntu Installation
## Linux Installation

`sudo apt-get install libcairo2-dev`
`sudo apt-get install libcairo2-dev`
5 changes: 5 additions & 0 deletions Sources/CCairo/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CCairo [system] {
header "shim.h"
link "cairo"
export *
}
4 changes: 4 additions & 0 deletions Sources/CCairo/shim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <cairo.h>
#include <cairo-pdf.h>
#include <cairo-svg.h>
#include <cairo-ft.h>
5 changes: 5 additions & 0 deletions Sources/CFontConfig/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CFontConfig [system] {
header "shim.h"
link "fontconfig"
export *
}
2 changes: 2 additions & 0 deletions Sources/CFontConfig/shim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include <fontconfig/fontconfig.h>
#include <fontconfig/fcfreetype.h>
5 changes: 5 additions & 0 deletions Sources/CFreeType/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module CFreeType [system] {
header "shim.h"
link "freetype"
export *
}
4 changes: 4 additions & 0 deletions Sources/CFreeType/shim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <ft2build.h>
#include FT_FREETYPE_H
#include <freetype/freetype.h>
#include <freetype/tttables.h>
16 changes: 0 additions & 16 deletions Sources/Cairo/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -493,19 +493,3 @@ public final class Context {
set { cairo_set_scaled_font(internalPointer, newValue.internalPointer) }
}
}

#if os(macOS) && Xcode

import Foundation
import AppKit

public extension Context {

@objc(debugQuickLookObject)
public var debugQuickLookObject: AnyObject {

return surface.debugQuickLookObject
}
}

#endif
54 changes: 35 additions & 19 deletions Sources/Cairo/Font.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public final class ScaledFont {

let bufferSize = 256
let buffer = UnsafeMutablePointer<CChar>.allocate(capacity: bufferSize)
defer { buffer.deallocate(capacity: bufferSize) }
defer { buffer.deallocate() }

FT_Get_Glyph_Name(fontFace, FT_UInt(glyph), buffer, FT_UInt(bufferSize))

Expand Down Expand Up @@ -244,7 +244,7 @@ public final class ScaledFont {

// MARK: - Private Methods

fileprivate func lockFontFace<T>(_ block: (FT_Face) -> T) -> T {
private func lockFontFace<T>(_ block: (FT_Face) -> T) -> T {

let ftFace = cairo_ft_scaled_font_lock_face(self.internalPointer)!

Expand Down Expand Up @@ -294,7 +294,7 @@ public final class FontFace {
public lazy var type: cairo_font_type_t = cairo_font_face_get_type(self.internalPointer) // Never changes
}

public final class FontOptions: Equatable, Hashable {
public final class FontOptions {

// MARK: - Properties

Expand Down Expand Up @@ -337,16 +337,11 @@ public final class FontOptions: Equatable, Hashable {
return FontOptions(cairo_font_options_copy(internalPointer))
}

public var hashValue: Int {
public var hintMetrics: FontHintMetrics {

return Int(bitPattern: cairo_font_options_hash(internalPointer))
}

public var hintMetrics: cairo_hint_metrics_t {
get { return FontHintMetrics(rawValue: cairo_font_options_get_hint_metrics(internalPointer).rawValue)! }

get { return cairo_font_options_get_hint_metrics(internalPointer) }

set { cairo_font_options_set_hint_metrics(internalPointer, newValue) }
set { cairo_font_options_set_hint_metrics(internalPointer, cairo_hint_metrics_t(rawValue: newValue.rawValue)) }
}

public var hintStyle: cairo_hint_style_t {
Expand All @@ -371,28 +366,49 @@ public final class FontOptions: Equatable, Hashable {
}
}

public func == (lhs: FontOptions, rhs: FontOptions) -> Bool {
extension FontOptions: Equatable {

return cairo_font_options_equal(lhs.internalPointer, rhs.internalPointer) != 0
public static func == (lhs: FontOptions, rhs: FontOptions) -> Bool {

return cairo_font_options_equal(lhs.internalPointer, rhs.internalPointer) != 0
}
}

extension FontOptions: Hashable {

public func hash(into hasher: inout Hasher) {

let hashValue = cairo_font_options_hash(internalPointer)
hashValue.hash(into: &hasher)
}
}

// MARK: - Supporting Types

/// cairo_font_slant_t
public enum FontSlant: UInt32 {

case normal, italic, oblique
case normal
case italic
case oblique
}

/// cairo_font_weight_t
public enum FontWeight: UInt32 {

case normal, bold
case normal
case bold
}

#if os(Linux)

public let FT_SFNT_OS2 = FT_Sfnt_Tag(rawValue: 2)
public let FT_SFNT_POST = FT_Sfnt_Tag(rawValue: 5)
/// cairo_hint_metrics_t
public enum FontHintMetrics: UInt32 {

case `default`
case off
case on
}

#if os(Linux)
public let FT_SFNT_OS2 = FT_Sfnt_Tag(rawValue: 2)
public let FT_SFNT_POST = FT_Sfnt_Tag(rawValue: 5)
#endif
33 changes: 33 additions & 0 deletions Sources/Cairo/QuickLook.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// QuickLook.swift
// Cairo
//
// Created by Alsey Coleman Miller on 5/3/19.
//

#if os(macOS) && Xcode

import Foundation
import AppKit

public extension Surface {

@objc(debugQuickLookObject)
var debugQuickLookObject: AnyObject {

let filePath = NSTemporaryDirectory() + "CairoSwiftQuickLook\(UUID().uuidString).png"
self.writePNG(atPath: filePath)
let image = NSImage(byReferencingFile: filePath)!
return image
}
}

public extension Context {

@objc(debugQuickLookObject)
var debugQuickLookObject: AnyObject {
return surface.debugQuickLookObject
}
}

#endif
22 changes: 0 additions & 22 deletions Sources/Cairo/Surface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,3 @@ internal func cairo_surface_create_check_status(_ internalPointer: OpaquePointer
throw error
}
}

#if os(macOS) && Xcode

import Foundation
import AppKit

public extension Surface {

@objc(debugQuickLookObject)
public var debugQuickLookObject: AnyObject {

let filePath = NSTemporaryDirectory() + "CairoSwiftQuickLook\(UUID().uuidString).png"

self.writePNG(atPath: filePath)

let image = NSImage(byReferencingFile: filePath)!

return image
}
}

#endif
17 changes: 12 additions & 5 deletions Sources/Cairo/SurfaceImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public extension Surface {

/// Image surfaces provide the ability to render to memory buffers either allocated by Cairo or by the calling code.
/// The supported image formats are those defined in `ImageFormat`.
public final class Image: Surface {
final class Image: Surface {

// MARK: - Initialization

Expand All @@ -30,7 +30,11 @@ public extension Surface {
}

/// Creates an image surface for the provided pixel data.
public init(mutableBytes bytes: UnsafeMutablePointer<UInt8>, format: ImageFormat, width: Int, height: Int, stride: Int) throws {
public init(mutableBytes bytes: UnsafeMutablePointer<UInt8>,
format: ImageFormat,
width: Int,
height: Int,
stride: Int) throws {

assert(format.stride(for: width) == stride, "Invalid stride")

Expand All @@ -44,10 +48,13 @@ public extension Surface {

var data = data

// a bit unsafe, but cant use self.init inside closure.
let bytes: UnsafeMutablePointer<UInt8> = data.withUnsafeMutableBytes { $0 }
let pointer = data.withUnsafeMutableBytes { $0.baseAddress!.assumingMemoryBound(to: UInt8.self) }

try self.init(mutableBytes: bytes, format: format, width: width, height: height, stride: stride)
try self.init(mutableBytes: pointer,
format: format,
width: width,
height: height,
stride: stride)
}

/// For internal use with extensions (e.g. `init(png:)`)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Cairo/SurfacePDF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CCairo

public extension Surface {

public final class PDF: Surface {
final class PDF: Surface {

// MARK: - Initialization

Expand Down
Loading

0 comments on commit 4dab21c

Please sign in to comment.