magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

.lib-font-face mixin - Fixed font formats

Open BenGlassAtlas opened this issue 10 years ago • 12 comments

The .lib-font-face() mixin is not very flexible as it requires you have the font in all the formats listed (eot, woff2, woff, ttf, and svg). If your font is not available in all these formats then you will run into problems with 404 errors.

For example the font I'm using isn't available as a Woff or Woff2 file so I can't use the .lib-font-face() mixin as it leaves me with several 404s:

screen shot 2016-05-23 at 08 09 25

Steps to reproduce

  1. Use a font that doesn't contain a Woff/Woff2 variant
  2. Check the console in your browser:
// Regular Noto Sans
.lib-font-face(
    @family-name: @font-family-name__base,
    @font-path: '@{baseDir}fonts/NotoSans-Regular',
    @font-weight: 400,
    @font-style: normal
);

// Italic Noto Sans
.lib-font-face(
    @family-name: @font-family-name__base,
    @font-path: '@{baseDir}fonts/NotoSans-Italic',
    @font-weight: 400,
    @font-style: italic
);

// Bold Noto Sans
.lib-font-face(
    @family-name: @font-family-name__base,
    @font-path: '@{baseDir}fonts/NotoSans-Bold',
    @font-weight: 700,
    @font-style: normal
);

Expected result

  1. I'd expect to have the option to specify the font formats I'd like to us, default should be all the fonts as it currently is but the option to exclude formats would be good.

Maybe (optionally) setting a variable to false could be the answer?

.lib-font-face(
    @family-name: @font-family-name__base,
    @font-path: '@{baseDir}fonts/NotoSans-Bold',
    @font-weight: 700,
    @font-style: normal,
    @eot: false,
    @woff: false,
    @woff2: false
);

Actual result

  1. The mixin attempts to retrieve all the font formats which leaves 404 errors.

Workaround

To get around this I've had to manually declare my fonts without using the mixin.

// Regular Noto Sans
@font-face {
    font-family: @font-family-name__base;
    src: url('@{baseDir}fonts/NotoSans-Regular.ttf') format('truetype');
    font-weight: 400;
    font-style: normal;
}

// Italic Noto Sans
@font-face {
    font-family: @font-family-name__base;
    src: url('@{baseDir}fonts/NotoSans-Italic.ttf') format('truetype');
    font-weight: 400;
    font-style: italic;
}

// Bold Noto Sans
@font-face {
    font-family: @font-family-name__base;
    src: url('@{baseDir}fonts/NotoSans-Bold.ttf') format('truetype');
    font-weight: 700;
    font-style: normal;
}

And I no longer get the 404s

screen shot 2016-05-23 at 08 23 32

BenGlassAtlas avatar May 23 '16 07:05 BenGlassAtlas

@BenSpace48 thank you! That's a really good point. Internal ticket MAGETWO-54520 created.

undefined-olha avatar Jun 17 '16 15:06 undefined-olha

An alternative workaround is to simply create the WOFF2 files for your font. There are plenty of free tools available which are able to convert TTF and other font formats into WOFF2, which may be a cleaner long-time solution if you are facing this problem now and comes with the upside of slightly decreasing the load times for your users through the reduced file size offered by WOFF2. Just make sure the font you are using permits / does not specifically prohibit this sort of conversion in its license agreement.

TiEul avatar Aug 26 '16 13:08 TiEul

@TiEul Thanks for the suggestion, but unfortunately the fonts are out of our control as they are purchased by a client and the licence does not permit it. Also I don't think it's a good idea that a mixin should be so opinionated, in my eyes it should be as flexible as possible.

BenGlassAtlas avatar Aug 30 '16 09:08 BenGlassAtlas

Oh, don't get me wrong, I fully agree that the mixin should not be that limited, it was more a suggestion for other people finding this issue, as I actually initially got here via Google since I was facing the same problem.

TiEul avatar Aug 30 '16 09:08 TiEul

@BenSpace48, thank you for your report. We've created internal ticket(s) MAGETWO-54520 to track progress on the issue.

magento-engcom-team avatar Nov 22 '17 12:11 magento-engcom-team

#mageconf

volodymyr-lisniak avatar Dec 15 '17 09:12 volodymyr-lisniak

@bobmotor thank you for joining. Please accept team invitation here and self-assign the issue.

magento-engcom-team avatar Dec 15 '17 09:12 magento-engcom-team

Please assign me this #MM18IN

shrikantvaghela avatar Jan 07 '18 06:01 shrikantvaghela

@shrikantvaghela thank you for joining. Please accept team invitation here and self-assign the issue.

magento-engcom-team avatar Jan 07 '18 06:01 magento-engcom-team

Hi @BenSpace48, I have looked into this issue and come up with a solution that if we have only one type of file. We can create a different mixin only for that file type. Code:

.lib-ttf-font-face(
    @family-name,
    @font-path,
    @font-weight: normal,
    @font-style: normal
) {
    @font-face{
        font-family: @family-name;
        src: url('@{font-path}.ttf') format('truetype');
        font-weight: @font-weight;
        font-style: @font-style;
    }
}

.lib-ttf-font-face(
  @family-name: @font-family-name__base,
  @font-path: '@{baseDir}@{font-path}@{font-family__font-name}/light/opensans-300',
  @font-weight: 300,
  @font-style: normal
); 

Result:

@font-face {
  font-family: 'Open Sans';
  src: url('../fonts/open-sans/light/opensans-300.ttf') format('truetype');
  font-weight: 300;
  font-style: normal;
}

Please suggest if this solution will be work for you. Also I am digging for some perfect solution. Thanks ShrikantV

shrikantvaghela avatar Jan 07 '18 10:01 shrikantvaghela

That would also work, thanks @shrikantvaghela

BenGlassAtlas avatar Jan 08 '18 11:01 BenGlassAtlas

Hi @Karlasa. Thank you for working on this issue. Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • [ ] 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.


m2-assistant[bot] avatar May 12 '19 18:05 m2-assistant[bot]