|
| 1 | +# `font-palette` and `@font-palette-values` |
| 2 | + |
| 3 | +## Authors |
| 4 | + |
| 5 | +* Dominik Röttsches, [@drott](drott@chromium.org) |
| 6 | + |
| 7 | +## Introduction ## |
| 8 | + |
| 9 | +The `font-palette` CSS property allows selecting a palette |
| 10 | +from a vector color font. |
| 11 | +In combination with the `@font-palette-values` at-rule, |
| 12 | +custom palettes can be defined. |
| 13 | +`font-palette` can increase efficiency of color font uses, |
| 14 | +as no server roundtrip is needed |
| 15 | +for changing the colors of the font. |
| 16 | +Use cases are listed below. |
| 17 | + |
| 18 | +## Technical background |
| 19 | + |
| 20 | +Color fonts in formats such as COLRv0 and COLRv1 |
| 21 | +allow control over their palette |
| 22 | +by means of the [`CPAL` OpenType table](https://docs.microsoft.com/en-us/typography/opentype/spec/cpal), |
| 23 | +from which the COLR paint tables reference colors. |
| 24 | + |
| 25 | +CPAL specifies a set of palettes, |
| 26 | +each with the same number of specified entries. |
| 27 | +Each entry is an RGB color value in 8-bit BGRA in sRGB color space. |
| 28 | +Individual palettes can be tagged to indicate |
| 29 | +that they are usable for a light or dark background respectively. |
| 30 | + |
| 31 | +A COLR/CPAL color font can be displayed in a different set of colors |
| 32 | +by selecting a palette from the ones specified in the font, |
| 33 | +or by providing a custom palette to the rasterization stage, |
| 34 | +for example by starting from a defined palette and overriding |
| 35 | +individual palette entries. |
| 36 | + |
| 37 | +[OT-SVG has a mechanism](https://docs.microsoft.com/en-us/typography/opentype/otspec181/svg#color-palettes) |
| 38 | +to populate `CPAL` color table color entries |
| 39 | +into CSS custom variables |
| 40 | +in the form of `--color0`, `--color1` etc.. |
| 41 | +This allows OT-SVG fonts to reference CPAL entries as well, |
| 42 | +in addition to existing ways of specifying color in SVG. |
| 43 | + |
| 44 | +# Use cases # |
| 45 | + |
| 46 | +1. I want to select a color scheme of my color font that works well with light mode or dark mode. |
| 47 | + |
| 48 | +2. I want to select one of the font designer provided color schemes that come with my font. |
| 49 | + |
| 50 | +3. I want to adapt a color font to my page design and override colors so that colors in page and font harmonize. |
| 51 | + |
| 52 | +# Non-Goals # |
| 53 | + |
| 54 | +`font-palette` is not intended to work with bitmap color fonts. |
| 55 | + |
| 56 | +# Proposed Syntax # |
| 57 | + |
| 58 | +See |
| 59 | +[9.1. Controlling Color Font Palettes: The font-palette property](https://www.w3.org/TR/css-fonts-4/#font-palette-prop) |
| 60 | + |
| 61 | +# Examples # |
| 62 | + |
| 63 | +## Use Case 1: Select light or dark-mode suitable palette ## |
| 64 | + |
| 65 | + |
| 66 | +```CSS |
| 67 | +@font-face { |
| 68 | + font-family: MyVectorColorFont; |
| 69 | + src: url(MyColorFont.woff2) tech(color-COLRv0); |
| 70 | +} |
| 71 | + |
| 72 | +@media (prefers-color-scheme: dark) { |
| 73 | + .font-colors: { font-palette: light; } |
| 74 | +} |
| 75 | + |
| 76 | +@media (prefers-color-scheme: light) { |
| 77 | + .font-colors: { font-palette: dark; } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Use Case 2: Select available palette ## |
| 82 | + |
| 83 | +```CSS |
| 84 | +@font-face { |
| 85 | + font-family: MyVectorColorFont; |
| 86 | + src: url(MyColorFont.woff2) tech(color-COLRv0); |
| 87 | +} |
| 88 | + |
| 89 | +@font-palette-values --SelectedPalette { |
| 90 | + font-family: MyVectorColorFont; |
| 91 | + base-palette: 3; |
| 92 | +} |
| 93 | + |
| 94 | +.palette-selection { |
| 95 | + font-palette: --SelectedPalette; |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Use Case 3: Custom colors ## |
| 100 | + |
| 101 | +```CSS |
| 102 | +@font-face { |
| 103 | + font-family: MyVectorColorFont; |
| 104 | + src: url(MyColorFont.woff2) tech(color-COLRv0); |
| 105 | +} |
| 106 | + |
| 107 | +@font-palette-values --OverridenColors { |
| 108 | + font-family: MyVectorColorFont; |
| 109 | + base-palette: 0; |
| 110 | + override-colors: 0 rgb(127, 63, 49), |
| 111 | + 1 rgb(70, 60, 50), |
| 112 | + 2 rgba(0, 30, 30, 30); |
| 113 | +} |
| 114 | + |
| 115 | +.palette-selection { |
| 116 | + font-palette: --OverridenColors; |
| 117 | +} |
| 118 | +``` |
0 commit comments