1010namespace OC \Avatar ;
1111
1212use Imagick ;
13+ use OC \User \User ;
1314use OCP \Color ;
1415use OCP \Files \NotFoundException ;
1516use OCP \IAvatar ;
17+ use OCP \IConfig ;
1618use Psr \Log \LoggerInterface ;
1719
1820/**
1921 * This class gets and sets users avatars.
2022 */
2123abstract 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