[orx-composition, orx-svg] Move Composition and SVG code from OPENRNDR to ORX

This commit is contained in:
Edwin Jakobs
2024-03-15 13:34:17 +01:00
parent e35037fbec
commit 8eeb74e1a8
19 changed files with 3558 additions and 0 deletions

View File

@@ -0,0 +1,153 @@
package org.openrndr.extra.svg
/** Color name map as per the CSS Color Module Level 4 */
internal val cssColorNames = mapOf(
"aliceblue" to 0xf0f8ff,
"antiquewhite" to 0xfaebd7,
"aqua" to 0x00ffff,
"aquamarine" to 0x7fffd4,
"azure" to 0xf0ffff,
"beige" to 0xf5f5dc,
"bisque" to 0xffe4c4,
"black" to 0x000000,
"blanchedalmond" to 0xffebcd,
"blue" to 0x0000ff,
"blueviolet" to 0x8a2be2,
"brown" to 0xa52a2a,
"burlywood" to 0xdeb887,
"cadetblue" to 0x5f9ea0,
"chartreuse" to 0x7fff00,
"chocolate" to 0xd2691e,
"coral" to 0xff7f50,
"cornflowerblue" to 0x6495ed,
"cornsilk" to 0xfff8dc,
"crimson" to 0xdc143c,
"cyan" to 0x00ffff,
"darkblue" to 0x00008b,
"darkcyan" to 0x008b8b,
"darkgoldenrod" to 0xb8860b,
"darkgray" to 0xa9a9a9,
"darkgrey" to 0xa9a9a9,
"darkgreen" to 0x006400,
"darkkhaki" to 0xbdb76b,
"darkmagenta" to 0x8b008b,
"darkolivegreen" to 0x556b2f,
"darkorange" to 0xff8c00,
"darkorchid" to 0x9932cc,
"darkred" to 0x8b0000,
"darksalmon" to 0xe9967a,
"darkseagreen" to 0x8fbc8f,
"darkslateblue" to 0x483d8b,
"darkslategray" to 0x2f4f4f,
"darkslategrey" to 0x2f4f4f,
"darkturquoise" to 0x00ced1,
"darkviolet" to 0x9400d3,
"deeppink" to 0xff1493,
"deepskyblue" to 0x00bfff,
"dimgray" to 0x696969,
"dimgrey" to 0x696969,
"dodgerblue" to 0x1e90ff,
"firebrick" to 0xb22222,
"floralwhite" to 0xfffaf0,
"forestgreen" to 0x228b22,
"fuchsia" to 0xff00ff,
"gainsboro" to 0xdcdcdc,
"ghostwhite" to 0xf8f8ff,
"gold" to 0xffd700,
"goldenrod" to 0xdaa520,
"gray" to 0x808080,
"grey" to 0x808080,
"green" to 0x008000,
"greenyellow" to 0xadff2f,
"honeydew" to 0xf0fff0,
"hotpink" to 0xff69b4,
"indianred" to 0xcd5c5c,
"indigo" to 0x4b0082,
"ivory" to 0xfffff0,
"khaki" to 0xf0e68c,
"lavender" to 0xe6e6fa,
"lavenderblush" to 0xfff0f5,
"lawngreen" to 0x7cfc00,
"lemonchiffon" to 0xfffacd,
"lightblue" to 0xadd8e6,
"lightcoral" to 0xf08080,
"lightcyan" to 0xe0ffff,
"lightgoldenrodyellow" to 0xfafad2,
"lightgray" to 0xd3d3d3,
"lightgrey" to 0xd3d3d3,
"lightgreen" to 0x90ee90,
"lightpink" to 0xffb6c1,
"lightsalmon" to 0xffa07a,
"lightseagreen" to 0x20b2aa,
"lightskyblue" to 0x87cefa,
"lightslategray" to 0x778899,
"lightslategrey" to 0x778899,
"lightsteelblue" to 0xb0c4de,
"lightyellow" to 0xffffe0,
"lime" to 0x00ff00,
"limegreen" to 0x32cd32,
"linen" to 0xfaf0e6,
"magenta" to 0xff00ff,
"maroon" to 0x800000,
"mediumaquamarine" to 0x66cdaa,
"mediumblue" to 0x0000cd,
"mediumorchid" to 0xba55d3,
"mediumpurple" to 0x9370d8,
"mediumseagreen" to 0x3cb371,
"mediumslateblue" to 0x7b68ee,
"mediumspringgreen" to 0x00fa9a,
"mediumturquoise" to 0x48d1cc,
"mediumvioletred" to 0xc71585,
"midnightblue" to 0x191970,
"mintcream" to 0xf5fffa,
"mistyrose" to 0xffe4e1,
"moccasin" to 0xffe4b5,
"navajowhite" to 0xffdead,
"navy" to 0x000080,
"oldlace" to 0xfdf5e6,
"olive" to 0x808000,
"olivedrab" to 0x6b8e23,
"orange" to 0xffa500,
"orangered" to 0xff4500,
"orchid" to 0xda70d6,
"palegoldenrod" to 0xeee8aa,
"palegreen" to 0x98fb98,
"paleturquoise" to 0xafeeee,
"palevioletred" to 0xd87093,
"papayawhip" to 0xffefd5,
"peachpuff" to 0xffdab9,
"peru" to 0xcd853f,
"pink" to 0xffc0cb,
"plum" to 0xdda0dd,
"powderblue" to 0xb0e0e6,
"purple" to 0x800080,
"rebeccapurple" to 0x663399,
"red" to 0xff0000,
"rosybrown" to 0xbc8f8f,
"royalblue" to 0x4169e1,
"saddlebrown" to 0x8b4513,
"salmon" to 0xfa8072,
"sandybrown" to 0xf4a460,
"seagreen" to 0x2e8b57,
"seashell" to 0xfff5ee,
"sienna" to 0xa0522d,
"silver" to 0xc0c0c0,
"skyblue" to 0x87ceeb,
"slateblue" to 0x6a5acd,
"slategray" to 0x708090,
"slategrey" to 0x708090,
"snow" to 0xfffafa,
"springgreen" to 0x00ff7f,
"steelblue" to 0x4682b4,
"tan" to 0xd2b48c,
"teal" to 0x008080,
"thistle" to 0xd8bfd8,
"tomato" to 0xff6347,
"turquoise" to 0x40e0d0,
"violet" to 0xee82ee,
"wheat" to 0xf5deb3,
"white" to 0xffffff,
"whitesmoke" to 0xf5f5f5,
"yellow" to 0xffff00,
"yellowgreen" to 0x9acd32
)

