Files
harbour-core/harbour/contrib/hbgd/gdbar.prg
Viktor Szakats 82ae26c290 2012-06-08 02:52 UTC+0200 Viktor Szakats (harbour syenar.net)
* contrib/hbgd/gdbar.prg
  * contrib/hbgd/gdbarcod.prg
  * contrib/hbgd/gdchart.prg
  * contrib/hbgd/gdimage.prg
    * formatting
    % using HB_DEFAULT() instead of DEFAULT TO and local DEFAULT() macro
      (it means that in these places, type checking is more thourough
      than before). I left DEFAULT TO in places where the default expression
      is complex.
    ! fixed TCODE():New() to not use Alert(), use ::DrawError() instead
    % HGetValue(): use HB_HGETDEF() instead of equivalent local logic
    % use HB_ISHASH() instead of ValType()
    % use hb_ntos() instead of LTrim( Str() )
    ; build tested only, pls review
2012-06-08 00:52:31 +00:00

355 lines
9.1 KiB
Plaintext

/*
* $Id$
*/
/*
* xHarbour Project source code:
* BAR engine library class
*
* Copyright 2005-2005 Laverson Espindola <laverson.espindola@gmail.com>
* www - http://www.xharbour.org http://harbour-project.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
*
* As a special exception, the Harbour Project gives permission for
* additional uses of the text contained in its release of Harbour.
*
* The exception is that, if you link the Harbour libraries with other
* files to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the Harbour library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the Harbour
* Project under the name Harbour. If you copy code from other
* Harbour Project or Free Software Foundation releases into a copy of
* Harbour, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for Harbour, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
*
*/
#include "gd.ch"
#include "hbclass.ch"
#define IMG_FORMAT_PNG 1
#define IMG_FORMAT_JPEG 2
#define IMG_FORMAT_WBMP 4
#define IMG_FORMAT_GIF 8
#define IMG_QUALITY 95
#define THICKNESS_I250 0
#define THICKNESS_I251 0
CREATE CLASS TBarCode FROM GDImage
// class attributes
VAR positionX AS NUMERIC INIT 4
VAR positionY AS NUMERIC
VAR maxHeight AS NUMERIC INIT 25
VAR maxHDefa AS NUMERIC INIT 25
VAR lastX AS NUMERIC
VAR lastY AS NUMERIC
VAR error AS NUMERIC
VAR imWidth AS NUMERIC
// Barcode attributes
VAR Parity
VAR LeftHand_Even AS ARRAY
VAR Right_Hand AS ARRAY
VAR LeftHand_Odd AS ARRAY
VAR keys AS ARRAY
VAR book AS LOGICAL INIT .F.
VAR acode AS ARRAY
VAR KeysModeA AS CHARACTER
VAR KeysModeB AS CHARACTER
VAR KeysModeC AS ARRAY
// image attributes
VAR res AS NUMERIC
VAR textfont AS NUMERIC
VAR TEXT AS CHARACTER
VAR filename AS CHARACTER
VAR color_b AS ARRAY
VAR color_f AS ARRAY
VAR FillColor AS NUMERIC
VAR BackColor AS NUMERIC
VAR lDrawValue AS LOGICAL INIT .T.
// Methods
METHOD CreateBar( sx, sy, filename, ccolor )
METHOD Configure( nmaxHeight, aFillColor, aBackColor, nres, ntextfont, lbook, lDrawValue )
METHOD Allocate()
METHOD DrawError( ptext )
METHOD DrawSingleBar( pcode )
METHOD DrawSingleI25( pcode )
METHOD DrawText( lIsI25 )
METHOD nextX( lI25 )
METHOD Finish( image_style, quality, nFG )
METHOD SetText( ptext )
METHOD ResetColor()
METHOD CheckCode()
METHOD CheckValInArray( cchar )
ENDCLASS
METHOD CreateBar( sx, sy, filename, ccolor ) CLASS TBarCode
::Create( sx, sy )
hb_default( @ccolor, { 255, 255, 255 } )
::SetColor( ccolor[ 1 ], ccolor[ 2 ], ccolor[ 3 ] )
::error := 0
::positionY := 0
::imWidth := sx
IF ! Empty( filename )
::filename := filename
ENDIF
::FillColor := ::SetColor( ::color_f[ 1 ] , ::color_f[ 2 ] , ::color_f[ 3 ] )
::BackColor := ::SetColor( ::color_b[ 1 ] , ::color_b[ 2 ] , ::color_b[ 3 ] )
::Setfont( "Arial" )
// configures Fontes
IF ::textfont == 1 ; ::SetFontSmall()
ELSEIF ::textfont == 2 ; ::SetFontLarge()
ELSEIF ::textfont == 3 ; ::SetFontMediumBold()
ELSEIF ::textfont == 4 ; ::SetFontGiant()
ELSEIF ::textfont == 5 ; ::SetFontTiny()
ENDIF
::SetFontPitch( ::textfont )
// always restores
::maxHeight := ::maxHDefa
RETURN Self
METHOD Configure( nmaxHeight, aFillColor, aBackColor, nres, ntextfont, lbook, lDrawValue ) CLASS TBarCode
hb_default( @lbook , .F. )
hb_default( @lDrawValue , .T. )
hb_default( @nmaxHeight , 25 )
hb_default( @ntextfont , 2 )
hb_default( @nres , 2 )
hb_default( @aBackColor , { 255, 255, 255 } )
hb_default( @aFillColor , { 0, 0, 0 } )
::book := lbook
::maxHeight := nmaxHeight
::res := nres
::textfont := ntextfont
::lDrawValue := lDrawValue
::color_b := AClone( aBackColor )
::color_f := AClone( aFillColor )
RETURN NIL
METHOD SetText( ptext ) CLASS TBarCode
::text := ptext
RETURN NIL
METHOD ResetColor() CLASS TBarCode
::FillColor := ::SetColor( ::color_f[ 1 ] , ::color_f[ 2 ] , ::color_f[ 3 ] )
::BackColor := ::SetColor( ::color_b[ 1 ] , ::color_b[ 2 ] , ::color_b[ 3 ] )
RETURN NIL
METHOD Allocate() CLASS TBarCode
LOCAL R := ::color_b[ 1 ]
LOCAL G := ::color_b[ 2 ]
LOCAL B := ::color_b[ 3 ]
RETURN ::SetColor( R, G, B )
METHOD DrawSingleBar( pcode ) CLASS TBarCode
LOCAL i
LOCAL j
FOR j := 1 TO Len( pcode )
FOR i := 1 TO ::res
::Line( ::positionX + i , ::positionY , ::positionX + i , ( ::positionY + ::maxHeight ) , ;
iif( SubStr( pcode, j, 1 ) $ "0", ::BackColor, ::FillColor ) )
NEXT
::NextX()
NEXT
RETURN NIL
METHOD DrawSingleI25( pcode ) CLASS TBarCode
LOCAL j
LOCAL widthSlimBar := 1
LOCAL widthFatBar := 3
LOCAL imgBar
LOCAL imgWid
LOCAL end_y
LOCAL qw
::positionX := 10
FOR j := 1 TO Len( pcode )
imgBar := iif( j % 2 == 0, ::FillColor, ::BackColor )
imgWid := iif( SubStr( pcode,j,1 ) == "0" , widthSlimBar, widthFatBar )
end_y := ::maxHeight
FOR qw := 1 TO imgWid
::Line( ::positionX, 1, ::positionX, end_y, imgBar )
::nextX( .T. )
NEXT
NEXT
RETURN NIL
METHOD DrawError( ptext ) CLASS TBarCode
::Say( 5, ::error * 15, ptext, ::FillColor )
::error ++
::lastX := iif( ( ::GetFontWidth() * Len(ptext ) ) > ::lastX , ( ::GetFontWidth() * Len(ptext ) ) , ::lastX )
::lastY := ::error * 15
RETURN NIL
METHOD nextX( lI25 ) CLASS TBarCode
hb_default( @li25, .F. )
IF li25
::positionX ++
ELSE
::positionX += ::res
ENDIF
RETURN NIL
METHOD DrawText( lIsI25 ) CLASS TBarCode
LOCAL xPosition
hb_default( @lIsI25, .F. )
IF lIsI25
If ::textfont != 0
xPosition := 10 * ::GetFontWidth()
::say( xPosition, ::maxHeight, "*" + ::text + "*" , ::FillColor )
::lastY := ::maxHeight + ::GetFontHeight()
ENDIF
ELSE
If ::textfont != 0
xPosition := ( ::positionX / 2 ) - ( Len( ::text ) / 2 ) * ::GetFontWidth()
::say( xPosition, ::maxHeight, ::text, ::FillColor )
::lastY := ::maxHeight + ::GetFontHeight()
ENDIF
ENDIF
RETURN .T.
METHOD CheckCode() CLASS TBarCode
LOCAL lRet := .T.
LOCAL i
FOR i := 1 TO Len( ::text )
IF HB_ISSTRING( ::CheckValInArray( SubStr( ::text, i, 1 ) ) )
::DrawError( "Character " + SubStr( ::text, i, 1 ) + " not allowed ." )
lRet := .F.
ENDIF
NEXT
RETURN lRet
METHOD CheckValInArray( cchar ) CLASS TBarCode
LOCAL npos
LOCAL uret
npos := AScan( ::keys, {| x | SubStr( x, 1, 1 ) == cchar } )
IF npos > 0
uret := npos
ELSE
uret := NIL
ENDIF
RETURN uret
METHOD Finish( image_style, quality, nFG ) CLASS TBarCode
hb_default( @image_style, IMG_FORMAT_PNG )
hb_default( @quality , 95 )
hb_default( @nFG , { 255, 255, 255 } )
IF Empty( ::filename ) .OR. ::filename == NIL
// Output std handle == 1
//::filename := ::text
IF image_style == IMG_FORMAT_PNG
::OutputPng()
ELSEIF image_style == IMG_FORMAT_JPEG
::OutputJpeg( , quality )
ELSEIF image_style == IMG_FORMAT_WBMP
::OutputWBmp( , nFG )
ELSEIF image_style == IMG_FORMAT_GIF
::OutputGif()
ENDIF
ELSE
IF image_style == IMG_FORMAT_PNG
::SavePng( ::filename )
ELSEIF image_style == IMG_FORMAT_JPEG
::Savejpeg( ::filename, quality )
ELSEIF image_style == IMG_FORMAT_WBMP
::SaveWBmp( ::filename, nFG )
ELSEIF image_style == IMG_FORMAT_GIF
::SaveGif( ::filename )
ENDIF
ENDIF
RETURN .T.