Skip to content
PureSwiftPublic

About

Swift Classic Toolbox API

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

346 Commits

Folders and files

Repository files navigation

Graphite

Swift Platforms License

The Mac Toolbox of the Carbon era, in pure Swift: QuickDraw; the Resource, File, Window, Menu, Control and Dialog Managers; Carbon Events, Apple Events, fonts and the Sound Manager, as Swift types with their own API.

Graphite has three kinds of clients:

  • Perry, whose Carbon provider exports Carbon's C ABI and calls Graphite.
  • Bridge32, which runs i386 and PowerPC Mac programs on Apple Silicon and needs Carbon's parts that have no 64-bit host.
  • Linux Swift programs, which import Graphite directly: a resource viewer, a file manager that shows classic icons, a port of an old game.

Graphite has no C ABI and no C headers. It uses Foundation for bytes (Data, ContiguousBytes) and property lists only. Its names and types are Swift's (Rect, PascalString, MacError), not Carbon's spellings: Pascal strings in memory, handles, OSErr returns and Darwin's structure layouts are the job of the layer that exports Carbon. Any UI it shows will be drawn with Cacao.

Status: early development. GraphiteBase, GraphiteTasks, GraphiteFiles, GraphiteResources, GraphiteAppleEvents, GraphiteQuickDraw, GraphiteText, GraphiteSound, GraphiteWindows, GraphiteMenus and GraphiteControls are in place, apart from the MACE codecs and double-buffered sound. The Toolbox's dialogs come next.

Modules

Module
GraphiteBase Points and rectangles, four-character codes, result codes, Pascal strings and MacRoman, Mac dates and clocks, Fixed, Fract, 80-bit extended and SANE decimal numbers, QuickDraw's Random, big-endian reading and writing, the Script Manager's answers and Gestalt's table.
GraphiteTasks The Time Manager, Multiprocessing Services (tasks, queues, events, semaphores, critical regions, aligned memory), AbsoluteTime and durations, and atomic operations on a program's memory.
GraphiteFiles The File Manager over a POSIX file system: FSSpecs and colon paths, folder IDs, names in any case, forks by reference number, resource forks and Finder info in AppleDouble files (._name) or name.rsrc, catalog information, aliases that follow moved items, and FindFolder's folders.
GraphiteResources The Resource Manager: resource maps read with every offset checked and written back, resource files changed in memory and updated, the chain of open files with Get and Get1 searches, and STR#, STR and vers resources.
GraphiteAppleEvents The Apple Event Manager: descriptors, lists, records and Apple events as values; coercions between text, numbers, booleans, codes, lists, records and file types (furl, fss , alis, and the host's), with installed handlers; the application's handlers by class and ID with wildcards; events sent to itself, and the required events.
GraphiteQuickDraw QuickDraw's pixels (1 to 8 bits indexed, 16 and 32 bits direct, big-endian on every CPU, in memory of its own or a program's), colors and the system's color tables, regions as real regions with QuickDraw's region records, patterns, transfer modes, ports, devices and offscreen worlds; drawing rectangles, ovals, round rectangles, arcs, polygons, regions and lines with the pen, and CopyBits, CopyMask and ScrollRect between any depths; PICT pictures, version 1 and 2, with PackBits and QuickTime-compressed images through a decoder the host gives.
GraphiteText Fonts: faces and their metrics, the Font Manager's family numbers and names with the classic fonts' and their substitutes, a built-in bitmap system font (from Pomme), and QuickDraw's text in a port's font, face, size and mode, with the styles a face hasn't drawn from its plain glyphs; ATSUI-style layouts with styled runs, line breaking, measuring, hit testing and drawing, and the text in PICT pictures; and, with the FreeType trait (on by default), faces from TrueType, OpenType and other outline fonts, found by family name in the system's font folders.
GraphiteSound The Sound Manager: sampled sounds from 'snd ' resources (formats 1 and 2, with the standard, compressed and extended headers) and AIFF and AIFF-C files, read to 16-bit PCM; the µ-law, A-law and IMA4 codecs (MACE is recognized but not decoded yet); sound channels with queued commands (SndDoCommand) including callBackCmd, waitCmd and syncCmd, immediate ones, volume, pan, pitch, loops and completion; a mixer that renders them to stereo float frames, for an audio device the host provides; and the frequencies of MIDI notes.
GraphiteWindows The Window Manager: windows of every class, from documents, dialogs and alerts to palettes, sheets and drawers, each with the standard Mac OS 8 and 9 frame or a window definition the host brings (the WDEF); the window list ordered by layer, FrontWindow, and the active document and the active palette; FindWindow and every part of a frame; showing, hiding, selecting, zooming between the user and standard states, collapsing; MoveWindow, SizeWindow and SetWindowBounds, with centering, cascading and the alert position; each window's content in an offscreen buffer with its visible region — what no window in front covers — its update region with BeginUpdate and EndUpdate, and the part of it the host is told to flush.
GraphiteMenus The Menu Manager: menus and their items — text in MacRoman, marks (the check mark and the diamond), icons, styles, keyboard equivalents with their modifiers, submenus by menu ID, command IDs — built with AppendMenu's metacharacter strings or item by item; checked, enabled and disabled; the menu bar with InsertMenu's order and its hierarchical list, GetMenuHandle, MenuKey and EnableMenuCommand; menu definitions (the MDEF) with the standard definition's sizes and item geometry; and 'MENU' resources read with every field as the classic kept it, enable flags and submenus included.
GraphiteControls The Control Manager: controls of the classic's five kinds — buttons, checkboxes, radio buttons, scroll bars and pop-up menus, their control definition IDs and the useWFont variation read as the kind beneath it; values, ranges and the pinning SetControlValue did, with the pop-up menu's resource ID and title width in its minimum and maximum as the classic kept them; titles, reference values, command IDs, identifiers, enabling, disabling, activating and HiliteControl's pressed part; the parts a point hits (inButton, inCheckBox, a scroll bar's arrows, pages and thumb), the fixed-size thumb of the classic's scroll bars and the value a drag names; a window's control list, FindControl, GetControlByID and Carbon's root control; and 'CNTL' resources read field by field.
Graphite Re-exports the modules above.

