鸿蒙api12及以上实现Latex数学表达式

41 阅读1分钟

话不多说: 1、导库"@cangjie-tpc/formula": "^1.0.0

2、方法我写了,拿去用吧

import { LatexMathColorFormat, latexStringToImage } from "@cangjie-tpc/formula"

@Component
export struct GetHomeworkContent {

  @Prop str: string = ""
  @State pixelMap: image.PixelMap = undefined!;
  @State imageWidth: number = 0;
  @State imageHeight: number = 0;

  async aboutToAppear(): Promise<void> {
    // 通过接口解析数学公式获取数学公式图片数组数据
    let buf: ArrayBuffer = await latexStringToImage(this.str, 50.0, 10.0, 0xFF1A1A1A, 0xFFFFFFFF,
      LatexMathColorFormat.COLOR_FORMAT_BGRA_8888)
    let imageSource = image.createImageSource(buf)
    // 图片pixelMap
    this.pixelMap = imageSource.createPixelMapSync()
    let size: Size = this.pixelMap.getImageInfoSync().size
    // 图片宽度
    this.imageWidth = px2vp(size.width)
    // 图片高度
    this.imageHeight = px2vp(size.height)
  }

  build() {
    Image(this.pixelMap)
      .objectFit(ImageFit.Contain)
      .width(this.imageWidth)
      .height(this.imageHeight)
      .margin({ top: 5, bottom: 5 })
  }
}

3、使用方法

GetHomeworkContent({
str:  "表达式"
})

效果在这里插入图片描述 4、作者v(llvy077),欢迎和我谈论哈