NanoMichael / MicroTeX

A dynamic, cross-platform, and embeddable LaTeX rendering library
MIT License
417 stars 69 forks source link

Using Skia and Render in Android device,crash in the CharBox Draw() funtion #165

Open tantianjack opened 1 year ago

tantianjack commented 1 year ago

我在使用Skia与microTex库,在安卓平台渲染公式的时候,会crash在Box->draw()里面。具体看下来,好像是字体加载没有成功。 导致了CharBox中使用了_drawWithFont(),字体为Null。具体流程如下:

我编译了skia安卓版本,然后把microTex也编译了一个安卓版本。在使用的过程中,我初始化了对应的字体与环境。

microtex::FontSrcFile math{"/sdcard/temp/XITSMath-Regular.clm1", "/sdcard/temp/XITSMath-Regular.otf" }; microtex::MicroTeX::init(math); microtex::PlatformFactory::registerFactory("skia", std::make_unique()); microtex::PlatformFactory::activate("skia");

之后,为了排查问题,我简单的设置了要解析的公式为一个字母。 auto render = microtex::MicroTeX::parse(std::string("x"), 600 - 0 * 2, 20, 20 / 3.f, 0xff424242);

然后新建了skia的canvas和一个bitmap,期望render可以渲染到bitmap上。 SkBitmap bitmap; bitmap.allocPixels(SkImageInfo::Make(render->getWidth(), render->getHeight(), kN32_SkColorType, kPremul_SkAlphaType));

SkCanvas canvas(bitmap);

Graphics2D_skia drawer(&canvas); render->draw(drawer,10,10);

但是最终 crash在了render->draw中。由于安卓平台没法调试和查看log,只能大概确认是CharBox这个类使用的_drawWithFont函数里面,factory->createFont生成的字体存在问题。

我想知道,是我初始化选择的字体有问题导致了Crash的发生,还是我的库使用的方式有问题?这真的让人感到困惑,可以有人为我解答一下吗?

sp1ritCS commented 1 year ago

Have you checked if you can even access /sdcard/temp/XITSMath-Regular.{clm1,otf} using libc calls on Android? I know it used to be possible but as far as I'm aware newer Android versions really fucked with your ability to access the "normal" filesystem without the special File Manager Permission/API (see scoped storage).

You might have better luck putting the files in /data/data/<app.identifier>/files/. That dir should still be readable but obv. requires root access if users wish to change the fonts. Alternatively you could also use FontSrcData to include the clm2 blob without extra font directly in your binary, but that makes changing the font impossible (unless you implement some sort of seperate font loader).

tantianjack commented 1 year ago

Have you checked if you can even access /sdcard/temp/XITSMath-Regular.{clm1,otf} using libc calls on Android? I know it used to be possible but as far as I'm aware newer Android versions really fucked with your ability to access the "normal" filesystem without the special File Manager Permission/API (see scoped storage).

You might have better luck putting the files in /data/data/<app.identifier>/files/. That dir should still be readable but obv. requires root access if users wish to change the fonts. Alternatively you could also use FontSrcData to include the clm2 blob without extra font directly in your binary, but that makes changing the font impossible (unless you implement some sort of seperate font loader).

Thank you very much for your answer. It gave me some ideas. If this path is incorrect, the initialization stage will not pass. At least this path should be accessible. I will verify this and reply with the results.

tantianjack commented 11 months ago

Compiled the skia debug version and added Android log printing to microtex. It is confirmed that the problem is due to an internal problem in the skia library and has nothing to do with microtex.