Skip to content

Commit

Permalink
Adding support for BLGlyphRunIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizZak committed May 25, 2019
1 parent b6b604b commit db1ee03
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 deletions.
125 changes: 125 additions & 0 deletions Sources/SwiftBlend2D/BLGlyphRun.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import blend2d

public extension BLGlyphRun {
var isEmpty: Bool {
return size == 0
}

mutating func setGlyphIdData(_ glyphIds: UnsafeMutablePointer<BLGlyphId>) {
setGlyphIdData(glyphIds, advance: MemoryLayout<BLGlyphId>.size)
}
mutating func setGlyphItemData(_ itemData: UnsafeMutablePointer<BLGlyphItem>) {
setGlyphIdData(itemData, advance: MemoryLayout<BLGlyphItem>.size)
}

mutating func setGlyphIdData(_ data: UnsafeMutableRawPointer, advance: Int) {
glyphIdData = data
glyphIdAdvance = Int8(advance)
}

mutating func resetGlyphIdData() {
glyphIdData = nil
glyphIdAdvance = 0
}

mutating func setPlacementData<T>(_ data: UnsafeMutablePointer<T>) {
setPlacementData(data, advance: MemoryLayout<T>.stride)
}

mutating func setPlacementData(_ data: UnsafeMutableRawPointer, advance: Int) {
placementData = data
placementAdvance = Int8(advance)
}

mutating func resetPlacementData() {
placementData = nil
placementAdvance = 0
}
}

/// A helper to iterate over a `BLGlyphRun`.
///
/// Takes into consideration glyph-id advance and glyph-offset advance.
///
/// Example:
///
/// ```
/// func inspectGlyphRun(_ glyphRun: BLGlyphRun) {
/// var it = BLGlyphRunIterator(glyphRun: self)
/// if it.hasPlacement {
/// while !it.atEnd {
/// let glyphId = it.glyphId()
/// let offset = it.placement(as: BLPoint.self)
///
/// // Do something with `glyphId` and `offset`.
///
/// it.advance()
/// }
/// } else {
/// while !it.atEnd {
/// let glyphId = it.glyphId()
///
/// // Do something with `glyphId`.
///
/// it.advance()
/// }
/// }
/// }
/// ```
public struct BLGlyphRunIterator {
var index: Int
var size: Int
var glyphIdData: UnsafeMutableRawPointer?
var placementData: UnsafeMutableRawPointer?
var glyphIdAdvance: Int
var placementAdvance: Int

var glyphRun: BLGlyphRun

public var atEnd: Bool {
return index >= size
}

public var hasPlacement: Bool {
return placementData != nil
}

init(glyphRun: BLGlyphRun) {
index = 0
size = glyphRun.size
glyphIdData = glyphRun.glyphIdData
placementData = glyphRun.placementData
glyphIdAdvance = Int(glyphRun.glyphIdAdvance)
placementAdvance = Int(glyphRun.placementAdvance)

if BLByteOrder.native == .be, var glyphIdData = glyphIdData {
glyphIdData =
UnsafeMutableRawPointer(
glyphIdData.assumingMemoryBound(to: UInt8.self)
+ Int(max(glyphRun.glyphIdSize, 2) - 2)
)

self.glyphIdData = glyphIdData
}

self.glyphRun = glyphRun
}

public func glyphId() -> BLGlyphId? {
return glyphIdData?.load(as: BLGlyphId.self)
}

public func placement<T>(as type: T.Type) -> T? {
return placementData?.load(as: type)
}

public mutating func advance() {
if index >= size {
return
}

index += 1
glyphIdData = glyphIdData.map { $0 + glyphIdAdvance }
placementData = placementData.map { $0 + placementAdvance }
}
}
12 changes: 12 additions & 0 deletions Sources/SwiftBlend2D/Enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,18 @@ public extension BLStyleType {
static let gradient = BL_STYLE_TYPE_GRADIENT
}

public extension BLByteOrder {
/// Little endian byte-order.
static let le = BL_BYTE_ORDER_LE
/// Big endian byte-order.
static let be = BL_BYTE_ORDER_BE

/// Native (host) byte-order.
static let native = BL_BYTE_ORDER_NATIVE
/// Swapped byte-order (BE if host is LE and vice versa).
static let swapped = BL_BYTE_ORDER_SWAPPED
}

extension BLImageCodecFeatures: OptionSet { }
extension BLPathFlags: OptionSet { }
extension BLContextCreateFlags: OptionSet { }
Expand Down
4 changes: 2 additions & 2 deletions Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ This may change at any moment.
`BLGlyphMappingState` | ℹ️ No work required | Character to glyph mapping state |
`BLGlyphOutlineSinkInfo` | ℹ️ No work required | Information passed to a BLPathSinkFunc sink by BLFont::getGlyphOutlines()|
`BLGlyphPlacement` | ℹ️ No work required | |
`BLGlyphRun` | ❌ Unstarted | |
`BLGlyphRunIterator` | ❌ Unstarted | |
`BLGlyphRun` | 🕒 Partial | `(Missing Swift tests)` |
`BLGlyphRunIterator` | 🕒 Partial | `(Missing Swift tests)` |
`BLGradient` | 🕒 Partial | Gradient [C++ API]|
`BLGradientStop` | ✅ Done | |
`BLImage` | 🕒 Partial | 2D raster image [C++ API]|
Expand Down

0 comments on commit db1ee03

Please sign in to comment.