In Scala.js, modifying svg.Stylable.style - svg

Working with Scala.js, I have created some SVG elements like Text, Line, Rect, and now I am trying to set the style attribute with code like this, where element is of type svg.Stylable:
element.style.fillOpacity = "0.0"
element.style.stroke = "yellow"
element.style.strokeWidth = "2"
I tried different variations of above code, but the desired style does not realize, and when I inspect the element in the Browser, the style attribute is an empty String (""). I am able to set other attributes with no problem (e.g. x, y, width, height).
How do I set the style? Thanks!

For SVG modications in Scala.js I normally use the d3 library (scala.js facade: https://github.com/spaced/scala-js-d3).
Then u can use:
d3.select("#mySvgElement").attr("style", "stroke:yellow; stroke-width:2") // etc.
*Edit: mySvgElement would be the ID of the element i want to change the style for. You can also other different kind of selectors.

Related

tk.Text: Tag Font

Is there a way to get a proper Font instance out of a tag, so I can change just the size? I know I can dump it in tk.font.Font but that seems really inefficient.
for tag in self.tag_names():
#I need `f` to be a Font instance, not just a string
f = self.tag_cget(tag, 'font')
If you used font objects to create the tags, then you can get the font name from the tag and then use nametofont to convert it to an instance of tkinter.font.Font. However, this only works if the tag has a font associated with it, and if the font is a font object rather than shorthand notation (eg: ("Helvetica", 24, "bold")).
from tkinter.font import nametofont
...
for tag_name in self.tag_names():
font_name = self.tag_cget(tag_name, "font")
if font_name:
font = nametofont(font_name)
size = int(font.cget("size"))
font.configure(size = size + delta)
One method is to premake all of the Font instances and assign the instances to the applicable tag's font option. When you want to change the sizes, loop over the stored Font instances, and change their sizes directly. Every tag that uses your Font instances will change accordingly.

Changing all Shape's colors at once JavaFX

I'm writing a small program in which multiple shapes will be visible in JavaFX. I'm trying to create a button through which it is possible to change all the shape's color to the chosen one by the user.
Right now, I'm only able to do that by changing each shape individually in my lambda expression. It's okay for now since there's only three shapes, but it will be inconvenient further one. Can anyone think of a way to group all the shapes together and access the "setFill" method to change them all at once?
Here's the code:
// Calling Semi-Circle method
Arc semicircle1 = shapes1.getsemicircle();
// create Colors button and set is as invisible until a shape value is passed by user
Button buttonColors = new Button();
buttonColors.setText("Choose a color for the Shape");
buttonColors.setVisible(true);
buttonColors.setOnAction( e ->
{
if (textField1.getText().equalsIgnoreCase("Grey"))
{
label1.setText("Grey");
semicircle1.setFill(Color.GREY);
}
});
You may create some property and bind all shapes to it
final ObjectProperty<Paint> fillProperty = new SimpleObjectProperty(Color.GREY);
...
semicirle1.fillProperty().bind(fillProperty);
pentagon1.fillProperty().bind(fillProperty);
rectangle1.fillProperty().bind(fillProperty);
...
fillProperty.set(Color.RED);

How to adjust style of table cell border?

When I add a cell to a table, I can manipulate its thickness (borderBottomSize), its color (borderBottomColor) but not its style (dashed, dotted, longdashed, ...).
When I read the code, the PhpOffice\PhpWord\Style\Cell extends the PhpOffice\PhpWord\Style\Border.
And the border class has the attribute borderBottomStyle.
But how to configure that?
I also looked into the PhpOffice\PhpWord\Style\Line, there I can find some "dashStyles", but none of them are working...
My question: How to change style of border (from strong line to for example dotted)?
You can define the style of the border by using the cell style property borderStyle. By default, it's set to solid, but you can use other values like dashed, dotted, double, etc. Sadly, this feature is not documented so I don't know all the available styles.
Don't forget to add a border size as well, otherwise no border will be rendered.
Example for a dotted cell:
$dottedCell = $tableRow->addCell($cellWidth, ['borderStyle' => 'dotted', 'borderSize' => 6]);

D3js: finding path's bounding box (without getBBox() )?

The following code works on Chromium :
var node = window.d3.selectAll('#L1 > *:nth-child(2)');
var bbox = node.node().getBBox();
console.log(bbox) // {height: 44, width: 44, y: -13, x: 144}
but not with nodejs + jsdom:
"TypeError: Object [ PATH ] has no method 'getBBox' "
M. Bostock pointed out that JSDOM doesn't support getBBox()
What D3js replacement to use to get the bounding box of #L1 > *:nth-child(2) ?
Past efforts lead me there : getBBox() based fiddle
Path's bounding box
Digging straight into the element's path data d="..." should work. An svg line is basically a set of x,y points. Assuming absolute coordinates without translation nor big bezier curves, which is the case of my D3js-generated svg lines, I'am finding in this data the min and max values for both x and y.
To do so, I get the d="..." svg line or multilines code. For simplicity sake, I rudely removes possible relative jumps such h30 or v20 since I never saw any in my D3js output, then clean out letters (aka svg commands : M,L,H,V,C,S,Q,T,A,Z), simplify the spaces and line jumps, then split by the remaining spaces. I get a clean arrays of coordinates.
Important to note, my selector directly target a single non-translated path.
var getBBox = function(selector){
var xmin, xmax, ymin, ymax,p;
// clean up path
var t = d3.select(selector).attr("d"); // get svg line's code
console.log(t)
t = t.replace(/[a-z].*/g," ") // remove relative coords, could rather tag it for later processing to absolute!
.replace(/[\sA-Z]+/gi," ").trim().split(" "); // remove letters and simplify spaces.
console.log(t)
for(var i in t){ // set valid initial values
if(t[i].length>1){
p = t[i].split(",");
xmin = xmax = p[0]; ymin = ymax = p[1]; }
}
for(var i in t){ // update xmin,xmax,ymin,ymax
p = t[i].split(",");
if(!p[1]){ p[0]=xmin; p[1] = ymin;} // ignore relative jumps such h20 v-10
xmin = Math.min(xmin, p[0]);
xmax = Math.max(xmax, p[0]);
ymin = Math.min(ymin, p[1]);
ymax = Math.max(ymax, p[1]);
} return [[xmin,ymax],[xmax,ymin]]; // [[left, bottom], [right, top]] as for https://github.com/mbostock/d3/wiki/Geo-Paths#bounds
}
var bb = getBBox("path");
JSfiddle DEMO
Groups bounding boxes
For groups of multiple paths, you may want to traverse the svg DOM to loop upon each single path of the group in order to update xmin, ymin, xmax, ymax.
Translated elements
To handle translated elements, adapt further.
Alternatives
Other better approaches may exist. Remember to check if getBBox() and getBoundingClientRect() are available in your context, since they are native and very convenient.
The reason why getBBox/getBoundingClientRect/getClientRect does not work in NodeJS+JSDOM is that calculating these values of an SVG (or HTML) element involves massive amounts of computation.
First, all CSS code in <style> elements must be parsed (which is already not trivial). Then the CSS selectors, cascading and inheritance rules must be applied to know what size, position or line width an element has. And even after you know all style property values, you need to do some non-trivial maths to calculate the bounding boxes: definition of different SVG transform functions, compositions of these, bounding boxes of SVG primitives and Bezier curves. Browsers support all of these (they have to, in order to draw the element), but JSDOM is simply not meant for all of these.
But fortunately, canvg is a JavaScript implementation of most of SVG, which uses a <canvas> element to draw the image. It does support most of the above, and although it does not have an interface for giving you those data, fortunately it has very nice (and MIT licensed) code, so hopefully you can copy and reuse parts of it. As of now, the code is written in a single file, and it has CSS parsing, applying cascading rules, path data parsing, definitions of SVG transforms, applying transformations, and bezier curve bounding box calculation. That is, almost everything you need to calculate bounding boxes :) It does not, however, support CSS selectors, but it can reuse another library. But unfortunately, as far as I can tell, canvg is not ready for running in NodeJS, you probably need some tweaks.
There is, however canvgc, an SVG to JS compiler, which contains an older version of canvg, and it is capable of running in NodeJS. So it is easier to start with that.

Mathematica: how to have text in multiple colors?

I'd like to have a manipulate control, something like
{{wAB, 1, "AB"}, 0, 1, Appearance -> "Labeled"},
but would like the A and the B to be different colors, say, Red and Blue.
I can change the overall color with Style["AB",Red], but haven't been able to get the A and the B in different colors. Any help would be appreciated!
you mean like this?
Manipulate[
wAB,
{{wAB,1,Row[{Style["A", Red], Style["B", Blue]}]},0,1,Appearance->"Labeled"}
]
and if you prefer to define the decoration part separately (which can be useful for larger and more complicated controls) and reference it in controls later on (like declaring a variable, sort of, but it is a macro actually) and reuse it for different controls, then you can use With, like this
Manipulate[wAB,
Evaluate#With[
{myStyle = Row[{Style["A", Red], Style["B", Blue]}]},
{{wAB, 1, myStyle}, 0, 1, Appearance -> "Labeled"}
]
]
Although it is safer to do styling with Style you can actually color the characters of the string directly using the Format menu or keyboard shortcuts, and Mathematica will preserve it in the dynamic control:
If vertical position is x, you must print one letter on x, then count width (w) and print next letter with another color on the new position x+=w.

Resources