Skip to content

Commit 16bf72a

Browse files
kesselbAndyScherzinger
authored andcommitted
feat: generate avatars for display name with chinese characters
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent 02dc1bc commit 16bf72a

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

core/fonts/NotoSansSC-Bold.ttf

10.1 MB
Binary file not shown.

core/fonts/NotoSansSC-Regular.ttf

10.1 MB
Binary file not shown.

lib/private/Avatar/Avatar.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,7 @@ public function get(int $size = 64, bool $darkTheme = false) {
100100
return $avatar;
101101
}
102102

103-
/**
104-
* {size} = 500
105-
* {fill} = hex color to fill
106-
* {letter} = Letter to display
107-
*
108-
* Generate SVG avatar
109-
*
110-
* @param int $size The requested image size in pixel
111-
* @return string
112-
*
113-
*/
114-
protected function getAvatarVector(int $size, bool $darkTheme): string {
115-
$userDisplayName = $this->getDisplayName();
103+
protected function getAvatarVector(string $userDisplayName, int $size, bool $darkTheme): string {
116104
$fgRGB = $this->avatarBackgroundColor($userDisplayName);
117105
$bgRGB = $fgRGB->alphaBlending(0.1, $darkTheme ? new Color(0, 0, 0) : new Color(255, 255, 255));
118106
$fill = sprintf("%02x%02x%02x", $bgRGB->red(), $bgRGB->green(), $bgRGB->blue());
@@ -122,6 +110,14 @@ protected function getAvatarVector(int $size, bool $darkTheme): string {
122110
return str_replace($toReplace, [$size, $fill, $fgFill, $text], $this->svgTemplate);
123111
}
124112

113+
protected function getFont(string $userDisplayName) {
114+
if (preg_match('/\p{Han}/u', $userDisplayName) === 1) {
115+
return __DIR__ . '/../../../core/fonts/NotoSansSC-Regular.ttf';
116+
}
117+
118+
return __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
119+
}
120+
125121
/**
126122
* Generate png avatar from svg with Imagick
127123
*/
@@ -134,9 +130,12 @@ protected function generateAvatarFromSvg(int $size, bool $darkTheme): ?string {
134130
if (in_array("RSVG", $formats, true)) {
135131
return null;
136132
}
133+
134+
$userDisplayName = $this->getDisplayName();
135+
$font = $this->getFont($userDisplayName);
136+
137137
try {
138-
$font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
139-
$svg = $this->getAvatarVector($size, $darkTheme);
138+
$svg = $this->getAvatarVector($userDisplayName, $size, $darkTheme);
140139
$avatar = new Imagick();
141140
$avatar->setFont($font);
142141
$avatar->readImageBlob($svg);
@@ -178,7 +177,8 @@ protected function generateAvatar(string $userDisplayName, int $size, bool $dark
178177
}
179178
imagefilledrectangle($im, 0, 0, $size, $size, $background);
180179

181-
$font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
180+
181+
$font = $this->getFont($userDisplayName);
182182

183183
$fontSize = $size * 0.4;
184184
[$x, $y] = $this->imageTTFCenter(

0 commit comments

Comments
 (0)