mirror of
https://github.com/snipe/snipe-it.git
synced 2026-02-04 08:05:23 +00:00
add Helper function for label layout, applied to 1933081 2112283"
This commit is contained in:
@ -13,6 +13,7 @@ use App\Models\Setting;
|
||||
use App\Models\Statuslabel;
|
||||
use App\Models\License;
|
||||
use App\Models\Location;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Illuminate\Contracts\Encryption\DecryptException;
|
||||
@ -1735,4 +1736,61 @@ class Helper
|
||||
}
|
||||
return $mismatched;
|
||||
}
|
||||
static public function labelFieldLayoutScaling(
|
||||
$pdf,
|
||||
iterable|\Closure $fields,
|
||||
float $currentX,
|
||||
float $usableWidth,
|
||||
float $usableHeight,
|
||||
float $baseLabelSize,
|
||||
float $baseFieldSize,
|
||||
float $baseFieldMargin,
|
||||
float $baseLabelPadding = 1.5,
|
||||
float $baseGap = 1.5,
|
||||
float $maxScale = 1.8,
|
||||
string $labelFont = 'freesans',
|
||||
) : array
|
||||
{
|
||||
$fieldCount = count($fields);
|
||||
$perFieldHeight = max($baseLabelSize, $baseFieldSize) + $baseFieldMargin;
|
||||
$baseHeight = $fieldCount * $perFieldHeight;
|
||||
$scale = 1.0;
|
||||
if ($baseHeight > 0 && $usableHeight > 0) {
|
||||
$scale = $usableHeight / $baseHeight;
|
||||
}
|
||||
|
||||
$scale = min($scale, $maxScale);
|
||||
|
||||
$labelSize = $baseLabelSize * $scale;
|
||||
$fieldSize = $baseFieldSize * $scale;
|
||||
$fieldMargin = $baseFieldMargin * $scale;
|
||||
|
||||
$rowAdvance = max($labelSize, $fieldSize) + $fieldMargin;
|
||||
$pdf->SetFont($labelFont, '', $baseLabelSize);
|
||||
|
||||
$maxLabelWidthPerUnit = 0;
|
||||
foreach ($fields as $field) {
|
||||
$label = rtrim($field['label'], ':') . ':';
|
||||
$width = $pdf->GetStringWidth($label);
|
||||
$maxLabelWidthPerUnit = max($maxLabelWidthPerUnit, $width / $baseLabelSize);
|
||||
}
|
||||
|
||||
$labelPadding = $baseLabelPadding * $scale;
|
||||
$gap = $baseGap * $scale;
|
||||
|
||||
$labelWidth = ($maxLabelWidthPerUnit * $labelSize) + $labelPadding;
|
||||
$valueX = $currentX + $labelWidth + $gap;
|
||||
$valueWidth = $usableWidth - $labelWidth - $gap;
|
||||
|
||||
return compact(
|
||||
'scale',
|
||||
'labelSize',
|
||||
'fieldSize',
|
||||
'fieldMargin',
|
||||
'rowAdvance',
|
||||
'labelWidth',
|
||||
'valueX',
|
||||
'valueWidth'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,8 @@
|
||||
namespace App\Models\Labels\Tapes\Dymo;
|
||||
|
||||
|
||||
use App\Helpers\Helper;
|
||||
|
||||
class LabelWriter_1933081 extends LabelWriter
|
||||
{
|
||||
private const BARCODE_MARGIN = 1.80;
|
||||
@ -92,46 +94,42 @@ class LabelWriter_1933081 extends LabelWriter
|
||||
}
|
||||
|
||||
$fields = $record->get('fields');
|
||||
// Below rescales the size of the field box to fit, it feels like it could/should be abstracted one class above
|
||||
// to be usable on other labels but im unsure of how to implement that, since it uses a lot of private
|
||||
// constants.
|
||||
|
||||
// Figure out how tall the label fields wants to be
|
||||
$fieldCount = count($fields);
|
||||
$perFieldHeight = (self::LABEL_SIZE + self::LABEL_MARGIN)
|
||||
+ (self::FIELD_SIZE + self::FIELD_MARGIN);
|
||||
$usableHeight = $pa->h
|
||||
- self::TAG_SIZE // bottom tag text
|
||||
- self::BARCODE_MARGIN; // gap between fields and 1D
|
||||
|
||||
$baseHeight = $fieldCount * $perFieldHeight;
|
||||
// If it doesn't fit in the available height, scale everything down
|
||||
$scale = 1.0;
|
||||
if ($baseHeight > $usableHeight && $baseHeight > 0) {
|
||||
$scale = $usableHeight / $baseHeight;
|
||||
}
|
||||
|
||||
$labelSize = self::LABEL_SIZE * $scale;
|
||||
$labelMargin = self::LABEL_MARGIN * $scale;
|
||||
$fieldSize = self::FIELD_SIZE * $scale;
|
||||
$fieldMargin = self::FIELD_MARGIN * $scale;
|
||||
$field_layout = Helper::labelFieldLayoutScaling(
|
||||
pdf: $pdf,
|
||||
fields: $fields,
|
||||
currentX: $currentX,
|
||||
usableWidth: $usableWidth,
|
||||
usableHeight: $usableHeight,
|
||||
baseLabelSize: self::LABEL_SIZE,
|
||||
baseFieldSize: self::FIELD_SIZE,
|
||||
baseFieldMargin: self::FIELD_MARGIN,
|
||||
baseLabelPadding: 1.5,
|
||||
baseGap: 1.5,
|
||||
maxScale: 1.8,
|
||||
labelFont: 'freesans',
|
||||
);
|
||||
|
||||
foreach ($fields as $field) {
|
||||
$labelText = rtrim($field['label'], ':') . ':';
|
||||
|
||||
static::writeText(
|
||||
$pdf, $field['label'],
|
||||
$pdf, $labelText,
|
||||
$currentX, $currentY,
|
||||
'freesans', '', $labelSize, 'L',
|
||||
$usableWidth, $labelSize, true, 0
|
||||
'freesans', '', $field_layout['labelSize'], 'L',
|
||||
$field_layout['labelWidth'], $field_layout['rowAdvance'], true, 0
|
||||
);
|
||||
$currentY += $labelSize + $labelMargin;
|
||||
|
||||
static::writeText(
|
||||
$pdf, $field['value'],
|
||||
$currentX, $currentY,
|
||||
'freemono', 'B', $fieldSize, 'L',
|
||||
$usableWidth, $fieldSize, true, 0, 0.01
|
||||
$field_layout['valueX'], $currentY,
|
||||
'freemono', 'B', $field_layout['fieldSize'], 'L',
|
||||
$field_layout['valueWidth'], $field_layout['rowAdvance'], true, 0, 0.01
|
||||
);
|
||||
$currentY += $fieldSize + $fieldMargin;
|
||||
$currentY += $field_layout['rowAdvance'];
|
||||
}
|
||||
|
||||
if ($record->has('barcode1d')) {
|
||||
|
||||
@ -3,6 +3,8 @@
|
||||
namespace App\Models\Labels\Tapes\Dymo;
|
||||
|
||||
|
||||
use App\Helpers\Helper;
|
||||
|
||||
class LabelWriter_2112283 extends LabelWriter
|
||||
{
|
||||
private const BARCODE_MARGIN = 1.80;
|
||||
@ -98,40 +100,42 @@ class LabelWriter_2112283 extends LabelWriter
|
||||
|
||||
// Figure out how tall the label fields wants to be
|
||||
$fieldCount = count($fields);
|
||||
$perFieldHeight = (self::LABEL_SIZE + self::LABEL_MARGIN)
|
||||
+ (self::FIELD_SIZE + self::FIELD_MARGIN);
|
||||
|
||||
$usableHeight = $pa->h
|
||||
- self::TAG_SIZE // bottom tag text
|
||||
- self::BARCODE_MARGIN; // gap between fields and 1D
|
||||
|
||||
$baseHeight = $fieldCount * $perFieldHeight;
|
||||
// If it doesn't fit in the available height, scale everything down
|
||||
$scale = 1.0;
|
||||
if ($baseHeight > $usableHeight && $baseHeight > 0) {
|
||||
$scale = $usableHeight / $baseHeight;
|
||||
}
|
||||
|
||||
$labelSize = self::LABEL_SIZE * $scale;
|
||||
$labelMargin = self::LABEL_MARGIN * $scale;
|
||||
$fieldSize = self::FIELD_SIZE * $scale;
|
||||
$fieldMargin = self::FIELD_MARGIN * $scale;
|
||||
$field_layout = Helper::labelFieldLayoutScaling(
|
||||
pdf: $pdf,
|
||||
fields: $fields,
|
||||
currentX: $currentX,
|
||||
usableWidth: $usableWidth,
|
||||
usableHeight: $usableHeight,
|
||||
baseLabelSize: self::LABEL_SIZE,
|
||||
baseFieldSize: self::FIELD_SIZE,
|
||||
baseFieldMargin: self::FIELD_MARGIN,
|
||||
baseLabelPadding: 1.5,
|
||||
baseGap: 1.5,
|
||||
maxScale: 1.8,
|
||||
labelFont: 'freesans',
|
||||
);
|
||||
|
||||
foreach ($fields as $field) {
|
||||
$labelText = rtrim($field['label'], ':') . ':';
|
||||
|
||||
static::writeText(
|
||||
$pdf, $field['label'],
|
||||
$pdf, $labelText,
|
||||
$currentX, $currentY,
|
||||
'freesans', '', $labelSize, 'L',
|
||||
$usableWidth, $labelSize, true, 0
|
||||
'freesans', '', $field_layout['labelSize'], 'L',
|
||||
$field_layout['labelWidth'], $field_layout['rowAdvance'], true, 0
|
||||
);
|
||||
$currentY += $labelSize + $labelMargin;
|
||||
|
||||
static::writeText(
|
||||
$pdf, $field['value'],
|
||||
$currentX, $currentY,
|
||||
'freemono', 'B', $fieldSize, 'L',
|
||||
$usableWidth, $fieldSize, true, 0, 0.01
|
||||
$field_layout['valueX'], $currentY,
|
||||
'freemono', 'B', $field_layout['fieldSize'], 'L',
|
||||
$field_layout['valueWidth'], $field_layout['rowAdvance'], true, 0, 0.01
|
||||
);
|
||||
$currentY += $fieldSize + $fieldMargin;
|
||||
$currentY += $field_layout['rowAdvance'];
|
||||
}
|
||||
if ($record->has('barcode1d')) {
|
||||
static::write1DBarcode(
|
||||
|
||||
Reference in New Issue
Block a user