annotate vendor/plugins/rfpdf/lib/barcode/barcode.rb @ 669:202986dd17e4 live bibliography_plugin_alpha

Merge from branch "cannam_integration"
author Chris Cannam
date Fri, 09 Sep 2011 16:56:21 +0100
parents cbce1fd3b1b7
children
rev   line source
Chris@441 1
Chris@441 2 #============================================================+
Chris@441 3 # File name : barcode.rb
Chris@441 4 # Begin : 2002-07-31
Chris@441 5 # Last Update : 2005-01-02
Chris@441 6 # Author : Karim Mribti [barcode@mribti.com]
Chris@441 7 # Version : 1.1 [0.0.8a (original code)]
Chris@441 8 # License : GNU LGPL (Lesser General Public License) 2.1
Chris@441 9 # http://www.gnu.org/copyleft/lesser.txt
Chris@441 10 # Source Code : http://www.mribti.com/barcode/
Chris@441 11 #
Chris@441 12 # Description : Generic Barcode Render Class for PHP using
Chris@441 13 # the GD graphics library.
Chris@441 14 #
Chris@441 15 # NOTE:
Chris@441 16 # This version contains changes by Nicola Asuni:
Chris@441 17 # - porting to Ruby
Chris@441 18 # - code style and formatting
Chris@441 19 # - automatic php documentation in PhpDocumentor Style
Chris@441 20 # (www.phpdoc.org)
Chris@441 21 # - minor bug fixing
Chris@441 22 # - $mCharSet and $mChars variables were added here
Chris@441 23 #============================================================+
Chris@441 24
Chris@441 25 #
Chris@441 26 # Barcode Render Class for PHP using the GD graphics library.
Chris@441 27 # @author Karim Mribti, Nicola Asuni
Chris@441 28 # @name BarcodeObject
Chris@441 29 # @package com.tecnick.tcpdf
Chris@441 30 # @@version 0.0.8a 2001-04-01 (original code)
Chris@441 31 # @since 2001-03-25
Chris@441 32 # @license http://www.gnu.org/copyleft/lesser.html LGPL
Chris@441 33 #
Chris@441 34
Chris@441 35 # Styles
Chris@441 36 # Global
Chris@441 37
Chris@441 38 #
Chris@441 39 # option: generate barcode border
Chris@441 40 #
Chris@441 41 define("BCS_BORDER", 1);
Chris@441 42
Chris@441 43 #
Chris@441 44 # option: use transparent background
Chris@441 45 #
Chris@441 46 define("BCS_TRANSPARENT", 2);
Chris@441 47
Chris@441 48 #
Chris@441 49 # option: center barcode
Chris@441 50 #
Chris@441 51 define("BCS_ALIGN_CENTER", 4);
Chris@441 52
Chris@441 53 #
Chris@441 54 # option: align left
Chris@441 55 #
Chris@441 56 define("BCS_ALIGN_LEFT", 8);
Chris@441 57
Chris@441 58 #
Chris@441 59 # option: align right
Chris@441 60 #
Chris@441 61 define("BCS_ALIGN_RIGHT", 16);
Chris@441 62
Chris@441 63 #
Chris@441 64 # option: generate JPEG image
Chris@441 65 #
Chris@441 66 define("BCS_IMAGE_JPEG", 32);
Chris@441 67
Chris@441 68 #
Chris@441 69 # option: generate PNG image
Chris@441 70 #
Chris@441 71 define("BCS_IMAGE_PNG", 64);
Chris@441 72
Chris@441 73 #
Chris@441 74 # option: draw text
Chris@441 75 #
Chris@441 76 define("BCS_DRAW_TEXT", 128);
Chris@441 77
Chris@441 78 #
Chris@441 79 # option: stretch text
Chris@441 80 #
Chris@441 81 define("BCS_STRETCH_TEXT", 256);
Chris@441 82
Chris@441 83 #
Chris@441 84 # option: reverse color
Chris@441 85 #
Chris@441 86 define("BCS_REVERSE_COLOR", 512);
Chris@441 87
Chris@441 88 #
Chris@441 89 # option: draw check
Chris@441 90 # (only for I25 code)
Chris@441 91 #
Chris@441 92 define("BCS_I25_DRAW_CHECK", 2048);
Chris@441 93
Chris@441 94 #
Chris@441 95 # set default background color
Chris@441 96 #
Chris@441 97 define("BCD_DEFAULT_BACKGROUND_COLOR", 0xFFFFFF);
Chris@441 98
Chris@441 99 #
Chris@441 100 # set default foreground color
Chris@441 101 #
Chris@441 102 define("BCD_DEFAULT_FOREGROUND_COLOR", 0x000000);
Chris@441 103
Chris@441 104 #
Chris@441 105 # set default style options
Chris@441 106 #
Chris@441 107 define("BCD_DEFAULT_STYLE", BCS_BORDER | BCS_ALIGN_CENTER | BCS_IMAGE_PNG);
Chris@441 108
Chris@441 109 #
Chris@441 110 # set default width
Chris@441 111 #
Chris@441 112 define("BCD_DEFAULT_WIDTH", 460);
Chris@441 113
Chris@441 114 #
Chris@441 115 # set default height
Chris@441 116 #
Chris@441 117 define("BCD_DEFAULT_HEIGHT", 120);
Chris@441 118
Chris@441 119 #
Chris@441 120 # set default font
Chris@441 121 #
Chris@441 122 define("BCD_DEFAULT_FONT", 5);
Chris@441 123
Chris@441 124 #
Chris@441 125 # st default horizontal resolution
Chris@441 126 #
Chris@441 127 define("BCD_DEFAULT_XRES", 2);
Chris@441 128
Chris@441 129 # Margins
Chris@441 130
Chris@441 131 #
Chris@441 132 # set default margin
Chris@441 133 #
Chris@441 134 define("BCD_DEFAULT_MAR_Y1", 0);
Chris@441 135
Chris@441 136 #
Chris@441 137 # set default margin
Chris@441 138 #
Chris@441 139 define("BCD_DEFAULT_MAR_Y2", 0);
Chris@441 140
Chris@441 141 #
Chris@441 142 # set default text offset
Chris@441 143 #
Chris@441 144 define("BCD_DEFAULT_TEXT_OFFSET", 2);
Chris@441 145
Chris@441 146 # For the I25 Only
Chris@441 147
Chris@441 148 #
Chris@441 149 # narrow bar option
Chris@441 150 # (only for I25 code)
Chris@441 151 #
Chris@441 152 define("BCD_I25_NARROW_BAR", 1);
Chris@441 153
Chris@441 154 #
Chris@441 155 # wide bar option
Chris@441 156 # (only for I25 code)
Chris@441 157 #
Chris@441 158 define("BCD_I25_WIDE_BAR", 2);
Chris@441 159
Chris@441 160 # For the C39 Only
Chris@441 161
Chris@441 162 #
Chris@441 163 # narrow bar option
Chris@441 164 # (only for c39 code)
Chris@441 165 #
Chris@441 166 define("BCD_C39_NARROW_BAR", 1);
Chris@441 167
Chris@441 168 #
Chris@441 169 # wide bar option
Chris@441 170 # (only for c39 code)
Chris@441 171 #
Chris@441 172 define("BCD_C39_WIDE_BAR", 2);
Chris@441 173
Chris@441 174 # For Code 128
Chris@441 175
Chris@441 176 #
Chris@441 177 # set type 1 bar
Chris@441 178 # (only for c128 code)
Chris@441 179 #
Chris@441 180 define("BCD_C128_BAR_1", 1);
Chris@441 181
Chris@441 182 #
Chris@441 183 # set type 2 bar
Chris@441 184 # (only for c128 code)
Chris@441 185 #
Chris@441 186 define("BCD_C128_BAR_2", 2);
Chris@441 187
Chris@441 188 #
Chris@441 189 # set type 3 bar
Chris@441 190 # (only for c128 code)
Chris@441 191 #
Chris@441 192 define("BCD_C128_BAR_3", 3);
Chris@441 193
Chris@441 194 #
Chris@441 195 # set type 4 bar
Chris@441 196 # (only for c128 code)
Chris@441 197 #
Chris@441 198 define("BCD_C128_BAR_4", 4);
Chris@441 199
Chris@441 200 #
Chris@441 201 # Barcode Render Class for PHP using the GD graphics library.
Chris@441 202 # @author Karim Mribti, Nicola Asuni
Chris@441 203 # @name BarcodeObject
Chris@441 204 # @package com.tecnick.tcpdf
Chris@441 205 # @@version 0.0.8a 2001-04-01 (original code)
Chris@441 206 # @since 2001-03-25
Chris@441 207 # @license http://www.gnu.org/copyleft/lesser.html LGPL
Chris@441 208 #
Chris@441 209 class BarcodeObject {
Chris@441 210 #
Chris@441 211 # @var Image width in pixels.
Chris@441 212 # @access protected
Chris@441 213 #
Chris@441 214 protected $mWidth;
Chris@441 215
Chris@441 216 #
Chris@441 217 # @var Image height in pixels.
Chris@441 218 # @access protected
Chris@441 219 #
Chris@441 220 protected $mHeight;
Chris@441 221
Chris@441 222 #
Chris@441 223 # @var Numeric code for Barcode style.
Chris@441 224 # @access protected
Chris@441 225 #
Chris@441 226 protected $mStyle;
Chris@441 227
Chris@441 228 #
Chris@441 229 # @var Background color.
Chris@441 230 # @access protected
Chris@441 231 #
Chris@441 232 protected $mBgcolor;
Chris@441 233
Chris@441 234 #
Chris@441 235 # @var Brush color.
Chris@441 236 # @access protected
Chris@441 237 #
Chris@441 238 protected $mBrush;
Chris@441 239
Chris@441 240 #
Chris@441 241 # @var Image object.
Chris@441 242 # @access protected
Chris@441 243 #
Chris@441 244 protected $mImg;
Chris@441 245
Chris@441 246 #
Chris@441 247 # @var Numeric code for character font.
Chris@441 248 # @access protected
Chris@441 249 #
Chris@441 250 protected $mFont;
Chris@441 251
Chris@441 252 #
Chris@441 253 # @var Error message.
Chris@441 254 # @access protected
Chris@441 255 #
Chris@441 256 protected $mError;
Chris@441 257
Chris@441 258 #
Chris@441 259 # @var Character Set.
Chris@441 260 # @access protected
Chris@441 261 #
Chris@441 262 protected $mCharSet;
Chris@441 263
Chris@441 264 #
Chris@441 265 # @var Allowed symbols.
Chris@441 266 # @access protected
Chris@441 267 #
Chris@441 268 protected $mChars;
Chris@441 269
Chris@441 270 #
Chris@441 271 # Class Constructor.
Chris@441 272 # @param int $Width Image width in pixels.
Chris@441 273 # @param int $Height Image height in pixels.
Chris@441 274 # @param int $Style Barcode style.
Chris@441 275 #
Chris@441 276 def __construct($Width=BCD_DEFAULT_WIDTH, $Height=BCD_DEFAULT_HEIGHT, $Style=BCD_DEFAULT_STYLE)
Chris@441 277 @mWidth = $Width;
Chris@441 278 @mHeight = $Height;
Chris@441 279 @mStyle = $Style;
Chris@441 280 @mFont = BCD_DEFAULT_FONT;
Chris@441 281 @mImg = ImageCreate(@mWidth, @mHeight);
Chris@441 282 $dbColor = @mStyle & BCS_REVERSE_COLOR ? BCD_DEFAULT_FOREGROUND_COLOR : BCD_DEFAULT_BACKGROUND_COLOR;
Chris@441 283 $dfColor = @mStyle & BCS_REVERSE_COLOR ? BCD_DEFAULT_BACKGROUND_COLOR : BCD_DEFAULT_FOREGROUND_COLOR;
Chris@441 284 @mBgcolor = ImageColorAllocate(@mImg, ($dbColor & 0xFF0000) >> 16,
Chris@441 285 ($dbColor & 0x00FF00) >> 8, $dbColor & 0x0000FF);
Chris@441 286 @mBrush = ImageColorAllocate(@mImg, ($dfColor & 0xFF0000) >> 16,
Chris@441 287 ($dfColor & 0x00FF00) >> 8, $dfColor & 0x0000FF);
Chris@441 288 if (!(@mStyle & BCS_TRANSPARENT))
Chris@441 289 ImageFill(@mImg, @mWidth, @mHeight, @mBgcolor);
Chris@441 290 end
Chris@441 291 end
Chris@441 292
Chris@441 293 #
Chris@441 294 # Class Destructor.
Chris@441 295 # Destroy image object.
Chris@441 296 #
Chris@441 297 def __destructor()
Chris@441 298 @DestroyObject();
Chris@441 299 end
Chris@441 300
Chris@441 301 #
Chris@441 302 # Returns the image object.
Chris@441 303 # @return object image.
Chris@441 304 # @author Nicola Asuni
Chris@441 305 # @since 1.5.2
Chris@441 306 #
Chris@441 307 def getImage()
Chris@441 308 return @mImg;
Chris@441 309 end
Chris@441 310
Chris@441 311 #
Chris@441 312 # Abstract method used to draw the barcode image.
Chris@441 313 # @param int $xres Horizontal resolution.
Chris@441 314 #
Chris@441 315 def DrawObject($xres) {
Chris@441 316 # there is not implementation neded, is simply the asbsract function.#
Chris@441 317 return false;
Chris@441 318 end
Chris@441 319
Chris@441 320 #
Chris@441 321 # Draws the barcode border.
Chris@441 322 # @access protected
Chris@441 323 #
Chris@441 324 protected function DrawBorder()
Chris@441 325 ImageRectangle(@mImg, 0, 0, @mWidth-1, @mHeight-1, @mBrush);
Chris@441 326 end
Chris@441 327
Chris@441 328 #
Chris@441 329 # Draws the alphanumeric code.
Chris@441 330 # @param int $Font Font type.
Chris@441 331 # @param int $xPos Horiziontal position.
Chris@441 332 # @param int $yPos Vertical position.
Chris@441 333 # @param int $Char Alphanumeric code to write.
Chris@441 334 # @access protected
Chris@441 335 #
Chris@441 336 protected function DrawChar($Font, $xPos, $yPos, $Char)
Chris@441 337 ImageString(@mImg,$Font,$xPos,$yPos,$Char,@mBrush);
Chris@441 338 end
Chris@441 339
Chris@441 340 #
Chris@441 341 # Draws a character string.
Chris@441 342 # @param int $Font Font type.
Chris@441 343 # @param int $xPos Horiziontal position.
Chris@441 344 # @param int $yPos Vertical position.
Chris@441 345 # @param int $Char string to write.
Chris@441 346 # @access protected
Chris@441 347 #
Chris@441 348 protected function DrawText($Font, $xPos, $yPos, $Char)
Chris@441 349 ImageString(@mImg,$Font,$xPos,$yPos,$Char,@mBrush);
Chris@441 350 end
Chris@441 351
Chris@441 352 #
Chris@441 353 # Draws a single barcode bar.
Chris@441 354 # @param int $xPos Horiziontal position.
Chris@441 355 # @param int $yPos Vertical position.
Chris@441 356 # @param int $xSize Horizontal size.
Chris@441 357 # @param int $xSize Vertical size.
Chris@441 358 # @return bool trur in case of success, false otherwise.
Chris@441 359 # @access protected
Chris@441 360 #
Chris@441 361 protected function DrawSingleBar($xPos, $yPos, $xSize, $ySize)
Chris@441 362 if ($xPos>=0 && $xPos<=@mWidth && ($xPos+$xSize)<=@mWidth &&
Chris@441 363 $yPos>=0 && $yPos<=@mHeight && ($yPos+$ySize)<=@mHeight)
Chris@441 364 for ($i=0;$i<$xSize;$i++)
Chris@441 365 ImageLine(@mImg, $xPos+$i, $yPos, $xPos+$i, $yPos+$ySize, @mBrush);
Chris@441 366 end
Chris@441 367 return true;
Chris@441 368 end
Chris@441 369 return false;
Chris@441 370 end
Chris@441 371
Chris@441 372 #
Chris@441 373 # Returns the current error message.
Chris@441 374 # @return string error message.
Chris@441 375 #
Chris@441 376 def GetError()
Chris@441 377 return @mError;
Chris@441 378 end
Chris@441 379
Chris@441 380 #
Chris@441 381 # Returns the font height.
Chris@441 382 # @param int $font font type.
Chris@441 383 # @return int font height.
Chris@441 384 #
Chris@441 385 def GetFontHeight($font)
Chris@441 386 return ImageFontHeight($font);
Chris@441 387 end
Chris@441 388
Chris@441 389 #
Chris@441 390 # Returns the font width.
Chris@441 391 # @param int $font font type.
Chris@441 392 # @return int font width.
Chris@441 393 #
Chris@441 394 def GetFontWidth($font)
Chris@441 395 return ImageFontWidth($font);
Chris@441 396 end
Chris@441 397
Chris@441 398 #
Chris@441 399 # Set font type.
Chris@441 400 # @param int $font font type.
Chris@441 401 #
Chris@441 402 def SetFont($font)
Chris@441 403 @mFont = $font;
Chris@441 404 end
Chris@441 405
Chris@441 406 #
Chris@441 407 # Returns barcode style.
Chris@441 408 # @return int barcode style.
Chris@441 409 #
Chris@441 410 def GetStyle()
Chris@441 411 return @mStyle;
Chris@441 412 end
Chris@441 413
Chris@441 414 #
Chris@441 415 # Set barcode style.
Chris@441 416 # @param int $Style barcode style.
Chris@441 417 #
Chris@441 418 def SetStyle ($Style)
Chris@441 419 @mStyle = $Style;
Chris@441 420 end
Chris@441 421
Chris@441 422 #
Chris@441 423 # Flush the barcode image.
Chris@441 424 #
Chris@441 425 def FlushObject()
Chris@441 426 if ((@mStyle & BCS_BORDER))
Chris@441 427 @DrawBorder();
Chris@441 428 end
Chris@441 429 if (@mStyle & BCS_IMAGE_PNG)
Chris@441 430 Header("Content-Type: image/png");
Chris@441 431 ImagePng(@mImg);
Chris@441 432 elsif (@mStyle & BCS_IMAGE_JPEG)
Chris@441 433 Header("Content-Type: image/jpeg");
Chris@441 434 ImageJpeg(@mImg);
Chris@441 435 end
Chris@441 436 end
Chris@441 437
Chris@441 438 #
Chris@441 439 # Destroy the barcode image.
Chris@441 440 #
Chris@441 441 def DestroyObject()
Chris@441 442 ImageDestroy(@mImg);
Chris@441 443 end
Chris@441 444 }
Chris@441 445
Chris@441 446 #============================================================+
Chris@441 447 # END OF FILE
Chris@441 448 #============================================================+