File SMGraph.mys

Graph utility functions

Author
Sylvain Machefert

Summary

ConstantTypeSummary
ORIENTATION_EASTstringOrientation of arrows and some drawings: east (to the right)
ORIENTATION_NORTHstringOrientation of arrows and some drawings: north (to the top)
ORIENTATION_NORTH_EASTstringOrientation of arrows and some drawings: north east (to right top)
ORIENTATION_NORTH_WESTstringOrientation of arrows and some drawings: north west (to left top)
ORIENTATION_SOUTHstringOrientation of arrows and some drawings: south (to the bottom)
ORIENTATION_SOUTH_EASTstringOrientation of arrows and some drawings: south east (to right bottom)
ORIENTATION_SOUTH_WESTstringOrientation of arrows and some drawings: south west (to left bottom)
ORIENTATION_WESTstringOrientation of arrows and some drawings: west (to the left)
PICTURE_SCALE_ALWAYSintPicture scaling: shrink large picture, and enlarge small pictures.
PICTURE_SCALE_ENLARGEintPicture scaling: enlarge small picture, never shrink large pictures.
PICTURE_SCALE_NEVERintPicture scaling: never.
PICTURE_SCALE_SHRINKintPicture scaling: shrink large picture, never enlarge small pictures.
VERTICAL_ALIGN_BOTTOMintText and picture vertical alignment: bottom
VERTICAL_ALIGN_MIDDLEintText and picture vertical alignment: middle
VERTICAL_ALIGN_TOPintText and picture vertical alignment: top
Return typeFunction and summary
intBuildPolygon(Graph graphContext, table points, bool autoClose)
Open a polygon, draw lines between each point and close the polygon.
 DrawArrow90DegreesHead(Graph graphContext, number left, number top, number right, number bottom, int orientation, int headSize, string headType, bool drawBody)
Draws a 90°-headed (2 lines) arrow in the given boundaries, pointing to the given orientation.
number, number, number, numberDrawPicture(Graph graphContext, string file, number left, number top, number right, number bottom, int verticalAlign, int horizontal, int scaling, bool keepAspectRatio)
Draw a picture file, using Graph.DrawPicture(...) with advanced and easy-to-use resizing and alignments.
number, numberDrawText(Graph graphContext, string text, string fontFamily, int fontSize, int fontFace, number left, number top, number right, number bottom, int verticalAlign, int horizontalAlign, bool allowOverflow, int originX, int originY, bool autoLineBreak, Score score)
Draw a text, using given font family, size and face, aligned within boundaries.
 FrameTriangle(Graph graphContext, number xA, number yA, number xB, number yB, number xC, number yC)
Draws an outline just inside of the borders of an ABC triangle.
 PaintTriangle(Graph graphContext, number xA, number yA, number xB, number yB, number xC, number yC)
Fills an ABC triangle with the current pen pattern, using the current pen transfer mode.
number, numberStringSize(Graph graphContext, string s, string fontFamily, int fontSize, int fontFace)
Get the string size using given font family, size and face.

Constants

int VERTICAL_ALIGN_TOP

Text and picture vertical alignment: top

See
DrawText
DrawPicture

int VERTICAL_ALIGN_MIDDLE

Text and picture vertical alignment: middle

See
DrawText
DrawPicture

int VERTICAL_ALIGN_BOTTOM

Text and picture vertical alignment: bottom

See
DrawText
DrawPicture

int PICTURE_SCALE_NEVER

Picture scaling: never.

Picture stay in its original size.

See
DrawPicture

int PICTURE_SCALE_SHRINK

Picture scaling: shrink large picture, never enlarge small pictures.

See
DrawPicture

int PICTURE_SCALE_ENLARGE

Picture scaling: enlarge small picture, never shrink large pictures.

See
DrawPicture

int PICTURE_SCALE_ALWAYS

Picture scaling: shrink large picture, and enlarge small pictures.

See
DrawPicture

string ORIENTATION_NORTH

Orientation of arrows and some drawings: north (to the top)

string ORIENTATION_NORTH_EAST

Orientation of arrows and some drawings: north east (to right top)

string ORIENTATION_EAST

Orientation of arrows and some drawings: east (to the right)

string ORIENTATION_SOUTH_EAST

Orientation of arrows and some drawings: south east (to right bottom)

string ORIENTATION_SOUTH

Orientation of arrows and some drawings: south (to the bottom)

string ORIENTATION_SOUTH_WEST

Orientation of arrows and some drawings: south west (to left bottom)

string ORIENTATION_WEST

Orientation of arrows and some drawings: west (to the left)

string ORIENTATION_NORTH_WEST

Orientation of arrows and some drawings: north west (to left top)

Functions

StringSize(Graph graphContext, string s, string fontFamily, int fontSize, int fontFace)

Get the string size using given font family, size and face.

Multi-line is not handled.

ParameterTypeDefaultDescription
graphContextGraph The GraphContext: dialog.GraphContext, surface.GraphContext...
sstring The string, rich string is handled, tokens are transformed (e.g. Moderato $(4) = 80)
fontFamilystring"geneva" 
fontSizeint11 
fontFaceintFACE_NONESum of FACE_* constants in MSDefine
Returns
number: Width of the string.
number: Height of the string.

DrawText(Graph graphContext, string text, string fontFamily, int fontSize, int fontFace, number left, number top, number right, number bottom, int verticalAlign, int horizontalAlign, bool allowOverflow, int originX, int originY, bool autoLineBreak, Score score)

Draw a text, using given font family, size and face, aligned within boundaries.

This may be useful if you create your own component that need to display text.

