font.go 2.8 KB

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