Пример #1
0
func (c osx8Collator) Compare(a, b Input) int {
	sa := C.CFStringCreateWithBytesNoCopy(
		nil,
		osxUInt8P(a.UTF8),
		C.CFIndex(len(a.UTF8)),
		C.kCFStringEncodingUTF8,
		C.Boolean(0),
		nil,
	)
	sb := C.CFStringCreateWithBytesNoCopy(
		nil,
		osxUInt8P(b.UTF8),
		C.CFIndex(len(b.UTF8)),
		C.kCFStringEncodingUTF8,
		C.Boolean(0),
		nil,
	)
	_range := C.CFRangeMake(0, C.CFStringGetLength(sa))
	return int(C.CFStringCompareWithOptionsAndLocale(sa, sb, _range, c.opt, c.loc))
}
Пример #2
0
// The function reads the content of the text reader given as argument and
// produces a C.CFAttributedStringRef object that most closely represtns the
// input.
// The program needs to call C.CFRelease on the returned C.CFAttributedStringRef
// when it doesn't need it anymore to avoid any memory leak.
func CFAttributedStringCreateWithTextReader(r text.Reader) C.CFAttributedStringRef {
	style := text.Style{}

	b := &bytes.Buffer{}
	b.Grow(1000)

	styles := make([]C.Style__, 0, 10)
	offset := 0

	for {
		c, err := r.ReadChar()
		if err != nil {
			break
		}

		s := text.StyleOf(c)
		s.Offset = 0

		if s != style {
			style = s
			s.Offset = offset

			font := C.CTFontRef(nil)

			switch f := s.Face.(type) {
			case *face:
				font = f.ref
			}

			styles = append(styles, C.Style__{
				_range:     C.CFRange{location: C.CFIndex(offset)},
				font:       font,
				foreground: makeRGBA__(imageColor(s.Foreground, color.Black)),
				background: makeRGBA__(imageColor(s.Background, color.Transparent)),
			})
		}

		b.WriteRune(c.Rune)
		offset++
	}

	if n := len(styles); n != 0 {
		n--
		styles[n]._range.length = C.CFIndex(offset) - styles[n]._range.location

		for i := range styles[:n] {
			s0 := &styles[i]
			s1 := &styles[i+1]
			s0._range.length = s1._range.location - s0._range.location
		}
	}

	d := b.Bytes()
	s := C.CFStringCreateWithBytesNoCopy(
		nil,
		(*C.UInt8)(&d[0]),
		C.CFIndex(len(d)),
		C.kCFStringEncodingUTF8,
		0,
		C.kCFAllocatorNull,
	)
	defer C.CFRelease(C.CFTypeRef(s))
	return C.CFAttributedStringCreateWithStyles__(nil, s, &styles[0], C.size_t(len(styles)))
}