ParameterTypeDefaultDescription
graphContextGraph The GraphContext: dialog.GraphContext, surface.GraphContext...
textstring The text to draw, rich strings are handled, tokens are transformed (e.g. $(4) = 80)
fontFamilystring"geneva" 
fontSizeint11 
fontFaceintFACE_NONESum of FACE_* constants in MSDefine
leftnumber Left boundary
topnumber Top boundary
rightnumber Right boundary
bottomnumber Bottom boundary
verticalAlignint VERTICAL_ALIGN_TOP, VERTICAL_ALIGN_MIDDLE or VERTICAL_ALIGN_BOTTOM
horizontalAlignint ALIGN_LEFT, ALIGN_CENTER or ALIGN_RIGHT
allowOverflowbooltrueAccepts that text can be displayed outside of the boundaries? If not allowed, alignment may be changed to minimize the risk of truncation.
originXintnilHorizontal origin of alignment, e.g. right aligned from the center (left+bottom)/2
originYintnilVertical origin of alignment, e.g. top aligned from 1/3 of the height.
autoLineBreakboolfalseSplit text into lines to try to avoid horizontal overflow?
scoreScorenilIn case of text with tokens like $T (title), $N (number of pages), if score is provided, tokens are transformed. See Text commands in manual.
Returns
number: Width needed to draw the full text, even if it overflows and as been truncated.
number: Height needed to draw the full text, even if it overflows and as been truncated.

DrawPicture(Graph graphContext, string file, number left, number top, number right, number bottom, int verticalAlign, int horizontal, int scaling, bool keepAspectRatio)

Draw a picture file, using Graph.DrawPicture(...) with advanced and easy-to-use resizing and alignments.

Download the demo script SMGraph DrawPicture. It installs in Scripts>Miscellaneous>Dev tools menu. It shows all behaviours, and you can copy/paste the code.

Although Graph.DrawPicture can a "broken icon" if file doesn't exist, this function will draw nothing, because size of the broken icon is unknown. A debugging message will appear in console and nil is returned.

left, top, right and bottom represent a rectangle, but if the rectangle have a width or height of 0, the picture can be scaled always keep its original aspect ratio.
If they represent a single point (width and height are zero), image can't be scaled.

ParameterTypeDefaultDescription
graphContextGraph The GraphContext: dialog.GraphContext, surface.GraphContext...
filestring A PICT (.pct), BMP, JPG, PNG file
leftnumber  
topnumber  
rightnumberleft 
bottomnumbertop 
verticalAlignintnilOne of VERTICAL_ALIGN_TOP, VERTICAL_ALIGN_MIDDLE or VERTICAL_ALIGN_BOTTOM. If nil and not a rectangle, default is VERTICAL_ALIGN_TOP. If nil and all rectangle boundaries are provided, default is VERTICAL_ALIGN_MIDDLE.
horizontalintnilOne of ALIGN_LEFT, ALIGN_RIGHT or ALIGN_CENTER. If nil and not a rectangle, default is ALIGN_LEFT. If nil and all rectangle boundaries are provided, default is ALIGN_CENTER.
scalingintPICTURE_SCALE_SHRINKOne of PICTURE_SCALE_NEVER, PICTURE_SCALE_SHRINK, PICTURE_SCALE_ENLARGE or PICTURE_SCALE_ALWAYS.
keepAspectRatiobooltrueIf resized, keep aspect ratio?
Returns
number: Left boundary of the drawn picture
number: Top boundary of the drawn picture
number: Right boundary of the drawn picture
number: Bottom boundary of the drawn picture

BuildPolygon(Graph graphContext, table points, bool autoClose)

Open a polygon, draw lines between each point and close the polygon.

Having the polygon, you can paint, erase, invert... and must kill it.

ParameterTypeDefaultDescription
graphContextGraph The GraphContext: dialog.GraphContext, surface.GraphContext...
pointstable Table of {x,y}
autoClosebooltrueClose the polygon? If true, 3 points draw a triangle, else it draw 2 lines.
Return
int: the id of the polygon.
Errors
if points is not a table or have less than 3 values
if each point is not a table with x and y not nil.
Example
local tri = BuildPolygon(item.GraphContext, {{xA,yA}, {xB,yB}, {xC,yC}})
item.GraphContext.FramePolygon(tri)
item.GraphContext.KillPolygon(tri)

FrameTriangle(Graph graphContext, number xA, number yA, number xB, number yB, number xC, number yC)

Draws an outline just inside of the borders of an ABC triangle.

ParameterTypeDefaultDescription
graphContextGraph The GraphContext: dialog.GraphContext, surface.GraphContext...
xAnumber  
yAnumber  
xBnumber  
yBnumber  
xCnumber  
yCnumber  
Error
if one coordinate is nil

PaintTriangle(Graph graphContext, number xA, number yA, number xB, number yB, number xC, number yC)

Fills an ABC triangle with the current pen pattern, using the current pen transfer mode.

ParameterTypeDefaultDescription
graphContextGraph The GraphContext: dialog.GraphContext, surface.GraphContext...
xAnumber  
yAnumber  
xBnumber  
yBnumber  
xCnumber  
yCnumber  
Error
if one coordinate is nil

DrawArrow90DegreesHead(Graph graphContext, number left, number top, number right, number bottom, int orientation, int headSize, string headType, bool drawBody)

Draws a 90°-headed (2 lines) arrow in the given boundaries, pointing to the given orientation.

ParameterTypeDefaultDescription
graphContextGraph The GraphContext: dialog.GraphContext, surface.GraphContext...
leftnumber  
topnumber  
rightnumber  
bottomnumber  
orientationint One of ORIENTATION_* constants
headSizeintmax in boundariesLimit head size?
headTypestring"angle""angle", "black triangle", "white triangle"
drawBodybooltrueDraw the arrow body (line)