import svg = require ( "./svg" ) ; import common = require ( "./svg.common" ) ; import types = require ( "tns-core-modules/utils/types" ) ; import fs = require ( "tns-core-modules/file-system" ) ; global . moduleMerge ( common , exports ) ; declare var SVGKImage ; export class ImageSourceSVG implements svg . ImageSourceSVG { private nativeView : any ; public loadFromResource ( name ) { this . nativeView = SVGKImage . imageNamed ( name ) || SVGKImage . imageNamed ( `${ name }.svg` ) ; return this . nativeView != null ; } public fromResource ( name ) { return new Promise < boolean > ( ( resolve , reject ) => { try { ( < any > SVGKImage ) . imageAsynchronouslyNamed ( name , ( image , parseResult ) => { if ( image ) { this . nativeView = image ; resolve ( true ) ; } else { ( < any > SVGKImage ) . imageAsynchronouslyNamed ( `${ name }.svg` , ( image , parseResult ) => { this . nativeView = image ; resolve ( true ) ; } ) ; } } ) ; } catch ( ex ) { reject ( ex ) ; } } ) ; } public loadFromFile ( path ) { var fileName = types . isString ( path ) ? path . trim ( ) : "" ; if ( fileName . indexOf ( "~/" ) === 0 ) { fileName = fs . path . join ( fs . knownFolders . currentApp ( ) . path , fileName . replace ( "~/" , "" ) ) ; } this . nativeView = SVGKImage . imageWithContentsOfFile ( fileName ) ; return this . nativeView != null ; } public fromFile ( path ) { return new Promise < boolean > ( ( resolve , reject ) => { try { var fileName = types . isString ( path ) ? path . trim ( ) : "" ; if ( fileName . indexOf ( "~/" ) === 0 ) { fileName = fs . path . join ( fs . knownFolders . currentApp ( ) . path , fileName . replace ( "~/" , "" ) ) ; } ( < any > SVGKImage ) . imageWithContentsOfFileAsynchronously ( fileName , image => { this . nativeView = image ; resolve ( true ) ; } ) ; } catch ( ex ) { reject ( ex ) ; } } ) ; } public loadFromData ( data ) { this . nativeView = SVGKImage . imageWithData ( data ) ; return this . nativeView != null ; } public fromData ( data ) { return new Promise < boolean > ( ( resolve , reject ) => { try { ( < any > SVGKImage ) . imageWithDataAsynchronously ( data , image => { this . nativeView = image ; resolve ( true ) ; } ) ; } catch ( ex ) { reject ( ex ) ; } } ) ; } public loadFromBase64 ( source ) { if ( types . isString ( source ) ) { var data = NSData . alloc ( ) . initWithBase64EncodedStringOptions ( source , NSDataBase64DecodingOptions . IgnoreUnknownCharacters ) ; this . nativeView = SVGKImage . imageWithData ( data ) ; } return this . nativeView != null ; } public fromBase64 ( source ) { return new Promise < boolean > ( ( resolve , reject ) => { try { var data = NSData . alloc ( ) . initWithBase64EncodedStringOptions ( source , NSDataBase64DecodingOptions . IgnoreUnknownCharacters ) ; SVGKImage . imageWithDataAsynchronously ( data , image => { this . nativeView = image ; resolve ( true ) ; } ) ; } catch ( ex ) { reject ( ex ) ; } } ) ; } public loadFromUrl ( url ) { this . nativeView = SVGKImage . imageWithContentsOfURL ( NSURL . URLWithString ( url ) ) ; return this . nativeView != null ; } public fromUrl ( url ) { return new Promise < boolean > ( ( resolve , reject ) => { resolve ( this . loadFromUrl ( url ) ) ; } ) ; } public setNativeSource ( source ) { this . nativeView = source ; return source != null ; } public saveToFile ( path ) { if ( ! this . nativeView ) { return false ; } var data = getImageData ( this . nativeView ) ; if ( data ) { return data . writeToFileAtomically ( path , true ) ; } return false ; } public toBase64String ( format ) { var res = null ; if ( ! this . nativeView ) { return res ; } var data = getImageData ( this . nativeView ) ; if ( data ) { res = data . base64EncodedStringWithOptions ( NSDataBase64EncodingOptions . Encoding64CharacterLineLength ) ; } return res ; } get height ( ) : number { if ( this . nativeView ) { return this . nativeView . size . height ; } return NaN ; } get width ( ) : number { if ( this . nativeView ) { return this . nativeView . size . width ; } return NaN ; } } function getImageData ( instance ) { var buffer = instance . source . stream ; throw new Error ( "Not supported operation" ) ; } declare var SVGKFastImageView ; export class SVGImage extends common . SVGImage { private _imageSourceAffectsLayout : boolean = true ; constructor ( ) { super ( ) ; this . nativeView = SVGKFastImageView . alloc ( ) . initWithSVGKImage ( new SVGKImage ( ) ) ; } public _setNativeImage ( nativeImage ) { this . nativeView . image = nativeImage . nativeView ; if ( this . _imageSourceAffectsLayout ) { this . requestLayout ( ) ; } } public onMeasure ( widthMeasureSpec , heightMeasureSpec ) { var utils = require ( "utils/utils" ) ; var width = utils . layout . getMeasureSpecSize ( widthMeasureSpec ) ; var widthMode = utils . layout . getMeasureSpecMode ( widthMeasureSpec ) ; var height = utils . layout . getMeasureSpecSize ( heightMeasureSpec ) ; var heightMode = utils . layout . getMeasureSpecMode ( heightMeasureSpec ) ; var nativeWidth = this . imageSource ? this . imageSource . width : 0 ; var nativeHeight = this . imageSource ? this . imageSource . height : 0 ; var measureWidth = Math . max ( nativeWidth , this . effectiveMinWidth ) ; var measureHeight = Math . max ( nativeHeight , this . effectiveMinHeight ) ; var finiteWidth = widthMode !== utils . layout . UNSPECIFIED ; var finiteHeight = heightMode !== utils . layout . UNSPECIFIED ; this . _imageSourceAffectsLayout = widthMode !== utils . layout . EXACTLY || heightMode !== utils . layout . EXACTLY ; if ( nativeWidth !== 0 && nativeHeight !== 0 && ( finiteWidth || finiteHeight ) ) { var resultW = nativeWidth ; var resultH = nativeHeight ; measureWidth = finiteWidth ? Math . min ( resultW , width ) : resultW ; measureHeight = finiteHeight ? Math . min ( resultH , height ) : resultH ; var trace = require ( "trace" ) ; if ( trace . enabled ) { trace . write ( "nativeWidth: " + nativeWidth + ", nativeHeight: " + nativeHeight , trace . categories . Layout ) ; } } var view = require ( "ui/core/view" ) ; var widthAndState = view . View . resolveSizeAndState ( measureWidth , width , widthMode , 0 ) ; var heightAndState = view . View . resolveSizeAndState ( measureHeight , height , heightMode , 0 ) ; this . setMeasuredDimension ( widthAndState , heightAndState ) ; } [ common . imageSourceProperty . setNative ] ( value ) { var image = < SVGImage > value ; if ( ! image || ! image . nativeView ) { return ; } this . _setNativeImage ( image ) ; } }