import svg = require ( "./svg" ) ; import common = require ( "./svg.common" ) ; import types = require ( "tns-core-modules/utils/types" ) ; import * as utilsModule from "tns-core-modules/utils/utils" ; import * as fileSystemModule from "tns-core-modules/file-system" ; import * as httpModule from "tns-core-modules/http" ; var http ; function ensureHttp ( ) { if ( ! http ) { http = require ( "http" ) ; } } global . moduleMerge ( common , exports ) ; var utils ; function ensureUtils ( ) { if ( ! utils ) { utils = require ( "utils/utils" ) ; } } var fs ; function ensureFS ( ) { if ( ! fs ) { fs = require ( "file-system" ) ; } } declare var android ; declare var com ; declare var java ; declare var org ; export class ImageSourceSVG implements svg . ImageSourceSVG { private nativeView : any ; public loadFromResource ( name ) { this . nativeView = null ; ensureUtils ( ) ; var res = utils . ad . getApplicationContext ( ) . getResources ( ) ; if ( res ) { var identifier = res . getIdentifier ( name , 'drawable' , utils . ad . getApplication ( ) . getPackageName ( ) ) ; if ( 0 < identifier ) { this . nativeView = new com . larvalabs . svgandroid . SVGParser . getSVGFromResource ( res , identifier ) ; } } return this . nativeView != null ; } public fromResource ( name ) { return new Promise < boolean > ( ( resolve , reject ) => { resolve ( this . loadFromResource ( name ) ) ; } ) ; } public loadFromFile ( path ) { ensureFS ( ) ; var fileName = types . isString ( path ) ? path . trim ( ) : "" ; if ( fileName . indexOf ( "~/" ) === 0 ) { fileName = fs . path . join ( fs . knownFolders . currentApp ( ) . path , fileName . replace ( "~/" , "" ) ) ; } this . nativeView = new com . larvalabs . svgandroid . SVGParser . getSVGFromInputStream ( new java . io . FileInputStream ( new java . io . File ( fileName ) ) ) ; return this . nativeView != null ; } public fromFile ( path ) { return new Promise < boolean > ( ( resolve , reject ) => { resolve ( this . loadFromFile ( path ) ) ; } ) ; } public loadFromData ( data ) { this . nativeView = new com . larvalabs . svgandroid . SVGParser . getSVGFromString ( data ) ; return this . nativeView != null ; } public fromData ( data ) { return new Promise < boolean > ( ( resolve , reject ) => { resolve ( this . loadFromData ( data ) ) ; } ) ; } public loadFromBase64 ( source ) { var bytes = android . util . Base64 . decode ( source , android . util . Base64 . DEFAULT ) ; this . nativeView = new com . larvalabs . svgandroid . SVGParser . getSVGFromString ( new java . lang . String ( bytes ) ) ; return this . nativeView != null ; } public fromBase64 ( data ) { return new Promise < boolean > ( ( resolve , reject ) => { resolve ( this . loadFromBase64 ( data ) ) ; } ) ; } public loadFromUrl ( url ) { ensureHttp ( ) ; var result = http . getString ( url ) ; return this . setNativeSource ( new com . larvalabs . svgandroid . SVGParser . getSVGFromString ( result ) ) ; } 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 ) { return false ; } public toBase64String ( format ) { if ( ! this . nativeView ) { return null ; ; } return android . util . Base64 . encodeToString ( format , android . util . Base64 . DEFAULT ) ; } get height ( ) : number { if ( this . nativeView ) { return this . nativeView . getPitcture ( ) . getHeight ( ) ; } return NaN ; } get width ( ) : number { if ( this . nativeView ) { return this . nativeView . getPitcture ( ) . getWidth ( ) ; } return NaN ; } } export class SVGImage extends common . SVGImage { private _drawable : android . graphics . drawable . PictureDrawable ; constructor ( ) { super ( ) ; } public createNativeView ( ) { return new org . nativescript . widgets . ImageView ( this . _context ) ; } public _setNativeImage ( nativeImage ) { this . _drawable = nativeImage . nativeView . createPictureDrawable ( ) ; this . nativeView . setLayerType ( android . view . View . LAYER_TYPE_SOFTWARE , null ) ; this . nativeView . setImageDrawable ( this . _drawable ) ; } [ common . imageSourceProperty . setNative ] ( value ) { var image = < SVGImage > value ; if ( ! image || ! image . nativeView ) { return ; } this . _setNativeImage ( image ) ; } }