From b0b1829426c19c724c0734a975d3abb54c60f743 Mon Sep 17 00:00:00 2001 From: Godfrey M Date: Tue, 13 Jan 2026 11:50:14 -0800 Subject: [PATCH] add Helper function for label layout, applied to 1933081 2112283" --- app/Helpers/Helper.php | 58 +++++++++++++++++++ .../Labels/Tapes/Dymo/LabelWriter_1933081.php | 52 ++++++++--------- .../Labels/Tapes/Dymo/LabelWriter_2112283.php | 48 ++++++++------- 3 files changed, 109 insertions(+), 49 deletions(-) diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index bf59aa2fa8..99c2cee965 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -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' + ); + } } diff --git a/app/Models/Labels/Tapes/Dymo/LabelWriter_1933081.php b/app/Models/Labels/Tapes/Dymo/LabelWriter_1933081.php index 420a058dc4..bba3c992dc 100644 --- a/app/Models/Labels/Tapes/Dymo/LabelWriter_1933081.php +++ b/app/Models/Labels/Tapes/Dymo/LabelWriter_1933081.php @@ -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')) { diff --git a/app/Models/Labels/Tapes/Dymo/LabelWriter_2112283.php b/app/Models/Labels/Tapes/Dymo/LabelWriter_2112283.php index 54d002998e..3fc91912e2 100644 --- a/app/Models/Labels/Tapes/Dymo/LabelWriter_2112283.php +++ b/app/Models/Labels/Tapes/Dymo/LabelWriter_2112283.php @@ -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(