View File

@@ -0,0 +1,147 @@
package org.openrndr.extra.svg
/** Element tag constants */
internal object Tag {
const val CIRCLE = "circle"
const val DEFS = "defs"
const val ELLIPSE = "ellipse"
const val G = "g"
const val IMAGE = "image"
const val LINE = "line"
const val LINEAR_GRADIENT = "linearGradient"
const val PATH = "path"
const val POLYGON = "polygon"
const val POLYLINE = "polyline"
const val RADIAL_GRADIENT = "radialGradient"
const val RECT = "rect"
const val STOP = "stop"
const val SVG = "svg"
const val TBREAK = "tbreak"
const val TEXT = "text"
const val TEXT_AREA = "textArea"
const val TSPAN = "tspan"
const val USE = "use"
val containerList = listOf(
DEFS,
G,
SVG,
USE
)
val graphicsList = listOf(
CIRCLE,
ELLIPSE,
IMAGE,
LINE,
PATH,
POLYGON,
POLYLINE,
RECT,
STOP,
TBREAK,
TEXT,
TEXT_AREA,
TSPAN
)
}
/** Attribute key constants */
internal object Attr {
const val BASE_PROFILE = "baseProfile"
const val CLASS = "class"
const val CX = "cx"
const val CY = "cy"
const val D = "d"
const val DX = "dx"
const val DY = "dy"
const val GRADIENT_UNITS = "gradientUnits"
const val HEIGHT = "height"
const val ID = "id"
const val OFFSET = "offset"
const val PATH_LENGTH = "pathLength"
const val POINTS = "points"
const val PRESERVE_ASPECT_RATIO = "preserveAspectRatio"
const val R = "r"
const val ROTATE = "rotate"
const val RX = "rx"
const val RY = "ry"
const val SPACE = "xml:space"
const val STYLE = "style"
const val TRANSFORM = "transform"
const val VERSION = "version"
const val VIEW_BOX = "viewBox"
const val WIDTH = "width"
const val X = "x"
const val X1 = "x1"
const val X2 = "x2"
const val Y = "y"
const val Y1 = "y1"
const val Y2 = "y2"
}
/**
* org.openrndr.shape.Property key constants
* These can also be defined in a style sheet/attribute
*/
internal object Prop {
const val COLOR = "color"
const val DIRECTION = "direction"
const val DISPLAY = "display"
const val DISPLAY_ALIGN = "display-align"
const val FILL = "fill"
const val FILL_OPACITY = "fill-opacity"
const val FILL_RULE = "fill-rule"
const val FONT_FAMILY = "font-family"
const val FONT_SIZE = "font-size"
const val FONT_STYLE = "font-style"
const val FONT_VARIANT = "font-variant"
const val FONT_WEIGHT = "font-weight"
const val OPACITY = "opacity"
const val STOP_COLOR = "stop-color"
const val STOP_OPACITY = "stop-opacity"
const val STROKE = "stroke"
const val STROKE_DASHARRAY = "stroke-dasharray"
const val STROKE_DASHOFFSET = "stroke-dashoffset"
const val STROKE_LINECAP = "stroke-linecap"
const val STROKE_LINEJOIN = "stroke-linejoin"
const val STROKE_MITERLIMIT = "stroke-miterlimit"
const val STROKE_OPACITY = "stroke-opacity"
const val STROKE_WIDTH = "stroke-width"
const val TEXT_ALIGN = "text-align"
const val TEXT_ANCHOR = "text-anchor"
const val UNICODE_BIDI = "unicode-bidi"
const val VECTOR_EFFECT = "vector-effect"
const val VISIBILITY = "visibility"
val list = listOf(
COLOR,
DIRECTION,
DISPLAY,
DISPLAY_ALIGN,
FILL,
FILL_OPACITY,
FILL_RULE,
FONT_FAMILY,
FONT_SIZE,
FONT_STYLE,
FONT_VARIANT,
FONT_WEIGHT,
OPACITY,
STOP_COLOR,
STOP_OPACITY,
STROKE,
STROKE_DASHARRAY,
STROKE_DASHOFFSET,
STROKE_LINECAP,
STROKE_LINEJOIN,
STROKE_MITERLIMIT,
STROKE_OPACITY,
STROKE_WIDTH,
TEXT_ALIGN,
TEXT_ANCHOR,
UNICODE_BIDI,
VECTOR_EFFECT,
VISIBILITY
)
}

