MCPcopy
hub / github.com/canvg/canvg / setTextData

Method setTextData

src/Document/TextPathElement.ts:329–453  ·  view source on GitHub ↗
(ctx: RenderingContext2D)

Source from the content-addressed store, hash-verified

327 // If some font will be loaded after this method call, <textPath> will not be rendered correctly.
328 // You need to call this method manually to update glyphs cache.
329 protected setTextData(ctx: RenderingContext2D) {
330 if (this.glyphInfo) {
331 return
332 }
333
334 const renderText = this.getText()
335 const chars = renderText.split('')
336 const spacesNumber = renderText.split(' ').length - 1
337 const dx = this.parent.getAttribute('dx').split().map(_ => _.getPixels('x'))
338 const dy = this.parent.getAttribute('dy').getPixels('y')
339 const anchor = this.parent.getStyle('text-anchor').getString('start')
340 const thisSpacing = this.getStyle('letter-spacing')
341 const parentSpacing = this.parent.getStyle('letter-spacing')
342 let letterSpacing = 0
343
344 if (!thisSpacing.hasValue()
345 || thisSpacing.getValue() === 'inherit'
346 ) {
347 letterSpacing = parentSpacing.getPixels()
348 } else
349 if (thisSpacing.hasValue()) {
350 if (thisSpacing.getValue() !== 'initial'
351 && thisSpacing.getValue() !== 'unset'
352 ) {
353 letterSpacing = thisSpacing.getPixels()
354 }
355 }
356
357 // fill letter-spacing cache
358 const letterSpacingCache: number[] = []
359 const textLen = renderText.length
360
361 this.letterSpacingCache = letterSpacingCache
362
363 for (let i = 0; i < textLen; i++) {
364 letterSpacingCache.push(
365 typeof dx[i] !== 'undefined'
366 ? dx[i]
367 : letterSpacing
368 )
369 }
370
371 const dxSum = letterSpacingCache.reduce(
372 (acc, cur, i) => (
373 i === 0
374 ? 0
375 : acc + cur || 0
376 ),
377 0
378 )
379 const textWidth = this.measureText(ctx)
380 const textFullWidth = Math.max(textWidth + dxSum, 0)
381
382 this.textWidth = textWidth
383 this.textHeight = this.getFontSize()
384 this.glyphInfo = []
385
386 const fullPathWidth = this.getPathLength()

Callers 2

renderChildrenMethod · 0.95
buildFunction · 0.80

Calls 11

getTextMethod · 0.95
measureTextMethod · 0.95
getPathLengthMethod · 0.95
findSegmentToFitCharMethod · 0.95
splitMethod · 0.80
getPixelsMethod · 0.80
getStringMethod · 0.80
hasValueMethod · 0.80
getValueMethod · 0.80
getFontSizeMethod · 0.80
getNumberMethod · 0.80

Tested by

no test coverage detected