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
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ var package = Package(
),
.package(
url: "https://github.com/PureSwift/Silica.git",
branch: "master"
branch: "master",
// only the Silica module: not its Cairo or web renderers' packages
traits: []
),
],
targets: [
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@ Then you can compile the app from the Android IDE.

## Drawing with Silica

`AndroidSilica` draws [Silica](https://github.com/PureSwift/Silica)'s drawing API on `android.graphics.Canvas`. It is a product of its own, and `AndroidKit` doesn't include it.
`AndroidSilica` draws [Silica](https://github.com/PureSwift/Silica)'s drawing API on `android.graphics.Canvas`, with `AndroidCanvasRenderer`. It is a product of its own, and `AndroidKit` doesn't include it.

```swift
import AndroidSilica

let context = try AndroidCanvasContext(pdf: url, size: CGSize(width: 200, height: 200))
// ... CoreGraphics style drawing code ...
let context = GraphicsContext(try AndroidCanvasRenderer(pdf: url, size: Silica.Size(width: 200, height: 200)))
context.fill(Silica.Path(ellipseIn: Silica.Rect(x: 20, y: 20, width: 160, height: 160)), with: .red)
try context.finish() // writes the PDF
```

`AndroidCanvasContext` also has `init(bitmap:)` for raster output, and `init(canvas:size:)` to draw into a canvas you already have, such as the one a view's draw pass hands you.
`AndroidCanvasRenderer` also has `init(bitmap:scale:)` for pixels, and `init(canvas:size:scale:)` to draw into a canvas you already have, such as the one a view's draw pass hands you. AndroidGraphics has its own `Path`, `Rect`, `Point`, `Color`, `Font` and `BlendMode`: in a file that imports both, write Silica's with the module name.

- Requires **API 31+** at runtime for glyph rendering (`Canvas.drawGlyphs`, `TextRunShaper`).
- `finish()` is **mandatory** for PDF contexts — the document is only written to disk when it is called.
- JNI-backed objects must be used from a JVM-attached thread; create, use, and finish a context on the same thread.
- The linear (scale/rotation) components of the text matrix are not applied to glyph shapes.
- Unknown font names fall back to the system typeface (Android provides no font-matching failure signal).
- Shadows are drawn without blur (a silhouette at the shadow offset).
- Blend modes `PorterDuff` lacks draw as normal (color dodge and burn, soft and hard light, difference, exclusion, hue, saturation, color, luminosity, plus darker), and a transparency layer casts no shadow.
- Radial gradients have one center: two circles with different centers draw around the end circle's.

### See Also

Expand Down
52 changes: 20 additions & 32 deletions Sources/AndroidSilica/AndroidBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,52 @@ import Foundation
import Silica

/// Android Canvas rendering backend.
/// The Android rendering backend: fonts from `Typeface`, PNG codecs from
/// `BitmapFactory` and `Bitmap`, and bitmaps drawn with `Canvas`.
public enum AndroidBackend: SilicaBackendProtocol {

/// Registers Android as the default rendering backend.
/// Makes Android the default rendering backend.
public static func register() {

SilicaBackend.default = AndroidBackend.self
SilicaBackend.register(AndroidBackend.self, name: "Android")
}

internal static func registerIfNeeded() {

guard SilicaBackend.default == nil else { return }
register()
SilicaBackend.registerIfNeeded(AndroidBackend.self, name: "Android")
}

// MARK: - SilicaBackendProtocol

public static func font(named name: String) -> Silica.CGFont? {

guard let handle = AndroidFontHandle(name: name)
else { return nil }

return Silica.CGFont(name: name, family: handle.familyName, handle: handle)
public static func fontFace(named name: String) -> (any Silica.FontFace)? {
AndroidFontHandle(name: name)
}

public static func decodePNG(_ data: Data) -> Silica.CGImage? {

public static func decodePNG(_ data: Data) -> Silica.Image? {
let bytes = data.map { Int8(bitPattern: $0) }

guard let factoryClass = try? JavaClass<BitmapFactory>(),
let bitmap = factoryClass.decodeByteArray(bytes, 0, Int32(bytes.count))
else { return nil }

return Silica.CGImage(bitmap)
return Silica.Image(bitmap)
}

public static func encodePNG(_ image: Silica.CGImage) -> Data? {

guard let bitmap = image.toAndroidBitmap()
else { return nil }

public static func encodePNG(_ image: Silica.Image) -> Data? {
guard let bitmap = image.toAndroidBitmap() else { return nil }
let stream = ByteArrayOutputStream()

guard bitmap.compress(Bitmap.CompressFormat(.PNG), 100, stream)
else { return nil }

guard bitmap.compress(Bitmap.CompressFormat(.PNG), 100, stream) else { return nil }
let bytes = stream.toByteArray()

return Data(bytes.map { UInt8(bitPattern: $0) })
}

public static func makeBitmapRenderer(size: Silica.Size, scale: Double) -> (any Renderer)? {
try? AndroidCanvasRenderer(bitmap: size, scale: scale)
}
}

// MARK: - CGImage Conversion

public extension Silica.CGImage {
public extension Silica.Image {

/// Creates a Silica bitmap image from an Android bitmap,
/// An image of an Android bitmap's pixels,
/// normalizing the pixel format to premultiplied RGBA.
convenience init?(_ bitmap: AndroidGraphics.Bitmap) {
convenience init?(_ bitmap: AndroidGraphics.Bitmap, scale: Double = 1) {

let width = Int(bitmap.getWidth())
let height = Int(bitmap.getHeight())
Expand Down Expand Up @@ -114,7 +102,7 @@ public extension Silica.CGImage {
}
}

self.init(width: width, height: height, bytesPerRow: bytesPerRow, data: data)
self.init(width: width, height: height, bytesPerRow: bytesPerRow, data: data, scale: scale)
}

/// Converts to an Android bitmap (`ARGB_8888`).
Expand Down
Loading
Loading