From b383cd9493447fead14077c9516419dc1f3ba88d Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 16 Sep 2025 12:10:01 +0100 Subject: [PATCH] Fixed CJK on labels --- app/Models/Labels/DefaultLabel.php | 5 +++-- app/Models/Labels/Label.php | 15 +++++++++++---- app/View/Label.php | 8 ++++++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/Models/Labels/DefaultLabel.php b/app/Models/Labels/DefaultLabel.php index fa60dafab3..0ac2eb14da 100644 --- a/app/Models/Labels/DefaultLabel.php +++ b/app/Models/Labels/DefaultLabel.php @@ -229,7 +229,7 @@ class DefaultLabel extends RectangleSheet static::writeText( $pdf, $record->get('title'), $textX1, 0, - 'freesans', 'b', $this->textSize, 'L', + Helper::isCjk($record->get('title')) ? 'cid0cs' : 'freesans', 'b', $this->textSize, 'L', $textW, $this->textSize, true, 0 ); @@ -246,11 +246,12 @@ class DefaultLabel extends RectangleSheet static::writeText( $pdf, (($field['label']) ? $field['label'].' ' : '') . $field['value'], $textX1, $textY, - 'freesans', '', $this->textSize, 'L', + Helper::isCjk($field['label']) ? 'cid0cs' : 'freesans', '', $this->textSize, 'L', $textW, $this->textSize, true, 0 ); + $textY += $this->textSize + self::TEXT_MARGIN; $fieldsDone++; } diff --git a/app/Models/Labels/Label.php b/app/Models/Labels/Label.php index cff859f359..2c463e0921 100644 --- a/app/Models/Labels/Label.php +++ b/app/Models/Labels/Label.php @@ -211,29 +211,36 @@ abstract class Label */ public final function writeText(TCPDF $pdf, $text, $x, $y, $font=null, $style=null, $size=null, $align='L', $width=null, $height=null, $squash=false, $border=0, $spacing=0) { + + $prevFamily = $pdf->getFontFamily(); $prevStyle = $pdf->getFontStyle(); $prevSizePt = $pdf->getFontSizePt(); + $text = !empty($text) ? $text : ''; $fontFamily = !empty($font) ? $font : $prevFamily; $fontStyle = !empty($style) ? $style : $prevStyle; - if ($size) { $fontSizePt = Helper::convertUnit($size, $this->getUnit(), 'pt', true); - } else { $fontSizePt = $prevSizePt; + + + if ($size) { + $fontSizePt = Helper::convertUnit($size, $this->getUnit(), 'pt', true); + } else { + $fontSizePt = $prevSizePt; } $pdf->SetFontSpacing($spacing); $parts = collect(explode('**', $text)) ->map( - function ($part, $index) use ($pdf, $fontFamily, $fontStyle, $fontSizePt) { + function ($part, $index) use ($pdf, $fontFamily, $fontStyle, $fontSizePt, $text) { $modStyle = ($index % 2 == 1) ? 'B' : $fontStyle; $pdf->setFont($fontFamily, $modStyle, $fontSizePt); return [ 'text' => $part, 'text_width' => $pdf->GetStringWidth($part), - 'font_family' => $fontFamily, + 'font_family' => Helper::isCjk($text) ? 'cid0cs' : $fontFamily, 'font_style' => $modStyle, 'font_size' => $fontSizePt, ]; diff --git a/app/View/Label.php b/app/View/Label.php index ee9a1ae2fd..96f88e0f5f 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -74,6 +74,10 @@ class Label implements View [0 => $template->getWidth(), 1 => $template->getHeight(), 'Rotate' => $template->getRotation()] ); + // Required for CJK languages, otherwise the embedded font can get too massive + $pdf->SetFontSubsetting(true); + + // Reset parameters $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); @@ -176,14 +180,14 @@ class Label implements View // For fields that have multiple options, we need to combine them // into a single field so all values are displayed. ->reduce(function ($previous, $current) { - // On the first iteration we simply return the item. + // On the first iteration, we simply return the item. // If there is only one item to be processed for the row // then this effectively skips everything below this if block. if (is_null($previous)) { return $current; } - // At this point we are dealing with a row with multiple items being displayed. + // At this point, we are dealing with a row with multiple items being displayed. // We need to combine the label and value of the current item with the previous item. // The end result of this will be in this format: