Skip to content

Commit

Permalink
Search OS paths for fonts that are not found in the data directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-clayton committed Mar 18, 2015
1 parent db70298 commit e441085
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 24 deletions.
2 changes: 1 addition & 1 deletion default_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func CreateDefaultAdapter() *DefaultAdapter {

func (a *DefaultAdapter) SetItemSizeAsLargest(theme Theme) {
s := math.Size{}
font := theme.DefaultLabelFont()
font := theme.DefaultFont()
for i := 0; i < a.Count(); i++ {
e := a.data.Index(i).Interface()
switch t := e.(type) {
Expand Down
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type Driver interface {
Terminate()
SetClipboard(str string)
GetClipboard() (string, error)
LoadFont(name string, size int) (Font, error)
CreateViewport(width, height int, name string) Viewport
CreateCanvas(math.Size) Canvas
CreateFont(name string, size int) Font
CreateTexture(img image.Image, pixelsPerDip float32) Texture
}
25 changes: 17 additions & 8 deletions drivers/gl/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package gl

import (
"container/list"
"fmt"
"image"
"io/ioutil"
"path/filepath"
"runtime"

"github.com/go-gl/glfw/v3.1/glfw"
"github.com/google/gxui"
"github.com/google/gxui/drivers/gl/platform"
"github.com/google/gxui/math"
)

Expand Down Expand Up @@ -140,6 +142,21 @@ func (d *Driver) GetClipboard() (str string, err error) {
return
}

func (d *Driver) LoadFont(name string, size int) (gxui.Font, error) {
// Try the data path first.
f, err := ioutil.ReadFile(filepath.Join(d.dataPath, name))
if err == nil {
return CreateFont(name, f, size)
}
// No luck. Search the OS font directories next...
for _, path := range platform.FontPaths {
if f, err := ioutil.ReadFile(filepath.Join(path, name)); err == nil {
return CreateFont(name, f, size)
}
}
return nil, fmt.Errorf("Unable to find font '%s'", name)
}

func (d *Driver) CreateViewport(width, height int, name string) gxui.Viewport {
var v *Viewport
d.syncDriver(func() {
Expand All @@ -159,11 +176,3 @@ func (d *Driver) CreateCanvas(s math.Size) gxui.Canvas {
func (d *Driver) CreateTexture(img image.Image, pixelsPerDip float32) gxui.Texture {
return CreateTexture(img, pixelsPerDip)
}

func (d *Driver) CreateFont(name string, size int) gxui.Font {
f, err := ioutil.ReadFile(filepath.Join(d.dataPath, name))
if err != nil {
panic(err)
}
return CreateFont(name, f, size)
}
6 changes: 3 additions & 3 deletions drivers/gl/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ type Font struct {
quads []Quad // Reused each call to Draw()
}

func CreateFont(name string, data []byte, size int) *Font {
func CreateFont(name string, data []byte, size int) (*Font, error) {
ttf, err := truetype.Parse(data)
if err != nil {
panic(err)
return nil, err
}

scale := int32(size << 6)
Expand All @@ -55,7 +55,7 @@ func CreateFont(name string, data []byte, size int) *Font {
resolutions: make(map[Resolution]*glyphTable),
glyphs: make(map[rune]*glyph),
quads: []Quad{},
}
}, nil
}

func (f *Font) glyph(r rune) *glyph {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !darwin
// +build linux

package platform

const ScrollSpeed = 20.0

// Paths to try when a font is not found in the local data directory
var FontPaths = []string{
"/usr/share/fonts",
"/usr/local/share/fonts",
"~/.fonts",
}
6 changes: 6 additions & 0 deletions drivers/gl/platform/osx_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@
package platform

const ScrollSpeed = 4.0

// Paths to try when a font is not found in the local data directory
var FontPaths = []string{
"/Library/Fonts/",
"/System/Library/Fonts/",
}
30 changes: 30 additions & 0 deletions drivers/gl/platform/windows_constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package platform

// #cgo LDFLAGS: -lShell32
// #include <Shlobj.h>
//
// void getWindowsFontDirectory(char* path) {
// SHGetFolderPath(0, CSIDL_FONTS, 0, 0, path);
// }
import "C"

const ScrollSpeed = 20.0

// Paths to try when a font is not found in the local data directory
var FontPaths = []string{
getWindowsFontDirectory(),
}

const max_path = 260

func getWindowsFontDirectory() string {
buf := make([]C.char, max_path)
C.getWindowsFontDirectory(&buf[0])
return C.GoString(&buf[0])
}
3 changes: 3 additions & 0 deletions mixins/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Label struct {
}

func (l *Label) Init(outer LabelOuter, theme gxui.Theme, font gxui.Font, color gxui.Color) {
if font == nil {
panic("Cannot create a label with a nil font")
}
l.Control.Init(outer, theme)
l.outer = outer
l.font = font
Expand Down
3 changes: 2 additions & 1 deletion theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ package gxui

type Theme interface {
Driver() Driver
DefaultLabelFont() Font
DefaultFont() Font
SetDefaultFont(Font)
CreateBubbleOverlay() BubbleOverlay
CreateButton() Button
CreateCodeEditor() CodeEditor
Expand Down
2 changes: 1 addition & 1 deletion themes/dark/code_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type CodeEditor struct {
func CreateCodeEditor(theme *Theme) gxui.CodeEditor {
t := &CodeEditor{}
t.theme = theme
t.Init(t, theme.driver, theme, theme.DefaultFont)
t.Init(t, theme.driver, theme, theme.defaultFont)
t.SetTextColor(theme.TextBoxDefaultStyle.FontColor)
t.SetMargin(math.Spacing{L: 3, T: 3, R: 3, B: 3})
t.SetPadding(math.Spacing{L: 3, T: 3, R: 3, B: 3})
Expand Down
2 changes: 1 addition & 1 deletion themes/dark/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func CreateLabel(theme *Theme) gxui.Label {
l := &mixins.Label{}
l.Init(l, theme, theme.DefaultFont, theme.LabelStyle.FontColor)
l.Init(l, theme, theme.defaultFont, theme.LabelStyle.FontColor)
l.SetMargin(math.Spacing{L: 3, T: 3, R: 3, B: 3})
return l
}
2 changes: 1 addition & 1 deletion themes/dark/textbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type TextBox struct {

func CreateTextBox(theme *Theme) gxui.TextBox {
t := &TextBox{}
t.Init(t, theme.driver, theme, theme.DefaultFont)
t.Init(t, theme.driver, theme, theme.defaultFont)
t.SetTextColor(theme.TextBoxDefaultStyle.FontColor)
t.SetMargin(math.Spacing{L: 3, T: 3, R: 3, B: 3})
t.SetPadding(math.Spacing{L: 3, T: 3, R: 3, B: 3})
Expand Down
24 changes: 18 additions & 6 deletions themes/dark/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
package dark

import (
"fmt"

"github.com/google/gxui"
)

const defaultFontName = "Arial.ttf"

type Theme struct {
driver gxui.Driver
DefaultFont gxui.Font
defaultFont gxui.Font

WindowBackground gxui.Color

Expand Down Expand Up @@ -40,8 +44,12 @@ type Theme struct {
}

func CreateTheme(driver gxui.Driver) gxui.Theme {
defaultFont := driver.CreateFont("SourceCodePro-Regular.ttf", 12)
defaultFont.LoadGlyphs(32, 126)
defaultFont, err := driver.LoadFont(defaultFontName, 12)
if err == nil {
defaultFont.LoadGlyphs(32, 126)
} else {
fmt.Printf("Warning: Failed to load default font - %v\n", err)
}

scrollBarRailDefaultBg := gxui.Black
scrollBarRailDefaultBg.A = 0.7
Expand All @@ -54,7 +62,7 @@ func CreateTheme(driver gxui.Driver) gxui.Theme {

return &Theme{
driver: driver,
DefaultFont: defaultFont,
defaultFont: defaultFont,

WindowBackground: gxui.Black,

Expand Down Expand Up @@ -90,8 +98,12 @@ func (t *Theme) Driver() gxui.Driver {
return t.driver
}

func (t *Theme) DefaultLabelFont() gxui.Font {
return t.DefaultFont
func (t *Theme) DefaultFont() gxui.Font {
return t.defaultFont
}

func (t *Theme) SetDefaultFont(f gxui.Font) {
t.defaultFont = f
}

func (t *Theme) CreateBubbleOverlay() gxui.BubbleOverlay {
Expand Down

0 comments on commit e441085

Please sign in to comment.