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
28 changes: 25 additions & 3 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1757,18 +1757,26 @@ static public function labelFieldLayoutScaling(
float $baseLabelSize,
float $baseFieldSize,
float $baseFieldMargin,
?string $title = null,
float $baseTitleSize = 0.0,
float $baseTitleMargin = 0.0,
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;
$baseFieldsHeight = $fieldCount * $perFieldHeight;

$hasTitle = is_string($title) && trim($title) !== '';
$baseTitleHeight = $hasTitle ? ($baseTitleSize + $baseTitleMargin) : 0.0;
$baseTotalHeight = $baseTitleHeight + $baseFieldsHeight;
$scale = 1.0;
if ($baseHeight > 0 && $usableHeight > 0) {
$scale = $usableHeight / $baseHeight;
if ($baseTotalHeight > 0 && $usableHeight > 0) {
$scale = $usableHeight / $baseTotalHeight;
}

$scale = min($scale, $maxScale);
Expand All @@ -1778,10 +1786,20 @@ static public function labelFieldLayoutScaling(
$fieldMargin = $baseFieldMargin * $scale;

$rowAdvance = max($labelSize, $fieldSize) + $fieldMargin;
$titleSize = $hasTitle ? ($baseTitleSize * $scale) : 0.0;
$titleMargin = $hasTitle ? ($baseTitleMargin * $scale) : 0.0;
$titleAdvance = $hasTitle ? ($titleSize + $titleMargin) : 0.0;

$pdf->SetFont($labelFont, '', $baseLabelSize);

$maxLabelWidthPerUnit = 0;
foreach ($fields as $field) {
$rawLabel = $field['label'] ?? null;

// If no label, do not include it in label-column sizing
if (!is_string($rawLabel) || trim($rawLabel) === '') {
continue;
}
$label = rtrim($field['label'], ':') . ':';
$width = $pdf->GetStringWidth($label);
$maxLabelWidthPerUnit = max($maxLabelWidthPerUnit, $width / $baseLabelSize);
Expand All @@ -1796,6 +1814,10 @@ static public function labelFieldLayoutScaling(

return compact(
'scale',
'hasTitle',
'titleSize',
'titleMargin',
'titleAdvance',
'labelSize',
'fieldSize',
'fieldMargin',
Expand Down
56 changes: 44 additions & 12 deletions app/Models/Labels/Tapes/Dymo/LabelWriter_11354.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,19 @@ public function write($pdf, $record)
}

// Right column
if ($record->has('title')) {
static::writeText(
$pdf, $record->get('title'),
$currentX, $currentY,
'freesans', 'b', self::TITLE_SIZE, 'L',
$usableWidth, self::TITLE_SIZE, true, 0
);
$currentY += self::TITLE_SIZE + self::TITLE_MARGIN;
$title = $record->has('title') ? $record->get('title') : null;
$fields = $record->get('fields');
$maxFields = $this->getSupportFields();
$fields = collect($fields);
if ($title) {
$maxFields = max(0, $maxFields - 1); // title consumes one row’s worth of space
}

$fields = $record->get('fields');
$fields = $fields->take($maxFields)->values();

$usableHeight = $pa->h
- self::TAG_SIZE // bottom tag text
- self::BARCODE_MARGIN; // gap between fields and 1D

$field_layout = Helper::labelFieldLayoutScaling(
pdf: $pdf,
Expand All @@ -117,18 +119,48 @@ public function write($pdf, $record)
baseLabelSize: self::LABEL_SIZE,
baseFieldSize: self::FIELD_SIZE,
baseFieldMargin: self::FIELD_MARGIN,
title: $title,
baseTitleSize: self::TITLE_SIZE,
baseTitleMargin: self::TITLE_MARGIN,
baseLabelPadding: 1.5,
baseGap: 1.5,
maxScale: 1.8,
labelFont: 'freesans',
);

if ($field_layout['hasTitle']) {
static::writeText(
$pdf, $title,
$currentX, $currentY,
'freesans', 'b', $field_layout['titleSize'], 'L',
$usableWidth, $field_layout['titleSize'], true, 0
);
$currentY += $field_layout['titleAdvance'];
}
foreach ($fields as $field) {
$rawLabel = $field['label'] ?? null;
$value = (string)($field['value'] ?? '');

// No label: value takes the whole row
if (!is_string($rawLabel) || trim($rawLabel) === '') {
static::writeText(
$pdf, $value,
$currentX, $currentY,
'freemono', 'B', $field_layout['fieldSize'], 'L',
$usableWidth, $field_layout['rowAdvance'], true, 0, 0.01
);

$currentY += $field_layout['rowAdvance'];
continue;
}

$labelText = rtrim($field['label'], ':') . ':';

static::writeText(
$pdf, $field['label'],
$pdf, $labelText,
$currentX, $currentY,
'freesans', '', $field_layout['labelSize'], 'L',
$field_layout['labelWidth'], $field_layout['rowAdvance'], true, 0
$field_layout['labelWidth'], $field_layout['rowAdvance'], true,
);

static::writeText(
Expand All @@ -137,7 +169,7 @@ public function write($pdf, $record)
'freemono', 'B', $field_layout['fieldSize'], 'L',
$field_layout['valueWidth'], $field_layout['rowAdvance'], true, 0, 0.01
);
$currentY += $field_layout['rowAdvance'];
$currentY += $field_layout['rowAdvance'];;
}
}

Expand Down
51 changes: 39 additions & 12 deletions app/Models/Labels/Tapes/Dymo/LabelWriter_1933081.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,18 @@ public function write($pdf, $record)
);
$currentX += $barcodeSize + self::BARCODE_MARGIN;
$usableWidth -= $barcodeSize + self::BARCODE_MARGIN;
}

if ($record->has('title')) {
static::writeText(
$pdf, $record->get('title'),
$currentX, $currentY,
'freesans', 'b', self::TITLE_SIZE, 'L',
$usableWidth, self::TITLE_SIZE, true, 0
);
$currentY += self::TITLE_SIZE + self::TITLE_MARGIN;
}

$title = $record->has('title') ? $record->get('title') : null;
$fields = $record->get('fields');
$maxFields = $this->getSupportFields();
$fields = collect($fields);
if ($title) {
$maxFields = max(0, $maxFields - 1); // title consumes one row’s worth of space
}

$fields = $fields->take($maxFields)->values();

$usableHeight = $pa->h
- self::TAG_SIZE // bottom tag text
- self::BARCODE_MARGIN; // gap between fields and 1D
Expand All @@ -107,20 +106,48 @@ public function write($pdf, $record)
baseLabelSize: self::LABEL_SIZE,
baseFieldSize: self::FIELD_SIZE,
baseFieldMargin: self::FIELD_MARGIN,
title: $title,
baseTitleSize: self::TITLE_SIZE,
baseTitleMargin: self::TITLE_MARGIN,
baseLabelPadding: 1.5,
baseGap: 1.5,
maxScale: 1.8,
labelFont: 'freesans',
);

if ($field_layout['hasTitle']) {
static::writeText(
$pdf, $title,
$currentX, $currentY,
'freesans', 'b', $field_layout['titleSize'], 'L',
$usableWidth, $field_layout['titleSize'], true, 0
);
$currentY += $field_layout['titleAdvance'];
}
foreach ($fields as $field) {
$rawLabel = $field['label'] ?? null;
$value = (string)($field['value'] ?? '');

// No label: value takes the whole row
if (!is_string($rawLabel) || trim($rawLabel) === '') {
static::writeText(
$pdf, $value,
$currentX, $currentY,
'freemono', 'B', $field_layout['fieldSize'], 'L',
$usableWidth, $field_layout['rowAdvance'], true, 0, 0.01
);

$currentY += $field_layout['rowAdvance'];
continue;
}

$labelText = rtrim($field['label'], ':') . ':';

static::writeText(
$pdf, $labelText,
$currentX, $currentY,
'freesans', '', $field_layout['labelSize'], 'L',
$field_layout['labelWidth'], $field_layout['rowAdvance'], true, 0
$field_layout['labelWidth'], $field_layout['rowAdvance'], true,
);

static::writeText(
Expand All @@ -129,7 +156,7 @@ public function write($pdf, $record)
'freemono', 'B', $field_layout['fieldSize'], 'L',
$field_layout['valueWidth'], $field_layout['rowAdvance'], true, 0, 0.01
);
$currentY += $field_layout['rowAdvance'];
$currentY += $field_layout['rowAdvance'];;
}

if ($record->has('barcode1d')) {
Expand Down
55 changes: 38 additions & 17 deletions app/Models/Labels/Tapes/Dymo/LabelWriter_2112283.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,20 @@ public function write($pdf, $record)
$usableWidth -= $barcodeSize + self::BARCODE_MARGIN;
}

if ($record->has('title')) {
static::writeText(
$pdf, $record->get('title'),
$currentX, $currentY,
'freesans', 'b', self::TITLE_SIZE, 'L',
$usableWidth, self::TITLE_SIZE, true, 0
);
$currentY += self::TITLE_SIZE + self::TITLE_MARGIN;
}

$title = $record->has('title') ? $record->get('title') : null;
$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.
$maxFields = $this->getSupportFields();
$fields = collect($fields);
if ($title) {
$maxFields = max(0, $maxFields - 1); // title consumes one row’s worth of space
}

// Figure out how tall the label fields wants to be
$fieldCount = count($fields);
$fields = $fields->take($maxFields)->values();

$usableHeight = $pa->h
- self::TAG_SIZE // bottom tag text
- self::BARCODE_MARGIN; // gap between fields and 1D

$field_layout = Helper::labelFieldLayoutScaling(
pdf: $pdf,
fields: $fields,
Expand All @@ -113,20 +106,48 @@ public function write($pdf, $record)
baseLabelSize: self::LABEL_SIZE,
baseFieldSize: self::FIELD_SIZE,
baseFieldMargin: self::FIELD_MARGIN,
title: $title,
baseTitleSize: self::TITLE_SIZE,
baseTitleMargin: self::TITLE_MARGIN,
baseLabelPadding: 1.5,
baseGap: 1.5,
maxScale: 1.8,
labelFont: 'freesans',
);

if ($field_layout['hasTitle']) {
static::writeText(
$pdf, $title,
$currentX, $currentY,
'freesans', 'b', $field_layout['titleSize'], 'L',
$usableWidth, $field_layout['titleSize'], true, 0
);
$currentY += $field_layout['titleAdvance'];
}
foreach ($fields as $field) {
$rawLabel = $field['label'] ?? null;
$value = (string)($field['value'] ?? '');

// No label: value takes the whole row
if (!is_string($rawLabel) || trim($rawLabel) === '') {
static::writeText(
$pdf, $value,
$currentX, $currentY,
'freemono', 'B', $field_layout['fieldSize'], 'L',
$usableWidth, $field_layout['rowAdvance'], true, 0, 0.01
);

$currentY += $field_layout['rowAdvance'];
continue;
}

$labelText = rtrim($field['label'], ':') . ':';

static::writeText(
$pdf, $labelText,
$currentX, $currentY,
'freesans', '', $field_layout['labelSize'], 'L',
$field_layout['labelWidth'], $field_layout['rowAdvance'], true, 0
$field_layout['labelWidth'], $field_layout['rowAdvance'], true,
);

static::writeText(
Expand All @@ -135,7 +156,7 @@ public function write($pdf, $record)
'freemono', 'B', $field_layout['fieldSize'], 'L',
$field_layout['valueWidth'], $field_layout['rowAdvance'], true, 0, 0.01
);
$currentY += $field_layout['rowAdvance'];
$currentY += $field_layout['rowAdvance'];;
}
if ($record->has('barcode1d')) {
static::write1DBarcode(
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/en-US/admin/settings/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@
'label2_template' => 'Template',
'label2_template_help' => 'Select which template to use for label generation',
'label2_title' => 'Title',
'label2_title_help' => 'The title to show on labels that support it',
'label2_title_help' => 'The title to show on labels that support it. <br>This will occupy the first Label Field row.',
'label2_title_help_phold' => 'The placeholder <code>{COMPANY}</code> will be replaced with the asset&apos;s company name',
'label2_asset_logo' => 'Use Asset Logo',
'label2_asset_logo_help' => 'Use the logo of the asset&apos;s assigned company, rather than the value at <code>:setting_name</code>',
Expand Down
Loading