andersnm/ExcelNumberFormat

Support rich formatting

andersnm opened this issue · 3 comments

Specifically: global color and alignment, variable width space, repeat-to-fill characters

The basic goal is to return something allowing the caller to render the formatted value more precisely with an arbitrary rendering backend, like f.ex a 2D canvas, SVG, HTML, while remaining neutral to font size and cell width.

@igitur FYI, these are my thoughts but have no immediate plans to implement myself.

F.ex a new method NumberFormat.FormatRich() could return a class shape like:

class FormatResult {
	Alignment Alignment
	Color Color
	List<FormatCommand> Commands
}

class FormatCommand {
	Command Command
	string Text
}

enum Command {
	SpaceForText,
	RepeatText,
	Text
}

enum Alignment {
	Left,
	Right,
	Center
}

enum Color {
	Red,
	Black,
	...
}

The Formatter class should probably operate on these classes internally instead of a StringBuilder, and rewrite the current Format() method to reduce the FormatResult to a string.

And I don't necessarily think these are good class/enum/method names: finding good names is the hardest.

So do you want to return an abstract syntax tree or something like that that represents the rich text? It will be left to the user to render the AST as he wants to.

Essentially yes, that's the idea. The user is given a list of strings with rendering hints + global color/alignment info.

Alternatively, the library could of returned a single string with escaped formatting codes, or even XML/JSON, but then users need to parse that to render it. Such an approach would look like:

string NumberFormat.FormatRich(object value, CultureInfo, out Alignment alignment, out Color color)

That's not too bad. The returned string mostly looks like a normal formatted value, except contains escape codes for the '_' and '*' space and repeat characters.

At the moment I have no strong preference for either direction, both should work.