diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index bf59aa2fa8..aee6d3c52b 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; + $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/Sheets/Avery/L4736_A.php b/app/Models/Labels/Sheets/Avery/L4736_A.php index de9c14e737..052edb585f 100644 --- a/app/Models/Labels/Sheets/Avery/L4736_A.php +++ b/app/Models/Labels/Sheets/Avery/L4736_A.php @@ -3,6 +3,8 @@ namespace App\Models\Labels\Sheets\Avery; +use App\Helpers\Helper; + class L4736_A extends L4736 { private const BARCODE_MARGIN = 1.80; @@ -96,38 +98,38 @@ class L4736_A extends L4736 $usableWidth -= $barcodeSize + self::BARCODE_MARGIN; } $fields = $record->get('fields'); - $fieldCount = count($fields); - $perFieldHeight = (self::LABEL_SIZE + self::LABEL_MARGIN) - + (self::FIELD_SIZE + self::FIELD_MARGIN); + $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', + ); - $baseHeight = $fieldCount * $perFieldHeight; - $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; foreach ($fields as $field) { static::writeText( $pdf, $field['label'], $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']; } } diff --git a/app/Models/Labels/Sheets/Avery/L6009_A.php b/app/Models/Labels/Sheets/Avery/L6009_A.php index 8c0cdc9ee0..abe902b824 100644 --- a/app/Models/Labels/Sheets/Avery/L6009_A.php +++ b/app/Models/Labels/Sheets/Avery/L6009_A.php @@ -2,6 +2,8 @@ namespace App\Models\Labels\Sheets\Avery; +use App\Helpers\Helper; + class L6009_A extends L6009 { private const BARCODE_MARGIN = 1.80; @@ -60,43 +62,36 @@ class L6009_A extends L6009 $usableWidth -= $barcodeSize + self::BARCODE_MARGIN; } $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); - - $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) { static::writeText( $pdf, $field['label'], $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']; } } } diff --git a/app/Models/Labels/Tapes/Brother/TZe_241.php b/app/Models/Labels/Tapes/Brother/TZe_241.php index a5a93137ca..97e0fbe542 100644 --- a/app/Models/Labels/Tapes/Brother/TZe_241.php +++ b/app/Models/Labels/Tapes/Brother/TZe_241.php @@ -2,6 +2,8 @@ namespace App\Models\Labels\Tapes\Brother; +use App\Helpers\Helper; + class TZe_241 extends TZe_18mm { private const LABEL_SIZE = 5.0; @@ -55,40 +57,36 @@ class TZe_241 extends TZe_18mm $fields = $record->get('fields') ?? []; - $fieldCount = count($fields); - - $perFieldHeight = (self::LABEL_SIZE + self::LABEL_MARGIN) - + (self::FIELD_SIZE + self::FIELD_MARGIN); - - $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) { static::writeText( $pdf, $field['label'], $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']; } } } \ No newline at end of file diff --git a/app/Models/Labels/Tapes/Brother/TZe_24mm_E.php b/app/Models/Labels/Tapes/Brother/TZe_24mm_E.php index 6035f7640e..dbb2897ed0 100644 --- a/app/Models/Labels/Tapes/Brother/TZe_24mm_E.php +++ b/app/Models/Labels/Tapes/Brother/TZe_24mm_E.php @@ -2,6 +2,8 @@ namespace App\Models\Labels\Tapes\Brother; +use App\Helpers\Helper; + class TZe_24mm_E extends TZe_24mm { private const BARCODE_MARGIN = 1.75; @@ -71,49 +73,39 @@ class TZe_24mm_E extends TZe_24mm } $fields = $record->get('fields'); - // 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); - $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; - } + $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', + ); - $labelSize = self::LABEL_SIZE * $scale; - $fieldSize = self::FIELD_SIZE * $scale; - $fieldMargin = self::FIELD_MARGIN * $scale; + foreach ($fields as $field) { + static::writeText( + $pdf, $field['label'], + $currentX, $currentY, + 'freesans', '', $field_layout['labelSize'], 'L', + $field_layout['labelWidth'], $field_layout['rowAdvance'], true, 0 + ); - foreach ($fields as $field) { - // Write label and value on the same line - // Calculate label width with proportional character spacing - $labelWidth = $pdf->GetStringWidth($field['label'], 'freesans', '', $labelSize); - $charCount = strlen($field['label']); - $spacingPerChar = 0.5; - $totalSpacing = $charCount * $spacingPerChar; - $adjustedWidth = $labelWidth + $totalSpacing; + static::writeText( + $pdf, $field['value'], + $field_layout['valueX'], $currentY, + 'freemono', 'B', $field_layout['fieldSize'], 'L', + $field_layout['valueWidth'], $field_layout['rowAdvance'], true, 0, 0.01 + ); + $currentY += $field_layout['rowAdvance']; + } - static::writeText( - $pdf, $field['label'], - $currentX, $currentY, - 'freesans', 'B', $labelSize, 'L', - $adjustedWidth, $labelSize, true, 0, $spacingPerChar - ); - - static::writeText( - $pdf, $field['value'], - $currentX + $adjustedWidth + 2, $currentY, - 'freesans', 'B', $fieldSize, 'L', - $usableWidth - $adjustedWidth - 2, $fieldSize, true, 0, 0.3 - ); - - $currentY += max($labelSize, $fieldSize) +$fieldMargin; - } - - if ($record->has('barcode1d')) { static::write1DBarcode( $pdf, $record->get('barcode1d')->content, $record->get('barcode1d')->type, diff --git a/app/Models/Labels/Tapes/Dymo/LabelWriter_11354.php b/app/Models/Labels/Tapes/Dymo/LabelWriter_11354.php index 60c311e2b7..f913acac3a 100644 --- a/app/Models/Labels/Tapes/Dymo/LabelWriter_11354.php +++ b/app/Models/Labels/Tapes/Dymo/LabelWriter_11354.php @@ -3,6 +3,8 @@ namespace App\Models\Labels\Tapes\Dymo; +use App\Helpers\Helper; + class LabelWriter_11354 extends LabelWriter { private const BARCODE1D_HEIGHT = 3.00; @@ -105,46 +107,37 @@ class LabelWriter_11354 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 - - self::BARCODE_MARGIN; - - $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) { static::writeText( $pdf, $field['label'], $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']; } } 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( diff --git a/resources/views/models/index.blade.php b/resources/views/models/index.blade.php index bc41dbe9aa..1d1d506b63 100755 --- a/resources/views/models/index.blade.php +++ b/resources/views/models/index.blade.php @@ -42,7 +42,6 @@ "ignoreColumn": ["actions","image","change","checkbox","checkincheckout","icon"] }'> -