@dan-uni/dan-any 0.7.6 → 0.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { assertType, it } from 'vitest'
1
+ import { assert, assertType, it } from 'vitest'
2
2
 
3
3
  import { measureTextWidth } from '../util/layout'
4
4
 
@@ -7,5 +7,5 @@ it('canvas measureTextWidth', () => {
7
7
  const width = measureTextWidth('SimHei', 25, false, text)
8
8
  assertType<number>(width)
9
9
  console.info(width, text.length)
10
- // assert(width >= 25 * text.length)
10
+ assert(width >= 25 * text.length)
11
11
  })
@@ -1,4 +1,5 @@
1
- import { createCanvas } from 'canvas'
1
+ import { Canvas as WebCanvas } from 'fabric'
2
+ import { StaticCanvas as NodeCanvas } from 'fabric/node'
2
3
  import type { UniPool } from '../..'
3
4
  import type { Danmaku, SubtitleStyle } from '../types'
4
5
 
@@ -78,7 +79,17 @@ const splitGrids = ({
78
79
  }
79
80
 
80
81
  export const measureTextWidth = (() => {
81
- const canvasContext = createCanvas(50, 50).getContext('2d')
82
+ let isWeb
83
+ try {
84
+ isWeb = !!window
85
+ } catch {
86
+ isWeb = false
87
+ }
88
+ const Canvas = isWeb ? WebCanvas : NodeCanvas
89
+ const canvasContext = new Canvas(undefined, {
90
+ width: 50,
91
+ height: 50,
92
+ }).getContext()
82
93
  const supportTextMeasure = !!canvasContext.measureText('中')
83
94
 
84
95
  if (supportTextMeasure) {
@@ -469,6 +469,8 @@ export class UniDM {
469
469
  if (this.options.dmid === undefined || this.options.dmid === true)
470
470
  this.options.dmid = createDMID
471
471
 
472
+ this.content = String(this.content)
473
+
472
474
  if (!this.SOID) this.SOID = def.SOID
473
475
  if (!this.progress) this.progress = def.progress
474
476
  if (!this.mode) this.mode = def.mode