How to expand Swift module functionality? How to apply custom font in Cairo?

Viewed 38

My Swift Vapor backend code uses this Graphics module: https://github.com/fwcd/swift-graphics

Graphics uses Cairo module: https://github.com/fwcd/swift-cairo.git

Graphics module has the CairoGraphics class.

When it is initalized:

import Cairo
import Utils

public class CairoGraphics: Graphics {
    private let context: Cairo.Context // <-- this is private, can not access from outer code
    private let image: Image

    deinit {
        flush()
    }

    public init(fromImage image: Image) {
        context = Cairo.Context(surface: image.surface) // <-- created here
        self.image = image
    }

it creates a Cairo.Context object.

Cairo.Context has a method, I would call from my code, to set custom font for rendering.

public func setFont(face: (family: String, slant: Cairo.FontSlant, weight: Cairo.FontWeight))
  1. How can I call this method from the outer code? CairoGraphics does not have a public property for it, only private, I highlighted above.

I know this is a naive question, but:

  1. Can I add a public method to CairoGraphics? Or there is a CairoGraphics's binary code, and this swift file is only an 'interface', or 'agreement'? If I add a new method it will not trigger anything as the 'binary' does not have corresponding logic?

I tried to add an extension, but the this error:

'context' is inaccessible due to 'private' protection level

extension CairoGraphics {
    func getContext() -> Cairo.Context {
        return context
    }
}

Or shall I pull, fork, push to GitHub, do slight changes, and use my own version?

0 Answers
Related