+ double sign_height = 1.0; // meter
+ bool lighted = true;
+ string newmat = "BlackSign";
+
+ vector<element_info *> elements;
+ element_info *close = 0;
+ double total_width = 0.0;
+ bool cmd = false;
+ int size = -1;
+ char oldtype = 0, newtype = 0;
+
+ osg::Group* object = new osg::Group;
+ object->setName(content);
+
+ SGMaterial *material;
+ osg::StateSet *lighted_state;
+ osg::StateSet *unlighted_state;
+
+ // Part I: parse & measure
+ for (const char *s = content.data(); *s; s++) {
+ string name;
+ string value;
+
+ if (*s == '{') {
+ if (cmd)
+ SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected { in sign contents");
+ cmd = true;
+ continue;
+
+ } else if (*s == '}') {
+ if (!cmd)
+ SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unexpected } in sign contents");
+ cmd = false;
+ continue;
+
+ } else if (!cmd) {
+ name = *s;
+
+ } else {
+ if (*s == ',')
+ continue;
+
+ for (; *s; s++) {
+ name += *s;
+ if (s[1] == ',' || s[1] == '}' || s[1] == '=')
+ break;
+ }
+ if (!*s) {
+ SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
+ } else if (s[1] == '=') {
+ for (s += 2; *s; s++) {
+ value += *s;
+ if (s[1] == ',' || s[1] == '}')
+ break;
+ }
+ if (!*s)
+ SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "unclosed { in sign contents");
+ }
+
+ if (name == "@size") {
+ sign_height = strtod(value.data(), 0);
+ continue;
+ }
+
+ if (name == "@light") {
+ lighted = (value != "0" && value != "no" && value != "off" && value != "false");
+ continue;
+ }
+
+ if (name == "@material") {
+ newmat = value.data();
+ continue;
+
+ } else if (name.size() == 2 || name.size() == 3) {
+ string n = name;
+ if (n.size() == 3 && n[2] >= '1' && n[2] <= '5') {
+ size = n[2] - '1';
+ n = n.substr(0, 2);
+ }
+
+ if (n == "@Y") {
+ if (size < 3) {
+ sign_height = HT[size < 0 ? 2 : size];
+ newmat = "YellowSign";
+ newtype = 'Y';
+ continue;
+ }
+
+ } else if (n == "@R") {
+ if (size < 3) {
+ sign_height = HT[size < 0 ? 2 : size];
+ newmat = "RedSign";
+ newtype = 'R';
+ continue;
+ }
+
+ } else if (n == "@L") {
+ if (size < 3) {
+ sign_height = HT[size < 0 ? 2 : size];
+ newmat = "FramedSign";
+ newtype = 'L';
+ continue;
+ }
+
+ } else if (n == "@B") {
+ if (size < 0 || size == 3 || size == 4) {
+ sign_height = HT[size < 0 ? 3 : size];
+ newmat = "BlackSign";
+ newtype = 'B';
+ continue;
+ }
+ }
+ }
+
+ for (int i = 0; cmds[i].keyword; i++) {
+ if (name == cmds[i].keyword) {
+ name = cmds[i].glyph_name;
+ break;
+ }
+ }
+
+ if (name[0] == '@') {
+ SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown command `" << name << '\'');
+ continue;
+ }
+ }
+
+ if (newmat.size()) {
+ material = matlib->find(newmat);
+ if (!material) {
+ SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << newmat << '\'');
+ continue;
+ }
+
+ // set material states (lighted & unlighted)
+ lighted_state = material->get_state();
+ string u = newmat + ".unlighted";
+
+ SGMaterial *m = matlib->find(u);
+ if (m) {
+ unlighted_state = m->get_state();
+ } else {
+ SG_LOG(SG_TERRAIN, SG_ALERT, SIGN "ignoring unknown material `" << u << '\'');
+ unlighted_state = lighted_state;
+ }
+ newmat = "";
+ }
+
+ SGMaterialGlyph *glyph = material->get_glyph(name);
+ if (!glyph) {
+ SG_LOG( SG_TERRAIN, SG_ALERT, SIGN "unsupported glyph `" << *s << '\'');
+ continue;
+ }
+
+ // in managed mode push frame stop and frame start first
+ osg::StateSet *state = lighted ? lighted_state : unlighted_state;
+ element_info *e;
+ if (newtype && newtype != oldtype) {
+ if (close) {
+ elements.push_back(close);
+ total_width += close->glyph->get_width() * close->scale;
+ close = 0;
+ }
+ oldtype = newtype;
+ SGMaterialGlyph *g = material->get_glyph("stop-frame");
+ if (g)
+ close = new element_info(material, state, g, sign_height);
+ g = material->get_glyph("start-frame");
+ if (g) {
+ e = new element_info(material, state, g, sign_height);
+ elements.push_back(e);
+ total_width += e->glyph->get_width() * e->scale;
+ }
+ }
+ // now the actually requested glyph
+ e = new element_info(material, state, glyph, sign_height);
+ elements.push_back(e);
+ total_width += e->glyph->get_width() * e->scale;
+ }