Переглянути джерело

доработки и исправления для демо
доработки демо приложения

kpmy 10 роки тому
батько
коміт
161859e7ee
5 змінених файлів з 92 додано та 8 видалено
  1. BIN
      demo/10414104.png
  2. 75 5
      demo/report.go
  3. 5 0
      mappers/attr/para.go
  4. 6 2
      mappers/attr/text.go
  5. 6 1
      mappers/fmt.go

BIN
demo/10414104.png


+ 75 - 5
demo/report.go

@@ -1,27 +1,97 @@
 package main
 
 import (
+	"bytes"
+	"github.com/kpmy/golorem"
 	"github.com/kpmy/ypk/assert"
+	"io"
+	"math/rand"
 	"odf/generators"
 	"odf/mappers"
+	"odf/mappers/attr"
 	"odf/model"
 	_ "odf/model/stub" // необходимо
 	"odf/xmlns"
 	"os"
 	"runtime"
 	"strconv"
+	"strings"
 	"sync"
+	"time"
 )
 
-func do(wg *sync.WaitGroup, suffix string) {
-	assert.For(suffix != "", 20)
+func report(suffix string, fm *mappers.Formatter) {
+	{ //first page
+		fm.WriteString("\n\n\n\n\n\n\n\n\n\n")
+		fm.SetAttr(new(attr.TextAttributes).Size(32).Bold()).SetAttr(new(attr.ParagraphAttributes).AlignCenter())
+		fm.WritePara("Periodic report")
+		fm.SetAttr(nil).SetAttr(new(attr.TextAttributes).Italic())
+		fm.WritePara("Report:\t")
+		fm.SetAttr(new(attr.TextAttributes).Bold())
+		fm.WriteString(suffix + "\n")
+		fm.SetAttr(new(attr.TextAttributes).Italic())
+		fm.WriteString("Date:\t")
+		fm.SetAttr(new(attr.TextAttributes).Bold())
+		fm.WriteString(time.Now().String())
+		fm.SetAttr(new(attr.ParagraphAttributes).PageBreak())
+		fm.WritePara("")
+		fm.SetAttr(nil)
+	}
+	para := func() {
+		fm.SetAttr(new(attr.TextAttributes).Bold().Size(18))
+		fm.WritePara(strings.ToUpper(lorem.Word(5, 15)))
+		fm.WriteLn()
+		fm.SetAttr(nil)
+		para := lorem.Paragraph(5, 10)
+		fm.WritePara("\t" + para + "\n\n")
+	}
+	for i := 0; i < 5; i++ {
+		para()
+	}
+
+	{ //appendix
+		fm.RegisterFont("Courier New", "Courier New") // may not work in Linux/MacOS
+		fm.SetAttr(nil).SetAttr(new(attr.ParagraphAttributes).PageBreak())
+		fm.SetAttr(new(attr.TextAttributes).Size(18).Bold())
+		fm.WritePara("Appendix A.\nListing of report.go")
+		fm.WriteLn()
+		fm.SetAttr(nil).SetAttr(new(attr.TextAttributes).FontFace("Courier New").Size(6))
+		if f, err := os.Open("report.go"); err == nil {
+			defer f.Close()
+			buf := bytes.NewBuffer(nil)
+			io.Copy(buf, f)
+			fm.WritePara(string(buf.Bytes()))
+		} else {
+			fm.WritePara("File not found.")
+		}
+	}
+}
+
+func do(wg *sync.WaitGroup) {
+	src := rand.New(rand.NewSource(time.Now().UnixNano() + rand.Int63()))
+	suffix := strconv.Itoa(src.Int())
 	output, _ := os.OpenFile("test-"+suffix+".odf", os.O_CREATE|os.O_WRONLY, 0666)
 	m := model.ModelFactory()
 	fm := &mappers.Formatter{}
 	fm.ConnectTo(m)
 	fm.MimeType = xmlns.MimeText
 	fm.Init()
-	generators.Generate(m, nil, output, fm.MimeType)
+	embed := make(map[string]generators.Embeddable)
+	{
+		const ImagePng xmlns.Mime = "image/png"
+		img, _ := os.Open("10414104.png")
+		d := mappers.NewDraw(img, ImagePng)
+		fm.SetAttr(new(attr.ParagraphAttributes).AlignRight())
+		url := d.WriteTo(fm, "Two Gophers", 4.0, 4.0) //magic? real size of `.png` in cm
+		embed[url] = d
+	}
+	fm.SetAttr(new(attr.TextAttributes).Bold())
+	fm.WriteString("\nSo Strange inc.")
+	fm.WriteString("\n" + lorem.Email())
+	fm.WriteString("\n" + lorem.Url())
+	fm.SetAttr(nil)
+	report(suffix, fm)
+	generators.GeneratePackage(m, embed, output, fm.MimeType)
 	assert.For(output.Close() == nil, 20)
 	wg.Done()
 }
@@ -29,8 +99,8 @@ func do(wg *sync.WaitGroup, suffix string) {
 func main() {
 	runtime.GOMAXPROCS(4)
 	wg := &sync.WaitGroup{}
-	for i := 0; i < 100; i++ {
-		go do(wg, strconv.Itoa(i))
+	for i := 0; i < 10; i++ {
+		go do(wg)
 		wg.Add(1)
 	}
 	wg.Wait()

+ 5 - 0
mappers/attr/para.go

@@ -33,6 +33,11 @@ func (p *ParagraphAttributes) AlignRight() *ParagraphAttributes {
 	return p
 }
 
+func (p *ParagraphAttributes) AlignCenter() *ParagraphAttributes {
+	p.put(fo.TextAlign, fo.Center, nil)
+	return p
+}
+
 func (p *ParagraphAttributes) PageBreak() *ParagraphAttributes {
 	p.put(fo.BreakBefore, true, func(v value) {
 		if x := v.data.(bool); x {

+ 6 - 2
mappers/attr/text.go

@@ -30,8 +30,12 @@ func (t *TextAttributes) Fit() model.LeafName { return text.Span }
 func (t *TextAttributes) Write(wr model.Writer) {
 	wr.Attr(style.Family, style.FamilyText)
 	wr.WritePos(New(style.TextProperties))
-	wr.Attr(style.FontName, t.fontFace)
-	wr.Attr(fo.FontSize, t.size)
+	if t.fontFace != "" {
+		wr.Attr(style.FontName, t.fontFace)
+	}
+	if t.size != 0 {
+		wr.Attr(fo.FontSize, t.size)
+	}
 	wr.Attr(fo.Color, t.col)
 	if t.bold {
 		wr.Attr(fo.FontWeight, fo.Bold)

+ 6 - 1
mappers/fmt.go

@@ -63,6 +63,10 @@ func (f *Formatter) WritePara(s string) {
 	f.WriteString(s)
 }
 
+func (f *Formatter) WriteLn() {
+	f.WriteString("\n")
+}
+
 func (f *Formatter) WriteString(_s string) {
 	assert.For(f.ready, 20)
 
@@ -142,7 +146,7 @@ func (f *Formatter) WriteString(_s string) {
 	}
 }
 
-func (f *Formatter) SetAttr(a attr.Attributes) {
+func (f *Formatter) SetAttr(a attr.Attributes) *Formatter {
 	assert.For(f.ready, 20)
 	if a != nil {
 		n := reflect.TypeOf(a).String()
@@ -158,6 +162,7 @@ func (f *Formatter) SetAttr(a attr.Attributes) {
 	} else {
 		f.attr.reset()
 	}
+	return f
 }
 
 func (f *Formatter) RegisterFont(name, fontface string) {