New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add parsing code for the Font Features, as well as new command #3
Changes from all commits
76df659
c7a3e4c
b6436c3
980a891
681d414
368e469
f0147c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ConradIrwin/font/sfnt" | ||
) | ||
|
||
// Features prints the gpos/gsub tables (contains font features). | ||
func Features() { | ||
if len(os.Args) < 2 { | ||
panic(fmt.Errorf("Specify a font file")) | ||
} | ||
|
||
file, err := os.Open(os.Args[1]) | ||
if err != nil { | ||
panic(fmt.Errorf("Failed to open font: %s", err)) | ||
} | ||
defer file.Close() | ||
|
||
font, err := sfnt.Parse(file) | ||
if err != nil { | ||
panic(fmt.Errorf("Failed to parse font: %s", err)) | ||
} | ||
|
||
layoutTable(font, sfnt.TagGsub, "Glyph Substitution Table (GSUB)") | ||
layoutTable(font, sfnt.TagGpos, "Glyph Positioning Table (GPOS)") | ||
} | ||
|
||
func layoutTable(font *sfnt.Font, tag sfnt.Tag, name string) { | ||
if font.HasTable(tag) { | ||
fmt.Printf("%s:\n", name) | ||
|
||
t := font.Table(tag).(*sfnt.TableLayout) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bramp This will panic if parsing fails. I'm not sure what to do about that except maybe change the library so that it parses tables on-demand instead of up-front so we can return a sensible error. Any better ideas? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, panic'ng sucks. After this I'd be happy to change the whole library to pass back the errors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That'd be incredible 😻 |
||
for _, script := range t.Scripts { | ||
fmt.Printf("\tScript %q%s:\n", script.Tag, bracketString(script)) | ||
|
||
fmt.Printf("\t\tDefault Language:\n") | ||
for _, feature := range script.DefaultLanguage.Features { | ||
fmt.Printf("\t\t\tFeature %q%s\n", feature.Tag, bracketString(feature)) | ||
} | ||
|
||
for _, lang := range script.Languages { | ||
fmt.Printf("\t\tLanguage %q%s:\n", lang.Tag, bracketString(lang)) | ||
for _, feature := range lang.Features { | ||
fmt.Printf("\t\t\tFeature %q%s\n", feature.Tag, bracketString(feature)) | ||
} | ||
} | ||
} | ||
} else { | ||
fmt.Printf("No %s\n", name) | ||
} | ||
} | ||
|
||
func bracketString(o fmt.Stringer) string { | ||
if s := o.String(); s != "" { | ||
return fmt.Sprintf(" (%s)", s) | ||
} | ||
return "" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ import ( | |
) | ||
|
||
func Info() { | ||
|
||
file, err := os.Open(os.Args[1]) | ||
if err != nil { | ||
panic(err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we start to improve the code for handling these errors, we should make it clear how to fix the problem, and make the error message more legible. (I appreciate the other commands aren't yet very good).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do that in a later PR.