marktoimage

package module
v0.0.0-...-1a793be Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 16 Imported by: 0

README

marktoimage

纯 Go 的 Markdown 转图片项目。

项目状态

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//默认文本
	TextStyleDefault = TextStyle{Inline: true, Size: 40}
	//换行文本
	TextStyleEnter = TextStyle{Inline: false, Size: 40}
	//用于组件间距
	TextStyleParagraph = TextStyle{Inline: false, Size: 20}
	//四级标题
	TextStyleHead = TextStyle{Inline: false, Bold: true, Size: 60}
	//三级标题
	TextStyleSubHead = TextStyle{Inline: false, Bold: true, Size: 50}
	//用于List的序号样式
	TextStyleList = TextStyle{Inline: true, Size: 40}
	//用于link
	TextStyleLink = TextStyle{Inline: true}
	//用于code代码块`abc`
	TextStyleCode = TextStyle{Inline: true, Size: 40, Base: true}
	//用于code代码段“`abc\ndef“`
	TextStyleCodeBlock = TextStyle{Inline: false, Size: 35, Block: true, Base: true}
	//引用
	//TextStyleNote.Color = cfg.Colors.NoteColor
	TextStyleNote = TextStyle{Inline: false, Size: 35}
	//strong
	TextStyleStrong = TextStyle{Inline: true, Bold: true}
	//Italic
	TextStyleItalic = TextStyle{Inline: true, Italic: true}
)

Functions

func TextWrap

func TextWrap(dc *gg.Context, s string, w float64) []string

Types

type Colors

type Colors struct {
	DefaultColor     color.Color //默认字体颜色
	BackgroundColor  color.Color
	CodeBlockBgColor color.Color
	CodeColor        color.Color
	NoteColor        color.Color //引用
	LinkColor        color.Color
}

type Config

type Config struct {
	FontData   [][]byte //用于全局的字体读取
	TopMargin  float64  //上边距留白
	TextMargin float64  //左右留白
	Width      float64  //宽度
	Height     float64  //高度
	LineHeight float64  //行高
	Colors     Colors
}

type HyperlinkSegment

type HyperlinkSegment struct {
	Style TextStyle
	Text  string
	URL   *url.URL
	// contains filtered or unexported fields
}

func (*HyperlinkSegment) Draw

func (h *HyperlinkSegment) Draw()

func (*HyperlinkSegment) Inline

func (h *HyperlinkSegment) Inline() bool

func (*HyperlinkSegment) SetParent

func (h *HyperlinkSegment) SetParent(r *RichText)

type ImageSegment

type ImageSegment struct {
	Path   string //支持的有相对路径,绝对路径,网络路径,base64
	Title  string
	Data   []byte      //也可以不写path构建
	Image  image.Image //也可以直接传入图片
	Width  int
	Height int
	// contains filtered or unexported fields
}

func (*ImageSegment) Draw

func (i *ImageSegment) Draw()

func (*ImageSegment) InitImage

func (i *ImageSegment) InitImage()

func (*ImageSegment) Inline

func (i *ImageSegment) Inline() bool

func (*ImageSegment) SetParent

func (i *ImageSegment) SetParent(r *RichText)

type ListSegment

type ListSegment struct {
	Items   []RichTextSegment
	Ordered bool
	// contains filtered or unexported fields
}

func (*ListSegment) Draw

func (l *ListSegment) Draw()

func (ListSegment) Inline

func (ListSegment) Inline() bool

func (*ListSegment) Segments

func (l *ListSegment) Segments() []RichTextSegment

更新需要绘制的文本

func (*ListSegment) SetParent

func (l *ListSegment) SetParent(r *RichText)

type RichText

type RichText struct {
	Segments []RichTextSegment
	Size     float64 //用于保存当前字体大小
	Cov      *gg.Context
	gg.Point //用于定位画笔
	Config
}

RichText 表示富文本

func NewRichText

func NewRichText(cfg Config) *RichText

func NewRichTextFromMarkdown

func NewRichTextFromMarkdown(content string) *RichText

func (*RichText) Align

func (r *RichText) Align()

防止最后一行时.Inline()==true导致Y没有移动到应该切割的位置,需要补充换行

func (*RichText) AppendMarkdown

func (t *RichText) AppendMarkdown(content string)

func (*RichText) AppendSegment

func (r *RichText) AppendSegment(rs ...RichTextSegment)

func (*RichText) Clear

func (r *RichText) Clear()

清空richtext,准备解析下一个内容

func (*RichText) Cut

func (r *RichText) Cut()

func (*RichText) Draw

func (r *RichText) Draw()

func (*RichText) Expansion

func (r *RichText) Expansion()

扩充画布高度

func (*RichText) ParseMarkdown

func (t *RichText) ParseMarkdown(content string)

func (*RichText) SetFontSize

func (r *RichText) SetFontSize(t TextStyle)

type RichTextSegment

type RichTextSegment interface {
	Inline() bool //本组件没有结束,继续在本行绘制
	SetParent(*RichText)
	Draw()
}

传参接口

type SeparatorSegment

type SeparatorSegment struct {
	// contains filtered or unexported fields
}

func (*SeparatorSegment) Draw

func (s *SeparatorSegment) Draw()

func (SeparatorSegment) Inline

func (SeparatorSegment) Inline() bool

func (*SeparatorSegment) SetParent

func (s *SeparatorSegment) SetParent(r *RichText)

type TextSegment

type TextSegment struct {
	Style TextStyle //文本样式
	Text  string
	// contains filtered or unexported fields
}

TextSegment 表示文本片段,实现RichTextSegment

func (*TextSegment) Draw

func (t *TextSegment) Draw()

绘制

func (*TextSegment) Inline

func (t *TextSegment) Inline() bool

内联

func (*TextSegment) SetParent

func (t *TextSegment) SetParent(r *RichText)

type TextStyle

type TextStyle struct {
	Inline    bool //是否在一行绘制/是否不要新建一行
	Size      float64
	FontFace  font.Face
	Color     color.Color
	Bold      bool //加粗
	Italic    bool //斜体
	Underline bool //下划线
	Block     bool //是否是块代码,作为一个整体绘制,解析\n
	Base      bool //背景色
}

TextStyle 表示文本样式

Jump to

Keyboard shortcuts

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