Help us shape the future of Go! Take the 2026 Go Developer Survey

layout

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

Package layout 把与版式无关的流式文档模型排版为 OFD 固定版式页面。

Markdown、DOCX 等流式格式的导入器只负责把源格式解析为 Document, 断行、分页、字体选择和 OFD 生成由本包统一处理。当前实现以 Markdown 常见元素(段落、标题、列表、引用、代码、表格、图片)为范围。

Index

Constants

View Source
const (
	StyleSize = uint32(1 << iota)
	StyleLineHeight
	StyleSpaceBefore
	StyleSpaceAfter
	StyleFirstLineIndent
)

Variables

This section is empty.

Functions

func Build

func Build(doc *Document, opts Options) (*creator.Document, error)

Build 把流式文档排版为 OFD 文档。返回的文档可继续补充元数据后写入。

func Compose added in v0.1.6

func Compose(ctx context.Context, source []FlowBlock, opts FlowOptions) (*creator.Document, error)

Compose 先排版,再按最终物理页码回填目录与引用;连续两轮目标页码一致才交付。 字体来源仅在首轮读取,此后的排版使用同一份字体字节快照。

func ValidateFlowText added in v0.1.6

func ValidateFlowText(text string) error

ValidateFlowText 检查 UTF-8 与控制字符,供公开创作入口与清单入口复用。

Types

type Align

type Align int

Align 是单元格水平对齐方式。

const (
	// AlignLeft 左对齐。
	AlignLeft Align = iota
	// AlignCenter 居中。
	AlignCenter
	// AlignRight 右对齐。
	AlignRight
)

type Block

type Block struct {
	Kind Kind
	// Level 是标题级别 1-6。
	Level int
	// Marker 是列表标记,如 "•" 或 "1."。
	Marker string
	// Indent 是缩进层级,用于列表和引用。
	Indent int
	// Inlines 是行内内容。
	Inlines []Inline
	// Code 是代码块纯文本。
	Code string
	// CodeLang 是围栏代码块的语言标记(“` 后的标识符),为空表示无语言。
	CodeLang string
	// Image 是块级图片。
	Image *Image
	// Table 是表格。
	Table *Table
}

Block 是块级元素。

type Cell

type Cell []Inline

Cell 是一个表格单元格的行内内容。

type Colophon added in v0.1.3

type Colophon struct {
	// Cc 是抄送机关,如 "省委办公厅,省政府办公厅。"。
	Cc string
	// IssuedBy 是印发机关,如 "××省档案局办公室"。
	IssuedBy string
	// IssuedDate 是印发日期,如 "2026年9月19日"。
	IssuedDate string
	// Size 是版记字号(磅);0 时使用默认值 14,对应四号。
	Size float64
}

Colophon 是公文末页的版记配置,为空则不渲染。 版记跟随正文流排在最后一面,含抄送与印发机关/日期两条,行间用分隔线。

type Document

type Document struct {
	// Title 是文档标题。
	Title string
	// Letterhead 是首页红色版头;为空则不渲染。设置后按 GB/T 9704-2012 编排。
	Letterhead *Letterhead
	// Sign 是落款(署名与成文日期);为空则不渲染。
	Sign *Signature
	// Footer 是页脚页码;为空则不渲染。
	Footer *Footer
	// Colophon 是末页版记;为空则不渲染。
	Colophon *Colophon
	// Blocks 是按顺序排列的块级元素。
	Blocks []Block
}

Document 是与版式无关的流式文档。

type FlowBlock added in v0.1.6

type FlowBlock struct {
	ID              string
	Kind            string
	Text            string
	Runs            []Inline
	Level           int
	Marker          string
	Style           *TextStyle
	KeepWithNext    bool
	KeepTogether    bool
	PageBreakBefore bool
	PageBreakAfter  bool
	Orphans         int
	Widows          int
	Image           *FlowImage
	Table           *FlowTable
	Fixed           *FlowFixed
	TOC             *FlowTOC
	Section         *FlowSection
	// contains filtered or unexported fields
}

FlowBlock 是顺序创作的内容块。Kind 为 paragraph、heading、recipient、list_item、image、table、fixed、toc、section、page_break 或 column_break。

type FlowCellBorders added in v0.1.6

type FlowCellBorders struct{ Top, Right, Bottom, Left *FlowTableBorder }

FlowCellBorders 的空边继承默认边框,Width 为零时隐藏该侧边框。

type FlowCellStyle added in v0.1.6

type FlowCellStyle struct {
	Fill          *creator.Color
	Padding       *FlowMargins
	VerticalAlign string
	Borders       *FlowCellBorders
}

FlowCellStyle 定义单元格的外观,nil 字段继承表格默认设置。

type FlowColumns added in v0.1.6

type FlowColumns struct {
	Count int
	Gap   float64
	// Balance 启用节末页栏高平衡,含显式换栏的末页保持原布局。
	Balance bool
}

FlowColumns 定义等宽分栏,Count 缺省为 1,Gap 为毫米单位的栏间距。

type FlowFixed added in v0.1.6

type FlowFixed struct {
	Width, Height float64
	Align         string
	Items         []creator.Item
	Caption       string
	Numbered      bool
	CaptionPrefix string
}

FlowFixed 是作为整体参与分页的固定区域,内部图元使用左上角为原点的毫米坐标。

type FlowFontFamily added in v0.1.6

type FlowFontFamily struct{ Regular, Bold, Italic, BoldItalic string }

FlowFontFamily 关联同一字体族的四种实际字体资源。

type FlowFootnote added in v0.1.6

type FlowFootnote struct {
	ID, Text string
	Runs     []Inline
	Style    *TextStyle
}

FlowFootnote 是与引用行同页的页底注释。

type FlowImage added in v0.1.6

type FlowImage struct {
	Data          []byte
	Width         float64
	Height        float64
	Caption       string
	Numbered      bool
	CaptionPrefix string
}

FlowImage 是图片及图注,尺寸为毫米;未给尺寸时按 96 DPI 推算并限制在版心内。

type FlowMargins added in v0.1.6

type FlowMargins struct{ Top, Right, Bottom, Left float64 }

FlowMargins 是毫米单位的页边距,指针用于区分继承和显式的零边距。

type FlowNamedStyle added in v0.1.6

type FlowNamedStyle struct {
	Name  string
	Style TextStyle
}

FlowNamedStyle 注册可复用的文字样式。

type FlowOfficial added in v0.1.6

type FlowOfficial struct {
	Fonts      FlowOfficialFonts
	Letterhead *FlowOfficialLetterhead
	Signature  *FlowOfficialSignature
	Colophon   *FlowOfficialColophon
}

type FlowOfficialColophon added in v0.1.6

type FlowOfficialColophon struct{ Cc, IssuedBy, IssuedDate string }

type FlowOfficialFonts added in v0.1.6

type FlowOfficialFonts struct{ Body, Title, Hei, Kai, Song string }

type FlowOfficialLetterhead added in v0.1.6

type FlowOfficialLetterhead struct {
	Org, DocNo, Signatory, SerialNo, Security, Urgency string
	OrgSize                                            float64
}

type FlowOfficialSignature added in v0.1.6

type FlowOfficialSignature struct{ Org, Date string }

type FlowOptions added in v0.1.6

type FlowOptions struct {
	PageSize         creator.PageSize
	MarginTop        float64
	MarginRight      float64
	MarginBottom     float64
	MarginLeft       float64
	Theme            Theme
	Official         *FlowOfficial
	Columns          FlowColumns
	Styles           []FlowNamedStyle
	Footnotes        []FlowFootnote
	FootnoteStyle    *TextStyle
	Fonts            []creator.Font
	FontFaces        map[string]int
	FontFamilies     []FlowFontFamily
	FallbackFonts    []string
	BoldFont         string
	ItalicFont       string
	BoldItalicFont   string
	PageNumbers      bool
	Header           string
	Footer           string
	GenerateOutlines bool

	ReferencePageNumbers string
	// contains filtered or unexported fields
}

FlowOptions 控制流式创作的页面、主题与字体。页面长度为毫米。

type FlowSection added in v0.1.6

type FlowSection struct {
	PageSize            *creator.PageSize
	Margins             *FlowMargins
	Columns             *FlowColumns
	Header              *string
	Footer              *string
	PageNumbers         *bool
	StartPageNumber     *int
	PageNumberStyle     string
	PageNumberPrefix    *string
	FirstPageHeader     *string
	FirstPageFooter     *string
	HideFirstPageNumber bool
}

FlowSection 覆盖后续页面设置,分页后只以标准物理页、文字和路径输出。

type FlowTOC added in v0.1.6

type FlowTOC struct {
	Title    string
	MaxLevel int
	Source   string
}

FlowTOC 在当前位置生成标题或图表目录,标题目录的 MaxLevel 缺省为 3。

type FlowTable added in v0.1.6

type FlowTable struct {
	// SplitRows 按完整文字行续排普通及 ColSpan 单元格,正文 RowSpan 不支持。
	SplitRows    bool
	Header       []string
	Rows         [][]string
	ColumnWidths []float64
	// Grid 与 Header/Rows 互斥,合并单元格占据的位置无需额外占位。
	Grid               [][]FlowTableCell
	HeaderRows         int
	Columns            []FlowTableColumn
	CellStyle          *FlowCellStyle
	StripeFill         *creator.Color
	Caption            string
	Numbered           bool
	CaptionPrefix      string
	RepeatCaption      bool
	ContinuationSuffix string
}

FlowTable 是简单网格表格,单元格支持自动换行,Header 在续页重复。

type FlowTableBorder added in v0.1.6

type FlowTableBorder struct {
	Width float64
	Color *creator.Color
}

FlowTableBorder 是位于单元格内部的实色边框。

type FlowTableCell added in v0.1.6

type FlowTableCell struct {
	Text       string
	Runs       []Inline
	ColSpan    int
	RowSpan    int
	Style      *TextStyle
	Appearance *FlowCellStyle
}

FlowTableCell 是支持局部样式、链接和跨行跨列的单元格。

type FlowTableColumn added in v0.1.6

type FlowTableColumn struct {
	Width, MinWidth, MaxWidth, Weight float64
	NoWrap                            bool
	Align                             string
}

FlowTableColumn 配置固定宽或按内容分配的列宽,长度为毫米,Weight 为剩余宽度比例。

type Footer struct {
	// PageNumber 指示是否在每页版心下边缘之下渲染页码。
	PageNumber bool
	// Size 是页码字号(磅);0 时使用默认值 14,对应四号。
	Size float64
}

Footer 是公文页脚的页码配置,为空则不渲染页码。

type Image

type Image struct {
	// Source 是原始地址,用于诊断信息。
	Source string
	// Alt 是替代文本。
	Alt string
	// Data 是图片二进制数据。
	Data []byte
	// Format 是图片格式,如 PNG、JPEG。
	Format string
	// PixelWidth 和 PixelHeight 是图片像素尺寸。
	PixelWidth  int
	PixelHeight int
}

Image 是块级图片。

type Inline

type Inline struct {
	Text string
	// Font 是流式创作时显式选择的字体资源名,空值继承段落样式。
	Font   string
	Bold   bool
	Italic bool
	Code   bool
	Strike bool
	// Link 为真时按链接着色。
	Link bool
	// Href 是点击目标,落到图元的 CLICK 动作上。为空时只是着色、不可点击——
	// Markdown 的空目标链接(如 `[文字]()`)属于这种,着色仍要保留。
	Href string
	// Target 是文内内容 ID;PageRef 按配置填入最终物理页码或节内页码。
	Target  string
	PageRef string
	// CaptionRef 显示编号图表的标签并跳转至内容起点。
	CaptionRef string
	// TitleRef 显示目标章节标题或完整图表题注。
	TitleRef string
	// Footnote 指向页底注释定义,显示独立编号标记。
	Footnote string
}

Inline 是行内文本的一段,具有相同的字符样式。 Text 为 "\n" 时表示硬换行。

type Kind

type Kind int

Kind 是块级元素的类型。

const (
	// KindParagraph 是普通段落。
	KindParagraph Kind = iota
	// KindHeading 是标题。
	KindHeading
	// KindCode 是代码块。
	KindCode
	// KindQuote 是引用块。
	KindQuote
	// KindListItem 是列表项。
	KindListItem
	// KindThematicBreak 是分隔线。
	KindThematicBreak
	// KindTable 是表格。
	KindTable
	// KindImage 是块级图片。
	KindImage
)

