View on GitHub


CSS property-list manipulation library

[Raku CSS Project] / [CSS-Properties] / CSS::Font

class CSS::Font

Abstract CSS font object


use CSS::Font;
my $font-props = 'italic bold 10pt/12pt times-roman';
my CSS::Font $font .= new: :$font-props;
say $font.em;                  # 10
say $font.ex;                  # 7.5
say $;               # italic
say $font.weight;              # 700
say $;              # times-roman
say $font.line-height;         # 12
say $font.units;               # pt
say $font.measure(:font-size); # 10
say $font.fontconfig-pattern;
# times-roman:slant=italic:weight=bold

# requires fontconfig to be installed
say $font.find-font;
# /usr/share/fonts/truetype/liberation/LiberationSerif-BoldItalic.ttf


CSS::Font is utility class for managing font related properties and computing fontconfig patterns.

method fontconfig-pattern

method fontconfig-pattern(
    @faces =
) returns Mu

compute a fontconfig pattern for the font

method font-props

method font-props() returns Mu

sets/gets the css font properties as a whole

e.g. $font.font-css = 'italic bold 10pt/12pt sans-serif';

method find-font

method find-font(
    Str $patt =
) returns Str

Return a path to a matching system font

Actually calls fc-match on $.font-config-patterm()

method match

method match(
) returns Array

Select matching @font-face font

This method matches a list of @font-face properties against the font to select matches, using the Font Matching Algorithm. Example:

use CSS::Font;
use CSS::Stylesheet;
my CSS::Font $font .= new: :font-style("italic bold 10pt/12pt Georgia,serif");
my $stylehseet = q:to<END>;
    @font-face {
      font-family:'Sans-serif'; src:url('/myfonts/sans-serif.otf');
    @font-face {
      font-family:'Serif'; src:url('/myfonts/serif.otf');
my CSS::Stylesheet $css .= load: :$stylesheet;
say $font.match($css.font-face).first; # font-family:'serif'; src:url('/myfonts/serif.otf');