Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我不知道应该如何设置我想要的字体(I don't know how to set the font I want). #120

Closed
woyishijiazhuang opened this issue Dec 3, 2024 · 2 comments

Comments

@woyishijiazhuang
Copy link

 const mupdfjs = await import(/* @vite-ignore */ mupdfScript);
  const url = '/test.pdf'
  let response = await fetch(url)
  if (!response.ok) {
      console.error(`Cannot fetch document: ${response.statusText}`)
      return
  }
  let arrayBuffer = await response.arrayBuffer()
  let document = await mupdfjs.Document.openDocument(
    arrayBuffer,
    'application/pdf'
  );
  document.setLanguage('zh-Hans')
  let font_res = await fetch('/QingNiaoHuaGuangJianMeiHei-2.ttf')
  let font_buffer = await font_res.arrayBuffer()
  const font = await new mupdfjs.Font('青鸟华光简美黑', font_buffer)

  console.log(`Character encoding for '帅': ${font.encodeCharacter('帅')}`); // print 2337, 验证是否正确创建Font
  await document.addCJKFont(font, 'zh-Hans') 

  const page = document.loadPage(0);

  let freeText = page.createAnnotation("FreeText")
  freeText.setContents("我是大帅哥")
  freeText.setLanguage('zh-Hans') // 如果使用默认值, 无法显示 '帅' 字
  console.log(font.getName()) // print 青鸟华光简美黑
  freeText.setDefaultAppearance(font.getName() , 16, [0,1,0]);
  console.log(freeText.getDefaultAppearance()) // print {font: 'Helv', size: 16, color: Array(3)}
  freeText.setRect([0,0,200,50])
  freeText.update()
  page.update()
  const scale = (window.devicePixelRatio * 96) / 72
  let pixmap = await page.toPixmap([scale, 0, 0, scale, 0, 0], mupdfjs.ColorSpace.DeviceRGB, false, true)

  let pngImage = await pixmap.asPNG()
  base64Image.value = URL.createObjectURL(new Blob([pngImage], { type: 'image/png' }));

页面上显示为
image
我想要的显示为
image

@woyishijiazhuang woyishijiazhuang changed the title 我不知道应该如何设置我想要的字体 我不知道应该如何设置我想要的字体(I don't know how to set the font I want). Dec 3, 2024
@jamie-lemon
Copy link
Collaborator

Thank you for this report and the code sample - I don't think you are doing anything wrong.
At the moment setDefaultAppearance can only use the in-built fonts as explained here: https://mupdfjs.readthedocs.io/en/latest/classes/PDFAnnotation.html#setDefaultAppearance

We will hopefully rectify this in the next release.

@jamie-lemon
Copy link
Collaborator

@ccxvii Another one to consider for the start of 2025! 🎆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants