flow

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 41 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

流式排版实现

公开入口为 pkg/creator/flow,Markdown 和清单导入均通过该入口使用本包。 本包将顺序内容转换为 creator.Document,不负责 CLI、清单解析或 XML/ZIP 写出。

文件 职责
model.go、options.go 内容与排版配置;类型无需 Flow 前缀
compose.go、engine.go 多轮重排编排、页面游标及运行态
geometry.go 独立于主题和字体选项的页面几何
block.go、paragraph.go、segment.go 内容准备、UAX #14 分段与文字排版
pagination.go、column.go、balance.go、keep.go 分页、分栏与跟随约束
font.go、measure.go 嵌入字体度量与逻辑字体回退
table*.go、callout*.go、official.go 表格、提示框与公文
navigation.go、catalog.go、footnote.go 链接、目录与脚注

输入 Block 与准备后的 preparedBlock、输入 Section 与运行态 sectionState 分别命名,避免把排版状态当作公开创作模型。公开 API 的模型转换层仍保留在 pkg/creator/flow,不使用类型别名暴露内部状态。

旧 internal/layout.Document / Build 路径已移除;其仍适用的断行、链接及背景对齐约束 由现用入口的回归测试保护。测试及基准:

go test ./internal/layout/... ./pkg/creator/flow ./pkg/converter/import/markdown -count=1
go test ./internal/manifest ./cmd/ofd-creator ./pkg/creator -count=1
go test ./internal/layout/flow -run '^$' -bench BenchmarkComposeReport -benchmem

Documentation

Overview

Package flow 将顺序内容排版为 OFD 固定版式页面。

本包负责字体度量、UAX #14 断行、分栏分页、表格和导航重排; 对外创作 API 位于 pkg/creator/flow,格式导入器统一经该公开入口使用排版。

Index

Constants

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

Variables

This section is empty.

Functions

func Compose

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

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

func ValidateText

func ValidateText(text string) error

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

Types

type Block

type Block struct {
	ID              string
	Kind            string
	Text            string
	Runs            []Run
	Level           int
	Marker          string
	Indent          float64
	LeftBorderColor *creator.Color
	LeadingIcon     *Icon
	CodeLang        string
	Style           *TextStyle
	KeepWithNext    bool
	KeepTogether    bool
	PageBreakBefore bool
	PageBreakAfter  bool
	Orphans         int
	Widows          int
	Image           *Image
	Table           *Table
	Fixed           *Fixed
	TOC             *TOC
	Section         *Section
	Callout         *Callout
	// contains filtered or unexported fields
}

Block 是顺序创作的内容块,另支持 Markdown 的 quote、code 和 thematic_break。

type Callout

type Callout struct {
	Variant, Title         string
	Text                   string
	Runs                   []Run
	ShowIcon               bool
	Blocks                 []Block
	Fill, AccentColor      *creator.Color
	BorderColor            *creator.Color
	Appearance, Pagination string
	RepeatTitle            bool
	ContinuationSuffix     string
	// contains filtered or unexported fields
}

Callout 是流式提示容器,容纳段落、列表与代码。

type CalloutPalette

type CalloutPalette struct {
	AccentColor, Fill, BorderColor *creator.Color
}

CalloutPalette 是单种提示语义的文档配色。

type CalloutTheme

type CalloutTheme struct {
	Neutral, Info, Warning, Danger, Success CalloutPalette
}

CalloutTheme 保存五种提示语义的配色。

type CellBorders

type CellBorders struct{ Top, Right, Bottom, Left *TableBorder }

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

type CellStyle

type CellStyle struct {
	Fill          *creator.Color
	Padding       *Margins
	VerticalAlign string
	Borders       *CellBorders
}

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

type Columns

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

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

type Fixed

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

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

type FontFamily

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

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

type Footnote

type Footnote struct {
	ID, Text string
	Runs     []Run
	Style    *TextStyle
}

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

type Icon

type Icon struct {
	Width, Height float64
	Data          string
	Rule          string
}

Icon 是首行前置的矢量图标,路径使用原始视口坐标。

type Image

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

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

type Margins

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

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

type NamedStyle

type NamedStyle struct {
	Name  string
	Style TextStyle
}

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

type Official

type Official struct {
	Fonts          OfficialFonts
	Letterhead     *OfficialLetterhead
	Signature      *OfficialSignature
	Colophon       *OfficialColophon
	PageNumberSize float64
}

type OfficialColophon

type OfficialColophon struct {
	Cc, IssuedBy, IssuedDate string
	Size                     float64
}

type OfficialFonts

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

type OfficialLetterhead

type OfficialLetterhead struct {
	Org, DocNo, Signatory, SerialNo, Security, Urgency string
	OrgSize                                            float64
	Height, DocNoSize                                  float64
}

type OfficialSignature

type OfficialSignature struct {
	Org, Date string
	Size      float64
}

type Options

type Options struct {
	PageSize          creator.PageSize
	MarginTop         float64
	MarginRight       float64
	MarginBottom      float64
	MarginLeft        float64
	Theme             Theme
	Official          *Official
	Columns           Columns
	Styles            []NamedStyle
	Footnotes         []Footnote
	FootnoteStyle     *TextStyle
	Fonts             []creator.Font
	AllowLogicalFonts bool
	CodeFont          string
	FontFaces         map[string]int
	FontFamilies      []FontFamily
	FallbackFonts     []string
	BoldFont          string
	ItalicFont        string
	BoldItalicFont    string
	PageNumbers       bool
	Header            string
	Footer            string
	GenerateOutlines  bool

	ReferencePageNumbers string
	// contains filtered or unexported fields
}

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

type Run

type Run struct {
	Text       string
	Font       string
	Bold       bool
	Italic     bool
	Code       bool
	Strike     bool
	Color      *creator.Color
	Link       bool
	Href       string
	Target     string
	PageRef    string
	CaptionRef string
	TitleRef   string
	Footnote   string
	// contains filtered or unexported fields
}

Run 是行内文字及其样式和引用;Color 供代码高亮等内部处理使用。

type Section

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

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

type TOC

type TOC struct {
	Title    string
	MaxLevel int
	Source   string
}

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

type Table

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

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

type TableBorder

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

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

type TableCell

type TableCell struct {
	Format     string
	Text       string
	Runs       []Run
	ColSpan    int
	RowSpan    int
	Style      *TextStyle
	Appearance *CellStyle
}

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

type TableColumn

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

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

type TextStyle

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

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

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

func Bootstrap5Theme

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

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

func Fluent2Theme() Theme

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

func GBT9704Theme

func GBT9704Theme(fonts OfficialFonts) Theme

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

func Material2Theme

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

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

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