Is it possible to get the locations and dimensions of all boxes in a StandardForm expression renderation?
This is not yet a complete answer but I think it may set you on a viable path. We can style each element using StyleBox
as follows:
boxes = RowBox[{"(",
FractionBox[
RowBox[{RowBox[{"(", RowBox[{"a", "+", "b"}], ")"}], SuperscriptBox["c", "d"]}],
SqrtBox["e"]], ")"}];
colors = ColorData[54, "ColorList"]
boxesNew =
Module[{i = 1},
boxes /. s_String :>
(StyleBox[s, FontColor -> #, Background -> #] & @ colors[[i++]])
];
The result:
boxesNew // DisplayForm
The object selected, revealing its parts:
We can create an Image
with Rasterize
:
img = Rasterize[boxesNew // DisplayForm, "Image"];
This image could then be processed to identify each area of solid color and these mapped back to the color list in colors
. (The colors chosen are only for human readability; sequential RGB values might be used for many segments.)
Details depend of your specific needs but it seems that information you desire is at least partially accesible via FrontEnd`UndocumentedGetSelectionPacket
.
RowBox[{"(",
FractionBox[
RowBox[{RowBox[{"(", RowBox[{"a", "+", "b"}], ")"}],
SuperscriptBox["c", "d"]}], SqrtBox["e"]], ")"}
] // RawBoxes
Dynamic[
FrontEndExecute @ FrontEnd`UndocumentedGetSelectionPacket[EvaluationNotebook[]],
UpdateInterval -> 1
]
VirtualSelectionRectangle
seems to give {xmin, xmax}, {ymin, ymax}
where x
are pixel columns counted from left edge of the notebook and y
coordinate determines pixel row counting from top edge (NOT from the beginning of the notebook).