dist/hdsf-hive.js
/**
  * Formats input text
  *
  * @param string text
  * @param number text configuration
  * @return text object with metrics
  */
Formats input text
@param string text
@param number text configuration
@return text object with metrics
Formats input text @param string text @param number text configuration @return text object with metrics
static format (content, cfg) {
    let defs = {
      x:0,
      y:0,
      height:100,
      width:300,
      attr:{},
      lineSpacing:0,
      textAnchor:'tm',
      pad:0,
      yOffset:0,
      justify:'left',
      ellipseize:true,
      rotate:0,
      format:d=>d
    };
    cfg = {...defs, ...cfg};
    content = String(cfg.format(content));
    let attr = {};
    Object.keys(cfg.attr).filter(d => d.startsWith('font-')).map(d => attr[this.snakeToCamel(d)] = cfg.attr[d]);
    let rv = layoutText(content, {maxWidth:cfg.width, maxLines: 9999}, attr, {'lineSpacing':cfg.lineSpacing});
    let rvLen = rv[0].lines.length;
    let singleHeight = rv[0].lines[0].measure.height;
    let lines = rv[0].lines.slice(0, parseInt(cfg.height / singleHeight));
    let maxWidth = lines.map(d => d.measure.width).reduce((a, b)=>Math.max(a, b));
    if (maxWidth > cfg.width) maxWidth = cfg.width;
    if (cfg.ellipseize && rvLen > lines.length) {  
      let last = lines.length-1;
      this.ellipseize(lines[last], maxWidth, attr);
    }
    this.reposition(lines, maxWidth, singleHeight, cfg);
    var e = document.createElementNS("http://www.w3.org/2000/svg", "g");
    let strAttr = (' '+JSON.stringify(cfg.attr)).replace(/[{}]/g, '').replaceAll(':', '=').replaceAll(',',' ')
                                                          .replaceAll('"=', '=').replaceAll(' "',' ');
    lines.forEach((item, i) => {
      e.innerHTML += `<text x=${item.position.x} y=${item.position.y} ${strAttr} text-anchor="start" transform="rotate(${cfg.rotate} ${cfg.x},${cfg.y})">${item.text.replace('&nbsp;', ' ')}</text>`;
    });
    e.innerHTML = e.innerHTML.replaceAll('&nbsp;', ' ');
    return ({maxWidth, lines, e, bbox:{w:rv[0].width, h:singleHeight*lines.length}});
  }
static format ( content , cfg ) { let defs = { x : 0 , y : 0 , height : 100 , width : 300 , attr : { } , lineSpacing : 0 , textAnchor : 'tm' , pad : 0 , yOffset : 0 , justify : 'left' , ellipseize : true , rotate : 0 , format : d => d } ; cfg = { ... defs , ... cfg } ; content = String ( cfg . format ( content ) ) ; let attr = { } ; Object . keys ( cfg . attr ) . filter ( d => d . startsWith ( 'font-' ) ) . map ( d => attr [ this . snakeToCamel ( d ) ] = cfg . attr [ d ] ) ; let rv = layoutText ( content , { maxWidth : cfg . width , maxLines : 9999 } , attr , { 'lineSpacing' : cfg . lineSpacing } ) ; let rvLen = rv [ 0 ] . lines . length ; let singleHeight = rv [ 0 ] . lines [ 0 ] . measure . height ; let lines = rv [ 0 ] . lines . slice ( 0 , parseInt ( cfg . height / singleHeight ) ) ; let maxWidth = lines . map ( d => d . measure . width ) . reduce ( ( a , b ) => Math . max ( a , b ) ) ; if ( maxWidth > cfg . width ) maxWidth = cfg . width ; if ( cfg . ellipseize && rvLen > lines . length ) { let last = lines . length - 1 ; this . ellipseize ( lines [ last ] , maxWidth , attr ) ; } this . reposition ( lines , maxWidth , singleHeight , cfg ) ; var e = document . createElementNS ( "http://www.w3.org/2000/svg" , "g" ) ; let strAttr = ( ' ' + JSON . stringify ( cfg . attr ) ) . replace ( / [{}] / g , '' ) . replaceAll ( ':' , '=' ) . replaceAll ( ',' , ' ' ) . replaceAll ( '"=' , '=' ) . replaceAll ( ' "' , ' ' ) ; lines . forEach ( ( item , i ) => { e . innerHTML += ` ${ item . position . x } ${ item . position . y } ${ strAttr } ${ cfg . rotate } ${ cfg . x } ${ cfg . y } ${ item . text . replace ( '&nbsp;' , ' ' ) } ` ; } ) ; e . innerHTML = e . innerHTML . replaceAll ( '&nbsp;' , ' ' ) ; return ( { maxWidth , lines , e , bbox : { w : rv [ 0 ] . width , h : singleHeight * lines . length } } ) ; }
Formats input text
 @param string text
 @param number text configuration
 @return text object with metrics
Formats input text @param string text @param number text configuration @return text object with metrics
// ellipseize // e.setAttribute('transform', `rotate(${cfg.rotate} ${cfg.x},${cfg.y})`)