implement a simplist font management

This commit is contained in:
legoff.laurent 2010-12-06 16:34:51 +00:00
parent ab3e5a08bf
commit 6f328d3b76
21 changed files with 140 additions and 77421 deletions

24
draw2d/fonts/README Normal file
View File

@ -0,0 +1,24 @@
All questions regarding this software should be directed at the
Xorg mailing list:
http://lists.freedesktop.org/mailman/listinfo/xorg
Please submit bug reports to the Xorg bugzilla:
https://bugs.freedesktop.org/enter_bug.cgi?product=xorg
The master development code repository can be found at:
git://anongit.freedesktop.org/git/xorg/font/bh-ttf
http://cgit.freedesktop.org/xorg/font/bh-ttf
For patch submission instructions, see:
http://www.x.org/wiki/Development/Documentation/SubmittingPatches
For more information on the git code manager, see:
http://wiki.x.org/wiki/GitPage

BIN
draw2d/fonts/luximb.ttf Normal file

Binary file not shown.

BIN
draw2d/fonts/luximbi.ttf Normal file

Binary file not shown.

BIN
draw2d/fonts/luximri.ttf Normal file

Binary file not shown.

BIN
draw2d/fonts/luxirb.ttf Normal file

Binary file not shown.

BIN
draw2d/fonts/luxirbi.ttf Normal file

Binary file not shown.

BIN
draw2d/fonts/luxirri.ttf Normal file

Binary file not shown.

BIN
draw2d/fonts/luxisb.ttf Normal file

Binary file not shown.

BIN
draw2d/fonts/luxisbi.ttf Normal file

Binary file not shown.

BIN
draw2d/fonts/luxisri.ttf Normal file

Binary file not shown.

View File

