SkiaKit icon indicating copy to clipboard operation
SkiaKit copied to clipboard

GetGlyphWidths using sk_font_get_widths_bounds

Open aakrandan opened this issue 1 year ago • 1 comments

`public func getGlyphWidths(text: String) -> [Float] { var paint: Paint? = nil var textChar = Character

    var n = countGlyphs(str: text)
    if n <= 0 {
        return [Float]()
    }
    
    var widths: [Float] = Array.init (repeating: Float.zero, count: Int(n))
    var bounds: [Rect] = Array.init (repeating: Rect(width: 0, height: 0), count: Int(n))
    
    var w: UnsafeMutablePointer<Float>? = nil
    if !widths.isEmpty {
        w = UnsafeMutablePointer<Float>.allocate(capacity: widths.count)
        w?.initialize(from: &widths, count: widths.count)
    }

    var b: UnsafeMutablePointer<Rect>? = nil
    if !bounds.isEmpty {
        b = UnsafeMutablePointer<Rect>.allocate(capacity: bounds.count)
        b?.initialize(from: &bounds, count: bounds.count)
    }

    
    if var glyphs = getGlyphs(str: text) {
        sk_font_get_widths_bounds(handle, &glyphs, Int32 (glyphs.count), w, b, paint?.handle)
    }
    
    
    // Deallocate memory and free pointers when done
    w?.deallocate()
    b?.deallocate()
    
    return widths
}

` I have implemented the above API, could you please let me know if there is something that i missed here?

aakrandan avatar Jun 21 '23 12:06 aakrandan

For some reason the format is not working for me, but i hope the code i pasted is readable. Any help is appreciated. Thanks!

aakrandan avatar Jun 21 '23 12:06 aakrandan