type Letterhead added in v0.1.3

type Letterhead struct {
	// Org 是发文机关标志,如 "××省档案局文件"。
	Org string
	// DocNo 是发文字号,如 "×档发〔2026〕1号"。
	DocNo string
	// Signatory 是签发人姓名;非空时发文字号左移、签发人右移(上行文版式)。
	Signatory string
	// SerialNo 是份号,如 "000001";非空时在版心左上角第一行显示。
	SerialNo string
	// Security 是密级和保密期限,如 "绝密★5年";非空时在份号下方显示。
	Security string
	// Urgency 是紧急程度,如 "特急";非空时在密级下方显示。
	Urgency string
	// Height 是版头区最小高度(毫米),仅当内容较短时用于撑高版头区;0 忽略。
	Height float64
	// OrgSize 是机关标志字号(磅);0 时使用默认值 56。
	OrgSize float64
	// DocNoSize 是发文字号、签发人与涉密标记字号(磅);0 时使用默认值 16,对应三号。
	DocNoSize float64
}

Letterhead 是公文首页的红色版头配置,对应 GB/T 9704-2012 的版头要素。 机关标志为红色、加粗并水平居中,上边缘距版心上边缘 35mm; 下行文/平行文发文字号在机关标志下空二行居中,上行文设置了 Signatory 时 发文字号居左空一字、签发人姓名居右空一字(同一行);红色分隔线印在 发文字号之下 4mm。份号、密级和紧急程度依次顶格堆叠在版心左上角。 版头只在首页渲染。

type Options

type Options struct {
	// PageWidth 和 PageHeight 是页面物理尺寸。
	PageWidth  float64
	PageHeight float64
	// MarginTop、MarginRight、MarginBottom、MarginLeft 是页边距。
	MarginTop    float64
	MarginRight  float64
	MarginBottom float64
	MarginLeft   float64
	// BodySize 是正文字号,MonoSize 是代码字号。
	BodySize float64
	MonoSize float64
	// LineHeight 是行高倍率。
	LineHeight float64
	// CodeLineHeight 是代码块的行高倍率;小于 LineHeight 可以让代码更紧凑。
	CodeLineHeight float64
	// CodePaddingX 和 CodePaddingY 是代码面板内边距,文字与面板边缘的留白。
	CodePaddingX float64
	CodePaddingY float64
	// CodeRadius 是代码面板圆角半径(毫米);不超过面板短边的一半。
	CodeRadius float64
	// CodeLabelSize 是语言标签字号(磅);0 时不绘制语言标签。
	CodeLabelSize float64
	// CodeLabelGap 是语言标签与首行代码之间的间距(毫米)。
	CodeLabelGap float64
	// InlineCodePaddingX 和 InlineCodePaddingY 是行内代码底色的内边距。
	InlineCodePaddingX float64
	InlineCodePaddingY float64
	// BlockSpacing 是块间距,单位为正文行高的倍率。
	BlockSpacing float64
	// BodyFamily 和 MonoFamily 是逻辑字体族名。
	BodyFamily string
	MonoFamily string
	// HeiFamily、KaiFamily、TitleFamily、SongFamily 是公文版式专用字族名,
	// 分别对应黑体、楷体、小标宋(文件标题/发文机关标志)与宋体(页码);
	// 为空时回退到 BodyFamily。
	HeiFamily   string
	KaiFamily   string
	TitleFamily string
	SongFamily  string
}

Options 控制页面尺寸、字体与间距。长度单位为毫米,字号单位为磅。

func DefaultOptions

func DefaultOptions() Options

DefaultOptions 返回 A4、20 毫米页边距、12 磅正文的默认排版参数。

func GBTOptions added in v0.1.3

func GBTOptions() Options

GBTOptions 返回 A4 公文版式(GB/T 9704-2012)的排版参数: 天头 37mm、订口 28mm、右/下白边 26/35mm,版心 156mm×225mm,正文三号仿宋、 每面 22 行。字体族按标准引用:正文仿宋、标题小标宋、序号黑体/楷体、页码宋体; OFD 以逻辑字体族名引用,阅读器缺字时回退到自身字体。

