Browse Source

изменил механизм проверки наличия аттрибутов для заданного узла

kpmy 10 năm trước cách đây
mục cha
commit
2a77e514af
3 tập tin đã thay đổi với 16 bổ sung19 xóa
  1. 2 0
      README.md
  2. 4 3
      mappers/attr.go
  3. 10 16
      mappers/fmt.go

+ 2 - 0
README.md

@@ -1,2 +1,4 @@
 # odf
 [Open Document Format](http://docs.oasis-open.org/office/v1.0) (ODF) generator library for Go.
+
+Формирование документа в формате Open Document (ODF) для языка Go.

+ 4 - 3
mappers/attr.go

@@ -56,13 +56,14 @@ func (a *Attr) Init(m model.Model) {
 	a.reset()
 }
 
-func (a *Attr) Fit(n model.LeafName) (ret attr.Attributes) {
+func (a *Attr) Fit(n model.LeafName, callback func(a attr.Attributes)) {
 	fit := make(map[model.LeafName]attr.Attributes)
 	for _, v := range a.current {
 		fit[v.Fit()] = v
 	}
-	ret = fit[n]
-	return
+	if a := fit[n]; a != nil {
+		callback(a)
+	}
 }
 
 func (a *Attr) RegisterFont(name, fontface string) {

+ 10 - 16
mappers/fmt.go

@@ -45,21 +45,15 @@ func (f *Formatter) Init() {
 	f.ready = true
 }
 
-func (f *Formatter) writeAttr() {
-	if !f.attr.stored {
-		f.attr.Flush()
-	}
-}
-
 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.writeAttr()
-	if a := f.attr.Fit(text.P); a != nil {
+	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) {
@@ -100,11 +94,11 @@ func (f *Formatter) WriteString(_s string) {
 	if f.rider.Pos().Name() != text.P {
 		f.WritePara(_s)
 	} else {
-		f.writeAttr()
-		if a := f.attr.Fit(text.Span); a != nil {
+		f.attr.Flush()
+		f.attr.Fit(text.Span, func(a attr.Attributes) {
 			f.rider.WritePos(New(text.Span))
 			f.rider.Attr(text.StyleName, a.Name())
-		}
+		})
 		s := []rune(_s)
 		br := false
 		for pos := 0; pos < len(s) && s[pos] != 0; {
@@ -116,9 +110,9 @@ func (f *Formatter) WriteString(_s string) {
 				f.rider.Write(New(text.LineBreak))
 			case '\r':
 				grow()
-				if f.attr.Fit(text.Span) != nil {
+				f.attr.Fit(text.Span, func(a attr.Attributes) {
 					f.rider.Pos(f.rider.Pos().Parent())
-				}
+				})
 				for pos = pos + 1; pos < len(s); pos++ {
 					buf = append(buf, s[pos])
 				}
@@ -141,9 +135,9 @@ func (f *Formatter) WriteString(_s string) {
 		}
 		if !br {
 			grow()
-			if f.attr.Fit(text.Span) != nil {
+			f.attr.Fit(text.Span, func(attr.Attributes) {
 				f.rider.Pos(f.rider.Pos().Parent())
-			}
+			})
 		}
 	}
 }