import { Image as NodeImage } from 'canvas' ; import { is } from './is' ; export { is , type } from './is' ; export { extend } from './extend' ; export function forin ( obj , cbk ) { for ( let k in obj ) { if ( obj . hasOwnProperty ( k ) ) { cbk ( k , obj [ k ] ) ; } } } export function belowIOS8 ( ) { if ( ENV === 'node' ) return false ; const UA = window . navigator . userAgent . toLowerCase ( ) ; const IOS = /(iPhone|iPad|iPod|iOS)/gi . test ( UA ) ; const IPAD = /(iPad)/gi . test ( UA ) ; if ( IOS ) { const version = IPAD ? UA . match ( /cpu os (\d*)/ ) : UA . match ( /iphone os (\d*)/ ) ; return ! ! ( version && + version [ 1 ] < 9 ) ; } else { return false ; } } export function deepCopy ( obj ) { return JSON . parse ( JSON . stringify ( obj ) ) ; } function isImg ( dom ) { return dom . tagName === 'IMG' ; } function isCanvas ( dom ) { return dom . tagName === 'CANVAS' ; } export type TGetSizeImage = HTMLImageElement | HTMLCanvasElement | NodeImage ; export function getSize ( img ) { if ( isImg ( img ) ) { return { iw : img . naturalWidth , ih : img . naturalHeight } ; } return { iw : img . width , ih : img . height } ; } export function include ( tar , value ) { return tar . indexOf && tar . indexOf ( value ) !== - 1 ; } export function transValue ( par , child , value , type ) { let result = value ; if ( is . str ( value ) ) { if ( include ( value , ':' ) && type === 'pos' ) { const [ _attr , _value ] = value . split ( ':' ) ; switch ( _attr ) { case 'left' : case 'top' : result = + ( _value . replace ( 'px' , '' ) ) ; break ; case 'right' : case 'bottom' : result = par - ( + ( _value . replace ( 'px' , '' ) ) ) - child ; break ; } } else if ( include ( value , 'px' ) ) { result = ( + value . replace ( 'px' , '' ) ) ; } else if ( include ( value , '%' ) ) { result = ( type === 'crop' ? child : par ) * ( + value . replace ( '%' , '' ) ) / 100 ; } else if ( include ( [ 'top' , 'left' ] , value ) ) { result = 0 ; } else if ( include ( [ 'bottom' , 'right' ] , value ) ) { result = par - child ; } else if ( value === 'center' ) { result = ( par - child ) / 2 ; } else if ( value === 'origin' ) { result = child ; } else { result = + value ; } } return Math . round ( result as number ) ; } export function getLength ( ref , value ) { let result = value ; if ( is . str ( value ) ) { if ( include ( value , 'px' ) ) { result = ( + value . replace ( 'px' , '' ) ) ; } else if ( include ( value , '%' ) ) { result = ref * ( + value . replace ( '%' , '' ) ) / 100 ; } else if ( value === 'center' ) { result = ref / 2 ; } else { result = + value ; } } return Math . round ( result as number ) ; } export function throwError ( msg ) { throw new Error ( `[MCanvas ERROR]: ${ msg }` ) ; } export function throwWarn ( msg ) { throw new Error ( `[MCanvas WARN]: ${ msg }` ) ; } function isLetter ( temp ) { const re = /^[A-Za-z]+$/g ; if ( re . test ( temp ) ) return true ; return false ; } function isSymbol ( temp ) { const re = /[\ |\s*(.*?)\s+$|\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\，|\。|\“|\”|\‘|\’|\¥|\？|\（|\）|\；|\：|\、|\！|\<|\.|\>|\/|\?]/g ; if ( re . test ( temp ) ) return true ; return false ; } export function splitWords ( context ) { if ( ! is . str ( context ) ) return [ ] ; const result = [ ] ; let i , l = context . length , word = '' ; for ( i = 0 ; i < l ; i ++ ) { const single = context [ i ] ; if ( isLetter ( single ) ) { const next = context [ i + 1 ] ; word += single ; if ( isSymbol ( next ) ) { result . push ( word ) ; word = '' ; } } else { result . push ( single ) ; } } return result ; }