Skip to content

Commit b514d75

Browse files
authored
Merge pull request #51855 from Phreeman33/master
fix: display chinese character avatar
2 parents 535bf3a + c67bdf0 commit b514d75

17 files changed

+605
-489
lines changed

.reuse/dep5

Lines changed: 0 additions & 356 deletions
This file was deleted.

REUSE.toml

Lines changed: 526 additions & 0 deletions
Large diffs are not rendered by default.

build/files-checker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
'.npmignore',
2828
'.php-cs-fixer.dist.php',
2929
'.pre-commit-config.yaml',
30-
'.reuse',
3130
'.tag',
3231
'.tx',
3332
'.user.ini',
@@ -42,6 +41,7 @@
4241
'DESIGN.md',
4342
'Makefile',
4443
'README.md',
44+
'REUSE.toml',
4545
'SECURITY.md',
4646
'apps',
4747
'autotest-checkers.sh',

core/fonts/LICENSE_OFL.txt

Lines changed: 0 additions & 92 deletions
This file was deleted.

core/fonts/NotoSansHK-Regular.ttf

6.76 MB
Binary file not shown.

core/fonts/NotoSansJP-Regular.ttf

5.47 MB
Binary file not shown.

core/fonts/NotoSansKR-Regular.ttf

5.91 MB
Binary file not shown.

core/fonts/NotoSansSC-Regular.ttf

10.1 MB
Binary file not shown.

core/fonts/NotoSansTC-Regular.ttf

6.78 MB
Binary file not shown.

lib/private/Avatar/Avatar.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
namespace OC\Avatar;
1111

1212
use Imagick;
13+
use OC\User\User;
1314
use OCP\Color;
1415
use OCP\Files\NotFoundException;
1516
use OCP\IAvatar;
17+
use OCP\IConfig;
1618
use Psr\Log\LoggerInterface;
1719

1820
/**
1921
* This class gets and sets users avatars.
2022
*/
2123
abstract class Avatar implements IAvatar {
22-
protected LoggerInterface $logger;
23-
2424
/**
2525
* https://github.com/sebdesign/cap-height -- for 500px height
2626
* Automated check: https://codepen.io/skjnldsv/pen/PydLBK/
@@ -35,8 +35,10 @@ abstract class Avatar implements IAvatar {
3535
<text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#{fgFill}">{letter}</text>
3636
</svg>';
3737

38-
public function __construct(LoggerInterface $logger) {
39-
$this->logger = $logger;
38+
public function __construct(
39+
protected IConfig $config,
40+
protected LoggerInterface $logger,
41+
) {
4042
}
4143

4244
/**
@@ -84,8 +86,7 @@ public function get(int $size = 64, bool $darkTheme = false) {
8486
* @return string
8587
*
8688
*/
87-
protected function getAvatarVector(int $size, bool $darkTheme): string {
88-
$userDisplayName = $this->getDisplayName();
89+
protected function getAvatarVector(string $userDisplayName, int $size, bool $darkTheme): string {
8990
$fgRGB = $this->avatarBackgroundColor($userDisplayName);
9091
$bgRGB = $fgRGB->alphaBlending(0.1, $darkTheme ? new Color(0, 0, 0) : new Color(255, 255, 255));
9192
$fill = sprintf('%02x%02x%02x', $bgRGB->red(), $bgRGB->green(), $bgRGB->blue());
@@ -95,10 +96,31 @@ protected function getAvatarVector(int $size, bool $darkTheme): string {
9596
return str_replace($toReplace, [$size, $fill, $fgFill, $text], $this->svgTemplate);
9697
}
9798

99+
/**
100+
* Select the rendering font based on the user's display name and language
101+
*/
102+
private function getFont(string $userDisplayName): string {
103+
if (preg_match('/\p{Han}/u', $userDisplayName) === 1) {
104+
switch ($this->getAvatarLanguage()) {
105+
case 'zh_TW':
106+
return __DIR__ . '/../../../core/fonts/NotoSansTC-Regular.ttf';
107+
case 'zh_HK':
108+
return __DIR__ . '/../../../core/fonts/NotoSansHK-Regular.ttf';
109+
case 'ja':
110+
return __DIR__ . '/../../../core/fonts/NotoSansJP-Regular.ttf';
111+
case 'ko':
112+
return __DIR__ . '/../../../core/fonts/NotoSansKR-Regular.ttf';
113+
default:
114+
return __DIR__ . '/../../../core/fonts/NotoSansSC-Regular.ttf';
115+
}
116+
}
117+
return __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
118+
}
119+
98120
/**
99121
* Generate png avatar from svg with Imagick
100122
*/
101-
protected function generateAvatarFromSvg(int $size, bool $darkTheme): ?string {
123+
protected function generateAvatarFromSvg(string $userDisplayName, int $size, bool $darkTheme): ?string {
102124
if (!extension_loaded('imagick')) {
103125
return null;
104126
}
@@ -107,9 +129,10 @@ protected function generateAvatarFromSvg(int $size, bool $darkTheme): ?string {
107129
if (in_array('RSVG', $formats, true)) {
108130
return null;
109131
}
132+
$text = $this->getAvatarText();
110133
try {
111-
$font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
112-
$svg = $this->getAvatarVector($size, $darkTheme);
134+
$font = $this->getFont($text);
135+
$svg = $this->getAvatarVector($userDisplayName, $size, $darkTheme);
113136
$avatar = new Imagick();
114137
$avatar->setFont($font);
115138
$avatar->readImageBlob($svg);
@@ -151,7 +174,7 @@ protected function generateAvatar(string $userDisplayName, int $size, bool $dark
151174
}
152175
imagefilledrectangle($im, 0, 0, $size, $size, $background);
153176

154-
$font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
177+
$font = $this->getFont($text);
155178

156179
$fontSize = $size * 0.4;
157180
[$x, $y] = $this->imageTTFCenter(
@@ -258,4 +281,12 @@ public function avatarBackgroundColor(string $hash): Color {
258281

259282
return $finalPalette[$this->hashToInt($hash, $steps * 3)];
260283
}
284+
285+
/**
286+
* Get the language to be used for avatar generation.
287+
* This is used to determine the font to use for the avatar text (e.g. CJK characters).
288+
*/
289+
protected function getAvatarLanguage(): string {
290+
return $this->config->getSystemValueString('default_language', 'en');
291+
}
261292
}

0 commit comments

Comments
 (0)