Merge pull request #51855 from Phreeman33/master

fix: display chinese character avatar
pull/54280/head
John Molakvoæ 2 months ago committed by GitHub
commit b514d75323
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 356
      .reuse/dep5
  2. 526
      REUSE.toml
  3. 2
      build/files-checker.php
  4. 92
      core/fonts/LICENSE_OFL.txt
  5. BIN
      core/fonts/NotoSansHK-Regular.ttf
  6. BIN
      core/fonts/NotoSansJP-Regular.ttf
  7. BIN
      core/fonts/NotoSansKR-Regular.ttf
  8. BIN
      core/fonts/NotoSansSC-Regular.ttf
  9. BIN
      core/fonts/NotoSansTC-Regular.ttf
  10. 51
      lib/private/Avatar/Avatar.php
  11. 6
      lib/private/Avatar/AvatarManager.php
  12. 4
      lib/private/Avatar/GuestAvatar.php
  13. 9
      lib/private/Avatar/PlaceholderAvatar.php
  14. 21
      lib/private/Avatar/UserAvatar.php
  15. 2
      tests/lib/Avatar/AvatarManagerTest.php
  16. 5
      tests/lib/Avatar/GuestAvatarTest.php
  17. 20
      tests/lib/Avatar/UserAvatarTest.php

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -27,7 +27,6 @@ $expectedFiles = [
'.npmignore',
'.php-cs-fixer.dist.php',
'.pre-commit-config.yaml',
'.reuse',
'.tag',
'.tx',
'.user.ini',
@ -42,6 +41,7 @@ $expectedFiles = [
'DESIGN.md',
'Makefile',
'README.md',
'REUSE.toml',
'SECURITY.md',
'apps',
'autotest-checkers.sh',

@ -1,92 +0,0 @@
This Font Software is licensed under the SIL Open Font License,
Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font
creation efforts of academic and linguistic communities, and to
provide a free and open framework in which fonts may be shared and
improved in partnership with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply to
any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software
components as distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to,
deleting, or substituting -- in part or in whole -- any of the
components of the Original Version, by changing formats or by porting
the Font Software to a new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed,
modify, redistribute, and sell modified and unmodified copies of the
Font Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components, in
Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the
corresponding Copyright Holder. This restriction only applies to the
primary font name as presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created using
the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -10,17 +10,17 @@ declare(strict_types=1);
namespace OC\Avatar;
use Imagick;
use OC\User\User;
use OCP\Color;
use OCP\Files\NotFoundException;
use OCP\IAvatar;
use OCP\IConfig;
use Psr\Log\LoggerInterface;
/**
* This class gets and sets users avatars.
*/
abstract class Avatar implements IAvatar {
protected LoggerInterface $logger;
/**
* https://github.com/sebdesign/cap-height -- for 500px height
* Automated check: https://codepen.io/skjnldsv/pen/PydLBK/
@ -35,8 +35,10 @@ abstract class Avatar implements IAvatar {
<text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#{fgFill}">{letter}</text>
</svg>';
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
public function __construct(
protected IConfig $config,
protected LoggerInterface $logger,
) {
}
/**
@ -84,8 +86,7 @@ abstract class Avatar implements IAvatar {
* @return string
*
*/
protected function getAvatarVector(int $size, bool $darkTheme): string {
$userDisplayName = $this->getDisplayName();
protected function getAvatarVector(string $userDisplayName, int $size, bool $darkTheme): string {
$fgRGB = $this->avatarBackgroundColor($userDisplayName);
$bgRGB = $fgRGB->alphaBlending(0.1, $darkTheme ? new Color(0, 0, 0) : new Color(255, 255, 255));
$fill = sprintf('%02x%02x%02x', $bgRGB->red(), $bgRGB->green(), $bgRGB->blue());
@ -95,10 +96,31 @@ abstract class Avatar implements IAvatar {
return str_replace($toReplace, [$size, $fill, $fgFill, $text], $this->svgTemplate);
}
/**
* Select the rendering font based on the user's display name and language
*/
private function getFont(string $userDisplayName): string {
if (preg_match('/\p{Han}/u', $userDisplayName) === 1) {
switch ($this->getAvatarLanguage()) {
case 'zh_TW':
return __DIR__ . '/../../../core/fonts/NotoSansTC-Regular.ttf';
case 'zh_HK':
return __DIR__ . '/../../../core/fonts/NotoSansHK-Regular.ttf';
case 'ja':
return __DIR__ . '/../../../core/fonts/NotoSansJP-Regular.ttf';
case 'ko':
return __DIR__ . '/../../../core/fonts/NotoSansKR-Regular.ttf';
default:
return __DIR__ . '/../../../core/fonts/NotoSansSC-Regular.ttf';
}
}
return __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
}
/**
* Generate png avatar from svg with Imagick
*/
protected function generateAvatarFromSvg(int $size, bool $darkTheme): ?string {
protected function generateAvatarFromSvg(string $userDisplayName, int $size, bool $darkTheme): ?string {
if (!extension_loaded('imagick')) {
return null;
}
@ -107,9 +129,10 @@ abstract class Avatar implements IAvatar {
if (in_array('RSVG', $formats, true)) {
return null;
}
$text = $this->getAvatarText();
try {
$font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
$svg = $this->getAvatarVector($size, $darkTheme);
$font = $this->getFont($text);
$svg = $this->getAvatarVector($userDisplayName, $size, $darkTheme);
$avatar = new Imagick();
$avatar->setFont($font);
$avatar->readImageBlob($svg);
@ -151,7 +174,7 @@ abstract class Avatar implements IAvatar {
}
imagefilledrectangle($im, 0, 0, $size, $size, $background);
$font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
$font = $this->getFont($text);
$fontSize = $size * 0.4;
[$x, $y] = $this->imageTTFCenter(
@ -258,4 +281,12 @@ abstract class Avatar implements IAvatar {
return $finalPalette[$this->hashToInt($hash, $steps * 3)];
}
/**
* Get the language to be used for avatar generation.
* This is used to determine the font to use for the avatar text (e.g. CJK characters).
*/
protected function getAvatarLanguage(): string {
return $this->config->getSystemValueString('default_language', 'en');
}
}

@ -92,10 +92,10 @@ class AvatarManager implements IAvatarManager {
return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
default:
// use a placeholder avatar which caches the generated images
return new PlaceholderAvatar($folder, $user, $this->logger);
return new PlaceholderAvatar($folder, $user, $this->config, $this->logger);
}
return new PlaceholderAvatar($folder, $user, $this->logger);
return new PlaceholderAvatar($folder, $user, $this->config, $this->logger);
}
/**
@ -129,6 +129,6 @@ class AvatarManager implements IAvatarManager {
* @param string $name The guest name, e.g. "Albert".
*/
public function getGuestAvatar(string $name): IAvatar {
return new GuestAvatar($name, $this->logger);
return new GuestAvatar($name, $this->config, $this->logger);
}
}

@ -10,6 +10,7 @@ namespace OC\Avatar;
use OCP\Files\SimpleFS\InMemoryFile;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
use Psr\Log\LoggerInterface;
/**
@ -23,9 +24,10 @@ class GuestAvatar extends Avatar {
*/
public function __construct(
private string $userDisplayName,
IConfig $config,
LoggerInterface $logger,
) {
parent::__construct($logger);
parent::__construct($config, $logger);
}
/**

@ -14,6 +14,7 @@ use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
use OCP\IImage;
use Psr\Log\LoggerInterface;
@ -27,9 +28,10 @@ class PlaceholderAvatar extends Avatar {
public function __construct(
private ISimpleFolder $folder,
private User $user,
IConfig $config,
LoggerInterface $logger,
) {
parent::__construct($logger);
parent::__construct($config, $logger);
}
/**
@ -87,8 +89,9 @@ class PlaceholderAvatar extends Avatar {
throw new NotFoundException;
}
if (!$data = $this->generateAvatarFromSvg($size, $darkTheme)) {
$data = $this->generateAvatar($this->getDisplayName(), $size, $darkTheme);
$userDisplayName = $this->getDisplayName();
if (!$data = $this->generateAvatarFromSvg($userDisplayName, $size, $darkTheme)) {
$data = $this->generateAvatar($userDisplayName, $size, $darkTheme);
}
try {

@ -26,11 +26,11 @@ class UserAvatar extends Avatar {
public function __construct(
private ISimpleFolder $folder,
private IL10N $l,
private User $user,
protected User $user,
LoggerInterface $logger,
private IConfig $config,
IConfig $config,
) {
parent::__construct($logger);
parent::__construct($config, $logger);
}
/**
@ -201,8 +201,9 @@ class UserAvatar extends Avatar {
try {
$ext = $this->getExtension($generated, $darkTheme);
} catch (NotFoundException $e) {
if (!$data = $this->generateAvatarFromSvg(1024, $darkTheme)) {
$data = $this->generateAvatar($this->getDisplayName(), 1024, $darkTheme);
$userDisplayName = $this->getDisplayName();
if (!$data = $this->generateAvatarFromSvg($userDisplayName, 1024, $darkTheme)) {
$data = $this->generateAvatar($userDisplayName, 1024, $darkTheme);
}
$avatar = $this->folder->newFile($darkTheme ? 'avatar-dark.png' : 'avatar.png');
$avatar->putContent($data);
@ -234,8 +235,9 @@ class UserAvatar extends Avatar {
throw new NotFoundException;
}
if ($generated) {
if (!$data = $this->generateAvatarFromSvg($size, $darkTheme)) {
$data = $this->generateAvatar($this->getDisplayName(), $size, $darkTheme);
$userDisplayName = $this->getDisplayName();
if (!$data = $this->generateAvatarFromSvg($userDisplayName, $size, $darkTheme)) {
$data = $this->generateAvatar($userDisplayName, $size, $darkTheme);
}
} else {
$avatar = new \OCP\Image();
@ -293,4 +295,9 @@ class UserAvatar extends Avatar {
public function isCustomAvatar(): bool {
return $this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', 'false') !== 'true';
}
#[\Override]
protected function getAvatarLanguage(): string {
return $this->config->getUserValue($this->user->getUID(), 'core', 'lang', parent::getAvatarLanguage());
}
}

@ -269,7 +269,7 @@ class AvatarManagerTest extends \Test\TestCase {
}
if ($expectedPlaceholder) {
$expected = new PlaceholderAvatar($folder, $user, $this->createMock(LoggerInterface::class));
$expected = new PlaceholderAvatar($folder, $user, $this->config, $this->logger);
} else {
$expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
}

@ -34,8 +34,9 @@ class GuestAvatarTest extends TestCase {
*/
public function setupGuestAvatar() {
/* @var MockObject|LoggerInterface $logger */
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$this->guestAvatar = new GuestAvatar('einstein', $logger);
$logger = $this->createMock(LoggerInterface::class);
$config = $this->createMock(\OCP\IConfig::class);
$this->guestAvatar = new GuestAvatar('einstein', $config, $logger);
}
/**

@ -18,21 +18,16 @@ use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
use OCP\IL10N;
use OCP\Image;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
class UserAvatarTest extends \Test\TestCase {
/** @var SimpleFolder | \PHPUnit\Framework\MockObject\MockObject */
private $folder;
/** @var \OC\Avatar\UserAvatar */
private $avatar;
/** @var User|\PHPUnit\Framework\MockObject\MockObject $user */
private $user;
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $config;
private UserAvatar $avatar;
private SimpleFolder&MockObject $folder;
private IConfig&MockObject $config;
private User&MockObject $user;
protected function setUp(): void {
parent::setUp();
@ -236,7 +231,7 @@ class UserAvatarTest extends \Test\TestCase {
}
public function testGenerateSvgAvatar(): void {
$avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [64, false]);
$avatar = $this->invokePrivate($this->avatar, 'getAvatarVector', [$this->user->getDisplayName(), 64, false]);
$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="64" height="64" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
@ -246,7 +241,6 @@ class UserAvatarTest extends \Test\TestCase {
$this->assertEquals($avatar, $svg);
}
#[\PHPUnit\Framework\Attributes\DataProvider('avatarTextData')]
public function testGetAvatarText($displayName, $expectedAvatarText): void {
$user = $this->getUserWithDisplayName($displayName);

Loading…
Cancel
Save