Example #1
0
func (self *face) Glyph(dot fixed.Point26_6, char rune) (dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
	var bounds fixed.Rectangle26_6
	var cached bool
	var cachedGlyph glyph

	if cachedGlyph, cached = self.cache.get(char); cached {
		bounds, advance, mask = cachedGlyph.bounds, cachedGlyph.advance, cachedGlyph.mask
	}

	if bounds == (fixed.Rectangle26_6{}) || advance == 0 {
		if bounds, advance, ok = self.GlyphBounds(char); !ok {
			return
		}
	}

	offset := fixed.Point26_6{
		X: abs26_6(bounds.Min.X),
		Y: self.metrics.Ascent,
	}

	origin := C.CGPoint{
		x: C.CGFloat(f64(offset.X)),
		y: C.CGFloat(f64(offset.Y)),
	}

	dx := (int(bounds.Max.X+offset.X) >> 6) + 1
	dy := (int(self.metrics.Ascent+self.metrics.Descent) >> 6) + 1

	dr.Min.X = (int(dot.X) >> 6) - int(origin.x)
	dr.Min.Y = (int(dot.Y) >> 6) - int(origin.y)
	dr.Max.X = dr.Min.X + dx
	dr.Max.Y = dr.Min.Y + dy

	if mask == nil {
		alpha := image.NewAlpha(image.Rect(0, 0, dx, dy))

		if !C.CTFontGlyphDraw__(
			self.ref,
			C.UTF32Char(char),
			origin,
			(*C.UInt8)(unsafe.Pointer(&alpha.Pix[0])),
			C.size_t(alpha.Stride),
			C.size_t(dx),
			C.size_t(dy),
		) {
			dr, advance = image.Rectangle{}, 0
			return
		}

		mask = alpha
		self.cache.set(glyph{rune: char, bounds: bounds, advance: advance, mask: mask})
	}

	ok = true
	return
}
Example #2
0
func (self *face) GlyphAdvance(char rune) (advance fixed.Int26_6, ok bool) {
	if cachedGlyph, cached := self.cache.get(char); cached {
		advance, ok = cachedGlyph.advance, true
		return
	}

	if a := C.CTFontGlyphAdvance__(self.ref, C.UTF32Char(char)); a != 0 {
		advance, ok = i26_6(a), true
		self.cache.set(glyph{rune: char, advance: advance})
	}

	return
}
Example #3
0
func (self *face) GlyphBounds(char rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) {
	if cachedGlyph, cached := self.cache.get(char); cached {
		if cachedGlyph.bounds != (fixed.Rectangle26_6{}) {
			bounds, advance, ok = cachedGlyph.bounds, cachedGlyph.advance, true
			return
		}
	}

	r := C.CGRect{}
	a := C.CTFontGlyphBounds__(self.ref, C.UTF32Char(char), &r)

	if a != 0 {
		bounds, advance, ok = r26_6(r), i26_6(a), true
		self.cache.set(glyph{rune: char, bounds: bounds, advance: advance})
	}

	return
}
Example #4
0
func (self *face) Kern(char0 rune, char1 rune) fixed.Int26_6 {
	k := C.CTFontKern__(self.ref, C.UTF32Char(char0), C.UTF32Char(char1))
	return i26_6(k)
}