Selaa lähdekoodia

после мерджа таблиц и демо с таблицами

kpmy 10 vuotta sitten
vanhempi
commit
09c2729075
6 muutettua tiedostoa jossa 58 lisäystä ja 34 poistoa
  1. 23 1
      demo/report.go
  2. 3 2
      mappers/embed.go
  3. 2 13
      mappers/fmt.go
  4. 23 11
      mappers/para.go
  5. 5 5
      mappers/table.go
  6. 2 2
      odf_test.go

+ 23 - 1
demo/report.go

@@ -48,7 +48,29 @@ func report(suffix string, fm *mappers.Formatter) {
 	for i := 0; i < 5; i++ {
 		para()
 	}
-
+	{ //huge table
+		fm.SetAttr(new(attr.TextAttributes).Bold().Size(18))
+		fm.WritePara("TABLE 50x5")
+		fm.SetAttr(nil)
+		tm := &mappers.TableMapper{}
+		tm.ConnectTo(fm)
+		tm.Write("test", 50+1, 5) //50+header row
+		tt := tm.List["test"]
+		tm.Span(tt, 0, 0, 1, 5)
+		fm.SetAttr(new(attr.ParagraphAttributes).AlignCenter()).SetAttr(new(attr.TextAttributes).Bold())
+		tm.Pos(tt, 0, 0).WriteString("Header")
+		fm.SetAttr(nil)
+		for i := 1; i < 51; i++ {
+			for j := 0; j < 5; j++ {
+				if j == 0 {
+					fm.SetAttr(new(attr.TextAttributes).Bold())
+				} else {
+					fm.SetAttr(nil)
+				}
+				tm.Pos(tt, i, j).WriteString(strconv.Itoa(i * j))
+			}
+		}
+	}
 	{ //appendix
 		fm.RegisterFont("Courier New", "Courier New") // may not work in Linux/MacOS
 		fm.SetAttr(nil).SetAttr(new(attr.ParagraphAttributes).PageBreak())

+ 3 - 2
mappers/embed.go

@@ -47,8 +47,9 @@ func (d *Draw) MimeType() xmlns.Mime {
 
 //запись изображения
 func (d *Draw) WriteTo(fm *Formatter, name string, w, h interface{}) string {
-	fm.makePara()
-	wr := fm.m.NewWriter(fm.rider)
+	fm.defaultParaMapper.makePara()
+	wr := fm.m.NewWriter()
+	wr.Pos(fm.defaultParaMapper.rider.Pos())
 	wr.WritePos(New(draw.Frame))
 	wr.Attr(draw.Name, name).Attr(text.AnchorType, text.Paragraph).Attr(svg.Width, w).Attr(svg.Height, h)
 	wr.WritePos(New(draw.Image))

+ 2 - 13
mappers/fmt.go

@@ -2,12 +2,12 @@ package mappers
 
 import (
 	"github.com/kpmy/ypk/assert"
+	"github.com/kpmy/ypk/halt"
 	"odf/mappers/attr"
 	"odf/model"
 	"odf/xmlns"
 	"odf/xmlns/office"
 	"reflect"
-	"ypk/halt"
 )
 
 var New func(name model.LeafName) model.Leaf
@@ -52,17 +52,6 @@ func (f *Formatter) Init() {
 	f.ready = true
 }
 
-func (f *Formatter) makePara() {
-	if pos := f.rider.Pos(); pos.Name() != office.Text || pos.Name() == text.P {
-		f.rider.Pos(f.text)
-	}
-	f.rider.WritePos(New(text.P))
-		f.attr.Flush()
-	f.attr.Fit(text.P, func(a attr.Attributes) {
-		f.rider.Attr(text.StyleName, a.Name())
-	})
-}
-
 func (f *Formatter) WritePara(s string) {
 	assert.For(f.ready, 20)
 	f.defaultParaMapper.WritePara(s)
@@ -72,7 +61,7 @@ func (f *Formatter) WriteLn() {
 	f.WriteString("\n")
 }
 
-func (f *Formatter) WriteString(_s string) {
+func (f *Formatter) WriteString(s string) {
 	assert.For(f.ready, 20)
 	f.defaultParaMapper.WriteString(s)
 }

+ 23 - 11
mappers/para.go

@@ -1,9 +1,10 @@
 package mappers
 
 import (
+	"github.com/kpmy/ypk/assert"
+	"odf/mappers/attr"
 	"odf/model"
 	"odf/xmlns/text"
-	"ypk/assert"
 )
 
 type ParaMapper struct {
@@ -11,6 +12,17 @@ type ParaMapper struct {
 	rider model.Writer
 }
 
+func (p *ParaMapper) makePara() {
+	if pos := p.rider.Pos(); pos.Name() == text.P {
+		p.rider.Pos(p.fm.root)
+	}
+	p.rider.WritePos(New(text.P))
+	p.fm.attr.Flush()
+	p.fm.attr.Fit(text.P, func(a attr.Attributes) {
+		p.rider.Attr(text.StyleName, a.Name())
+	})
+}
+
 func (p *ParaMapper) ConnectTo(fm *Formatter) {
 	p.fm = fm
 	p.rider = fm.m.NewWriter()
@@ -22,10 +34,10 @@ func (p *ParaMapper) WritePara(s string) {
 		p.rider.Pos(pos.Parent())
 	}
 	p.rider.WritePos(New(text.P))
-	p.fm.writeAttr()
-	if a := p.fm.attr.Fit(text.P); a != nil {
+	p.fm.attr.Flush()
+	p.fm.attr.Fit(text.P, func(a attr.Attributes) {
 		p.rider.Attr(text.StyleName, a.Name())
-	}
+	})
 	p.WriteString(s)
 
 }
@@ -60,11 +72,11 @@ func (p *ParaMapper) WriteString(_s string) {
 	if p.rider.Pos().Name() != text.P {
 		p.WritePara(_s)
 	} else {
-		p.fm.writeAttr()
-		if a := p.fm.attr.Fit(text.Span); a != nil {
+		p.fm.attr.Flush()
+		p.fm.attr.Fit(text.Span, func(a attr.Attributes) {
 			p.rider.WritePos(New(text.Span))
 			p.rider.Attr(text.StyleName, a.Name())
-		}
+		})
 		s := []rune(_s)
 		br := false
 		for pos := 0; pos < len(s) && s[pos] != 0; {
@@ -76,9 +88,9 @@ func (p *ParaMapper) WriteString(_s string) {
 				p.rider.Write(New(text.LineBreak))
 			case '\r':
 				grow()
-				if p.fm.attr.Fit(text.Span) != nil {
+				p.fm.attr.Fit(text.Span, func(a attr.Attributes) {
 					p.rider.Pos(p.rider.Pos().Parent())
-				}
+				})
 				for pos = pos + 1; pos < len(s); pos++ {
 					buf = append(buf, s[pos])
 				}
@@ -101,9 +113,9 @@ func (p *ParaMapper) WriteString(_s string) {
 		}
 		if !br {
 			grow()
-			if p.fm.attr.Fit(text.Span) != nil {
+			p.fm.attr.Fit(text.Span, func(a attr.Attributes) {
 				p.rider.Pos(p.rider.Pos().Parent())
-			}
+			})
 		}
 	}
 }

+ 5 - 5
mappers/table.go

@@ -1,9 +1,9 @@
 package mappers
 
 import (
+	"github.com/kpmy/ypk/assert"
 	"odf/model"
 	"odf/xmlns/table"
-	"ypk/assert"
 )
 
 type Table struct {
@@ -35,7 +35,7 @@ func (t *TableMapper) ConnectTo(fm *Formatter) {
 func (t *TableMapper) Write(name string, rows, cols int) {
 	assert.For(t.Ready(), 20)
 	assert.For(name != "" && t.List[name] == nil, 21)
-	t.fm.writeAttr()
+	t.fm.attr.Flush()
 	this := &Table{Rows: rows, Columns: cols}
 	t.List[name] = this
 	wr := t.newWriter()
@@ -62,7 +62,7 @@ func (t *TableMapper) Write(name string, rows, cols int) {
 
 func (t *TableMapper) WriteRows(this *Table, rows int) {
 	assert.For(t.Ready(), 20)
-	t.fm.writeAttr()
+	t.fm.attr.Flush()
 	wr := t.newWriter()
 	for i := 0; i < rows; i++ {
 		wr.Pos(this.Root)
@@ -79,7 +79,7 @@ func (t *TableMapper) WriteRows(this *Table, rows int) {
 
 func (t *TableMapper) WriteColumns(this *Table, cols int) {
 	assert.For(t.Ready(), 20)
-	t.fm.writeAttr()
+	t.fm.attr.Flush()
 	wr := t.newWriter()
 	var last model.Leaf
 	if this.Columns > 0 {
@@ -99,7 +99,7 @@ func (t *TableMapper) WriteColumns(this *Table, cols int) {
 
 func (t *TableMapper) WriteCells(this *Table, _row int, cells int) {
 	assert.For(t.Ready(), 20)
-	t.fm.writeAttr()
+	t.fm.attr.Flush()
 	wr := t.newWriter()
 	row := this.rowCache[_row]
 	wr.Pos(row)

+ 2 - 2
odf_test.go

@@ -102,7 +102,7 @@ func TestTables(t *testing.T) {
 		fm.MimeType = xmlns.MimeText
 		fm.Init()
 		table(fm)
-		generators.Generate(m, output, fm.MimeType)
+		generators.GeneratePackage(m, nil, output, fm.MimeType)
 		assert.For(output.Close() == nil, 20)
 	}
 	{
@@ -113,7 +113,7 @@ func TestTables(t *testing.T) {
 		fm.MimeType = xmlns.MimeSpreadsheet
 		fm.Init()
 		table(fm)
-		generators.Generate(m, output, fm.MimeType)
+		generators.GeneratePackage(m, nil, output, fm.MimeType)
 		assert.For(output.Close() == nil, 20)
 	}
 }