import { ToBufferOptions , IFontLib } from "bwip-js" ; /**
 *  This is a fork of the drawing SVG example from bwip-js repository here:
 *  https://github.com/metafloor/bwip-js/blob/master/examples/drawing-svg.js
 *
 *  Forked to preserve from unexpected changes.
 */ export function DrawingSVG ( opts , FontLib ) { let tx0 = 0 , tx1 = 0 , tx2 = 0 , tx3 = 0 ; let ty0 = 0 , ty1 = 0 , ty2 = 0 , ty3 = 0 ; let svg = "" ; let path ; let lines = { } ; let ELLIPSE_MAGIC = 0.55228475 - 0.00045 ; let gs_width , gs_height ; let gs_dx , gs_dy ; return { scale ( _ , __ ) { } , measure ( str , font , fwidth , fheight ) { fwidth = fwidth | 0 ; fheight = fheight | 0 ; let fontid = FontLib . lookup ( font ) ; let width = 0 ; let ascent = 0 ; let descent = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { let ch = str . charCodeAt ( i ) ; let glyph = FontLib . getpaths ( fontid , ch , fwidth , fheight ) ; if ( ! glyph ) { continue ; } ascent = Math . max ( ascent , glyph . ascent ) ; descent = Math . max ( descent , - glyph . descent ) ; width += glyph . advance ; } return { width , ascent , descent } ; } , init ( width , height ) { let padl = opts . paddingleft ; let padr = opts . paddingright ; let padt = opts . paddingtop ; let padb = opts . paddingbottom ; let rot = opts . rotate || "N" ; width += padl ! + padr ! ; height += padt ! + padb ! ; switch ( rot ) { case "R" : tx1 = - 1 ; tx2 = 1 ; ty0 = 1 ; break ; case "I" : tx0 = - 1 ; tx2 = 1 ; ty1 = - 1 ; ty3 = 1 ; break ; case "L" : tx1 = 1 ; ty0 = - 1 ; ty3 = 1 ; break ; default : tx0 = ty1 = 1 ; break ; } let swap = rot == "L" || rot == "R" ; gs_width = swap ? height : width ; gs_height = swap ? width : height ; gs_dx = padl ; gs_dy = padt ; svg = "" ; } , line ( x0 , y0 , x1 , y1 , lw , rgb ) { x0 = x0 | 0 ; y0 = y0 | 0 ; x1 = x1 | 0 ; y1 = y1 | 0 ; lw = Math . round ( lw ) ; if ( lw & 1 ) { if ( x0 == x1 ) { x0 += 0.5 ; x1 += 0.5 ; } if ( y0 == y1 ) { y0 += 0.5 ; y1 += 0.5 ; } } let key = "" + lw + "#" + rgb ; if ( ! ( lines as any ) [ key ] ) { ( lines as any ) [ key ] = '<path stroke="#' + rgb + '" stroke-width="' + lw + '" d="' ; } ( lines as any ) [ key ] += "M" + transform ( x0 , y0 ) + "L" + transform ( x1 , y1 ) ; } , polygon ( pts ) { if ( ! path ) { path = '<path d="' ; } path += "M" + transform ( pts [ 0 ] [ 0 ] , pts [ 0 ] [ 1 ] ) ; for ( let i = 1 , n = pts . length ; i < n ; i ++ ) { let p = pts [ i ] ; path += "L" + transform ( p [ 0 ] , p [ 1 ] ) ; } path += "Z" ; } , hexagon ( pts , _ ) { this . polygon ( pts ) ; } , ellipse ( x , y , rx , ry , _ ) { if ( ! path ) { path = '<path d="' ; } let dx = rx * ELLIPSE_MAGIC ; let dy = ry * ELLIPSE_MAGIC ; path += "M" + transform ( x - rx , y ) + "C" + transform ( x - rx , y - dy ) + " " + transform ( x - dx , y - ry ) + " " + transform ( x , y - ry ) + "C" + transform ( x + dx , y - ry ) + " " + transform ( x + rx , y - dy ) + " " + transform ( x + rx , y ) + "C" + transform ( x + rx , y + dy ) + " " + transform ( x + dx , y + ry ) + " " + transform ( x , y + ry ) + "C" + transform ( x - dx , y + ry ) + " " + transform ( x - rx , y + dy ) + " " + transform ( x - rx , y ) + "Z" ; } , fill ( rgb ) { if ( path ) { svg += path + '" fill="#' + rgb + '" fill-rule="evenodd" />\n' ; path = null ; } } , text ( x , y , str , rgb , font ) { let fontid = FontLib . lookup ( font . name ) ; let fwidth = font . width | 0 ; let fheight = font . height | 0 ; let dx = font . dx | 0 ; let path = "" ; for ( let k = 0 ; k < str . length ; k ++ ) { let ch = str . charCodeAt ( k ) ; let glyph = FontLib . getpaths ( fontid , ch , fwidth , fheight ) ; if ( ! glyph ) { continue ; } if ( glyph . length ) { for ( let i = 0 , l = glyph . length ; i < l ; i ++ ) { let seg = glyph [ i ] ; if ( seg . type == "M" || seg . type == "L" ) { path += seg . type + transform ( seg . x + x , y - seg . y ) ; } else if ( seg . type == "Q" ) { path += seg . type + transform ( seg . cx + x , y - seg . cy ) + " " + transform ( seg . x + x , y - seg . y ) ; } else if ( seg . type == "C" ) { path += seg . type + transform ( seg . cx1 + x , y - seg . cy1 ) + " " + transform ( seg . cx2 + x , y - seg . cy2 ) + " " + transform ( seg . x + x , y - seg . y ) ; } } path += "Z" ; } x += glyph . advance + dx ; } if ( path ) { svg += '<path d="' + path + '" fill="#' + rgb + '" />\n' ; } } , end ( ) { let linesvg = "" ; for ( let key in lines ) { linesvg += ( lines as any ) [ key ] + '" />\n' ; } let bg = opts . backgroundcolor ; return ( '<svg version="1.1" width="' + gs_width + '" height="' + gs_height + '" xmlns="http://www.w3.org/2000/svg">\n' + ( /^[0-9A-Fa-f]{6}$/ . test ( "" + bg ) ? '<rect width="100%" height="100%" fill="#' + bg + '" />\n' : "" ) + linesvg + svg + "</svg>\n" ) ; } } ; function transform ( x , y ) { x += gs_dx ; y += gs_dy ; let tx = tx0 * x + tx1 * y + tx2 * ( gs_width - 1 ) + tx3 * ( gs_height - 1 ) ; let ty = ty0 * x + ty1 * y + ty2 * ( gs_width - 1 ) + ty3 * ( gs_height - 1 ) ; return ( "" + ( ( tx | 0 ) == tx ? tx : tx . toFixed ( 2 ) ) + " " + ( ( ty | 0 ) == ty ? ty : ty . toFixed ( 2 ) ) ) ; } }