⛰️ 일산에 사는 개발자들

Web Font 알아보기

dorage
dorage

참조

Optimize WebFont loading and rendering

Avoid invisible text during font loading

Fundamental text and font styling - Learn web development | MDN

글꼴에서 세리프(Serif)와 산세리프(Sans-Serif)의 차이점이 무엇일까?

NAVER D2

Controlling Font Performance with font-display - Chrome Developers

Web Safe Fonts

대부분의 환경에서 지원을 하는 공통적인 폰트들로 해당 시스템이 이러한 폰트를 갖고 있을지 하는 걱정 없이 사용할 수 있는 폰트들이다.

SerifSans-Serif
SerifSans-Serif
NameGeneric typeNotes
Arialsans-serifIt’s often considered best practice to also add Helvetica as a preferred alternative to Arial as, although their font faces are almost identical, Helvetica is considered to have a nicer shape, even if Arial is more broadly available.
Courier NewmonospaceSome OSes have an alternative (possibly older) version of the Courier New font called Courier. It’s considered best practice to use both with Courier New as the preferred alternative.
Georgiaserif
Times New RomanserifSome OSes have an alternative (possibly older) version of the Times New Roman font called Times. It’s considered best practice to use both with Times New Roman as the preferred alternative.
Trebuchet MSsans-serifYou should be careful with using this font — it isn’t widely available on mobile OSes.
Verdanasans-serif

다음 사이트에서 웹 세이프 폰트들의 목록을 확인할 수 있다.

CSS Font Stack: Web Safe and Web Font Family with HTML and CSS code.

Generic Type

css는 5개의 형태로 폰트를 구분한다.

TermDefinition
serifFonts that have serifs (the flourishes and other small details you see at the ends of the strokes in some typefaces).
sans-serifFonts that don’t have serifs.
monospaceFonts where every character has the same width, typically used in code listings.
cursiveFonts that are intended to emulate handwriting, with flowing, connected strokes.
fantasyFonts that are intended to be decorative.

serif와 sans-serif, monospace의 경우 예상 가능하고 의미있는 정보를 제공해야 한다. cursive와 fantasy 타입의 경우 예측 가능성이 낮으므로 조심스럽게 사용하는 것을 권장한다.

Font Size

폰트 사이즈 스타일링에 있어 3가지의 유닛을 사용한다.

<!-- document base font-size is 16px -->
<article>
    <!-- If my font-size is 1.5em -->
    <p>My paragraph</p>
    <!-- ?? -->
</article>

위와 같이 em을 사용한 경우 article은 24px의 폰트 사이즈를 갖게 된다.

만약 여기서 p의 폰트 사이즈를 20px로 맞추고 싶다면, p의 폰트 사이즈는 0.83333… em 을 설정해주어야 한다. 이처럼 em으로 사이징을 할 경우 어려움이 있을 수 있다. 따라서 rem을 사용해주는 것이 좋다.

Rendering Process

Untitled

font는 언제 렌더링 되는가?

  1. HTTP 요청을 통해 html을 받아온다.
  2. HTML 파싱 과정을 거쳐 DOM을 구성한다
  3. HTML 파싱 중 만난 CSS 리소스를 요청하고 CSS를 받아온다.
  4. CSS 파싱 과정을 거쳐 CSSOM을 구성한다
  5. DOM과 CSSOM을 결합하여 렌더트리를 구성한다.
  6. 레이아웃과 페인트를 진행한다.

폰트의 요청은 렌더트리를 구성하는 5번 시점에서 비동기적으로 진행된다.

그렇기에 동시에 브라우저는 레이아웃을 수행하고 페인트 과정을 거쳐 화면에 요소들을 그리는데 글꼴의 응답이 완료되지 않아 아직 사용할 수 없는 경우 브라우저에서는 텍스트 픽셀을 렌더링 하지 못할 수 있다.

preload 어트리뷰트를 통해 리소스의 우선순위를 지정하거나 font-display CSS 를 이용해 해결할 수 있다.

font-display - CSS: Cascading Style Sheets | MDN

Preload WebFont Resoures

만약 구체적인 WebFont 가 호스팅 된 URL을 알고 있다면,

<link rel="”preload”" />

어트리뷰트를 통해 리소스 우선순위를 줄 수 있고, 이는 CSSOM이 구성되기 이전에 웹 폰트를 요청하게 만든다.

FOIT & FOUT

브라우저에 따라 아직 로드되지 않은 글꼴을 처리하는 방법이 2가지가 있다.

글꼴이 로드가 되고 변경점이 적용될 때 글자가 번쩍- 하고 바뀐다 해서 Flash Of Something Text라고 한다.

Differences In Font Rendering

BrowserTimeoutFallbackSwap
Chrome 35+3 secondsYesYes
Opera3 secondsYesYes
Firefox3 secondsYesYes
Internet Explorer0 secondsYesYes
SafariNo timeoutN/AN/A

Customize The Text Rendering Delay

font-display는 폰트 다운로드의 생명 주기를 3개의 주요한 구간으로 나눈다.

이 구간들을 이해하는 것은 font-display를 통해 내가 어떻게 글꼴의 렌더링을 조정할 수 있을지 알게 한다. 또한, font-display를 이용하려면 @font-face를 사용해야 한다.

@font-face {
  font-family: 'Awesome Font';
  font-style: normal;
  font-weight: 400;
  **font-display: auto; /* or block, swap, fallback, optional */**
  src: local('Awesome Font'),
       url('/fonts/awesome-l.woff2') format('woff2'), /* will be preloaded */
       url('/fonts/awesome-l.woff') format('woff'),
       url('/fonts/awesome-l.ttf') format('truetype'),
       url('/fonts/awesome-l.eot') format('embedded-opentype');
  unicode-range: U+000-5FF; /* Latin glyphs */
}

CSS Font-Display

아래는 font-display의 속성 별 동작 방식이다.

Font Loading API

와 font-display를 동시에 사용하는 것은 오버헤드 없이 글꼴 로딩과 렌더링을 조정 하는데 있어서 아주 좋은 선택이다.

하지만 조금 더 커스텀하거나 이를 위해 기꺼이 오버헤드를 감수하겠다면, Font Loading API를 사용할 수 있다.

Font Loading API는 CSS font faces 조작, 다운로드 상태 추적, 기본 lazyload 행위의 오버라이드 등을 할 수 있다.

CSS Font Loading API - Web APIs | MDN

Chaching Fonts

글꼴 리소스는 기본적으로 거의 업데이트가 일어나지 않는 정적인 리소스다. 따라서 서버가 지정할 수 있는 최대의 max-age를 주어도 상관이 없다.

약 웹 서비스가 service worker를 사용한다면, cache-first startegy으로 폰트를 전달하는 것이 대부분 사용 사례에서 적절하다.

하지만 localStorage나 IndexedDB를 통해 글꼴을 저장해서는 안된다. 이는 성능 상의 문제점이 있다. 브라우저의 HTTP 캐시가 글꼴 리소스를 전달하는데 최고의 메커니즘을 제공한다.