Section Graphics

Utility functions for images, drawing on Graph

See
SMGraph.mys

Summary

Return typeFunction and summary
intBuildPolygon(Graph graphContext, table points, boolean autoClose)
Open a polygon, draw lines between each point and close the polygon.
booleanCropSurface(Surface surface, int red, int green, int blue, int opacity)
Crop a Surface for a given color.
 DrawArrow90DegreesHead(Graph graphContext, number left, number top, number right, number bottom, int orientation, int headSize, string headType, boolean drawBody)
Draws a 90°-headed (2 lines) arrow in the given boundaries, pointing to the given orientation.
 DrawCheckbox(Graph graphContext, int left, int top, int right, int bottom, boolean checked, boolean disabled, string foreColor, string backColor, string disableColor)
Draw a checkbox.
 DrawHorizontalSwitch(Graph graphContext, int left, int top, int right, int bottom, boolean turnedOn, boolean disabled, string onColor, string offColor, string unknownColor, string backColor, string disableColor)
Draw ah horizontal switch.
number, number, number, numberDrawPicture(Graph graphContext, string file, number left, number top, number right, number bottom, int verticalAlign, int horizontal, int scaling, boolean keepAspectRatio)
Draw a picture file, using Graph.DrawPicture(...) with advanced and easy-to-use resizing and alignments.
 DrawRadio(Graph graphContext, int left, int top, int right, int bottom, boolean checked, boolean disabled, string foreColor, string backColor, string disableColor)
Draw a radio button.
number, numberDrawText(Graph graphContext, string text, string fontFamily, int fontSize, int fontFace, number left, number top, number right, number bottom, int verticalAlign, int horizontalAlign, boolean allowOverflow, int originX, int originY, boolean 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 the outline inside 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.

Functions

DrawText(Graph graphContext, string text, string fontFamily, int fontSize, int fontFace, number left, number top, number right, number bottom, int verticalAlign, int horizontalAlign, boolean allowOverflow, int originX, int originY, boolean 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
allowOverflowbooleantrueAccepts 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.
autoLineBreakbooleanfalseSplit 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, boolean keepAspectRatio)

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

Run demo script > Misceleanneous > Dev tools > SMGraph DrawPicture, 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 left
topnumber top
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.
keepAspectRatiobooleantrueIf 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, boolean 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}
autoClosebooleantrueClose 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 the outline inside the borders of an ABC triangle.

ParameterTypeDefaultDescription
graphContextGraph The GraphContext: dialog.GraphContext, surface.GraphContext...
xAnumber xA
yAnumber yA
xBnumber xB
yBnumber yB
xCnumber xC
yCnumber yC
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 xA
yAnumber yA
xBnumber xB
yBnumber yB
xCnumber xC
yCnumber yC
Error
if one coordinate is nil

DrawArrow90DegreesHead(Graph graphContext, number left, number top, number right, number bottom, int orientation, int headSize, string headType, boolean 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 left
topnumber top
rightnumber right
bottomnumber bottom
orientationint One of ORIENTATION_* constants
headSizeintmax in boundariesLimit head size?
headTypestring"angle""angle", "black triangle", "white triangle"
drawBodybooleantrueDraw the arrow body (line)

CropSurface(Surface surface, int red, int green, int blue, int opacity)

Crop a Surface for a given color.

For example, CropSurface(mySurface, 255, 255, 255, 255) crops surface to remove white areas on 4 sides of a drawn image/text/staff.

ParameterTypeDefaultDescription
surfaceSurface The surface to crop
redint Red value from 0 to 255
greenint Green value from 0 to 255
blueint Blue value from 0 to 255
opacityint255Opacity value from 0 to 255
Return
boolean: true if crop succeed, return by Surface.Crop(...) MyrScript function.

DrawCheckbox(Graph graphContext, int left, int top, int right, int bottom, boolean checked, boolean disabled, string foreColor, string backColor, string disableColor)

Draw a checkbox.

ParameterTypeDefaultDescription
graphContextGraph graphContext
leftint left
topint top
rightint right
bottomint bottom
checkedbooleanniltrue to check, false to uncheck, nil for unknown state
disabledbooleanfalsetrue=disabled, the checkbox is gray,. false=enabled, the checkbox is colored.
foreColorstring"0000FF"Foreground color in RGB format
backColorstring"FFFFFF"Background color in RGB format
disableColorstring"808080"Background color in RGB format

DrawRadio(Graph graphContext, int left, int top, int right, int bottom, boolean checked, boolean disabled, string foreColor, string backColor, string disableColor)

Draw a radio button.

ParameterTypeDefaultDescription
graphContextGraph graphContext
leftint left
topint top
rightint right
bottomint bottom
checkedbooleanniltrue to check, false to uncheck, nil for unknown state
disabledbooleanfalsetrue=disabled, the radio is gray,. false=enabled, the radio is colored.
foreColorstring"0000FF"Foreground color in RGB format
backColorstring"FFFFFF"Background color in RGB format
disableColorstring"808080"Background color in RGB format

DrawHorizontalSwitch(Graph graphContext, int left, int top, int right, int bottom, boolean turnedOn, boolean disabled, string onColor, string offColor, string unknownColor, string backColor, string disableColor)

Draw ah horizontal switch.

ParameterTypeDefaultDescription
graphContextGraph graphContext
leftint left
topint top
rightint right
bottomint bottom
turnedOnbooleanniltrue if turned on, false if turned off, nil for unknown state
disabledbooleanfalsetrue=disabled, the switch is gray,. false=enabled, the switch is colored.
onColorstring007700""Color when switch is turned on
offColorstring"FF0000"Color when switch is turned off
unknownColorstring"FF8C00"Color when switch is in unknown state
backColorstring"FFFFFF"Background color
disableColorstring"808080"Color when switch is disabled.