Skip to content

Commit

Permalink
New high level interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pgundlach committed Dec 10, 2021
1 parent 44c78a6 commit 0ba193b
Show file tree
Hide file tree
Showing 7 changed files with 878 additions and 680 deletions.
660 changes: 660 additions & 0 deletions License.md

Large diffs are not rendered by default.

69 changes: 63 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,69 @@ Contact: Patrick Gundlach <[email protected]>

## Sample code

### High level interface

```lua
-- This document uses the high level interface with document/mknodes.
-- When you need more control, you can create all nodes yourself and
-- output these on the page.
document.info("Reading myfile.lua")
local d = document.new("out.pdf")

local lang_en, msg = d.loadpattern("hyphenationpatterns/hyph-en-us.pat.txt")
if not lang_en then
print(msg)
os.exit(-1)
end
d.defaultlanguage = lang_en

-- font sources
local reg = {name="crimson pro regular",source="fonts/CrimsonPro-Regular.ttf"}
local bold = {name="crimson pro bold",source="fonts/CrimsonPro-Bold.ttf"}

local ff = d.newfontfamily("textfamily")
-- font source, weight (0-1000), style (normal,italic)
ff.addmember(reg,400,"normal")
ff.addmember(bold,700,"normal")


local te = {
settings = { fontfamily = ff },
"Hello ",
{ settings = { weight = 700, color = "rebeccapurple"},
{"nice"}
},
" world",
}

local head,tail = d.mknodes(te)
d.hyphenate(head)
node.append_lineend(tail)

local param = {
hsize = document.sp("134pt"),
lineheight = document.sp("12pt"),
}

local vl = node.linebreak(head,param)

d.outputat(document.sp("1cm"),document.sp("27cm"),vl)
d.currentpage().shipout()

local ok, msg = d.finish()
if not ok then
print(msg)
os.exit(-1)
end

document.info("Reading myfile.lua...done")
```

### Low level interface with DIY node creation.

