package cff

import (
	

	
)

//go:generate ../../../../../typesetting-utils/generators/binarygen/cmd/generator . _src.go

type header2 struct {
	majorVersion  uint8  //	Format major version. Set to 2.
	minorVersion  uint8  //	Format minor version. Set to zero.
	headerSize    uint8  //	Header size (bytes).
	topDictLength uint16 //	Length of Top DICT structure in bytes.
}

type indexStart struct {
	count   uint32 //	Number of objects stored in INDEX
	offSize uint8  //	Offset array element size
	// then
	// offset  []Offset
	// data    []byte
}

//lint:ignore U1000 this type is required so that the code generator add a ParseFdSelect function
type dummy struct {
	fd fdSelect
}

// fdSelect holds a CFF font's Font Dict Select data.
type fdSelect interface {
	isFdSelect()

	fontDictIndex(glyph tables.GlyphID) (byte, error)
	// return the maximum index + 1 (it's the length of an array
	// which can be safely indexed by the indexes)
	extent() int
}

func (fdSelect0) () {}
func (fdSelect3) () {}
func (fdSelect4) () {}

type fdSelect0 struct {
	format uint8   `unionTag:"0"` //	Set to 0
	fds    []uint8 // [nGlyphs]	FD selector array
}

var errGlyph = errors.New("invalid glyph index")

func ( fdSelect0) ( tables.GlyphID) (byte, error) {
	if int() >= len(.fds) {
		return 0, errGlyph
	}
	return .fds[], nil
}

func ( fdSelect0) () int {
	 := -1
	for ,  := range .fds {
		if int() >  {
			 = int()
		}
	}
	return  + 1
}

type fdSelect3 struct {
	format   uint8    `unionTag:"3"` //	Set to 3
	nRanges  uint16   //	Number of ranges
	ranges   []range3 `arrayCount:"ComputedField-nRanges"` // [nRanges]	Array of Range3 records (see below)
	sentinel uint16   //	Sentinel GID
}

type range3 struct {
	first tables.GlyphID //	First glyph index in range
	fd    uint8          //	FD index for all glyphs in range
}

func ( fdSelect3) ( tables.GlyphID) (byte, error) {
	,  := 0, len(.ranges)
	for  <  {
		 := ( + ) / 2
		 := .ranges[]
		 := .first
		if  <  {
			 = 
			continue
		}
		 := .sentinel
		if  < len(.ranges)-1 {
			 = .ranges[+1].first
		}
		if  <=  {
			 =  + 1
			continue
		}
		return .fd, nil
	}
	return 0, errGlyph
}

func ( fdSelect3) () int {
	 := -1
	for ,  := range .ranges {
		if int(.fd) >  {
			 = int(.fd)
		}
	}
	return  + 1
}

type fdSelect4 struct {
	format   uint8    `unionTag:"4"` //	Set to 4
	nRanges  uint32   //	Number of ranges
	ranges   []range4 `arrayCount:"ComputedField-nRanges"` // [nRanges]	Array of Range4 records (see below)
	sentinel uint32   //	Sentinel GID
}

type range4 struct {
	first uint32 //	First glyph index in range
	fd    uint16 //	FD index for all glyphs in range
}

func ( fdSelect4) ( tables.GlyphID) (byte, error) {
	,  := .fontDictIndex32(uint32())
	return byte(), 
}

func ( fdSelect4) ( uint32) (uint16, error) {
	,  := 0, len(.ranges)
	for  <  {
		 := ( + ) / 2
		 := .ranges[]
		 := .first
		if  <  {
			 = 
			continue
		}
		 := .sentinel
		if  < len(.ranges)-1 {
			 = .ranges[+1].first
		}
		if  <=  {
			 =  + 1
			continue
		}
		return .fd, nil
	}
	return 0, errGlyph
}

func ( fdSelect4) () int {
	 := -1
	for ,  := range .ranges {
		if int(.fd) >  {
			 = int(.fd)
		}
	}
	return  + 1
}