github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/text/text.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package text
     4  
     5  import (
     6  	"github.com/gop9/olt/gio/op"
     7  	"github.com/gop9/olt/gio/unit"
     8  	"golang.org/x/image/font"
     9  	"golang.org/x/image/math/fixed"
    10  )
    11  
    12  // A Line contains the measurements of a line of text.
    13  type Line struct {
    14  	Text String
    15  	// Width is the width of the line.
    16  	Width fixed.Int26_6
    17  	// Ascent is the height above the baseline.
    18  	Ascent fixed.Int26_6
    19  	// Descent is the height below the baseline, including
    20  	// the line gap.
    21  	Descent fixed.Int26_6
    22  	// Bounds is the visible bounds of the line.
    23  	Bounds fixed.Rectangle26_6
    24  }
    25  
    26  type String struct {
    27  	String string
    28  	// Advances contain the advance of each rune in String.
    29  	Advances []fixed.Int26_6
    30  }
    31  
    32  // A Layout contains the measurements of a body of text as
    33  // a list of Lines.
    34  type Layout struct {
    35  	Lines []Line
    36  }
    37  
    38  // LayoutOptions specify the constraints of a text layout.
    39  type LayoutOptions struct {
    40  	// MaxWidth is the available width of the layout.
    41  	MaxWidth int
    42  }
    43  
    44  // Style is the font style.
    45  type Style int
    46  
    47  // Weight is a font weight, in CSS units.
    48  type Weight int
    49  
    50  // Font specify a particular typeface, style and size.
    51  type Font struct {
    52  	Typeface Typeface
    53  	Variant  Variant
    54  	Size     unit.Value
    55  	Style    Style
    56  	// Weight is the text weight. If zero, Normal is used instead.
    57  	Weight Weight
    58  }
    59  
    60  // Face implements text layout and shaping for a particular font.
    61  type Face interface {
    62  	Layout(ppem fixed.Int26_6, str string, opts LayoutOptions) *Layout
    63  	Shape(ppem fixed.Int26_6, str String) op.CallOp
    64  	Metrics(ppem fixed.Int26_6) font.Metrics
    65  }
    66  
    67  // Typeface identifies a particular typeface design. The empty
    68  // string denotes the default typeface.
    69  type Typeface string
    70  
    71  // Variant denotes a typeface variant such as "Mono" or "Smallcaps".
    72  type Variant string
    73  
    74  type Alignment uint8
    75  
    76  const (
    77  	Start Alignment = iota
    78  	End
    79  	Middle
    80  )
    81  
    82  const (
    83  	Regular Style = iota
    84  	Italic
    85  )
    86  
    87  const (
    88  	Normal Weight = 400
    89  	Medium Weight = 500
    90  	Bold   Weight = 600
    91  )
    92  
    93  func (a Alignment) String() string {
    94  	switch a {
    95  	case Start:
    96  		return "Start"
    97  	case End:
    98  		return "End"
    99  	case Middle:
   100  		return "Middle"
   101  	default:
   102  		panic("unreachable")
   103  	}
   104  }