Everything depends only on the Swift standard library, the C library (Glibc, Musl or Darwin), Foundation (for bytes and property lists) and System: Apple's on macOS, or one installed with CMake, else swift-system's package. With the FreeType trait, GraphiteText also depends on swift-freetype. Graphite builds for Linux on x86_64, arm64, i686 and 32-bit PowerPC, and for macOS, where it can be compared with Apple's CarbonCore.

Usage

import Graphite

let rect = Rect(top: 0, left: 0, bottom: 100, right: 200)
let pinned = rect.pin(Point(v: 150, h: -5))           // (v: 99, h: 0)

let name = PascalString("Café")!                       // MacRoman bytes
let type: FourCharCode = "snd "

let date = MacDate.now.parts                           // DateTimeRec

let files = FileSystem()
let (spec, _) = try files.makeSpec(volume: 0, directory: 0, name: ":Data:Level 1")
let resources = ResourceChain(fileSystem: files)
try resources.open(files.path(of: spec)!)
let messages = try StringList(bytes: resources.get(type: "STR#", id: 128).bytes ?? [])

let queue = MPQueue()
let task = try MPTask.create(queue: queue, terminationWords: (1, 2)) { 0 }
let ended = try queue.wait(timeout: .milliseconds(1000))

Building

Outline fonts need FreeType (libfreetype-dev on Debian, brew install freetype on macOS). Without it, build with swift build --disable-default-traits: text then uses the built-in bitmap font alone.

swift build
swift test

The optional GraphiteSDLEvents product supplies an SDL3 event-loop host. Install SDL3 (brew install sdl3 or the SDL3 development package) and enable its trait with swift build --traits SDL3. Initialize SDL's event subsystem before constructing SDLEventHost, and supply a callback for SDL events the host does not translate. The SDL event pump must run on the main thread. The host's globalPosition callback maps window-local SDL mouse coordinates to global coordinates; provide it when windows are not at the origin. Translated events carry their SDL window ID in the swin parameter.

License

MIT. Code ported from Pomme keeps its MIT notice in each ported file.

About

Swift Classic Toolbox API

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages