Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -1735,4 +1736,61 @@ class_basename(get_class($item)),
}
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'
);
}
}
42 changes: 22 additions & 20 deletions app/Models/Labels/Sheets/Avery/L4736_A.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,38 +98,38 @@ public function write($pdf, $record)
$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);

$baseHeight = $fieldCount * $perFieldHeight;
$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;
$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'];
}

}
Expand Down
49 changes: 22 additions & 27 deletions app/Models/Labels/Sheets/Avery/L6009_A.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,43 +62,36 @@ public function write($pdf, $record)
$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'];
}
}
}
Expand Down
46 changes: 22 additions & 24 deletions app/Models/Labels/Tapes/Brother/TZe_241.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,40 +57,36 @@ public function write($pdf, $record)

$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'];
}
}
}
74 changes: 33 additions & 41 deletions app/Models/Labels/Tapes/Brother/TZe_24mm_E.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,49 +73,39 @@ public function write($pdf, $record) {
}

$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;
}

$labelSize = self::LABEL_SIZE * $scale;
$fieldSize = self::FIELD_SIZE * $scale;
$fieldMargin = self::FIELD_MARGIN * $scale;

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['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;
}
$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', '', $field_layout['labelSize'], 'L',
$field_layout['labelWidth'], $field_layout['rowAdvance'], true, 0
);

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'];
}


if ($record->has('barcode1d')) {
static::write1DBarcode(
$pdf, $record->get('barcode1d')->content, $record->get('barcode1d')->type,
Expand Down
Loading
Loading