import { SVG } from "../util" ; import { Blob } from "./Blob" ; import { EMFRecords } from "./EMFRecords" ; import { GDIContext } from "./GDIContext" ; import { EMFJSError , Helper } from "./Helper" ; export interface IRendererSettings { width : string ; height : string ; wExt : number ; hExt : number ; xExt : number ; yExt : number ; mapMode : number ; } export class Renderer { private _img : EMF ; constructor ( blob ) { this . parse ( blob ) ; Helper . log ( "EMFJS.Renderer instantiated" ) ; } public render ( info ) { const svgElement = document . createElementNS ( "http://www.w3.org/2000/svg" , "svg" ) ; this . _render ( new SVG ( svgElement ) , info . mapMode , info . wExt , info . hExt , info . xExt , info . yExt ) ; svgElement . setAttribute ( "viewBox" , [ 0 , 0 , info . xExt , info . yExt ] . join ( " " ) ) ; svgElement . setAttribute ( "preserveAspectRatio" , "none" ) ; svgElement . setAttribute ( "width" , info . width ) ; svgElement . setAttribute ( "height" , info . height ) ; return svgElement ; } private parse ( blob ) { this . _img = null ; const reader = new Blob ( blob ) ; const type = reader . readUint32 ( ) ; if ( type !== 0x00000001 ) { throw new EMFJSError ( "Not an EMF file" ) ; } const size = reader . readUint32 ( ) ; if ( size % 4 !== 0 ) { throw new EMFJSError ( "Not an EMF file" ) ; } this . _img = new EMF ( reader , size ) ; if ( this . _img == null ) { throw new EMFJSError ( "Format not recognized" ) ; } } private _render ( svg , mapMode , w , h , xExt , yExt ) { const gdi = new GDIContext ( svg ) ; gdi . setWindowExtEx ( w , h ) ; gdi . setViewportExtEx ( xExt , yExt ) ; gdi . setMapMode ( mapMode ) ; Helper . log ( "[EMF] BEGIN RENDERING --->" ) ; this . _img . render ( gdi ) ; Helper . log ( "[EMF] <--- DONE RENDERING" ) ; } } class EMF { private _hdrsize : number ; private _records : EMFRecords ; constructor ( reader , hdrsize ) { this . _hdrsize = hdrsize ; this . _records = new EMFRecords ( reader , this . _hdrsize ) ; } public render ( gdi ) { this . _records . play ( gdi ) ; } }