package org.jetbrains.skia

import org.jetbrains.skia.impl.*

/**
 * Fusion-renderer (OHRender) only: create a [Typeface] directly from font [data].
 *
 * Backed by the OHRender fork's `SkTypeface::MakeFromData` (a data-only typeface;
 * its glyphs are resolved later when `TypefaceFontProvider::registerTypeface`
 * registers the buffer into the OHOS FontCollection by family name).
 *
 * This restores the `Typeface.makeFromData` binding that skiko removed in
 * d04654c6 ("update to skia m126 #969") — upstream skia m126 dropped the public
 * `SkTypeface::MakeFromData`, which is why it was removed for all targets. It is
 * re-added here in the fusion ohos source set ONLY (paired with the
 * `org_jetbrains_skia_Typeface__1nMakeFromData` C++ binding guarded by
 * `#ifdef USE_OHRENDER`), so iOS / native / js / ohos-skia are unaffected.
 */
fun Typeface.Companion.makeFromData(data: Data, index: Int = 0): Typeface {
    return try {
        Stats.onNativeCall()
        val ptr = _nMakeFromData(getPtr(data), index)
        require(ptr != Native.NullPointer) { "Failed to create Typeface from data $data" }
        Typeface(ptr)
    } finally {
        reachabilityBarrier(data)
    }
}

@ExternalSymbolName("org_jetbrains_skia_Typeface__1nMakeFromData")
private external fun _nMakeFromData(dataPtr: NativePointer, index: Int): NativePointer
