/**
* Print a Linear Barcode.
* @param string $code code to print
* @param string $type type of barcode (see barcodes.php for supported formats).
* @param int $x x position in user units
* @param int $y y position in user units
* @param int $w width in user units
* @param int $h height in user units
* @param float $xres width of the smallest bar in user units
* @param array $style array of options:<ul><li>string $style['position'] barcode position inside the specified width: L = left (default for LTR); C = center; R = right (default for RTL); S = stretch</li><li>boolean $style['border'] if true prints a border around the barcode</li><li>int $style['padding'] padding to leave around the barcode in user units</li><li>array $style['fgcolor'] color array for bars and text</li><li>mixed $style['bgcolor'] color array for background or false for transparent</li><li>boolean $style["text"] boolean if true prints text below the barcode</li><li>string $style['font'] font name for text</li><li>int $style['fontsize'] font size for text</li><li>int $style['stretchtext']: 0 = disabled; 1 = horizontal scaling only if necessary; 2 = forced horizontal scaling; 3 = character spacing only if necessary; 4 = forced character spacing</li></ul>
* @param string $align Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
* @author Nicola Asuni
* @since 3.1.000 (2008-06-09)
* @access public
*/
public function write1DBarcode($code, $type, $x = '', $y = '', $w = '', $h = '', $xres = 0.4, $style = '', $align = '')
{
if ($this->empty_string($code)) {
return;
}
require_once dirname(__FILE__) . '/barcodes.php';
// save current graphic settings
$gvars = $this->getGraphicVars();
// create new barcode object
$barcodeobj = new TCPDFBarcode($code, $type);
$arrcode = $barcodeobj->getBarcodeArray();
if ($arrcode === false) {
$this->Error('Error in 1D barcode string');
}
// set default values
if (!isset($style['position'])) {
if ($this->rtl) {
$style['position'] = 'R';
} else {
$style['position'] = 'L';
}
}
if (!isset($style['padding'])) {
$style['padding'] = 0;
}
if (!isset($style['fgcolor'])) {
$style['fgcolor'] = array(0, 0, 0);
// default black
}
if (!isset($style['bgcolor'])) {
$style['bgcolor'] = false;
// default transparent
}
if (!isset($style['border'])) {
$style['border'] = false;
}
$fontsize = 0;
if (!isset($style['text'])) {
$style['text'] = false;
}
if ($style['text'] and isset($style['font'])) {
if (isset($style['fontsize'])) {
$fontsize = $style['fontsize'];
}
$this->SetFont($style['font'], '', $fontsize);
}
if (!isset($style['stretchtext'])) {
$style['stretchtext'] = 4;
}
// set foreground color
$this->SetDrawColorArray($style['fgcolor']);
$this->SetTextColorArray($style['fgcolor']);
if ($this->empty_string($w) or $w <= 0) {
if ($this->rtl) {
$w = $this->x - $this->lMargin;
} else {
$w = $this->w - $this->rMargin - $this->x;
}
}
if ($this->empty_string($x)) {
$x = $this->GetX();
}
if ($this->rtl) {
$x = $this->w - $x;
}
if ($this->empty_string($y)) {
$y = $this->GetY();
}
if ($this->empty_string($xres)) {
$xres = 0.4;
}
$fbw = $arrcode['maxw'] * $xres + 2 * $style['padding'];
$extraspace = $this->cell_height_ratio * $fontsize / $this->k + 2 * $style['padding'];
if ($this->empty_string($h) or $h <= 0) {
$h = 10 + $extraspace;
}
$prev_x = $this->x;
if ($this->checkPageBreak($h, $y)) {
$y = $this->GetY() + $this->cMargin;
if ($this->rtl) {
$x += $prev_x - $this->x;
} else {
$x += $this->x - $prev_x;
}
}
// maximum bar height
$barh = $h - $extraspace;
switch ($style['position']) {
case 'L':
// left
if ($this->rtl) {
$xpos = $x - $w;
} else {
$xpos = $x;
}
break;
case 'C':
// center
$xdiff = ($w - $fbw) / 2;
if ($this->rtl) {
$xpos = $x - $w + $xdiff;
} else {
$xpos = $x + $xdiff;
}
break;
case 'R':
// right
if ($this->rtl) {
$xpos = $x - $fbw;
} else {
$xpos = $x + $w - $fbw;
}
break;
case 'S':
// stretch
$fbw = $w;
$xres = ($w - 2 * $style['padding']) / $arrcode['maxw'];
if ($this->rtl) {
$xpos = $x - $w;
} else {
$xpos = $x;
}
break;
}
$xpos_rect = $xpos;
$xpos = $xpos_rect + $style['padding'];
$xpos_text = $xpos;
// barcode is always printed in LTR direction
$tempRTL = $this->rtl;
$this->rtl = false;
// print background color
if ($style['bgcolor']) {
$this->Rect($xpos_rect, $y, $fbw, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
} elseif ($style['border']) {
$this->Rect($xpos_rect, $y, $fbw, $h, 'D');
}
// print bars
if ($arrcode !== false) {
foreach ($arrcode['bcode'] as $k => $v) {
$bw = $v['w'] * $xres;
if ($v['t']) {
// draw a vertical bar
$ypos = $y + $style['padding'] + $v['p'] * $barh / $arrcode['maxh'];
$this->Rect($xpos, $ypos, $bw, $v['h'] * $barh / $arrcode['maxh'], 'F', array(), $style['fgcolor']);
}
$xpos += $bw;
}
}
// print text
if ($style['text']) {
// print text
$this->x = $xpos_text;
$this->y = $y + $style['padding'] + $barh;
$this->Cell($arrcode['maxw'] * $xres, $this->cell_height_ratio * $fontsize / $this->k, $code, 0, 0, 'C', 0, '', $style['stretchtext']);
}
// restore original direction
$this->rtl = $tempRTL;
// restore previous settings
$this->setGraphicVars($gvars);
// set bottomcoordinates
$this->img_rb_y = $y + $h;
if ($this->rtl) {
// set left side coordinate
$this->img_rb_x = $this->w - $x - $w;
} else {
// set right side coordinate
$this->img_rb_x = $x + $w;
}
// set pointer to align the successive text/objects
switch ($align) {
case 'T':
$this->y = $y;
$this->x = $this->img_rb_x;
break;
case 'M':
$this->y = $y + round($h / 2);
$this->x = $this->img_rb_x;
break;
case 'B':
$this->y = $this->img_rb_y;
$this->x = $this->img_rb_x;
break;
case 'N':
$this->SetY($this->img_rb_y);
break;
default:
break;
}
}