示例#1
0
//解析需要打印的内容
//需要解析子结构,深度遍历->广度遍历
//cur:解析树中的当前节点
//par:父节点
func parsePrintContent(cur dom.Element, par PrintInterface, p *PageSetting) PrintInterface {
	var i int
	var (
		n    dom.Node
		t    dom.Element
		z    PrintInterface
		prev PrintInterface
		te   dom.Text
	)
	f := cur.ChildNodes()

	for i = 0; i < len(f); i++ {
		n = f[i]
		z = nil
		switch n.Kind() {
		case dom.ElementKind:
			t = n.(dom.Element)
			utils.Info("find element %s", t.Name())

			switch t.Name() {
			case "Br":
				z = parseBrElement(t, p)
			case "Table":
				z = parseTableElement(t, p)
			case "Rect":
				z = parseRectElement(t, p)
			case "Tr":
				z = parseTrElement(t, p)
			case "Td":
				z = parseTdElement(t, p)
			case "Bold":
				z = parseBoldElement(t, p)
			case "Sup":
				z = parseSupElement(t, p)
			case "Sub":
				z = parseSubElement(t, p)
			case "Underline":
				z = parseUnderElement(t, p)
			case "Doubleheight":
				z = parseDoubleHeightElement(t, p)
			case "Doublewidth":
				z = parseDoubleWidthElement(t, p)

			default:
				utils.Error("can't find element name: %s", t.Name())
				continue
			}

		case dom.TextKind:
			te = n.(dom.Text)
			t := strings.TrimSpace(te.Data())
			//可能是换行符
			//if t == "" || strings.HasPrefix(t, "\r") || strings.HasPrefix(t, "\n") {
			//utils.Info("detect text line character,text is %s", te.Data())
			//continue
			// changed by jinsl 20151222
			if t == "" {
				utils.Info("detect text line character,text is null")
				continue
			} else if strings.HasPrefix(t, "\r") {
				utils.Info("detect text line character,text is \\r")
				continue
			} else if strings.HasPrefix(t, "\n") {
				utils.Info("detect text line character,text is \\n")
				continue
			} else {
				utils.Info("find text node,content %s", te.Data())
				z = parseTextNode(te, p)
			}

		default:
			utils.Debug("find default node %s ,kind %d", n.String(), n.Kind())
		}
		if z == nil {
			continue
		}
		if par != nil {
			par.AddChild(z)
			z.SetParent(par)
		}
		//加入到列表
		if prev == nil {
			prev = z
		}
	}
	return prev
}
示例#2
0
//解析Text元素,会嵌套其它元素
//解析完后会进行计算
func parseTextNode(cur dom.Text, p *PageSetting) *PrintText {
	z := NewPrintText(0, 0)
	z.SetName("Text")
	z.Con = cur.Data()
	return z
}