View File

@@ -0,0 +1,46 @@
package org.openrndr.extra.svg
import org.openrndr.shape.Shape
import org.openrndr.shape.ShapeContour
fun Shape.toSvg(): String {
val sb = StringBuilder()
contours.forEach {
it.segments.forEachIndexed { index, segment ->
if (index == 0) {
sb.append("M ${segment.start.x} ${segment.start.y}")
}
sb.append(
when (segment.control.size) {
1 -> "Q${segment.control[0].x} ${segment.control[0].y} ${segment.end.x} ${segment.end.y}"
2 -> "C${segment.control[0].x} ${segment.control[0].y} ${segment.control[1].x} ${segment.control[1].y} ${segment.end.x} ${segment.end.y}"
else -> "L${segment.end.x} ${segment.end.y}"
}
)
}
if (it.closed) {
sb.append("z")
}
}
return sb.toString()
}
fun ShapeContour.toSvg(): String {
val sb = StringBuilder()
segments.forEachIndexed { index, segment ->
if (index == 0) {
sb.append("M ${segment.start.x} ${segment.start.y}")
}
sb.append(
when (segment.control.size) {
1 -> "C${segment.control[0].x}, ${segment.control[0].y} ${segment.end.x} ${segment.end.y}"
2 -> "C${segment.control[0].x}, ${segment.control[0].y} ${segment.control[1].x} ${segment.control[1].y} ${segment.end.x} ${segment.end.y}"
else -> "L${segment.end.x} ${segment.end.y}"
}
)
}
if (closed) {
sb.append("z")
}
return sb.toString()
}