packageDocumentation module import * as React from "react" ; import { UiError } from "@itwin/appui-abstract" ; import { SvgSprite , WebFontIcon , WebFontIconProps } from "@itwin/core-react" ; import { UiComponents } from "../UiComponents" ; import { Image , ImageFileFormat , LoadedBinaryImage , LoadedImage } from "./IImageLoader" ; internal export class ImageRenderer { private hexToBase64 ( hexstring ) { const match = hexstring . match ( /\w{2}/g ) ; if ( ! match ) return "" ; return btoa ( match . map ( ( a ) => { return String . fromCharCode ( parseInt ( a , 16 ) ) ; } ) . join ( "" ) ) ; } /** Render raw binary image */ private renderBinary ( data , format ) { const dataAsBase64 = this . hexToBase64 ( data ) ; return ( < img src = { `data:image/${ format };base64,${ dataAsBase64 }` } alt = "" / > ) ; } private renderSvg ( svg ) { return ( < div > < SvgSprite src = { svg } / > < /div>); } private renderUrl ( url ) { return ( < img src = { url } alt = "" / > ) ; } private renderCoreIcon ( iconName ) { return ( < WebFontIcon iconName = { iconName } / > ) ; } private normalizeEscapedIconString ( escapedIconString ) { return escapedIconString . replace ( /\\:/g , ":" ) ; } private extractIconClassAndName ( iconName ) { const matches = iconName . match ( /(\\.|[^:])+/g ) ; if ( ! matches || matches . length !== 2 ) return { iconClassName : undefined , iconName } ; return { iconClassName : this . normalizeEscapedIconString ( matches [ 0 ] ) , iconName : this . normalizeEscapedIconString ( matches [ 1 ] ) } ; } private renderWebfontIcon ( iconName ) { const iconInfo = this . extractIconClassAndName ( iconName ) ; return ( < WebFontIcon { ... iconInfo } / > ) ; } public render ( loadedImage ) { switch ( loadedImage . sourceType ) { case "binary" : const image = loadedImage as LoadedBinaryImage ; return this . renderBinary ( image . value , image . fileFormat ) ; case "url" : return this . renderUrl ( ( loadedImage as LoadedImage ) . value ) ; case "svg" : return this . renderSvg ( ( loadedImage as LoadedImage ) . value ) ; case "core-icon" : return this . renderCoreIcon ( ( loadedImage as LoadedImage ) . value ) ; case "webfont-icon" : return this . renderWebfontIcon ( ( loadedImage as LoadedImage ) . value ) ; default : const unhandledSourceType = loadedImage . sourceType ; throw new UiError ( UiComponents . loggerCategory ( this ) , `Can't handle sourceType: "${ unhandledSourceType }"` ) ; } } }