Exemplo n.º 1
0
 function Stroke($aData, $aFile = '', $aShowDetails = false, $aShowEncodingDetails = false)
 {
     $textmargin = 5;
     $this->iEncoder->AddChecksum($this->iUseChecksum);
     $spec = $this->iEncoder->Enc($aData);
     $this->AdjustSpec($spec);
     if ($this->iFontFam == -1) {
         if ($this->iModuleWidth > 1) {
             $this->iFontFam = FF_FONT2;
             $this->iFontStyle = FS_BOLD;
         } else {
             $this->iFontFam = FF_FONT1;
             $this->iFontStyle = FS_BOLD;
         }
     }
     $s = '';
     $n = count($spec->iBar);
     $g = new CanvasGraph(0, 0);
     $g->img->SetImgFormat($this->iImgFormat);
     if ($aShowDetails) {
         $s = $spec->iEncoding . "\n";
         $s .= 'Data: ' . $spec->iData . "\n";
         if ($spec->iInfo != '') {
             $s .= 'Info: ' . $spec->iInfo . "\n";
         }
     }
     $w = round($spec->iModuleWidth);
     $totwidth = $spec->iLeftMargin * $w;
     $n = count($spec->iBar);
     for ($i = 0; $i < $n; ++$i) {
         $b = $spec->iBar[$i];
         $bn = strlen($b[3]);
         for ($j = 0; $j < $bn; ++$j) {
             $wb = substr($b[3], $j, 1) * $w;
             $totwidth += $wb;
         }
     }
     if ($spec->iInterCharModuleSpace) {
         $totwidth += ($n - 2) * $w;
     }
     $totwidth += $spec->iRightMargin * $w + 1;
     $height = $this->iHeight;
     if ($aShowDetails) {
         $g->img->SetFont(FF_FONT2);
         $height += $g->img->GetTextHeight($s);
     }
     $g->img->SetFont($this->iFontFam, $this->iFontStyle, $this->iFontSize);
     $th = $g->img->GetTextHeight($spec->iData);
     if ($spec->iStrokeDataBelow) {
         $height += $th + $this->iDataBelowMargin;
     }
     if ($height < round(0.15 * ($totwidth - $spec->iRightMargin * $w - $spec->iLeftMargin * $w))) {
         $height = round(0.15 * $totwidth);
     }
     $g->img->SetFont(FF_FONT2);
     $tw = 2 * $textmargin + $g->img->GetTextWidth($s);
     $width = $totwidth;
     if ($width < $tw) {
         $width = $tw;
     }
     if ($aShowEncodingDetails) {
         $g->img->SetFont(FF_FONT2);
         $height += $n * $g->img->GetTextHeight('0');
         $width = max(300, $totwidth);
     }
     $g = new CanvasGraph($width, $height);
     $g->img->SetImgFormat($this->iImgFormat);
     $g->SetMarginColor('white');
     if ($this->iShowFrame) {
         $g->frame_color = $this->iFrameColor;
         $g->InitFrame();
     }
     $g->img->SetColor('black');
     $x = $w * $spec->iLeftMargin;
     $ystart = $this->iTopMargin;
     $yend = $height - $this->iBottomMargin - 1;
     if ($aShowDetails) {
         $ystart += $g->img->GetTextHeight($s);
     }
     if ($aShowEncodingDetails) {
         $g->img->SetFont(FF_FONT2);
         $ystart += $n * $g->img->GetTextHeight('0');
     }
     if ($spec->iStrokeDataBelow) {
         $yend -= $th + $this->iDataBelowMargin;
     }
     $inunder = false;
     $under_s = '';
     $under_x = 0;
     for ($i = 0; $i < $n; ++$i) {
         $b = $spec->iBar[$i];
         if ($aShowEncodingDetails) {
             $s .= sprintf("%02d", $i) . " : {$b['0']}, {$b['1']}, {$b['2']}, {$b['3']}\n";
         }
         $bn = strlen($b[3]);
         if ($b[2] == 0 && !$this->iNoHumanText) {
             if (!$inunder) {
                 $inunder = true;
                 $under_x = $x;
                 $under_s = $b[0];
             } else {
                 $under_s .= $b[0];
             }
         } else {
             if ($inunder) {
                 $inunder = false;
                 if ($under_s != '') {
                     $t = new Text($under_s, ($under_x + $x - 1) / 2, $yend - $th / 1.3);
                     $t->SetFont($this->iFontFam, $this->iFontStyle, $this->iFontSize);
                     $t->Align('center', 'top');
                     $t->Stroke($g->img);
                 }
             }
         }
         $startx = $x;
         for ($j = 0; $j < $bn; ++$j) {
             $wb = substr($b[3], $j, 1) * $w;
             if ($j % 2 == $b[1]) {
                 $g->img->SetColor($this->iBkgColor);
             } else {
                 $g->img->SetColor($this->iColor);
             }
             if ($b[2] == 1 || $this->iNoHumanText) {
                 $g->img->FilledRectangle($x, $ystart, $x + $wb - 1, $yend);
             } else {
                 $g->img->FilledRectangle($x, $ystart, $x + $wb - 1, $yend - $th);
             }
             $x += $wb;
         }
         if ($this->iDebugBackground) {
             $g->SetAlphaBlending();
             if (($i & 1) == 0) {
                 $g->img->SetColor('lightblue@0.5');
             } else {
                 $g->img->SetColor('yellow@0.5');
             }
             $g->img->FilledRectangle($startx, $ystart - 2, $x, $yend);
         }
         if ($spec->iInterCharModuleSpace) {
             $x += $w;
         }
     }
     $g->img->SetColor($this->iColor);
     if (!($spec->iLeftData === '')) {
         $g->img->SetTextAlign('right', 'top');
         $g->img->SetFont($this->iSmallFontFam, $this->iSmallFontStyle, $this->iSmallFontSize);
         $g->img->StrokeText($w * $spec->iLeftMargin - 3, $yend - $th, $spec->iLeftData);
     }
     if (!($spec->iRightData === '')) {
         $g->img->SetTextAlign('left', 'top');
         $g->img->SetFont($this->iSmallFontFam, $this->iSmallFontStyle, $this->iSmallFontSize);
         $g->img->StrokeText($x + 3, $yend - $th, $spec->iRightData);
     }
     if ($spec->iStrokeDataBelow) {
         $y = $yend + $this->iDataBelowMargin;
         $bw = $totwidth - $spec->iLeftMargin * $w - $spec->iRightMargin * $w;
         $x = $spec->iLeftMargin * $w + floor($bw / 2);
         $g->img->SetTextAlign('center', 'top');
         $g->img->SetFont($this->iFontFam, $this->iFontStyle, $this->iFontSize);
         if ($this->iHumanTxt !== '') {
             $g->img->StrokeText($x, $y, $this->iHumanTxt);
         } else {
             $g->img->StrokeText($x, $y, $spec->iData);
         }
     }
     if ($aShowDetails) {
         $g->img->SetColor('navy');
         $g->img->SetTextAlign('left', 'top');
         $g->img->SetFont(FF_FONT2);
         $g->img->StrokeText($textmargin, $this->iTopMargin, $s);
     }
     if (ADD_DEMOTXT === true) {
         $t = new Text("<<DEMO>>", $totwidth / 2, $ystart);
         if ($this->iModuleWidth > 1) {
             if ($this->iModuleWidth > 4) {
                 $t->SetFont(FF_ARIAL, FS_BOLD, 32);
                 $step = 140;
                 $yadj = 50;
             } else {
                 $t->SetFont(FF_ARIAL, FS_BOLD, 24);
                 $step = 110;
                 $yadj = 40;
             }
         } else {
             $t->SetFont(FF_ARIAL, FS_BOLD, 18);
             $step = 80;
             $yadj = 30;
         }
         $t->SetColor('red@0.4');
         $t->Align('center', 'center');
         $t->SetAngle(-25);
         $n = ceil($totwidth / $step);
         for ($i = 0; $i < $n; ++$i) {
             $t->SetPos(-30 + $i * $step, ($yend - $ystart) / 2 - $yadj);
             $t->Stroke($g->img);
         }
     }
     if ($this->iVertical) {
         $g->img->img = $this->Rotate($g->img->img, 90);
     }
     if ($this->iScale != 1) {
         $nwidth = round($width * $this->iScale);
         $nheight = round($height * $this->iScale);
         if ($this->iVertical) {
             $tmp = $height;
             $height = $width;
             $width = $tmp;
             $tmp = $nheight;
             $nheight = $nwidth;
             $nwidth = $tmp;
         }
         $img = @imagecreatetruecolor($nwidth, $nheight);
         if ($img) {
             imagealphablending($img, true);
             imagecopyresampled($img, $g->img->img, 0, 0, 0, 0, $nwidth, $nheight, $width, $height);
             $g->img->CreateImgCanvas($nwidth, $nheight);
             $g->img->img = $img;
         } else {
             return false;
         }
     }
     return $g->Stroke($aFile);
 }