本系列介绍 文本转图片 工具支持的图形描述语言。图形描述语言将图片描述为代码或者文本,有利于版本管理,很适合嵌入 LaTeX 或 Markdown 文档。用于网站时,还可以减轻图片存储压力,因为图片可以根据代码在使用时生成。本文是该系列第二篇,介绍 mscgen。
mscgen是什么
Mscgen 是一个小程序,它解析 消息序列图描述 (Message Sequence Chart descriptions)并生成 PNG、SVG、EPS 或服务器端图像映射(ismaps)作为输出。消息序列图(Message Sequence Charts, MSCs)是表示一段时间内实体和交互的一种方式,通常与 SDL
结合使用。虽然 MSCs 的创建和使用并不复杂,但 MSCs 在指定协议如何操作方面在通信领域中很流行。Mscgen 旨在提供一种简单的文本语言,可以清晰地创建、编辑和理解,也可以转换为用于显示或打印的通用图像格式。
语法介绍
消息序列图的一般格式由以下带注释的示例给出。应该注意,输入解析器会忽略空白和换行。
Input fragment | Meaning |
---|---|
# MSC for some fictional process | 注释。使用 # 或者 // 或者 C 语言风格的块注释 /* ... */ |
msc { | MSC 开始 |
width = "800"; | 输出控制选项。注意 hscale 选项用于按比例设置宽度 |
a, b, "c"; | 列出将在消息序列图中使用的实体,按它们在页面中从左到右和水平方向出现的顺序排列。请注意,每个实体名称可以加引号,也可以不加引号,除非该名称包含空格,在这种情况下需要双引号。 |
a->b | 指示某个消息从 a 传递到 b 。在本例中,命名实体 a 和 b 必须在消息序列图的开始处声明。同样,如果实体名包含空格,则应该使用双引号。 |
a<-c [label="return"]; | 指示某些消息从 c 传递到 a 。在本例中,方括号中还提供了一些属性。label 属性提供写在消息弧之上的文本。 |
} | 消息序列图的末尾 |
语法非常简单。需要注意的重要部分是,第一行给出了消息序列图选项和实体,下面的每一行描述了一个消息弧。下面介绍每种可能的消息弧线类型,包括所谓的“特殊”弧线,这些弧线不显示实体之间的关系,而是在图表上水平地描述一些内容。
消息类型
msc {
a,b;
# 消息: -> or <-
a->b [label="Message"];
a<-b;
# 方法或函数调用: => or <=
a=>b [label="Method or function call"];
a<=b;
# 方法或函数返回值: >> or <<
a>>b [label="Method or function return value"];
a<<b;
# 回调: =>> or <<=
a=>>b [label="Callback"];
a<<=b;
# 强调消息: :> or <:
a:>b [label="Emphasised Message"];
a<:b;
# 消息丢失: -x or x-
a-xb [label="Lost Message"];
# 注意 ax 间要有空格,或者 a 用引号包裹
"a"x-b;
# 表示msc可能故意省略某些信号,或者可能经过了一段很长的时间: ...
...[label="Lots of time"];
# 用于向信号块添加注释或指示发生了某种操作或状态: ---
--- [label="Comment"];
# 用于在行之间添加额外的空间: |||
||| [label="||| Used to add extra space between rows."];
# 广播弧线,其中弧线扩展到除源实体之外的所有实体。任何弧线标签都以整个图表为中心: ->* or *<-
a ->* [label="Broadcast"];
*<-b [label="Broadcast"];
# 框弧,其中弧被所选实体之间的框替换。如果需要,框弧的标签包裹在在盒子中并且居中: box
a box a [label="box"];
# 圆角框弧,其中弧被所选实体之间的框替换。如果需要,框弧的标签包裹在在盒子中并且居中: rbox
b rbox b [label="rbox"];
# 三角框弧,其中弧被所选实体之间的框替换。如果需要,框弧的标签包裹在在盒子中并且居中: abox
a abox b [label="abox"];
# 注解框弧,其中弧被所选实体之间的框替换。如果需要,框弧的标签包裹在在盒子中并且居中: note
a note a [label="note"];
}
框类型允许添加状态框和条件框。在源中,这些仍然表示为实体之间的关系,但在本例中,框的作用是从源扩展到目标实体。
# Example MSC using boxes
msc {
# The entities
A, B, C, D;
# Small gap before the boxes
|||;
# Next four on same line due to ','
A box A [label="box"],
B rbox B [label="rbox"],
C abox C [label="abox"],
D note D [label="note"];
# Example of the boxes with filled backgrounds
A abox B [label="abox", textbgcolour="#ff7f7f"];
B rbox C [label="rbox", textbgcolour="#7fff7f"];
C note D [label="note", textbgcolour="#7f7fff"];
}
属性
除了 arc
类型之外,还可以为每个消息 arc
或实体提供少量属性。这些属性放在方括号中,在实体名或消息弧之后立即分开,属性将被附加到这些实体名或消息弧上:
a->b [label="A to B", textcolour="red", linecolour="#ffb011"];
下表描述了所有可能的属性及其含义,注意到每个属性都可以为特殊的 …
和 ---
arc
类型,以及任何其他 arc
或实体。
Source Attribute | Meaning |
---|---|
label | A label to place on the message arc, or label to use for an entity. This is reproduced literally above the message arc or in place of the entity name. When used as a message arc label this may also contain parameters or required text. If a \n is encountered, it will be interpreted as a newline, placing text below the arc as well as above it in the normal position, or expanding entity tiles onto more lines. |
URL | When generating the image, colour the label in blue. If generating an image map, create an entry for the label to the specified URL. If the message sequence chart is to be embedded in Doxygen documentation, the URL maybe specified as \ref xxx where xxx is the name of some documented element; in this case a link to the element will automatically be made. |
ID | Adds a superscript identifier to the label. Typically this maybe a number such that specific elements can be identified and referred to in surrounding descriptive text. |
IDURL | Similar to the URL attribute, but links from the ID text rather than the message label. This has the same semantics as the URL attribute, but is only of use if an arc also has an ID. |
arcskip | This offsets the vertical position at which an arc reaches its destination entity. |
linecolour, linecolor | For arcs or entities, set the line to the specified colour. |
textcolour, textcolor | For arcs or entities, set label text to the specified colour. |
textbgcolour, textbgcolor | For arcs or entities, set a colour for the text background block. Sets the fill colour for 'box', 'abox', 'rbox' and 'note' shapes. |
arclinecolour, arclinecolor | Only meaningful on a entity, this sets the default line colour for all arcs that originating from that entity. This can be overridden by linecolour attributes on specific arcs as desired. |
arctextcolour, arctextcolor | Only meaningful on a entity, this sets the default text colour for all arcs that originating from that entity. This can be overridden by textcolour attributes on specific arcs as desired. |
arctextbgcolour, arctextbgcolor | Only meaningful on a entity, this sets the default colour for the text background for all arc oriniationg from that entity. This can be overridden by textbgcolour attributes on specific arcs as desired. |
颜色可以指定为 RGB
颜色代码,使用 #
前缀作为每个 HTML 标记,也可以使用一些预定义的颜色名称,如下例所示。
Name | Code |
---|---|
white | #ffffff |
silver | #c0c0c0 |
gray | #808080 |
black | #000000 |
maroon | #800000 |
red | #ff0000 |
orange | #ffb000 |
yellow | #ffff00 |
olive | #808000 |
green/lime | #00ff00 |
aqua | #00ffff |
teal | #008080 |
blue | #0000ff |
navy | #000080 |
indigo | #440088 |
purple | #800080 |
violet | #d02090 |
fuchsia | #ff00ff |
选项
最后,可以将选项添加到输入文件中,以控制生成的图像的各个方面。目前只支持几个选项,并且必须在输入文件的顶部给出,如下面的示例所示。表中描述了允许的选项。
msc {
# Options section
hscale="1.5", arcgradient="5";
# Entities
A, B;
# Arcs
A -> B;
}
Option | Meaning |
---|---|
hscale | Scale the image width by this factor, which must be a non-zero value. Normally the output width is 600 pixels for a PNG, but can be scaled to any value by specifying fractional multipliers. For example, specifying a value of 1.5 would set the width to 600 x 1.5 = 900 pixels. |
width | Set the image width to this values, which must be non-zero and is given in pixels. This can be used instead of the hscale option, in cases where the explicit image width is preferred to a scale factor. |
arcgradient | This causes message arcs to slope down the page rather than running horizontally. The value given is the number of pixels below the start of the message arc that the end of the arc should meet the target entity. |
wordwraparcs | If set to 'true', 'on' or '1', this will cause long lines of text on arrow arcs to be automatically word wrapped. By default word wrapping is only enabled for 'box', 'abox', 'rbox' and 'note' style arcs. |
mscgen画廊
以上就是全部内容,欢迎到 文本转图片工具 试用。
发表回复