github.com/utopiagio/gio@v0.0.8/font/font.go (about)

     1  /*
     2  Package font provides type describing font faces attributes.
     3  */
     4  package font
     5  
     6  import (
     7  	"github.com/go-text/typesetting/font"
     8  )
     9  
    10  // A FontFace is a Font and a matching Face.
    11  type FontFace struct {
    12  	Font Font
    13  	Face Face
    14  }
    15  
    16  // Style is the font style.
    17  type Style int
    18  
    19  // Weight is a font weight, in CSS units subtracted 400 so the zero value
    20  // is normal text weight.
    21  type Weight int
    22  
    23  // Font specify a particular typeface variant, style and weight.
    24  type Font struct {
    25  	// Typeface specifies the name(s) of the the font faces to try. See [Typeface]
    26  	// for details.
    27  	Typeface Typeface
    28  	// Style specifies the kind of text style.
    29  	Style Style
    30  	// Weight is the text weight.
    31  	Weight Weight
    32  }
    33  
    34  // Face is an opaque handle to a typeface. The concrete implementation depends
    35  // upon the kind of font and shaper in use.
    36  type Face interface {
    37  	Face() font.Face
    38  }
    39  
    40  // Typeface identifies a list of font families to attempt to use for displaying
    41  // a string. The syntax is a comma-delimited list of family names. In order to
    42  // allow for the remote possibility of needing to express a font family name
    43  // containing a comma, name entries may be quoted using either single or double
    44  // quotes. Within quotes, a literal quotation mark can be expressed by escaping
    45  // it with `\`. A literal backslash may be expressed by escaping it with another
    46  // `\`.
    47  //
    48  // Here's an example Typeface:
    49  //
    50  //	Times New Roman, Georgia, serif
    51  //
    52  // This is equivalent to the above:
    53  //
    54  //	"Times New Roman", 'Georgia', serif
    55  //
    56  // Here are some valid uses of escape sequences:
    57  //
    58  //	"Contains a literal \" doublequote", 'Literal \' Singlequote', "\\ Literal backslash", '\\ another'
    59  //
    60  // This syntax has the happy side effect that most CSS "font-family" rules are
    61  // valid Typefaces (without the trailing semicolon).
    62  //
    63  // Generic CSS font families are supported, and are automatically expanded to lists
    64  // of known font families with a matching style. The supported generic families are:
    65  //
    66  //   - fantasy
    67  //   - math
    68  //   - emoji
    69  //   - serif
    70  //   - sans-serif
    71  //   - cursive
    72  //   - monospace
    73  type Typeface string
    74  
    75  const (
    76  	Regular Style = iota
    77  	Italic
    78  )
    79  
    80  const (
    81  	Thin       Weight = -300
    82  	ExtraLight Weight = -200
    83  	Light      Weight = -100
    84  	Normal     Weight = 0
    85  	Medium     Weight = 100
    86  	SemiBold   Weight = 200
    87  	Bold       Weight = 300
    88  	ExtraBold  Weight = 400
    89  	Black      Weight = 500
    90  )
    91  
    92  func (s Style) String() string {
    93  	switch s {
    94  	case Regular:
    95  		return "Regular"
    96  	case Italic:
    97  		return "Italic"
    98  	default:
    99  		panic("invalid Style")
   100  	}
   101  }
   102  
   103  func (w Weight) String() string {
   104  	switch w {
   105  	case Thin:
   106  		return "Thin"
   107  	case ExtraLight:
   108  		return "ExtraLight"
   109  	case Light:
   110  		return "Light"
   111  	case Normal:
   112  		return "Normal"
   113  	case Medium:
   114  		return "Medium"
   115  	case SemiBold:
   116  		return "SemiBold"
   117  	case Bold:
   118  		return "Bold"
   119  	case ExtraBold:
   120  		return "ExtraBold"
   121  	case Black:
   122  		return "Black"
   123  	default:
   124  		panic("invalid Weight")
   125  	}
   126  }