type Signature added in v0.1.3

type Signature struct {
	// Org 是发文机关署名。
	Org string
	// Date 是成文日期,如 "2026年9月19日"。
	Date string
	// Size 是落款字号(磅);0 时使用默认值 16,对应三号。
	Size float64
}

Signature 是不加盖印章公文的落款(发文机关署名与成文日期)配置, 对应 GB/T 9704-2012 的 7.3.5.2:署名在正文(或附件说明)下空一行、右空二字; 成文日期在署名下一行,首字比署名首字右移二字。

type Table

type Table struct {
	Header []Cell
	Rows   [][]Cell
	Align  []Align
	// Landscape 强制横排表格;为空时按列宽是否超出版心自动判定。
	Landscape bool
}

Table 是表格的与版式无关表示。

type TextStyle added in v0.1.6

type TextStyle struct {
	BasedOn         string
	Set             uint32
	Font            string
	Size            float64
	LineHeight      float64
	SpaceBefore     float64
	SpaceAfter      float64
	FirstLineIndent float64
	Align           string
	Color           *creator.Color
}

TextStyle 是创作主题中的文字样式。字号为磅,段间距与缩进为毫米。

type Theme added in v0.1.6

type Theme struct {
	Body        TextStyle
	Headings    [6]TextStyle
	Caption     TextStyle
	Table       TextStyle
	BorderColor *creator.Color
	HeaderFill  *creator.Color
}

Theme 定义正文、各级标题、图注与表格的印刷样式。

func Bootstrap5Theme added in v0.1.6

func Bootstrap5Theme() Theme

Bootstrap5Theme 按 Bootstrap 5.3 的 H1–H6、正文及 figure-caption 定义印刷样式。 根字号按 16px = 12pt 映射,保留标题 1.2 倍行高与段后 1rem;Medium 使用 Go Bold 近似。 来源:https://github.com/twbs/bootstrap/blob/v5.3.8/scss/_variables.scss

func CarbonTheme added in v0.1.6

func CarbonTheme() Theme

CarbonTheme 将 Carbon 的静态 heading-06 至 heading-01、body-02、body-compact-01 与 label-01 映射到印刷样式。 按 96px = 72pt 映射字号;Light 使用 Go Regular,Semibold 使用 Go Bold,段间距为印刷预设。 角色来源:https://github.com/carbon-design-system/carbon/blob/main/packages/type/scss/_styles.scss

func Fluent2Theme added in v0.1.6

func Fluent2Theme() Theme

Fluent2Theme 将 Fluent 2 Web 的字体角色和行高比例用于印刷排版。 按 96px = 72pt 映射字号,段间距为印刷预设;内置字体使用 Go Regular/Bold。 角色来源:https://fluent2.microsoft.design/typography#type-ramp

func GBT9704Theme added in v0.1.6

func GBT9704Theme(fonts FlowOfficialFonts) Theme

GBT9704Theme 只供 GBT9704Options 组合页面、字体及公文装饰,不独立充当通用 UI 主题。

func Material2Theme added in v0.1.6

func Material2Theme() Theme

Material2Theme 将 Material 2 Web 的 Headline 1–6、Body 和 Caption 角色用于印刷排版。 按 96px = 72pt 映射字号;Light 使用内置 Go Regular,Medium 使用 Go Bold,段间距与配色为印刷预设。 角色来源:https://github.com/material-components/material-components-web/blob/v14.0.0/packages/mdc-typography/_typography.scss

func Material3Theme added in v0.1.6

func Material3Theme() Theme

Material3Theme 将 Material 3 Web 的 Headline、Title 和 Body 角色用于印刷排版。 按 96px = 72pt 映射字号,段间距为印刷预设;Medium 字重使用内置 Go Bold 字体面。 角色来源:https://github.com/material-components/material-web/blob/main/tokens/versions/v0_192/_md-sys-typescale.scss

func MaterialTheme added in v0.1.6

func MaterialTheme() Theme

MaterialTheme 返回适合 A4 报告的主题,借鉴 Material 的字号层级与间距节律。 这里的磅值是印刷预设,不是 dp/sp 到磅的等价换算。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL