Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
a75d2ea
Added cursor state: set, hide, show and named cursors
colemancda Nov 26, 2026
f2da663
Kept the cursor state in QuickDraw
colemancda Nov 26, 2026
d712f67
Tested cursor state and named cursors
colemancda Nov 26, 2026
30111ef
Added palettes and their entries
colemancda Nov 26, 2026
f90240f
Tested palettes
colemancda Nov 26, 2026
0ca1448
Added Index2Color, SetEntries and RestoreDeviceClut on devices
colemancda Nov 26, 2026
f57f89e
Tested the device color table calls
colemancda Nov 26, 2026
78e23d5
Included FreeType's sfnt table header
colemancda Nov 26, 2026
1684661
Added horizontal font metrics in ems
colemancda Nov 26, 2026
8e3e1f4
Added sfnt tables and horizontal metrics to font faces
colemancda Nov 26, 2026
24cc9bc
Read sfnt tables and horizontal metrics through FreeType
colemancda Nov 26, 2026
15bbaa4
Tested font tables and horizontal metrics
colemancda Nov 26, 2026
c409aaa
Added reading a bundle's Info.plist strings
colemancda Nov 26, 2026
27f4e6c
Tested reading Info.plist strings
colemancda Nov 26, 2026
8031ddf
Added reading a whole fork
colemancda Nov 26, 2026
2bd3fb8
Tested reading a whole fork
colemancda Nov 26, 2026
b6f9b00
Made GraphiteQuickDraw depend on the File and Resource Managers
colemancda Nov 26, 2026
c42dce5
Added finding the icon an item has of its own
colemancda Nov 26, 2026
f557901
Added a temporary folder to the QuickDraw tests
colemancda Nov 26, 2026
d5e4c38
Tested finding an item's own icon
colemancda Nov 26, 2026
7ea4fd4
Read Info.plist through Foundation's property list reader
colemancda Nov 26, 2026
36cf5f9
Tested Info.plist reading with binary lists
colemancda Nov 26, 2026
9e0b9e1
Tested a bundle with a binary Info.plist
colemancda Nov 26, 2026
9047db4
Added reading bytes from anything contiguous
colemancda Nov 26, 2026
ca31ed1
Tested reading bytes from anything contiguous
colemancda Nov 26, 2026
675d2fd
Read icon families from any contiguous bytes
colemancda Nov 26, 2026
be6588f
Tested reading icon families from any bytes
colemancda Nov 26, 2026
b00ac2b
Read Info.plists from any contiguous bytes
colemancda Nov 26, 2026
9caba06
Tested reading Info.plists from any bytes
colemancda Nov 26, 2026
bbd0193
Gave a whole fork as Data, mapped when opened read-only
colemancda Nov 26, 2026
178ef2c
Tested reading a whole fork as Data
colemancda Nov 26, 2026
4895b47
Read color tables from any contiguous bytes
colemancda Nov 26, 2026
f7e1e5b
Copied AppleDouble entries out of the reader's Data
colemancda Nov 26, 2026
996bcc1
Read big-endian fields from Data in place
colemancda Nov 26, 2026
3c5eac5
Made readers of contiguous bytes use Data
colemancda Nov 26, 2026
1adfa41
Tested readers over Data and its slices
colemancda Nov 26, 2026
ed59109
Added Data with indices from zero from any contiguous bytes
colemancda Nov 26, 2026
91908fc
Parsed resource maps from any contiguous bytes
colemancda Nov 26, 2026
4324b2c
Held a resource file's bytes as Data, mapped when read-only
colemancda Nov 26, 2026
5f90635
Tested resource maps and files from Data and its slices
colemancda Nov 26, 2026
bb8e7a6
Read version resources from any contiguous bytes
colemancda Nov 26, 2026
f2f76ad
Read string lists and STR resources from any contiguous bytes
colemancda Nov 26, 2026
c8d07cf
Opened chain files from any contiguous bytes
colemancda Nov 26, 2026
f9bf40d
Tested typed resources from Data and its slices
colemancda Nov 26, 2026
13af609
Read AppleDouble files from any contiguous bytes
colemancda Nov 26, 2026
74536e5
Tested AppleDouble files from Data and its slices
colemancda Nov 26, 2026
abee2fd
Read pictures and PICT files from any contiguous bytes
colemancda Nov 26, 2026
dbc6a27
Packed and unpacked PackBits from any contiguous bytes
colemancda Nov 26, 2026
ef2d05c
Read region records from any contiguous bytes
colemancda Nov 26, 2026
b8f55da
Read polygon records from any contiguous bytes
colemancda Nov 26, 2026
69300fc
Read color icons from any contiguous bytes
colemancda Nov 26, 2026
fa10e15
Read cursors from any contiguous bytes
colemancda Nov 26, 2026
ce955d5
Tested pictures, regions and cursors from Data and its slices
colemancda Nov 26, 2026
0507351
Loaded FreeType faces from any contiguous bytes
colemancda Nov 26, 2026
006f2e5
Added reading sfnt tables and their fields in place
colemancda Nov 26, 2026
6508a1d
Tested sfnt tables on fonts built in the tests
colemancda Nov 26, 2026
06368d7
Depended on swift-freetype instead of a CFreeType target
colemancda Nov 26, 2026
2dcd004
Built FreeType faces on swift-freetype, reading tables from the font
colemancda Nov 26, 2026
5725d07
Removed the CFreeType shim header
colemancda Nov 26, 2026
f68e47e
Removed the CFreeType module map
colemancda Nov 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ let package = Package(
.default(enabledTraits: ["FreeType"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-system.git", from: "1.9.0")
.package(url: "https://github.com/apple/swift-system.git", from: "1.9.0"),
.package(url: "https://github.com/PureSwift/swift-freetype.git", branch: "master"),
],
targets: [
.target(
Expand Down Expand Up @@ -103,18 +104,13 @@ let package = Package(
),
.target(
name: "GraphiteQuickDraw",
dependencies: ["GraphiteBase"]
),
.systemLibrary(
name: "CFreeType",
pkgConfig: "freetype2",
providers: [.apt(["libfreetype-dev"]), .brew(["freetype"])]
dependencies: ["GraphiteBase", "GraphiteFiles", "GraphiteResources"]
),
.target(
name: "GraphiteText",
dependencies: [
"GraphiteBase", "GraphiteQuickDraw",
.target(name: "CFreeType", condition: .when(traits: ["FreeType"])),
.product(name: "FreeTypeSwift", package: "swift-freetype", condition: .when(traits: ["FreeType"])),
]
),
.target(
Expand Down Expand Up @@ -143,7 +139,7 @@ let package = Package(
),
.testTarget(
name: "GraphiteQuickDrawTests",
dependencies: ["GraphiteQuickDraw"]
dependencies: ["GraphiteQuickDraw", "GraphiteFiles", "GraphiteResources"]
),
.testTarget(
name: "GraphiteTextTests",
Expand Down
5 changes: 0 additions & 5 deletions Sources/CFreeType/module.modulemap

This file was deleted.

3 changes: 0 additions & 3 deletions Sources/CFreeType/shim.h

This file was deleted.

41 changes: 27 additions & 14 deletions Sources/GraphiteBase/BigEndianReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// AppleDouble files from bytes. Every read checks its bounds and throws
// past the end; none traps, whatever the bytes say.

import struct Foundation.Data

/// A read that went past the bytes there are.
public struct BigEndianReadError: Error, Hashable, Sendable {

Expand All @@ -24,26 +26,33 @@ public struct BigEndianReadError: Error, Hashable, Sendable {
/// Reads typed fields one after another from bytes.
public struct BigEndianReader: Sendable {

public let bytes: [UInt8]
/// The bytes as given, which may be a slice that doesn't start at 0;
/// offsets are from its start.
private let storage: Data

/// Where the next read starts.
public var offset: Int

public init(_ bytes: [UInt8], offset: Int = 0) {
self.bytes = bytes
/// Reads the bytes where they are, without copying them: a file mapped
/// into memory is read as its pages are used.
public init(_ bytes: Data, offset: Int = 0) {
storage = bytes
self.offset = offset
}

public var count: Int { bytes.count }
/// All the bytes, from index 0 (a copy, if the reader is of a slice).
public var bytes: Data { storage.startIndex == 0 ? storage : Data(storage) }

public var count: Int { storage.count }

public var remaining: Int { max(0, bytes.count - offset) }
public var remaining: Int { max(0, storage.count - offset) }

public var isAtEnd: Bool { offset >= bytes.count }
public var isAtEnd: Bool { offset >= storage.count }

/// The range of `count` bytes at `offset`, if they're all there.
private func range(at offset: Int, count: Int) throws(BigEndianReadError) -> Range<Int> {
guard offset >= 0, count >= 0, offset <= bytes.count, count <= bytes.count - offset else {
throw BigEndianReadError(offset: offset, count: count, available: bytes.count)
guard offset >= 0, count >= 0, offset <= storage.count, count <= storage.count - offset else {
throw BigEndianReadError(offset: offset, count: count, available: storage.count)
}
return offset ..< offset + count
}
Expand All @@ -53,7 +62,7 @@ public struct BigEndianReader: Sendable {
let range = try range(at: offset, count: MemoryLayout<T>.size)
var value: T = 0
for index in range {
value = value << 8 | T(truncatingIfNeeded: bytes[index])
value = value << 8 | T(truncatingIfNeeded: storage[storage.startIndex + index])
}
return value
}
Expand All @@ -64,10 +73,13 @@ public struct BigEndianReader: Sendable {
return value
}

public mutating func readBytes(_ count: Int) throws(BigEndianReadError) -> ArraySlice<UInt8> {
/// The next `count` bytes, as a slice of the reader's: its indices are
/// the reader's, not from 0, so take them with `Array(_:)` or by
/// iterating.
public mutating func readBytes(_ count: Int) throws(BigEndianReadError) -> Data {
let range = try range(at: offset, count: count)
offset = range.upperBound
return bytes[range]
return storage[storage.startIndex + range.lowerBound ..< storage.startIndex + range.upperBound]
}

public mutating func skip(_ count: Int) throws(BigEndianReadError) {
Expand All @@ -85,17 +97,18 @@ public struct BigEndianReader: Sendable {
public mutating func align(to alignment: Int) {
guard alignment > 1 else { return }
let padding = (alignment - offset % alignment) % alignment
offset = min(offset + padding, max(offset, bytes.count))
offset = min(offset + padding, max(offset, storage.count))
}

/// A reader of the next `count` bytes, which this one moves past.
public mutating func subreader(_ count: Int) throws(BigEndianReadError) -> BigEndianReader {
BigEndianReader(Array(try readBytes(count)))
BigEndianReader(try readBytes(count))
}

/// A reader of `count` bytes at `offset`, without moving.
public func subreader(at offset: Int, count: Int) throws(BigEndianReadError) -> BigEndianReader {
BigEndianReader(Array(bytes[try range(at: offset, count: count)]))
let range = try range(at: offset, count: count)
return BigEndianReader(storage[storage.startIndex + range.lowerBound ..< storage.startIndex + range.upperBound])
}
}

Expand Down
44 changes: 44 additions & 0 deletions Sources/GraphiteBase/ContiguousBytes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Bytes from anywhere: an array, a Data (which may be a file mapped into
// memory), a raw buffer or a slice of one. Parsers take `some
// ContiguousBytes` so a caller keeps what it has, and copy out only what
// they keep.

import protocol Foundation.ContiguousBytes
import struct Foundation.Data

public extension Array where Element == UInt8 {

/// The bytes of anything contiguous, copied. An array is returned as it
/// is, and shares its storage until one of them changes. Copying a
/// mapped Data reads all of it.
init(copying bytes: some ContiguousBytes) {
if let array = bytes as? [UInt8] {
self = array
} else {
self = bytes.withUnsafeBytes { Array($0) }
}
}
}

public extension Data {

/// The bytes of anything contiguous as a Data whose indices start at 0.
/// A Data that already does is itself, mapped or not; a slice of one,
/// and any other bytes, are copied.
init(zeroBased bytes: some ContiguousBytes) {
if let data = bytes as? Data, data.startIndex == 0 {
self = data
} else {
self = bytes.withUnsafeBytes { Data($0) }
}
}
}

public extension BigEndianReader {

/// A reader of the bytes of anything contiguous, from `offset`. A Data
/// is read where it is; anything else is copied into one.
init(_ bytes: some ContiguousBytes, offset: Int = 0) {
self.init(bytes as? Data ?? bytes.withUnsafeBytes { Data($0) }, offset: offset)
}
}
7 changes: 4 additions & 3 deletions Sources/GraphiteFiles/AppleDouble.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Files/HostVolume.cpp (ADFJumpToResourceFork):
// Copyright (c) 2020 Iliyas Jorio. MIT License.

import protocol Foundation.ContiguousBytes
import GraphiteBase

/// An AppleDouble or AppleSingle file's entries.
Expand Down Expand Up @@ -52,13 +53,13 @@ public struct AppleDouble: Hashable, Sendable {
}

/// Whether bytes start as an AppleDouble or AppleSingle file does.
public static func isAppleDouble(_ bytes: [UInt8]) -> Bool {
public static func isAppleDouble(_ bytes: some ContiguousBytes) -> Bool {
guard let magic = try? BigEndianReader(bytes).peek(UInt32.self, at: 0) else { return false }
return magic == appleDoubleMagic || magic == appleSingleMagic
}

/// Throws for bytes that aren't one, or an entry past their end.
public init(bytes: [UInt8]) throws(MacError) {
public init(bytes: some ContiguousBytes) throws(MacError) {
do {
var reader = BigEndianReader(bytes)
let magic = try reader.read(UInt32.self)
Expand All @@ -72,7 +73,7 @@ public struct AppleDouble: Hashable, Sendable {
let offset = Int(try reader.read(UInt32.self))
let length = Int(try reader.read(UInt32.self))
let entry = try reader.subreader(at: offset, count: length)
entries.append(Entry(id: EntryID(rawValue: id), bytes: entry.bytes))
entries.append(Entry(id: EntryID(rawValue: id), bytes: Array(entry.bytes)))
}
} catch let error as MacError {
throw error
Expand Down
3 changes: 2 additions & 1 deletion Sources/GraphiteQuickDraw/ColorTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// the tables indexed pixels look their colors up in, with the system's
// standard ones for each depth.

import protocol Foundation.ContiguousBytes
import GraphiteBase

/// QuickDraw's RGBColor: 16 bits a component.
Expand Down Expand Up @@ -140,7 +141,7 @@ public struct ColorTable: Hashable, Sendable {
/// A 'clut' resource: ctSeed, ctFlags, ctSize (the count less one),
/// then each entry's value and red, green and blue. A device table's
/// entries are in order whatever their values say.
public init(clut bytes: [UInt8]) throws(MacError) {
public init(clut bytes: some ContiguousBytes) throws(MacError) {
var reader = BigEndianReader(bytes)
do {
seed = Int32(bitPattern: try reader.read(UInt32.self))
Expand Down
17 changes: 10 additions & 7 deletions Sources/GraphiteQuickDraw/Cursor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// Where data and mask are both set the pixel is black; mask alone, white;
// data alone, the screen inverted; neither, the screen as it is.

import protocol Foundation.ContiguousBytes
import struct Foundation.Data
import GraphiteBase

public struct Cursor: Hashable, Sendable {
Expand Down Expand Up @@ -32,7 +34,7 @@ public struct Cursor: Hashable, Sendable {

/// A 'CURS' resource or a Cursor record: data, mask, hot spot (68
/// bytes).
public init(curs bytes: [UInt8]) throws(MacError) {
public init(curs bytes: some ContiguousBytes) throws(MacError) {
do {
var reader = BigEndianReader(bytes)
let data = try (0 ..< 16).map { _ throws(BigEndianReadError) in try reader.read(UInt16.self) }
Expand All @@ -57,17 +59,18 @@ public struct Cursor: Hashable, Sendable {
/// A 'crsr' resource: its CCrsr record (crsrType, offsets of its pixel
/// map and pixels, its 1-bit data, mask and hot spot), then the pixel
/// map, which points to its color table by offset too.
public init(crsr bytes: [UInt8]) throws(MacError) {
public init(crsr bytes: some ContiguousBytes) throws(MacError) {
let source = Data(zeroBased: bytes)
do {
var reader = BigEndianReader(bytes)
var reader = BigEndianReader(source)
try reader.skip(2) // crsrType
let mapOffset = Int(try reader.read(UInt32.self))
let dataOffset = Int(try reader.read(UInt32.self))
try reader.skip(4 + 2 + 4) // crsrXData, crsrXValid, crsrXHandle
let data = try (0 ..< 16).map { _ throws(BigEndianReadError) in try reader.read(UInt16.self) }
let mask = try (0 ..< 16).map { _ throws(BigEndianReadError) in try reader.read(UInt16.self) }
let v = Int16(bitPattern: try reader.read(UInt16.self)), h = Int16(bitPattern: try reader.read(UInt16.self))
var map = try BigEndianReader(bytes).subreader(at: mapOffset, count: 50)
var map = try BigEndianReader(source).subreader(at: mapOffset, count: 50)
try map.skip(4)
let rowBytes = Int(try map.read(UInt16.self) & 0x3FFF)
let top = Int(Int16(bitPattern: try map.read(UInt16.self))), left = Int(Int16(bitPattern: try map.read(UInt16.self)))
Expand All @@ -78,11 +81,11 @@ public struct Cursor: Hashable, Sendable {
let tableOffset = Int(try map.read(UInt32.self))
let width = right - left, height = bottom - top
guard width > 0, height > 0, width <= 64, height <= 64, [1, 2, 4, 8].contains(depth) else { throw MacError.parameter }
var tableReader = try BigEndianReader(bytes).subreader(at: tableOffset, count: 8)
var tableReader = try BigEndianReader(source).subreader(at: tableOffset, count: 8)
try tableReader.skip(6)
let entries = Int(try tableReader.read(UInt16.self)) + 1
let table = try ColorTable(clut: try BigEndianReader(bytes).subreader(at: tableOffset, count: 8 + entries * 8).bytes)
let pixels = try BigEndianReader(bytes).subreader(at: dataOffset, count: rowBytes * height).bytes
let table = try ColorTable(clut: try BigEndianReader(source).subreader(at: tableOffset, count: 8 + entries * 8).bytes)
let pixels = try BigEndianReader(source).subreader(at: dataOffset, count: rowBytes * height).bytes
var image: [UInt32] = []
for row in 0 ..< height {
for column in 0 ..< width {
Expand Down
67 changes: 67 additions & 0 deletions Sources/GraphiteQuickDraw/CursorState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// The cursor a program has set: InitCursor, SetCursor, HideCursor and
// ShowCursor, and the cursors it made of its own pixels and set by name.
// A host reads `current` and `isVisible` to show the cursor.

import GraphiteBase

public struct CursorState: Hashable, Sendable {

/// The cursor to show: the arrow until a program sets another.
public private(set) var current = Cursor.arrow

/// HideCursor calls not yet matched by ShowCursor.
public private(set) var hideCount = 0

private var named: [String: Cursor] = [:]

public init() {}

public var isVisible: Bool { hideCount == 0 }

/// InitCursor: the arrow, shown.
public mutating func initialize() {
current = .arrow
hideCount = 0
}

/// SetCursor.
public mutating func set(_ cursor: Cursor) {
current = cursor
}

/// HideCursor. Calls nest.
public mutating func hide() {
hideCount += 1
}

/// ShowCursor. More calls than HideCursor's change nothing.
public mutating func show() {
if hideCount > 0 { hideCount -= 1 }
}

/// QDRegisterNamedPixMapCursor: a cursor of 32-bit ARGB pixels, kept
/// under a name; a name used before is replaced. The pixels are copied,
/// as the program goes on to free its world.
public mutating func register(pixels: PixelBuffer, hotSpot: Point, name: String) throws(MacError) {
guard !name.isEmpty, pixels.format == .argb32, pixels.width > 0, pixels.height > 0 else { throw .parameter }
var image: [UInt32] = []
image.reserveCapacity(pixels.width * pixels.height)
for row in 0 ..< pixels.height {
for column in 0 ..< pixels.width {
image.append(pixels.value(column: column, row: row))
}
}
named[name] = Cursor(data: [], mask: [], hotSpot: hotSpot, colorImage: image, colorWidth: pixels.width)
}

/// QDSetNamedPixMapCursor.
public mutating func setNamed(_ name: String) throws(MacError) {
guard let cursor = named[name] else { throw .parameter }
current = cursor
}

/// QDUnregisterNamedPixMapCursor. The cursor on show stays until another is set.
public mutating func unregister(_ name: String) throws(MacError) {
guard named.removeValue(forKey: name) != nil else { throw .parameter }
}
}
Loading
Loading