Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Properties

* New **scaledWidth** and **scaledHeight** properties have been added. These are read-only values
that expose the effective size of the rendered SVG path independent of the widget size. When
maintainAspectRatio is false, then these values are equal to the width and height of the widget.
83 changes: 71 additions & 12 deletions extensions/widgets/svgpath/svgpath.lcb
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ use com.livecode.library.widgetutils
-- adding metadata to ensure the extension displays correctly in livecode
metadata title is "SVG Icon"
metadata author is "LiveCode"
metadata version is "2.0.0"
metadata version is "2.1.0"
metadata svgicon is "M61.8,0H4C1.8,0,0,1.8,0,4v27.8c0,2.2,1.8,4,4,4h57.8c2.2,0,4-1.8,4-4V4C65.8,1.8,64,0,61.8,0z M20.9,25.3c-1.2,0.9-2.8,1.4-4.9,1.4c-0.8,0-1.6-0.1-2.2-0.2c-0.7-0.1-1.3-0.3-1.9-0.5c-0.1,0.2-0.3,0.4-0.5,0.5s-0.5,0.2-0.8,0.2c-0.6,0-1-0.1-1.2-0.3c-0.2-0.2-0.4-0.6-0.4-1.2l-0.1-3.2v-0.2c0-0.6,0.1-1,0.3-1.3c0.2-0.2,0.6-0.4,1.2-0.4c0.6,0,1.1,0.4,1.5,1.3c0.1,0.2,0.2,0.4,0.2,0.5c0.4,0.7,0.9,1.2,1.5,1.6c0.6,0.4,1.4,0.5,2.4,0.5c0.8,0,1.5-0.2,2-0.5c0.5-0.4,0.7-0.8,0.7-1.5c0-1-1.1-1.7-3.2-2.2c-0.6-0.1-1.1-0.3-1.5-0.4c-1.8-0.5-3-1.1-3.7-1.8c-0.7-0.7-1-1.7-1-2.9c0-1.6,0.6-2.9,1.8-3.9c1.2-1,2.7-1.5,4.6-1.5c0.6,0,1.2,0.1,1.8,0.2c0.6,0.1,1.2,0.3,1.8,0.6c0.1-0.3,0.3-0.5,0.5-0.6c0.2-0.1,0.4-0.2,0.8-0.2c0.5,0,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4.5,2.3-6.2c1.5-1.6,3.4-2.5,5.8-2.5c0.6,0,1.3,0.1,1.9,0.2s1.3,0.4,2,0.7c0.2-0.3,0.4-0.6,0.6-0.7c0.2-0.1,0.4-0.2,0.7-0.2c0.5,0,0.8,0.1,1,0.4c0.2,0.2,0.3,0.6,0.3,1.2l0,3.6c0,0.6-0.1,1.1-0.3,1.3c-0.2,0.2-0.6,0.3-1.1,0.3c-0.4,0-0.7-0.1-0.9-0.3c-0.2-0.2-0.4-0.5-0.5-1C53.3,14,53,13.4,52.5,13s-1.2-0.6-2.2-0.6c-1.4,0-2.5,0.5-3.3,1.4c-0.8,1-1.1,2.3-1.1,4.2s0.4,3.2,1.2,4.2c0.8,1,1.9,1.5,3.4,1.5c0.3,0,0.7,0,1.1-0.1c0.4-0.1,0.9-0.2,1.5-0.4V21h-1.6c-0.6,0-1.1-0.1-1.3-0.3c-0.3-0.2-0.4-0.6-0.4-1.1c0-0.6,0.1-0.9,0.4-1.2s0.7-0.4,1.3-0.4h5c0.6,0,1.1,0.1,1.4,0.3s0.4,0.6,0.4,1.2C58.2,20,58.1,20.4,57.8,20.6z"

private variable mPathName as String
private variable mPath as String
private variable mHilited as Boolean
private variable mPath as String
private variable mHilited as Boolean
private variable mMaintainAspectRatio as Boolean
-- some SVG Paths need to be flipped (all font awesome SVG paths do,
-- Illustrator SVG does not)
private variable mFlipVertically as Boolean
private variable mAngle as Real
private variable mFlipVertically as Boolean
private variable mAngle as Real
private variable mFillRule as String
private variable mScaledWidth as Real
private variable mScaledHeight as Real


/**
Syntax:
Expand Down Expand Up @@ -156,6 +159,36 @@ property maintainAspectRatio get mMaintainAspectRatio set setMaintainAspectRat
metadata maintainAspectRatio.default is "true"
metadata maintainAspectRatio.label is "Fix aspect ratio"

/**
Syntax:
get the scaledWidth of <widget>

Summary: The width of the bounding rect of the SVG path, in fractional pixels.

Description:
If the <maintainAspectRatio> of the SVG path is `true`, then the <scaledWidth>
of the object will return the width of the bounding rect of the SVG path.
Otherwise, it will return the width of the widget.

Related: scaledHeight(property), maintainAspectRatio(property)
*/
property "scaledWidth" get mScaledWidth

/**
Syntax:
get the scaledHeight of <widget>

Summary: The height of the bounding rect of the SVG path, in fractional pixels.

Description:
If the <maintainAspectRatio> of the SVG path is `true`, then the <scaledHeight>
of the object will return the height of the bounding rect of the SVG path.
Otherwise, it will return the height of the widget.

Related: scaledWidth(property), maintainAspectRatio(property)
*/
property "scaledHeight" get mScaledHeight

