tags:

views:

56

answers:

2

I'm working on a project which will deliver small pieces of text to a display engine that will show them to the user. One of the requirements is rich styling: position, color, font, the works. Each transmission should stand alone, with its own embedded style information. I have already built a web service to deliver the text.

How should I represent this style metadata so that it's compact, flexible, easy to parse and easy to render? I haven't decided on a client to display the text, so it should be as presentation-agnostic as possible and easy to transform if I need to. I was thinking of using CSS, but I'm not on top of all the different ways to style text these days. What would you recommend?

A: 

I would say this is a perfect place for using XML. You can go way beyond the permissible values of CSS in defining your meta-information.

If you wanted something more compact you could send back objects via json with the same information.

CSS is really meant for rigid web-browser-compatible display. If that's not what you're doing here I wouldn't recommend it myself.

EDIT:

Example:

<data>
    <font>Garamond</font>
    <text>This is the message I'm sending back</text>
    <font-weight>bold</font-weight>
    <color-of-the-third-letter>green</color-of-the-third-letter>
</data>

CSS ultimately cannot be as specific as custom XML. It all depends what you want from it, though.

Gabriel Hurley
So, basically XHTML? Isn't that actually less flexible?
Chris McCall
Maybe he meant XAML not XML.
ChrisW
XML does not equal XHTML at all. XML can be used to systematically define pretty much anything as long as you're willing to define your namespace and definitions.
Gabriel Hurley
"How should I represent this style metadata so that it's *compact*, flexible, easy to parse and *easy to render*?" I know what XML is, and arbitrary XML only meets two out of the four requirements. XHTML meets three. CSS meets all four.
Chris McCall
+2  A: 

Some more specifics about what you're trying to do might be useful. As far as I can tell though, I don't see any real reason not to use CSS or something similar.

It's easy to type, easy to learn, widely known, and there are pre-existing engines for rendering it. It can do all the basics of text styling and positioning. (And CSS3 transforms can give a lot more flexibility in terms of text positioning.) And if you end up implementing your own rendering engine for some reason, it's easy to parse and there aren't too many rules if you only need to worry about text and absolute positioning.

There are a couple of reasons I can see for not using CSS. One is if you need more advanced transformations--say, you want to skew or distort the text (in cases like this, you're going to need to end up rendering an image instead of text). In that case, I don't know of anything pre-existing that will fit your needs. (I think the closest match in that case would be SVG, but then you lose any prospect of it remaining at all simple or easy to use.)

Sam DeFabbia-Kane
+1: Unless you're trying to perform some crazy voodoo magic on the text, CSS should give you all the control you need.
EvilChookie