Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
47 changes: 27 additions & 20 deletions host/apple/Sources/ExactKit/Gradient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ struct Gradient {
case conic(CGPoint, CGFloat)
}

private let css: String?
let css: String?
/// The dark scheme's `css` where it interpolates otherwise.
private let darkCss: String?
let darkCss: String?
/// The view's tint, light then dark: extended sRGB, unclipped, and alpha 0–1.
private let tint: [[Float]]?
private let resolvedPlacement: Placement?
Expand All @@ -41,7 +41,7 @@ struct Gradient {
/// extended linear sRGB with alpha 0–1.
private let lightLinear: Bool
private let darkLinear: Bool
private func linear(dark: Bool) -> Bool { dark && self.dark != nil ? darkLinear : lightLinear }
func linear(dark: Bool) -> Bool { dark && self.dark != nil ? darkLinear : lightLinear }

init?(_ value: BatchValue?, tint: PlatformColor? = nil) {
guard case .object(let o)? = value else { return nil }
Expand Down Expand Up @@ -82,7 +82,7 @@ struct Gradient {
}

/// Resolve in the kernel after the presenter knows the gradient box.
private func resolved(in box: CGRect, dark: Bool) -> Gradient? {
func resolved(in box: CGRect, dark: Bool) -> Gradient? {
guard let css = dark ? darkCss ?? css : css else { return nil }
return Owner.shared.sync {
let input = Array(css.utf8), w = Float(box.width), h = Float(box.height)
Expand Down Expand Up @@ -235,24 +235,27 @@ struct Gradient {
}
let (locations, colors) = stops(dark: dark)
if case .axial(let a, let b) = place {
let cuts = locations.indices.dropFirst().filter {
locations[$0] == locations[$0 - 1] && locations[$0] > locations[0] && locations[$0] < locations.last!
}
if !cuts.isEmpty {
paintAxialRuns(ctx, start: a, end: b, locations: locations, colors: colors, cuts: Array(cuts), dark: dark)
return
}
paintAxial(ctx, start: a, end: b, locations: locations, colors: colors, dark: dark)
return
}
guard let g = CGGradient(colorsSpace: CGColorSpace(name: linear(dark: dark) ? CGColorSpace.extendedLinearSRGB : CGColorSpace.sRGB), colors: colors as CFArray, locations: locations) else { return }
switch place {
case .axial(let a, let b):
ctx.drawLinearGradient(g, start: a, end: b, options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])
case .radial(let c, let r):
ctx.translateBy(x: c.x, y: c.y); ctx.scaleBy(x: r.width, y: r.height)
ctx.drawRadialGradient(g, startCenter: .zero, startRadius: 0, endCenter: .zero, endRadius: 1, options: [.drawsAfterEndLocation])
case .conic:
break
guard case .radial(let c, let r) = place,
let g = CGGradient(colorsSpace: CGColorSpace(name: linear(dark: dark) ? CGColorSpace.extendedLinearSRGB : CGColorSpace.sRGB), colors: colors as CFArray, locations: locations) else { return }
ctx.translateBy(x: c.x, y: c.y); ctx.scaleBy(x: r.width, y: r.height)
ctx.drawRadialGradient(g, startCenter: .zero, startRadius: 0, endCenter: .zero, endRadius: 1, options: [.drawsAfterEndLocation])
}

/// A linear ramp from `start` to `end` over the clip, its stops
/// `locations` along that line.
func paintAxial(_ ctx: CGContext, start a: CGPoint, end b: CGPoint, locations: [CGFloat], colors: [CGColor], dark: Bool) {
let cuts = locations.indices.dropFirst().filter {
locations[$0] == locations[$0 - 1] && locations[$0] > locations[0] && locations[$0] < locations.last!
}
if !cuts.isEmpty {
paintAxialRuns(ctx, start: a, end: b, locations: locations, colors: colors, cuts: Array(cuts), dark: dark)
return
}
guard let g = CGGradient(colorsSpace: CGColorSpace(name: linear(dark: dark) ? CGColorSpace.extendedLinearSRGB : CGColorSpace.sRGB), colors: colors as CFArray, locations: locations) else { return }
ctx.drawLinearGradient(g, start: a, end: b, options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])
}

/// Core Graphics quantizes hard stops inside a ramp: an expanded hatch
Expand Down Expand Up @@ -430,6 +433,10 @@ extension NodeView {
// A fixed gradient is the layer's (LLP 1066 D7).
if gradientLayered && boxGradient != nil { return }
#endif
#if os(iOS) || os(tvOS)
// Tiles are their layers' (`GradientTilesIOS.swift`).
if gradientTiles?.layer != nil { return }
#endif
// The last layer first, so the first is on top (LLP 1077 D5).
for g in Gradient.layers(style["background_image"], tint: gradientTint).reversed() {
g.paint(ctx, clip: clip, box: gradientBox, dark: drawsDark)
Expand Down
188 changes: 188 additions & 0 deletions host/apple/Sources/ExactKit/GradientTile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// A repeating linear gradient as tiles of device pixels. Its stripes run
// across the box at one angle, so the picture repeats along each axis after
// the period divided by that axis's share of the gradient line: tiles about
// that size fall at nearly the same place in a period, and those that fall
// at the same place share one picture.
import CoreGraphics
import Foundation

extension Gradient {
/// One period of a repeating linear gradient whose stops are all
/// lengths, so it is the same period at every box size.
struct Period {
/// The resolved gradient: its colour space.
fileprivate let ramp: Gradient
fileprivate let dark: Bool
/// The gradient line's unit direction, y down.
let direction: CGPoint
/// Points along the line.
let length: CGFloat
/// Where a period begins, in points along the line from the
/// corner the line starts at (CSS places the line's start on the
/// perpendicular through that corner), 0 up to `length`.
let offset: CGFloat
/// One period's stops, 0 to 1 of it.
let locations: [CGFloat]
let colors: [CGColor]
}

/// The period, when the gradient is a repeating linear one whose
/// period and place along the line do not change with the box: no
/// percentage stop and no `to <corner>`, which it checks by resolving
/// at two sizes. A percentage that an earlier length stop clamps at
/// both sizes is free at a larger one, so any percentage is refused
/// first: the kernel writes colours as hex, so a `%` is a stop.
func period(dark: Bool) -> Period? {
guard let css = dark ? darkCss ?? css : css, css.hasPrefix("repeating-linear-gradient("), !css.contains("%") else { return nil }
let boxes = [CGRect(x: 0, y: 0, width: 1000, height: 1000), CGRect(x: 0, y: 0, width: 1037, height: 1291)]
let periods = boxes.compactMap { box in resolved(in: box, dark: dark)?.cycle(in: box, dark: dark) }
guard periods.count == 2, periods[0].matches(periods[1]) else { return nil }
return periods[0]
}

/// The resolved gradient's whole periods, laid end to end from the
/// line's start (`placed.rs`), as one period.
private func cycle(in box: CGRect, dark: Bool) -> Period? {
guard case .angle = shape, !linear(dark: dark), case .axial(let a, let b) = placement(in: box) else { return nil }
let (locations, colors) = stops(dark: dark)
let span = hypot(b.x - a.x, b.y - a.y), n = locations.count
guard n >= 2, span > 0 else { return nil }
// Premultiplying can add a stop where one period meets the next,
// which the line's start has none of, so the stop count need not
// divide evenly: a period is the shortest run from the first stop
// that the rest repeats, at least twice over.
guard let count = (1...(n / 2)).first(where: { q in
let d = locations[q] - locations[0]
return d > 0 && (q..<n).allSatisfy { colors[$0] == colors[$0 - q] && abs(locations[$0] - locations[$0 - q] - d) < 1e-5 }
}) else { return nil }
let d = locations[count] - locations[0]
var direction = CGPoint(x: (b.x - a.x) / span, y: (b.y - a.y) / span)
// The kernel's line is single precision: a right angle it misses by
// a hair would otherwise repeat along that axis only after millions
// of pixels.
if abs(direction.x) < 1e-5 { direction = CGPoint(x: 0, y: direction.y < 0 ? -1 : 1) }
if abs(direction.y) < 1e-5 { direction = CGPoint(x: direction.x < 0 ? -1 : 1, y: 0) }
let length = d * span
let corner = Gradient.startCorner(of: box, direction: direction)
let along = (a.x - corner.x) * direction.x + (a.y - corner.y) * direction.y
let offset = along - (along / length).rounded(.down) * length
return Period(ramp: self, dark: dark, direction: direction, length: length, offset: offset,
locations: locations[..<count].map { ($0 - locations[0]) / d }, colors: Array(colors[..<count]))
}

/// The corner of `box` the gradient line starts across: the least far
/// along `direction`.
static func startCorner(of box: CGRect, direction u: CGPoint) -> CGPoint {
CGPoint(x: u.x > 0 ? box.minX : box.maxX, y: u.y > 0 ? box.minY : box.maxY)
}
}

extension Gradient.Period {
fileprivate func matches(_ other: Gradient.Period) -> Bool {
let tolerance: CGFloat = 1e-3
var shift = abs(offset - other.offset)
shift = min(shift, length - shift)
return abs(length - other.length) < tolerance && shift < tolerance
&& abs(direction.x - other.direction.x) < 1e-6 && abs(direction.y - other.direction.y) < 1e-6
&& locations.count == other.locations.count && colors == other.colors
&& zip(locations, other.locations).allSatisfy { abs($0 - $1) < 1e-4 }
}

/// A tile's device pixels along each axis: a whole number of pixels
/// that comes closest to whole periods, so that tiles fall at nearly
/// the same place in a period and share pictures, grown to about 256
/// so a box needs few tiles; 256 along an axis the stripes run along,
/// or one too long to repeat within 512.
func tile(scale: CGFloat) -> (width: Int, height: Int) {
func side(_ share: CGFloat) -> Int {
let limit = 512
let repeats = share == 0 ? .infinity : length * scale / abs(share)
guard repeats <= CGFloat(limit) else { return 256 }
var best = (pixels: 256, error: CGFloat.infinity)
for k in 1...Int(CGFloat(limit) / repeats) {
let exact = repeats * CGFloat(k), pixels = max(1, Int(exact.rounded()))
let error = abs(exact - CGFloat(pixels)) / CGFloat(pixels)
if error < best.error - 1e-12 { best = (pixels, error) }
}
return best.pixels * max(1, 256 / best.pixels)
}
return (side(direction.x), side(direction.y))
}

/// Tiles over a layer's `bounds` for a gradient box `box`.
struct Layout {
/// One tile's device pixels.
let width: Int, height: Int
/// The first tile's top left, from the bounds' top left: at most a
/// tile up and left of it, in whole device pixels.
let origin: CGPoint
let columns: Int, rows: Int
/// The tile at the start corner, and how far along the line its
/// top left is past where a period begins, in points.
let anchor: (column: Int, row: Int)
let phase: CGFloat
/// How much further along the line each column and row is, in points.
let column: CGFloat, row: CGFloat
}

/// The tiles start from the device pixel of the corner the line starts
/// at, so a size that moves that corner by whole pixels moves them and
/// leaves each tile's picture as it was. A size that moves it within a
/// pixel changes every tile's phase and draws their pictures again; a
/// sheet dragged on the iOS simulator was sized in whole pixels.
func layout(box: CGRect, bounds: CGRect, scale: CGFloat) -> Layout? {
guard bounds.width > 0, bounds.height > 0 else { return nil }
let (w, h) = tile(scale: scale), (tw, th) = (CGFloat(w), CGFloat(h)), u = direction
let corner = Gradient.startCorner(of: box, direction: u)
let ax = ((corner.x - bounds.minX) * scale).rounded(), ay = ((corner.y - bounds.minY) * scale).rounded()
let (jx, jy) = ((ax / tw).rounded(.up), (ay / th).rounded(.up))
let gx = ax - tw * jx, gy = ay - th * jy
// The tile inside the box at the corner: right of and below it, or left of and above it.
let (column, row) = (Int(jx) - (u.x > 0 ? 0 : 1), Int(jy) - (u.y > 0 ? 0 : 1))
let top = CGPoint(x: bounds.minX + (gx + CGFloat(column) * tw) / scale, y: bounds.minY + (gy + CGFloat(row) * th) / scale)
let along = (top.x - corner.x) * u.x + (top.y - corner.y) * u.y - offset
return Layout(width: w, height: h, origin: CGPoint(x: gx / scale, y: gy / scale),
columns: Int(((bounds.width * scale - gx) / tw).rounded(.up)), rows: Int(((bounds.height * scale - gy) / th).rounded(.up)),
anchor: (column, row), phase: along - (along / length).rounded(.down) * length, column: tw / scale * u.x, row: th / scale * u.y)
}

/// The picture for the tile at `column`, `row`: how far its phase is
/// from the corner tile's, less whole periods, in thirty-seconds of a
/// device pixel. Tiles at the corner tile's phase share its exact
/// picture.
func key(_ layout: Layout, column: Int, row: Int, scale: CGFloat) -> Int {
let along = layout.column * CGFloat(column - layout.anchor.column) + layout.row * CGFloat(row - layout.anchor.row)
return Int(((along - (along / length).rounded() * length) * scale * 32).rounded())
}

/// The tile picture for `key` (`key(_:column:row:scale:)`).
func image(_ layout: Layout, key: Int, scale: CGFloat) -> CGImage? {
let along = layout.phase + CGFloat(key) / (scale * 32)
return image(width: layout.width, height: layout.height, scale: scale, phase: along - (along / length).rounded(.down) * length)
}

/// A tile `width` × `height` device pixels at `scale` whose top left
/// is `phase` points along the line from where a period begins, drawn
/// as `Gradient.paint` draws the whole box.
func image(width: Int, height: Int, scale: CGFloat, phase: CGFloat) -> CGImage? {
guard let ctx = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0,
space: CGColorSpace(name: CGColorSpace.sRGB)!,
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return nil }
// y down, as the view's backing store.
ctx.translateBy(x: 0, y: CGFloat(height)); ctx.scaleBy(x: scale, y: -scale)
let rect = CGRect(x: 0, y: 0, width: CGFloat(width) / scale, height: CGFloat(height) / scale)
let u = direction, origin = CGPoint(x: -u.x * phase, y: -u.y * phase)
let along = [CGPoint(x: rect.minX, y: rect.minY), CGPoint(x: rect.maxX, y: rect.minY), CGPoint(x: rect.minX, y: rect.maxY), CGPoint(x: rect.maxX, y: rect.maxY)]
.map { (($0.x - origin.x) * u.x + ($0.y - origin.y) * u.y) / length }
let first = Int(along.min()!.rounded(.down)) - 1, last = Int(along.max()!.rounded(.up)) + 1, count = last - first
var stops: [CGFloat] = [], colors: [CGColor] = []
for c in 0..<count {
stops += locations.map { (CGFloat(c) + $0) / CGFloat(count) }
colors += self.colors
}
func point(_ periods: Int) -> CGPoint { CGPoint(x: origin.x + u.x * length * CGFloat(periods), y: origin.y + u.y * length * CGFloat(periods)) }
ctx.addRect(rect); ctx.clip()
ramp.paintAxial(ctx, start: point(first), end: point(last), locations: stops, colors: colors, dark: dark)
return ctx.makeImage()
}
}
7 changes: 4 additions & 3 deletions host/apple/Sources/ExactKit/IOS/BoxLayerIOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ extension NodeView {
/// `draw(_:)` paints the gradient there instead, in the same place —
/// unless it is `background-attachment: fixed` (`gradientLayered`).
func applyGradientLayer() {
applyGradientTiles()
let fixed = gradientLayered
guard !cssVisibilityHidden, !drawsPaint || fixed, surface == nil, let gradient = Gradient(style["background_image"], tint: gradientTint), !gradient.needsDrawing else {
boxGradient?.removeFromSuperlayer(); boxGradient = nil
Expand Down Expand Up @@ -355,7 +356,7 @@ extension NodeView {
// content painted there, or it covers the image or text.
let paintsContent = (kind == "image" && symbolView == nil && raster != nil) || (isParagraph && !canRasterText)
|| (Capture.capturing && (kind == "canvas" || Capture.web[id] != nil))
boxDrawn = !away && ((shapedRadii && paintsContent) || patterned || !((oneBorder || edges) && (oneRadius || shapedRadii) && (shape == nil || continuous)) || gradientDraws || backgroundClip != "border-box") && (fill != nil || gradient || widths.contains { $0 > 0 })
boxDrawn = !away && ((shapedRadii && paintsContent) || patterned || !((oneBorder || edges) && (oneRadius || shapedRadii) && (shape == nil || continuous)) || (gradientDraws && !gradientTiled) || backgroundClip != "border-box") && (fill != nil || gradient || widths.contains { $0 > 0 })
let onLayer = !boxDrawn
var corners: CACornerMask = []
let masks: [CACornerMask] = [.layerMinXMinYCorner, .layerMaxXMinYCorner, .layerMaxXMaxYCorner, .layerMinXMaxYCorner]
Expand Down Expand Up @@ -436,10 +437,10 @@ extension NodeView {
}
extension NodeView {
/// The box's sublayers keep CSS's paint order, all under the children:
/// shadow, gradient, inset shadow, image, border, text — whichever of
/// shadow, gradient, its tiles, inset shadow, image, border, text — whichever of
/// them joins first (as `Mac/BoxLayerMac.swift`).
func insertBoxSublayer(_ l: CALayer) {
let order: [CALayer?] = [shadowCaster, boxFill, boxGradient, insetCaster, imageLayer, boxBorder, textBoxLayer]
let order: [CALayer?] = [shadowCaster, boxFill, boxGradient, gradientTiles?.layer, insetCaster, imageLayer, boxBorder, textBoxLayer]
guard let rank = order.firstIndex(where: { $0 === l }) else { return }
if let below = order[..<rank].reversed().compactMap({ $0 }).first(where: { $0.superlayer === layer }) {
layer.insertSublayer(l, above: below)
Expand Down
Loading
Loading