/**
Syntax:
set the flipped of <widget> to {true|false}
Expand Down Expand Up @@ -213,7 +246,6 @@ encirclements.
See https://www.w3.org/TR/SVG/painting.html#FillRuleProperty for examples of
the "non-zero" and "even odd" fill rules.
*/

property fillRule get mFillRule set setFillRule
metadata fillRule.editor is "com.livecode.pi.enum"
metadata fillRule.options is "non-zero,even odd"
Expand Down Expand Up @@ -256,10 +288,19 @@ public handler OnLoad(in pProperties as Array)
put pProperties["flip vertically"] into mFlipVertically
put pProperties["angle"] into mAngle

// Older SVG widgets do not have this property
// Older SVG widgets do not have this property
if "fillRule" is among the keys of pProperties then
put pProperties["fillRule"] into mFillRule
end if

CalculateScaledSizes()
end handler
----------

----------
-- this handler is called when the widget is resized
public handler OnGeometryChanged()
CalculateScaledSizes()
end handler
----------

Expand All @@ -286,19 +327,17 @@ public handler OnMouseLeave()
end handler
----------


public handler setIsHilited(in pBoolean as Boolean)
put pBoolean into mHilited
redraw all
end handler


public handler setMaintainAspectRatio(in pBoolean as Boolean)
put pBoolean into mMaintainAspectRatio
CalculateScaledSizes()
redraw all
end handler


public handler setFlipVertically(in pBoolean as Boolean)
put pBoolean into mFlipVertically
redraw all
Expand All @@ -307,18 +346,20 @@ end handler
public handler setPath(in pPath as String) returns nothing
put pPath into mPath
put "" into mPathName
CalculateScaledSizes()
redraw all
end handler

public handler setPathPreset(in pPresetName as String) returns nothing
put iconSVGPathFromName(pPresetName) into mPath
put pPresetName into mPathName
CalculateScaledSizes()
redraw all
end handler


public handler setAngle(in pAngle as Number) returns nothing
put pAngle into mAngle
CalculateScaledSizes()
redraw all
end handler

Expand Down Expand Up @@ -353,7 +394,7 @@ public handler OnPaint()
translate tPath by [the left of tBounds * -1, the top of tBounds * -1]
end if

if mHilited is false then
if mHilited is false then
set the paint of this canvas to my foreground paint
else
set the paint of this canvas to my highlight paint
Expand All @@ -363,5 +404,23 @@ public handler OnPaint()

fill tPath on this canvas
end handler
----------

----------
-- used in various places to calculate the scaledWidth & scaledHeight variables
private handler CalculateScaledSizes()
//recalculate the bounding box for the path
variable tPath as Path
put path mPath into tPath
if mMaintainAspectRatio then
constrainPathToRect(my bounds, tPath)
put the width of the bounding box of tPath into mScaledWidth
put the height of the bounding box of tPath into mScaledHeight
else
put my width into mScaledWidth
put my height into mScaledHeight
end if
end handler
----------

end widget
65 changes: 65 additions & 0 deletions extensions/widgets/svgpath/tests/properties.livecodescript
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
script "SvgPathWidgetProperties"
/*
Copyright (C) 2016 LiveCode Ltd.

This file is part of LiveCode.

LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.

LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */

private function getPathData
return "M10 10 H 90 V 90 H 10 L 10 10"
end getPathData

on TestSetup
TestLoadExtension "com.livecode.library.widgetutils"
TestLoadExtension "com.livecode.library.iconSVG"
TestLoadExtension "com.livecode.widget.svgpath"
create widget "testSvgPath" as "com.livecode.widget.svgpath"
end TestSetup

on TestTeardown
delete widget "testSvgPath"
end TestTeardown

on TestCreateSvgPath
TestAssert "created svgPath exists", exists(widget "testSvgPath")
end TestCreateSvgPath

on TestSetPath
set the iconPath of widget "testSvgPath" to getPathData()
TestAssert "iconPath set to default 'star' value", the iconPath of widget "testSvgPath" is getPathData()
end TestSetPath

on TestMaintainAspectRatio
set the maintainAspectRatio of widget "testSvgPath" to true
TestAssert "maintainAspectRatio set to true", the maintainAspectRatio of widget "testSvgPath" is true

set the maintainAspectRatio of widget "testSvgPath" to false
TestAssert "maintainAspectRatio set to false", the maintainAspectRatio of widget "testSvgPath" is false
end TestMaintainAspectRatio

on TestSetRect
set the rect of widget "testSvgPath" to "0,0,100,100"
TestAssert "the rect was set to 0,0,100,100", the rect of widget "testSvgPath" is "0,0,100,100"
end TestSetRect

on TestScaledSizing
set the iconPath of widget "testSvgPath" to getPathData()
set the maintainAspectRatio of widget "testSvgPath" to true

set the rect of widget "testSvgPath" to "0,0,90,100"
TestAssert "scaled sizing with maintainAspectRatio = true", (round(the scaledWidth of widget "testSvgPath") is 90 and round(the scaledHeight of widget "testSvgPath") is 90)

set the maintainAspectRatio of widget "testSvgPath" to false
TestAssert "scaled sizing with maintainAspectRatio = false", (round(the scaledWidth of widget "testSvgPath") is 90 and round(the scaledHeight of widget "testSvgPath") is 100)
end TestScaledSizing