Skip to content

Commit 664e398

Browse files
authored
Merge pull request #17877 from grokability/label-cjk-fix
Fixed CJK on labels
2 parents 665c13e + b383cd9 commit 664e398

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

app/Models/Labels/DefaultLabel.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function write($pdf, $record)
229229
static::writeText(
230230
$pdf, $record->get('title'),
231231
$textX1, 0,
232-
'freesans', 'b', $this->textSize, 'L',
232+
Helper::isCjk($record->get('title')) ? 'cid0cs' : 'freesans', 'b', $this->textSize, 'L',
233233
$textW, $this->textSize,
234234
true, 0
235235
);
@@ -246,11 +246,12 @@ public function write($pdf, $record)
246246
static::writeText(
247247
$pdf, (($field['label']) ? $field['label'].' ' : '') . $field['value'],
248248
$textX1, $textY,
249-
'freesans', '', $this->textSize, 'L',
249+
Helper::isCjk($field['label']) ? 'cid0cs' : 'freesans', '', $this->textSize, 'L',
250250
$textW, $this->textSize,
251251
true, 0
252252
);
253253

254+
254255
$textY += $this->textSize + self::TEXT_MARGIN;
255256
$fieldsDone++;
256257
}

app/Models/Labels/Label.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,29 +211,36 @@ public final function getPrintableArea()
211211
*/
212212
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)
213213
{
214+
215+
214216
$prevFamily = $pdf->getFontFamily();
215217
$prevStyle = $pdf->getFontStyle();
216218
$prevSizePt = $pdf->getFontSizePt();
217219

220+
218221
$text = !empty($text) ? $text : '';
219222

220223
$fontFamily = !empty($font) ? $font : $prevFamily;
221224
$fontStyle = !empty($style) ? $style : $prevStyle;
222-
if ($size) { $fontSizePt = Helper::convertUnit($size, $this->getUnit(), 'pt', true);
223-
} else { $fontSizePt = $prevSizePt;
225+
226+
227+
if ($size) {
228+
$fontSizePt = Helper::convertUnit($size, $this->getUnit(), 'pt', true);
229+
} else {
230+
$fontSizePt = $prevSizePt;
224231
}
225232

226233
$pdf->SetFontSpacing($spacing);
227234

228235
$parts = collect(explode('**', $text))
229236
->map(
230-
function ($part, $index) use ($pdf, $fontFamily, $fontStyle, $fontSizePt) {
237+
function ($part, $index) use ($pdf, $fontFamily, $fontStyle, $fontSizePt, $text) {
231238
$modStyle = ($index % 2 == 1) ? 'B' : $fontStyle;
232239
$pdf->setFont($fontFamily, $modStyle, $fontSizePt);
233240
return [
234241
'text' => $part,
235242
'text_width' => $pdf->GetStringWidth($part),
236-
'font_family' => $fontFamily,
243+
'font_family' => Helper::isCjk($text) ? 'cid0cs' : $fontFamily,
237244
'font_style' => $modStyle,
238245
'font_size' => $fontSizePt,
239246
];

app/View/Label.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public function render(callable $callback = null)
7474
[0 => $template->getWidth(), 1 => $template->getHeight(), 'Rotate' => $template->getRotation()]
7575
);
7676

77+
// Required for CJK languages, otherwise the embedded font can get too massive
78+
$pdf->SetFontSubsetting(true);
79+
80+
7781
// Reset parameters
7882
$pdf->SetPrintHeader(false);
7983
$pdf->SetPrintFooter(false);
@@ -176,14 +180,14 @@ public function render(callable $callback = null)
176180
// For fields that have multiple options, we need to combine them
177181
// into a single field so all values are displayed.
178182
->reduce(function ($previous, $current) {
179-
// On the first iteration we simply return the item.
183+
// On the first iteration, we simply return the item.
180184
// If there is only one item to be processed for the row
181185
// then this effectively skips everything below this if block.
182186
if (is_null($previous)) {
183187
return $current;
184188
}
185189

186-
// At this point we are dealing with a row with multiple items being displayed.
190+
// At this point, we are dealing with a row with multiple items being displayed.
187191
// We need to combine the label and value of the current item with the previous item.
188192

189193
// The end result of this will be in this format:

0 commit comments

Comments
 (0)