@ -1,11 +0,0 @@
The *.ttf and COPYING files in this directory were copied from the X.org
project, specifically
http://xorg.freedesktop.org/releases/individual/font/font-bh-ttf-1.0.0.tar.bz2
There are three fonts: sans (s), serif (r) and monospaced (m). For example,
luxisr.ttf is Luxi Sans. The 'r' here means regular, as opposed to bold.
The *.ttx files in this directory were generated from the *.ttf files
by the ttx command-line tool.
http://www.letterror.com/code/ttx/index.html

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -13,8 +13,8 @@ import (
"math"
"image"
"image/png"
//"draw2d"
"draw2d.googlecode.com/svn/trunk/draw2d/src/pkg/draw2d"
"draw2d"
//"draw2d.googlecode.com/svn/trunk/draw2d/src/pkg/draw2d"
)
const (
@ -495,11 +495,13 @@ func TestPathTransform() {
}
func TestFillString() {
draw2d.SetFontFolder("../../fonts/")
i, gc := initGc(100, 100)
draw2d.RoundRect(gc, 5, 5, 95, 95, 10, 10)
gc.FillStroke()
gc.SetFontSize(18)
gc.MoveTo(10, 52)
gc.SetFontData(draw2d.FontData{"luxi", draw2d.FontFamilyMono, draw2d.FontStyleBold|draw2d.FontStyleItalic})
width := gc.FillString("cou")
fmt.Printf("width: %f\n", width)
gc.RMoveTo(width+1, 0)

View File

@ -7,32 +7,10 @@ import (
"exp/draw"
"image"
"log"
"io/ioutil"
"freetype-go.googlecode.com/hg/freetype"
"freetype-go.googlecode.com/hg/freetype/raster"
"freetype-go.googlecode.com/hg/freetype/truetype"
)
var (
font *truetype.Font
)
func init() {
// Read the font data.
fontBytes, err := ioutil.ReadFile("../../luxi-fonts/luxisr.ttf")
if err != nil {
log.Println(err)
return
}
font, err = freetype.ParseFont(fontBytes)
if err != nil {
log.Println(err)
return
}
}
type FillRule int
const (
@ -45,6 +23,7 @@ type GraphicContext struct {
fillRasterizer *raster.Rasterizer
strokeRasterizer *raster.Rasterizer
freetype *freetype.Context
defaultFontData FontData
DPI int
current *contextStack
}
@ -62,6 +41,7 @@ type contextStack struct {
join Join
previous *contextStack
fontSize float
fontData FontData
}
/**
@ -75,9 +55,9 @@ func NewGraphicContext(pi *image.RGBA) *GraphicContext {
gc.strokeRasterizer = raster.NewRasterizer(width, height)
gc.DPI = 92
gc.defaultFontData = FontData{"luxi", FontFamilySans, FontStyleNormal}
gc.freetype = freetype.NewContext()
gc.freetype.SetDPI(gc.DPI)
gc.freetype.SetFont(font)
gc.freetype.SetClip(pi.Bounds())
gc.freetype.SetDst(pi)
@ -92,6 +72,7 @@ func NewGraphicContext(pi *image.RGBA) *GraphicContext {
gc.current.fillRule = FillRuleEvenOdd
gc.current.join = RoundJoin
gc.current.fontSize = 10
gc.current.fontData = gc.defaultFontData
return gc
}
@ -163,6 +144,14 @@ func (gc *GraphicContext) GetFontSize() float {
return gc.current.fontSize
}
func (gc *GraphicContext) SetFontData(fontData FontData) {
gc.current.fontData = fontData
}
func (gc *GraphicContext) GetFontData() FontData {
return gc.current.fontData
}
func (gc *GraphicContext) SetDPI(dpi int) {
gc.DPI = dpi
gc.freetype.SetDPI(dpi)
@ -176,6 +165,7 @@ func (gc *GraphicContext) GetDPI() int {
func (gc *GraphicContext) Save() {
context := new(contextStack)
context.fontSize = gc.current.fontSize
context.fontData = gc.current.fontData
context.lineWidth = gc.current.lineWidth
context.strokeColor = gc.current.strokeColor
context.fillColor = gc.current.fillColor
@ -253,6 +243,14 @@ func (gc *GraphicContext) FillString(text string) (cursor float){
gc.current.tr.Transform(&x, &y)
x0, fontSize := 0.0, gc.current.fontSize
gc.current.tr.VectorTransform(&x0, &fontSize)
font := GetFont(gc.current.fontData)
if(font == nil) {
font = GetFont(gc.defaultFontData)
}
if(font == nil) {
return 0
}
gc.freetype.SetFont(font)
gc.freetype.SetFontSize(fontSize)
pt := freetype.Pt(int(x), int(y))
p, err := gc.freetype.DrawString(text, pt)
@ -261,9 +259,7 @@ func (gc *GraphicContext) FillString(text string) (cursor float){
}
x1, _ := gc.current.path.LastPoint()
x2, y2 := float(p.X) / 256, float(p.Y) / 256
log.Printf("x2: %f, y2: %f\n", x2, y2)
gc.current.tr.InverseTransform(&x2, &y2)
log.Printf("x2: %f, y2: %f\n", x2, y2)
width := x2 - x1
return width
}

View File

@ -0,0 +1,91 @@
package draw2d
import (
"freetype-go.googlecode.com/hg/freetype"
"freetype-go.googlecode.com/hg/freetype/truetype"
"path"
"log"
"io/ioutil"
)
var (
fontFolder = "../../fonts/"
fonts = make(map[string] *truetype.Font)
)
type FontStyle byte
const (
FontStyleNormal FontStyle = iota
FontStyleBold
FontStyleItalic
)
type FontFamily byte
const (
FontFamilySans FontFamily = iota
FontFamilySerif
FontFamilyMono
)
type FontData struct {
Name string
Family FontFamily
Style FontStyle
}
func GetFont(fontData FontData) (*truetype.Font) {
fontFileName := fontData.Name
switch fontData.Family {
case FontFamilySans: fontFileName += "s"
case FontFamilySerif: fontFileName += "r"
case FontFamilyMono: fontFileName += "m"
}
if(fontData.Style & FontStyleBold != 0) {
fontFileName += "b"
} else {
fontFileName += "r"
}
if(fontData.Style & FontStyleItalic != 0) {
fontFileName += "i"
}
fontFileName += ".ttf"
font := fonts[fontFileName]
if(font != nil) {
return font
}
fonts[fontFileName] = loadFont(fontFileName)
return fonts[fontFileName]
}
func GetFontFolder() string {
return fontFolder
}
func SetFontFolder(folder string) {
fontFolder = folder
}
func loadFont(fontFileName string) (*truetype.Font) {
fontBytes, err := ioutil.ReadFile(path.Join(fontFolder, fontFileName))
if err != nil {
log.Println(err)
return nil
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
log.Println(err)
return nil
}
return font
}