ExcelNumberFormat icon indicating copy to clipboard operation
ExcelNumberFormat copied to clipboard

Support rich formatting

Open andersnm opened this issue 7 years ago • 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.

andersnm avatar Nov 01 '17 12:11 andersnm

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.

igitur avatar Nov 01 '17 16:11 igitur

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.

andersnm avatar Nov 01 '17 18:11 andersnm

I'd prefer not to have to parse a string again. It will severely impact performance.

On 1 Nov 2017 20:11, "andersnm" [email protected] wrote:

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.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/andersnm/ExcelNumberFormat/issues/7#issuecomment-341191966, or mute the thread https://github.com/notifications/unsubscribe-auth/AAI5vqhk8gsigwN08ZhBRwgRdiv5pFpVks5syLRmgaJpZM4QOHqS .

igitur avatar Nov 01 '17 20:11 igitur