import { DIBitmap } from "./Bitmap" ; import { Blob } from "./Blob" ; import { Helper } from "./Helper" ; import { Obj , PointL } from "./Primitives" ; export class ColorRef { public r : number ; public g : number ; public b : number ; constructor ( reader , r ? , g ? , b ? ) { if ( reader != null ) { this . r = reader . readUint8 ( ) ; this . g = reader . readUint8 ( ) ; this . b = reader . readUint8 ( ) ; reader . skip ( 1 ) ; } else { this . r = r ; this . g = g ; this . b = b ; } } public clone ( ) { return new ColorRef ( null , this . r , this . g , this . b ) ; } public toHex ( ) { const rgb = ( this . r << 16 ) | ( this . g << 8 ) | this . b ; return ( 0x1000000 + rgb ) . toString ( 16 ) . slice ( 1 ) ; } public toString ( ) { return "{r: " + this . r + ", g: " + this . g + ", b: " + this . b + "}" ; } } export class Font extends Obj { public height : number ; public width : number ; public escapement : number ; public orientation : number ; public weight : number ; public italic : number ; public underline : number ; public strikeout : number ; public charset : number ; public outprecision : number ; public clipprecision : number ; public quality : number ; public pitch : number ; public family : number ; public facename : string ; constructor ( reader , copy ) { super ( "font" ) ; if ( reader != null ) { this . height = reader . readInt32 ( ) ; this . width = reader . readInt32 ( ) ; this . escapement = reader . readInt32 ( ) ; this . orientation = reader . readInt32 ( ) ; this . weight = reader . readInt32 ( ) ; this . italic = reader . readUint8 ( ) ; this . underline = reader . readUint8 ( ) ; this . strikeout = reader . readUint8 ( ) ; this . charset = reader . readUint8 ( ) ; this . outprecision = reader . readUint8 ( ) ; this . clipprecision = reader . readUint8 ( ) ; this . quality = reader . readUint8 ( ) ; const pitchAndFamily = reader . readUint8 ( ) ; this . pitch = pitchAndFamily & 0xf ; this . family = ( pitchAndFamily >> 6 ) & 0x3 ; const dataLength = copy as number ; const start = reader . pos ; this . facename = reader . readFixedSizeUnicodeString ( Math . min ( dataLength - ( reader . pos - start ) , 32 ) ) ; } else if ( copy != null ) { copy = copy as Font ; this . height = copy . height ; this . width = copy . width ; this . escapement = copy . escapement ; this . orientation = copy . orientation ; this . weight = copy . weight ; this . italic = copy . italic ; this . underline = copy . underline ; this . strikeout = copy . strikeout ; this . charset = copy . charset ; this . outprecision = copy . outprecision ; this . clipprecision = copy . clipprecision ; this . quality = copy . quality ; this . pitch = copy . pitch ; this . family = copy . family ; this . facename = copy . facename ; } else { this . height = - 80 ; this . width = 0 ; this . escapement = 0 ; this . orientation = 0 ; this . weight = 400 ; this . italic = 0 ; this . underline = 0 ; this . strikeout = 0 ; this . charset = 0 ; this . outprecision = 0 ; this . clipprecision = 0 ; this . quality = 0 ; this . pitch = 0 ; this . family = 0 ; this . facename = "Helvetica" ; } } public clone ( ) { return new Font ( null , this ) ; } public toString ( ) { return JSON . stringify ( this ) ; } } export class Brush extends Obj { public style : number ; public color : ColorRef ; public pattern : DIBitmap ; public dibpatternpt : DIBitmap ; public hatchstyle : number ; constructor ( reader , copy ? ) { super ( "brush" ) ; if ( reader != null ) { const start = reader . pos ; this . style = reader . readUint32 ( ) ; switch ( this . style ) { case Helper . GDI . BrushStyle . BS_SOLID : this . color = new ColorRef ( reader ) ; break ; case Helper . GDI . BrushStyle . BS_PATTERN : this . pattern = new DIBitmap ( reader ) ; break ; case Helper . GDI . BrushStyle . BS_DIBPATTERNPT : this . dibpatternpt = new DIBitmap ( reader ) ; break ; case Helper . GDI . BrushStyle . BS_HATCHED : this . color = new ColorRef ( reader ) ; this . hatchstyle = reader . readUint32 ( ) ; break ; } reader . seek ( start + 12 ) ; } else { this . style = copy . style ; switch ( this . style ) { case Helper . GDI . BrushStyle . BS_SOLID : this . color = copy . color . clone ( ) ; break ; case Helper . GDI . BrushStyle . BS_PATTERN : this . pattern = copy . pattern ; break ; case Helper . GDI . BrushStyle . BS_DIBPATTERNPT : this . dibpatternpt = copy . dibpatternpt ; break ; case Helper . GDI . BrushStyle . BS_HATCHED : this . color = copy . color . clone ( ) ; this . hatchstyle = copy . hatchstyle ; break ; } } } public clone ( ) { return new Brush ( null , this ) ; } public toString ( ) { let ret = "{style: " + this . style ; switch ( this . style ) { case Helper . GDI . BrushStyle . BS_SOLID : ret += ", color: " + this . color . toString ( ) ; break ; case Helper . GDI . BrushStyle . BS_HATCHED : ret += ", color: " + this . color . toString ( ) + ", hatchstyle: " + this . hatchstyle ; break ; } return ret + "}" ; } } export class Pen extends Obj { public style : number | { header : { off : number ; size : number ; } ; data : { off : number ; size : number ; } ; } ; public width : number ; public brush : Brush ; public color : ColorRef ; constructor ( reader , style ? , width ? , color ? , brush ? ) { super ( "pen" ) ; if ( reader != null ) { if ( style != null ) { this . style = reader . readUint32 ( ) & 0xFF ; this . width = reader . readUint32 ( ) ; this . brush = new Brush ( reader ) ; this . color = this . brush . color != null ? this . brush . color . clone ( ) : new ColorRef ( null , 0 , 0 , 0 ) ; } else { this . style = reader . readUint32 ( ) & 0xFF ; this . width = ( new PointL ( reader ) ) . x ; this . color = new ColorRef ( reader ) ; } } else { this . style = style ; this . width = width ; if ( color != null ) { this . color = color ; } if ( brush != null ) { this . brush = brush ; } } } public clone ( ) { return new Pen ( null , this . style , this . width , this . color != null ? this . color . clone ( ) : null , this . brush != null ? this . brush . clone ( ) : null ) ; } public toString ( ) { return "{style: " + this . style + ", width: " + this . width + ", color: " + ( this . color != null ? this . color . toString ( ) : "none" ) + "}" ; } }