@ -16,6 +16,10 @@ type scaledBarcode struct {
rect image . Rectangle
rect image . Rectangle
}
}
type intCSscaledBC struct {
scaledBarcode
}
func ( bc * scaledBarcode ) Content ( ) string {
func ( bc * scaledBarcode ) Content ( ) string {
return bc . wrapped . Content ( )
return bc . wrapped . Content ( )
}
}
@ -36,8 +40,11 @@ func (bc *scaledBarcode) At(x, y int) color.Color {
return bc . wrapperFunc ( x , y )
return bc . wrapperFunc ( x , y )
}
}
func ( bc * scaledBarcode ) CheckSum ( ) int {
func ( bc * intCSscaledBC ) CheckSum ( ) int {
return bc . wrapped . CheckSum ( )
if cs , ok := bc . wrapped . ( BarcodeIntCS ) ; ok {
return cs . CheckSum ( )
}
return 0
}
}
// Scale returns a resized barcode with the given width and height.
// Scale returns a resized barcode with the given width and height.
@ -52,6 +59,19 @@ func Scale(bc Barcode, width, height int) (Barcode, error) {
return nil , errors . New ( "unsupported barcode format" )
return nil , errors . New ( "unsupported barcode format" )
}
}
func newScaledBC ( wrapped Barcode , wrapperFunc wrapFunc , rect image . Rectangle ) Barcode {
result := & scaledBarcode {
wrapped : wrapped ,
wrapperFunc : wrapperFunc ,
rect : rect ,
}
if _ , ok := wrapped . ( BarcodeIntCS ) ; ok {
return & intCSscaledBC { * result }
}
return result
}
func scale2DCode ( bc Barcode , width , height int ) ( Barcode , error ) {
func scale2DCode ( bc Barcode , width , height int ) ( Barcode , error ) {
orgBounds := bc . Bounds ( )
orgBounds := bc . Bounds ( )
orgWidth := orgBounds . Max . X - orgBounds . Min . X
orgWidth := orgBounds . Max . X - orgBounds . Min . X
@ -59,7 +79,7 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
factor := int ( math . Min ( float64 ( width ) / float64 ( orgWidth ) , float64 ( height ) / float64 ( orgHeight ) ) )
factor := int ( math . Min ( float64 ( width ) / float64 ( orgWidth ) , float64 ( height ) / float64 ( orgHeight ) ) )
if factor <= 0 {
if factor <= 0 {
return nil , fmt . Errorf ( "can not scale barcode to an image smaller the n %dx%d" , orgWidth , orgHeight )
return nil , fmt . Errorf ( "can not scale barcode to an image smaller tha n %dx%d" , orgWidth , orgHeight )
}
}
offsetX := ( width - ( orgWidth * factor ) ) / 2
offsetX := ( width - ( orgWidth * factor ) ) / 2
@ -77,11 +97,11 @@ func scale2DCode(bc Barcode, width, height int) (Barcode, error) {
return bc . At ( x , y )
return bc . At ( x , y )
}
}
return & scaledBarcode {
return newScaledBC (
bc ,
bc ,
wrap ,
wrap ,
image . Rect ( 0 , 0 , width , height ) ,
image . Rect ( 0 , 0 , width , height ) ,
} , nil
) , nil
}
}
func scale1DCode ( bc Barcode , width , height int ) ( Barcode , error ) {
func scale1DCode ( bc Barcode , width , height int ) ( Barcode , error ) {
@ -90,7 +110,7 @@ func scale1DCode(bc Barcode, width, height int) (Barcode, error) {
factor := int ( float64 ( width ) / float64 ( orgWidth ) )
factor := int ( float64 ( width ) / float64 ( orgWidth ) )
if factor <= 0 {
if factor <= 0 {
return nil , fmt . Errorf ( "can not scale barcode to an image smaller the n %dx1" , orgWidth )
return nil , fmt . Errorf ( "can not scale barcode to an image smaller tha n %dx1" , orgWidth )
}
}
offsetX := ( width - ( orgWidth * factor ) ) / 2
offsetX := ( width - ( orgWidth * factor ) ) / 2
@ -106,10 +126,9 @@ func scale1DCode(bc Barcode, width, height int) (Barcode, error) {
return bc . At ( x , 0 )
return bc . At ( x , 0 )
}
}
return & scaledBarcode {
return newScaledBC (
bc ,
bc ,
wrap ,
wrap ,
image . Rect ( 0 , 0 , width , height ) ,
image . Rect ( 0 , 0 , width , height ) ,
} , nil
) , nil
}
}