Skip to content

Commit

Permalink
Amendments
Browse files Browse the repository at this point in the history
  • Loading branch information
Finesse committed Mar 1, 2020
1 parent 90eb962 commit 6c3fda6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 25 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2017-2019 Surgie Finesse
Copyright (c) 2017-2020 Surgie Finesse

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ You may omit the styles list. In this case the regular style (`400`) is used.
<link rel="stylesheet" href="http://web-fonts-repository.local/css?family=Open+Sans" />
```

You can specify a value for the [font-display](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display)
style property using `display` parameter. Example:

```html
<link rel="stylesheet" href="http://web-fonts-repository.local/css?family=Open+Sans&display=swap" />
```

Then embed a font in a CSS code:

```css
Expand All @@ -153,13 +160,6 @@ body {
}
```

You can specify a value for the font-display property. To do this, add the display parameter to the request and set its value.
The value can be one of: auto, block, swap, fallback, optional.

```html
<link rel="stylesheet" href="http://web-fonts-repository.local/css?family=Open+Sans:400,400i,700&display=swap" />
```

## Versions compatibility

The project follows the [Semantic Versioning](http://semver.org).
Expand Down
12 changes: 5 additions & 7 deletions src/Controllers/CSSGeneratorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
return $this->createErrorResponse('The `family` query parameter is not set');
}


try {
$requestedFonts = $this->parseFamilyParameter($requestParams['family']);
$fontDisplay = isset($requestParams['display']) ? $this->checkFontDisplay($requestParams['display']) : '';
$fontDisplay = $this->parseDisplayParameter($requestParams['display'] ?? null);
} catch (\InvalidArgumentException $error) {
return $this->createErrorResponse($error->getMessage());
}
Expand All @@ -62,7 +61,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
$this->container->get('logger')->error($error);
return $this->createErrorResponse('The app settings are invalid: '.$error->getMessage(), 500);
}

try {
$cssCode = $webfontCSSGenerator->makeCSS($requestedFonts, $fontDisplay);
} catch (\InvalidArgumentException $error) {
Expand Down Expand Up @@ -119,16 +117,16 @@ protected function parseFamilyParameter(string $value): array
}

/**
* Check font-display value. Value must be one of: auto, block, swap, fallback, optional.
* Checks `display` request query value.
*
* @param $fontDisplay Font-display value or null
* @param mixed $fontDisplay Parameter value
* @return string Valid font-display css value, or empty string, if $fontDisplay is null or empty.
* @throws \InvalidArgumentException If the parameter set, but has not valid value. The message may be sent
* back to the client.
*/
protected function checkFontDisplay($fontDisplay): string
protected function parseDisplayParameter($fontDisplay): string
{
if (!isset($fontDisplay) || $fontDisplay === '') {
if ($fontDisplay === null) {
return '';
}
if (!is_string($fontDisplay)) {
Expand Down
9 changes: 2 additions & 7 deletions src/Services/WebfontCSSGenerator/WebfontCSSGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function createFromSettings(array $settings, string $rootURL = '')
* 'Roboto' => ['100', '100i', '400', '400i']
* ]
* </pre>
* @param string|null $fontDisplay Font-display css property
* @param string $fontDisplay Font-display css property
* @return string
* @throws \InvalidArgumentException
*/
Expand Down Expand Up @@ -201,18 +201,13 @@ protected function makeFontStyleCSS(string $familyName, string $styleName, strin
$sources[] = "url(".CSSHelpers::formatString($files['svg'].'#webfontregular').") format('svg')";
}

$fontDisplayCSS = '';
if (isset($fontDisplay) && $fontDisplay !== '') {
$fontDisplayCSS = "\tfont-display: " . $fontDisplay . ";\n";
}

return "@font-face {\n"
. "\tfont-family: ".CSSHelpers::formatString($family->name).";\n"
. "\tfont-weight: $style->weight;\n"
. "\tfont-style: ".($style->isItalic ? 'italic' : 'normal').";\n"
. ($fontDisplay !== '' ? "\tfont-display: $fontDisplay;\n" : '')
. (isset($files['eot']) ? "\tsrc: url(".CSSHelpers::formatString($files['eot']).");\n" : '')
. "\tsrc: ".implode(', ', $sources).";\n"
. $fontDisplayCSS
. "}";
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/WebfontCSSGenerator/WebfontCSSGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,22 @@ public function testMakeCSS()
'Roboto' => ['400', '400', '900'] // Regular style two times
]));


$this->assertCSSEquals("
@font-face {
font-family: 'Open Sans';
font-weight: 400;
font-style: italic;
font-display: swap;
src: url('/generator/fonts/OpenSans/opensans_italic.eot');
src: local('Open Sans Italic'), local('OpenSans-Italic'), url('/generator/fonts/OpenSans/opensans_italic.eot?#iefix') format('embedded-opentype'), url('/generator/fonts/OpenSans/opensans_italic.woff2') format('woff2'), url('/generator/fonts/OpenSans/opensans_italic.woff') format('woff'), url('/generator/fonts/OpenSans/opensans_italic.ttf') format('truetype'), url('/generator/fonts/OpenSans/opensans_italic.otf') format('opentype'), url('/generator/fonts/OpenSans/opensans_italic.svg#webfontregular') format('svg');
font-display: swap;
}
@font-face {
font-family: 'Open Sans';
font-weight: 500;
font-style: normal;
font-display: swap;
src: url('/generator/fonts/OpenSans/opensans_demi.eot');
src: local('Open Sans DemiBold'), local('OpenSans-DemiBold'), url('/generator/fonts/OpenSans/opensans_demi.eot?#iefix') format('embedded-opentype'), url('/generator/fonts/OpenSans/opensans_demi.woff2') format('woff2'), url('/generator/fonts/OpenSans/opensans_demi.woff') format('woff'), url('/generator/fonts/OpenSans/opensans_demi.ttf') format('truetype'), url('/generator/fonts/OpenSans/opensans_demi.otf') format('opentype'), url('/generator/fonts/OpenSans/opensans_demi.svg#webfontregular') format('svg');
font-display: swap;
}
", $generator->makeCSS([
'Open Sans' => ['400i', '500']
Expand Down

0 comments on commit 6c3fda6

Please sign in to comment.