```lua
-- This is still a very vey early preview and will probably not work when
-- you experiment with the code.
--
-- Two functions for fonts and for images just to show how to
-- create objects and output them.

document.info("Reading myfile.lua")

local ok, face, msg, fnt, lang_en, imgfile, image
Expand All @@ -62,7 +118,7 @@ favorite plaything.]])

local head, cur = lang, lang
for _, glyph in ipairs(tbl) do
if glyph.glyph == 32 then
if glyph.isspace then
local glu = node.new("glue")
glu.width = fnt.space
glu.stretch = fnt.stretch
Expand Down Expand Up @@ -106,12 +162,13 @@ end
-- The document d is the most important item here.
local d = document.new("out.pdf")

face, msg = d.loadFace("fonts/CrimsonPro-Regular.ttf")
face, msg = d.loadFace({name = "regular", source = "fonts/CrimsonPro-Regular.ttf"})
if not face then
print(msg)
os.exit(-1)
end


fnt = d.createFont(face,document.sp("12pt"))

lang_en, msg = d.loadpattern("hyphenationpatterns/hyph-en-us.pat.txt")
Expand Down
60 changes: 60 additions & 0 deletions core/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,15 @@ func indexDoc(l *lua.LState) int {
case "loadpattern":
l.Push(l.NewFunction(documentLoadPatternFile(doc.d)))
return 1
case "mknodes":
l.Push(l.NewFunction(documentMknodes(doc.d)))
return 1
case "newpage":
l.Push(l.NewFunction(documentNewPage(doc.d)))
return 1
case "newfontfamily":
l.Push(l.NewFunction(documentNewFontfamily(doc.d)))
return 1
case "outputat":
l.Push(l.NewFunction(documentOutputAt(doc.d)))
return 1
Expand Down Expand Up @@ -168,6 +174,60 @@ func documentLoadPatternFile(doc *document.Document) lua.LGFunction {
}
}

func teFromTable(l *lua.LState, tbl *lua.LTable) *document.TypesettingElement {
te := &document.TypesettingElement{}
var ts = make(document.TypesettingSettings)

te.Settings = ts
tbl.ForEach(func(k, v lua.LValue) {
switch k.Type() {
case lua.LTNumber:
switch v.Type() {
case lua.LTString:
te.Items = append(te.Items, v.String())
case lua.LTTable:
te.Items = append(te.Items, teFromTable(l, v.(*lua.LTable)))
}
case lua.LTString:
switch k.String() {
case "settings":
if settingstbl, ok := v.(*lua.LTable); ok {
ffLvalue := settingstbl.RawGetString("fontfamily")
if ffud, ok := ffLvalue.(*lua.LUserData); ok {
if ff, ok := ffud.Value.(*document.FontFamily); ok {
ts[document.SettingFontFamily] = ff
}
}
if colorLvalue := settingstbl.RawGetString("color"); colorLvalue.Type() == lua.LTString {
ts[document.SettingColor] = colorLvalue.String()
}
if weightLvalue := settingstbl.RawGetString("weight"); weightLvalue.Type() == lua.LTNumber {
value := weightLvalue.(lua.LNumber)
ts[document.SettingFontWeight] = int(float64(value))
}
}
}
}
})

return te
}

func documentMknodes(doc *document.Document) lua.LGFunction {
return func(l *lua.LState) int {
tbl := l.CheckTable(1)
te := teFromTable(l, tbl)

hlist, tail, err := doc.Mknodes(te)
if err != nil {
return lerr(l, err.Error())
}
l.Push(newUserDataFromNode(l, hlist))
l.Push(newUserDataFromNode(l, tail))
return 2
}
}

func documentOutputAt(doc *document.Document) lua.LGFunction {
return func(l *lua.LState) int {
x := l.CheckNumber(1)
Expand Down
94 changes: 89 additions & 5 deletions core/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,30 @@ import (
)

const (
luaFaceTypeName = "face"
luaFontTypeName = "font"
luaFaceTypeName = "face"
luaFontTypeName = "font"
luaFontFamilyTypeName = "fontfamily"
)

func documentLoadFace(doc *document.Document) lua.LGFunction {
return func(l *lua.LState) int {
fn := l.CheckString(1)
f, err := doc.LoadFace(fn, 0)
fn := l.CheckTable(1)
nameValue := fn.RawGetString("name")
srcValue := fn.RawGetString("source")
if nameValue.Type() != lua.LTString {
return lerr(l, "the value of name must be a string")
}
if srcValue.Type() != lua.LTString {
return lerr(l, "the value of source must be a string")
}
fs := document.FontSource{
Name: nameValue.String(),
Source: srcValue.String(),
}
f, err := doc.LoadFace(&fs)
if err != nil {
return lerr(l, err.Error())
}

mt := l.NewTypeMetatable(luaFaceTypeName)
ud := l.NewUserData()
ud.Value = f
Expand Down Expand Up @@ -83,6 +95,7 @@ func fontShape(fnt *font.Font, fntObj lua.LValue) lua.LGFunction {
glyphtbl.RawSetString("components", lua.LString(glyph.Components))
glyphtbl.RawSetString("glyph", lua.LNumber(glyph.Glyph))
glyphtbl.RawSetString("hyphenate", lua.LBool(glyph.Hyphenate))
glyphtbl.RawSetString("isspace", lua.LBool(glyph.IsSpace))
glyphtbl.RawSetString("font", fntObj)

tbl.Append(glyphtbl)
Expand Down Expand Up @@ -115,3 +128,74 @@ func indexFont(l *lua.LState) int {
}
return 0
}

// Font families
func documentNewFontfamily(doc *document.Document) lua.LGFunction {
return func(l *lua.LState) int {
familyname := l.CheckString(1)
ff := doc.NewFontFamily(familyname)
l.Push(newUserdataFontfamily(l, ff))
return 1
}
}

func checkFontfamily(l *lua.LState, argpos int) *document.FontFamily {
ud := l.CheckUserData(argpos)
if v, ok := ud.Value.(*document.FontFamily); ok {
return v
}
l.ArgError(argpos, "fontfamily expected")
return nil
}

func newUserdataFontfamily(l *lua.LState, ff *document.FontFamily) *lua.LUserData {
ud := l.NewUserData()
ud.Value = ff
mt := l.NewTypeMetatable(luaFontFamilyTypeName)
l.SetField(mt, "__index", l.NewFunction(fontfamilyIndex))
l.SetMetatable(ud, mt)
return ud
}

func fontfamilyIndex(l *lua.LState) int {
ff := checkFontfamily(l, 1)
switch l.CheckString(2) {
case "addmember":
l.Push(l.NewFunction(fontfamilyaddmember(ff)))
return 1
case "id":
l.Push(lua.LNumber(ff.ID))
return 1
}
return 0
}

func fontfamilyaddmember(p *document.FontFamily) lua.LGFunction {
return func(l *lua.LState) int {
fn := l.CheckTable(1)
nameValue := fn.RawGetString("name")
srcValue := fn.RawGetString("source")
if nameValue.Type() != lua.LTString {
return lerr(l, "the value of name must be a string")
}
if srcValue.Type() != lua.LTString {
return lerr(l, "the value of source must be a string")
}
fs := &document.FontSource{
Name: nameValue.String(),
Source: srcValue.String(),
}

weight := l.CheckInt(2)
stylestring := l.CheckString(3)
var style document.FontStyle
switch stylestring {
case "regular", "normal":
style = document.FontStyleNormal
case "italic":
style = document.FontStyleItalic
}
p.AddMember(fs, weight, style)
return 0
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/speedata/ets
go 1.17

require (
github.com/speedata/boxesandglue v0.0.0-20211129134402-170b3c4c07fb
github.com/speedata/boxesandglue v0.0.0-20211210131222-4caeb0a48247
github.com/speedata/optionparser v1.0.0
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9
go.uber.org/zap v1.19.1
Expand All @@ -14,7 +14,7 @@ require (
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/speedata/gofpdi v1.0.15 // indirect
github.com/speedata/gootf v0.0.0-20211102101716-062a89e3221e // indirect
github.com/speedata/gootf v0.0.0-20211207074951-f4a725db4ae3 // indirect
github.com/speedata/hyphenation v1.0.1 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/speedata/boxesandglue v0.0.0-20211111193940-f1fa41337424 h1:PldJT1hdoYf4akvjCw8aqeAk6OeNxyL3j6uxP0D99RM=
github.com/speedata/boxesandglue v0.0.0-20211111193940-f1fa41337424/go.mod h1:9BV4WsjEDggvn0iSppPMrFV//NB0wlOqX+XC9RImjh8=
github.com/speedata/boxesandglue v0.0.0-20211129134402-170b3c4c07fb h1:WyYV5b5hOKuTh1FnC6+jPluwhAeKjUcLfAyRYdxQx8I=
github.com/speedata/boxesandglue v0.0.0-20211129134402-170b3c4c07fb/go.mod h1:9BV4WsjEDggvn0iSppPMrFV//NB0wlOqX+XC9RImjh8=
github.com/speedata/boxesandglue v0.0.0-20211210131222-4caeb0a48247 h1:f2DAbeD4bLz1bERHjAP10FGC0Jdu6tc0uXBf1oYuZTw=
github.com/speedata/boxesandglue v0.0.0-20211210131222-4caeb0a48247/go.mod h1:GB+5S9HVM40UiBOjBWmNQyYr13gQublYhWZ3hjd1iNQ=
github.com/speedata/gofpdi v1.0.15 h1:PLFmzHTAdhgvCDMsWgvL/njaDFzTmvsYewqgw3UUEg4=
github.com/speedata/gofpdi v1.0.15/go.mod h1:aRZ6VjhFbzJWwCoHeKOdbUjImuf4GMVCJpwS9+bCYuM=
github.com/speedata/gootf v0.0.0-20211102101716-062a89e3221e h1:NHpP15eopboLD2vKal/0qQ3IuXYa+ohenSH9+2YjDKs=
github.com/speedata/gootf v0.0.0-20211102101716-062a89e3221e/go.mod h1:W9eH+4fle8N24D52HYisQ4hJcp050D2lkx6QZ0e4xUs=
github.com/speedata/gootf v0.0.0-20211207074951-f4a725db4ae3 h1:BZEXquUjQtJZlAUBp88bltxdGNczckwNtahNOJbAD2A=
github.com/speedata/gootf v0.0.0-20211207074951-f4a725db4ae3/go.mod h1:W9eH+4fle8N24D52HYisQ4hJcp050D2lkx6QZ0e4xUs=
github.com/speedata/hyphenation v1.0.1 h1:O9gKtVw2JvyiHybCYJwpB3zzHaeXEvDtXjiExZdm1TU=
github.com/speedata/hyphenation v1.0.1/go.mod h1:vwrKKvBvJWFll0sVZw99hyWS/+r4YlMI7MAYjnje0nM=
github.com/speedata/optionparser v1.0.0 h1:O9Bvoq20BQWMPq0b/id0kBT9oI0njNlHzY/wtQ3JA1o=
Expand Down
Loading

0 comments on commit 0ba193b

Please sign in to comment.