3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-02-04 07:45:29 +00:00

add Helper function for label layout, applied to 1933081 2112283"

This commit is contained in:
Godfrey M
2026-01-13 11:50:14 -08:00
parent f5955e14ff
commit b0b1829426
3 changed files with 109 additions and 49 deletions

View File

@ -13,6 +13,7 @@ use App\Models\Setting;
use App\Models\Statuslabel; use App\Models\Statuslabel;
use App\Models\License; use App\Models\License;
use App\Models\Location; use App\Models\Location;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Crypt;
use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\DecryptException;
@ -1735,4 +1736,61 @@ class Helper
} }
return $mismatched; 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'
);
}
} }

View File

@ -3,6 +3,8 @@
namespace App\Models\Labels\Tapes\Dymo; namespace App\Models\Labels\Tapes\Dymo;
use App\Helpers\Helper;
class LabelWriter_1933081 extends LabelWriter class LabelWriter_1933081 extends LabelWriter
{ {
private const BARCODE_MARGIN = 1.80; private const BARCODE_MARGIN = 1.80;
@ -92,46 +94,42 @@ class LabelWriter_1933081 extends LabelWriter
} }
$fields = $record->get('fields'); $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 $usableHeight = $pa->h
- self::TAG_SIZE // bottom tag text - self::TAG_SIZE // bottom tag text
- self::BARCODE_MARGIN; // gap between fields and 1D - self::BARCODE_MARGIN; // gap between fields and 1D
$baseHeight = $fieldCount * $perFieldHeight; $field_layout = Helper::labelFieldLayoutScaling(
// If it doesn't fit in the available height, scale everything down pdf: $pdf,
$scale = 1.0; fields: $fields,
if ($baseHeight > $usableHeight && $baseHeight > 0) { currentX: $currentX,
$scale = $usableHeight / $baseHeight; usableWidth: $usableWidth,
} usableHeight: $usableHeight,
baseLabelSize: self::LABEL_SIZE,
$labelSize = self::LABEL_SIZE * $scale; baseFieldSize: self::FIELD_SIZE,
$labelMargin = self::LABEL_MARGIN * $scale; baseFieldMargin: self::FIELD_MARGIN,
$fieldSize = self::FIELD_SIZE * $scale; baseLabelPadding: 1.5,
$fieldMargin = self::FIELD_MARGIN * $scale; baseGap: 1.5,
maxScale: 1.8,
labelFont: 'freesans',
);
foreach ($fields as $field) { foreach ($fields as $field) {
$labelText = rtrim($field['label'], ':') . ':';
static::writeText( static::writeText(
$pdf, $field['label'], $pdf, $labelText,
$currentX, $currentY, $currentX, $currentY,
'freesans', '', $labelSize, 'L', 'freesans', '', $field_layout['labelSize'], 'L',
$usableWidth, $labelSize, true, 0 $field_layout['labelWidth'], $field_layout['rowAdvance'], true, 0
); );
$currentY += $labelSize + $labelMargin;
static::writeText( static::writeText(
$pdf, $field['value'], $pdf, $field['value'],
$currentX, $currentY, $field_layout['valueX'], $currentY,
'freemono', 'B', $fieldSize, 'L', 'freemono', 'B', $field_layout['fieldSize'], 'L',
$usableWidth, $fieldSize, true, 0, 0.01 $field_layout['valueWidth'], $field_layout['rowAdvance'], true, 0, 0.01
); );
$currentY += $fieldSize + $fieldMargin; $currentY += $field_layout['rowAdvance'];
} }
if ($record->has('barcode1d')) { if ($record->has('barcode1d')) {

View File

@ -3,6 +3,8 @@
namespace App\Models\Labels\Tapes\Dymo; namespace App\Models\Labels\Tapes\Dymo;
use App\Helpers\Helper;
class LabelWriter_2112283 extends LabelWriter class LabelWriter_2112283 extends LabelWriter
{ {
private const BARCODE_MARGIN = 1.80; 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 // Figure out how tall the label fields wants to be
$fieldCount = count($fields); $fieldCount = count($fields);
$perFieldHeight = (self::LABEL_SIZE + self::LABEL_MARGIN)
+ (self::FIELD_SIZE + self::FIELD_MARGIN);
$usableHeight = $pa->h $usableHeight = $pa->h
- self::TAG_SIZE // bottom tag text - self::TAG_SIZE // bottom tag text
- self::BARCODE_MARGIN; // gap between fields and 1D - self::BARCODE_MARGIN; // gap between fields and 1D
$field_layout = Helper::labelFieldLayoutScaling(
$baseHeight = $fieldCount * $perFieldHeight; pdf: $pdf,
// If it doesn't fit in the available height, scale everything down fields: $fields,
$scale = 1.0; currentX: $currentX,
if ($baseHeight > $usableHeight && $baseHeight > 0) { usableWidth: $usableWidth,
$scale = $usableHeight / $baseHeight; usableHeight: $usableHeight,
} baseLabelSize: self::LABEL_SIZE,
baseFieldSize: self::FIELD_SIZE,
$labelSize = self::LABEL_SIZE * $scale; baseFieldMargin: self::FIELD_MARGIN,
$labelMargin = self::LABEL_MARGIN * $scale; baseLabelPadding: 1.5,
$fieldSize = self::FIELD_SIZE * $scale; baseGap: 1.5,
$fieldMargin = self::FIELD_MARGIN * $scale; maxScale: 1.8,
labelFont: 'freesans',
);
foreach ($fields as $field) { foreach ($fields as $field) {
$labelText = rtrim($field['label'], ':') . ':';
static::writeText( static::writeText(
$pdf, $field['label'], $pdf, $labelText,
$currentX, $currentY, $currentX, $currentY,
'freesans', '', $labelSize, 'L', 'freesans', '', $field_layout['labelSize'], 'L',
$usableWidth, $labelSize, true, 0 $field_layout['labelWidth'], $field_layout['rowAdvance'], true, 0
); );
$currentY += $labelSize + $labelMargin;
static::writeText( static::writeText(
$pdf, $field['value'], $pdf, $field['value'],
$currentX, $currentY, $field_layout['valueX'], $currentY,
'freemono', 'B', $fieldSize, 'L', 'freemono', 'B', $field_layout['fieldSize'], 'L',
$usableWidth, $fieldSize, true, 0, 0.01 $field_layout['valueWidth'], $field_layout['rowAdvance'], true, 0, 0.01
); );
$currentY += $fieldSize + $fieldMargin; $currentY += $field_layout['rowAdvance'];
} }
if ($record->has('barcode1d')) { if ($record->has('barcode1d')) {
static::write1DBarcode( static::write1DBarcode(