1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
|
/* Copyright (C) 2004 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/**
* @file myx_gc_svgparser.cpp
* @brief Parser for svg elements, which are converted to OpenGL calls.
*
*/
#include "myx_gc_gl_helper.h"
#include "myx_gc_svgparser.h"
//----------------------------------------------------------------------------------------------------------------------
typedef enum
{
GC_SVG_PRIMITIVE_UNKNOWN = -1,
GC_SVG_PRIMITIVE_RECT,
GC_SVG_PRIMITIVE_LINE,
GC_SVG_PRIMITIVE_POLYLINE,
GC_SVG_PRIMITIVE_POLYGON,
GC_SVG_PRIMITIVE_CIRCLE,
GC_SVG_PRIMITIVE_TEXT,
GC_SVG_PRIMITIVE_TSPAN,
GC_SVG_PRIMITIVE_GROUP,
GC_SVG_PRIMITIVE_IMAGE
} TSvgPrimitive;
extern const string defaultFontFamily;
extern const string defaultFontStyle;
extern const string defaultFontWeight;
extern const int defaultFontSize;
//----------------------------------------------------------------------------------------------------------------------
/**
* Converts a primitive name to a number.
*
* @param name The name to convert.
* @result The primitive that corresponds to the given name.
*/
TSvgPrimitive findPrimitive(string name)
{
static map<string, TSvgPrimitive> primitives;
if (primitives.size() == 0)
{
// Initialize the map first if necessary.
primitives["rect"] = GC_SVG_PRIMITIVE_RECT;
primitives["line"] = GC_SVG_PRIMITIVE_LINE;
primitives["polyline"] = GC_SVG_PRIMITIVE_POLYLINE;
primitives["polygon"] = GC_SVG_PRIMITIVE_POLYGON;
primitives["circle"] = GC_SVG_PRIMITIVE_CIRCLE;
primitives["text"] = GC_SVG_PRIMITIVE_TEXT;
primitives["tspan"] = GC_SVG_PRIMITIVE_TSPAN;
primitives["g"] = GC_SVG_PRIMITIVE_GROUP;
primitives["image"] = GC_SVG_PRIMITIVE_IMAGE;
};
map<string, TSvgPrimitive>::iterator iterator = primitives.find(name);
if (iterator == primitives.end())
return GC_SVG_PRIMITIVE_UNKNOWN;
else
return iterator->second;
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Scans the given string for coordinate values and fills the Points vector with them.
*/
void parsePoints(xmlChar* Raw, CVertexVector* Points)
{
Points->clear();
StringTokenizer tokenizer((char*) Raw, " ,\t\n", true);
while (tokenizer.hasMoreTokens())
{
// Invalid input results in a value of 0, so we don't need to take special
// care for such cases.
TVertex v;
v.x = tokenizer.nextTokenAsFloat();
if (tokenizer.hasMoreTokens())
{
v.y = tokenizer.nextTokenAsFloat();
Points->push_back(v);
};
};
}
//----------------- CSVGParser -----------------------------------------------------------------------------------------
CSVGParser::CSVGParser(void)
{
}
//----------------------------------------------------------------------------------------------------------------------
CSVGParser::~CSVGParser(void)
{
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Parses the content of a circle definition.
*
* @see parseElement for a description of the parameters.
*/
void CSVGParser::parseCircle(xmlNodePtr svg, bool doFill, GLubyte* fillColor, bool doStroke, GLubyte* strokeColor,
float strokeWidth)
{
GLUquadricObj *quadric = gluNewQuadric();
float x = getFloatAttributeDef(svg, "cx", 0);
float y = getFloatAttributeDef(svg, "cy", 0);
float innerRadius = getFloatAttributeDef(svg, "inner-radius", 0);
float outerRadius = getFloatAttributeDef(svg, "r", 1);
float startAngle = getFloatAttributeDef(svg, "start-angle", 0);
float sweepAngle = getFloatAttributeDef(svg, "sweep-angle", 360);
GLint slices = getIntAttributeDef(svg, "slices", 10);
GLint loops = getIntAttributeDef(svg, "rings", 3);
// Update the accumulated bounding box.
GLfloat matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
FBoundsComputer.include(matrix, x - outerRadius, y - outerRadius);
FBoundsComputer.include(matrix, x + outerRadius, y + outerRadius);
gluQuadricOrientation(quadric, GLU_INSIDE);
if (fillColor != NULL)
glColor4ubv(fillColor);
glTranslatef(x, y, 0);
if (doFill)
{
gluQuadricDrawStyle(quadric, GLU_FILL);
gluQuadricNormals(quadric, GLU_SMOOTH);
gluQuadricTexture(quadric, GL_TRUE);
gluPartialDisk(quadric, innerRadius, outerRadius, slices, loops, startAngle, sweepAngle);
};
if (doStroke)
{
if (strokeColor != NULL)
glColor4ubv(strokeColor);
gluQuadricDrawStyle(quadric, GLU_SILHOUETTE);
gluPartialDisk(quadric, innerRadius, outerRadius, slices, loops, startAngle, sweepAngle);
};
gluDeleteQuadric(quadric);
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Recursively called function that parses the given svg xml element and converts it to OpenGL calls.
*
* @param svg Any of the supported primitives.
* @param doFill The parent's fill flag. If this is true then also this element is filled.
* @param fillColor The parent's fill color. Only used if the we have a local opacity.
* @param doStroke The parent's outline flag. If this true then also this element is outlined.
* @param strokeColor The parent's stroke color. Only used if the we have a local opacity.
* @param strokeWidth The parent's stroke width. Used only if we draw an outline at all and no local width is given.
* @param masterAlpha The accumulated alpha value, which is currently active. Used in conjunction with a local opacity.
* @return Returns a new display list for the content of this element.
*/
GLuint CSVGParser::parseElement(xmlNodePtr svg, bool doFill, GLubyte* fillColor, bool doStroke, GLubyte* strokeColor,
float strokeWidth, float masterAlpha)
{
TSvgPrimitive primitive = findPrimitive((char*) svg->name);
CGCTexture* texture = NULL;
GLuint resultList = 0;
if (primitive != GC_SVG_PRIMITIVE_UNKNOWN)
{
bool display = true;
xmlChar* attribute = xmlGetProp(svg, (xmlChar*) "display");
if (attribute != NULL)
{
display = xmlStrcmp(attribute, (const xmlChar *) "none") != 0;
xmlFree(attribute);
};
if (display)
{
attribute = xmlGetProp(svg, (xmlChar*) "texture");
if (attribute != NULL)
{
texture = FModel->texture(utf8ToUtf16((char*) attribute));
xmlFree(attribute);
};
// See if we have a local color and/or a local opacity.
float opacity = masterAlpha;
bool hasOpacity = getFloatAttribute(svg, "opacity", opacity);
if (hasOpacity)
opacity *= masterAlpha;
GLubyte localFillColor[4];
GLubyte* fillColorPtr = NULL;
int conversion = convertColor(svg, "fill", localFillColor);
// Fill this element if either filling is not completely disabled (fill ="none")
// and either a local color is given or the parent element used filling already.
bool localFill = (conversion != 3) && (doFill || fillColor != NULL || conversion == 0);
if (localFill)
{
// If there is no valid local color then use that of the parent.
if (conversion != 0)
{
// Combine the parent color with this opacity to create a new color.
localFillColor[0] = fillColor[0];
localFillColor[1] = fillColor[1];
localFillColor[2] = fillColor[2];
};
localFillColor[3] = GLubyte(255 * opacity);
fillColorPtr = localFillColor;
};
GLubyte localStrokeColor[4];
GLubyte* strokeColorPtr = NULL;
conversion = convertColor(svg, "stroke", localStrokeColor);
bool localStroke = (conversion != 3) && (doStroke || strokeColor != NULL || conversion == 0);
if (localStroke)
{
// If there is no valid local color then use that of the parent.
if (conversion != 0)
{
// Combine the parent color with this opacity to create a new color.
localStrokeColor[0] = strokeColor[0];
localStrokeColor[1] = strokeColor[1];
localStrokeColor[2] = strokeColor[2];
};
localStrokeColor[3] = GLubyte(255 * opacity);
strokeColorPtr = localStrokeColor;
};
float localStrokeWidth = getFloatAttributeDef(svg, "stroke-width", strokeWidth);
// Prepare child display lists before starting with the one for this element.
GLuint localList = 0;
switch (primitive)
{
case GC_SVG_PRIMITIVE_TEXT:
case GC_SVG_PRIMITIVE_TSPAN:
{
localList = parseText(svg, localFill, fillColorPtr, localStroke, strokeColorPtr, localStrokeWidth, opacity);
break;
};
case GC_SVG_PRIMITIVE_GROUP:
{
localList = parseGroup(svg, localFill, fillColorPtr, localStroke, strokeColorPtr, localStrokeWidth, opacity);
break;
};
case GC_SVG_PRIMITIVE_IMAGE:
{
parseImage(svg, false);
break;
};
};
if (texture != NULL)
texture->validateHandle();
resultList = glGenLists(1);
// We need the display lists executed while being compiled to have the modelview matrix updated correctly.
// This is needed to compute the bounding box correctly.
glNewList(resultList, GL_COMPILE_AND_EXECUTE);
glPushMatrix();
if (texture != NULL)
texture->activateTexture();
attribute = xmlGetProp(svg, (xmlChar*) "transform");
if (attribute != NULL)
{
parseTransformation((char*) attribute);
xmlFree(attribute);
};
switch (primitive)
{
case GC_SVG_PRIMITIVE_RECT:
{
parseRectangle(svg, localFill, fillColorPtr, localStroke, strokeColorPtr, localStrokeWidth);
break;
};
case GC_SVG_PRIMITIVE_LINE:
{
parseLine(svg, localStroke, strokeColorPtr, localStrokeWidth);
break;
};
case GC_SVG_PRIMITIVE_POLYLINE:
{
parsePolyline(svg, localFill, fillColorPtr, localStroke, strokeColorPtr, localStrokeWidth);
break;
};
case GC_SVG_PRIMITIVE_POLYGON:
{
parsePolygon(svg, localFill, fillColorPtr, localStroke, strokeColorPtr, localStrokeWidth);
break;
};
case GC_SVG_PRIMITIVE_CIRCLE:
{
parseCircle(svg, localFill, fillColorPtr, localStroke, strokeColorPtr, localStrokeWidth);
break;
};
case GC_SVG_PRIMITIVE_TEXT:
case GC_SVG_PRIMITIVE_TSPAN:
case GC_SVG_PRIMITIVE_GROUP:
{
if (localList != 0)
glCallList(localList);
break;
};
case GC_SVG_PRIMITIVE_IMAGE:
{
parseImage(svg, true);
break;
};
};
if (texture != NULL)
texture->deactivateTexture();
glPopMatrix();
glEndList();
};
};
return resultList;
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Collects all data in an <svg:g> element.
*
* @return A new display list comprising all subelements.
* @see parseElement for the description of the parameters.
*/
GLuint CSVGParser::parseGroup(xmlNodePtr svg, bool doFill, GLubyte* fillColor, bool doStroke, GLubyte* strokeColor,
float strokeWidth, float masterAlpha)
{
vector<GLuint> lists;
for (xmlNodePtr child = svg->children; child != NULL; child = child->next)
if (child->type == XML_ELEMENT_NODE)
{
GLuint List = parseElement(child, doFill, fillColor, doStroke, strokeColor, strokeWidth, masterAlpha);
lists.push_back(List);
};
GLuint resultList = glGenLists(1);
glNewList(resultList, GL_COMPILE);
for (vector<GLuint>::iterator iterator = lists.begin(); iterator != lists.end(); iterator++)
glCallList(*iterator);
glEndList();
return resultList;
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Parses the content of an image definition.
*
* @param svg The XML svg element containing the definition.
* @param render If true then the appropriate OpenGL calls for rendering are issued, otherwise setup is performed.
*/
void CSVGParser::parseImage(xmlNodePtr svg, bool render)
{
string filename = getStringAttributeDef(svg, "href", "");
wstring id = utf8ToUtf16(getStringAttributeDef(svg, "id", filename));
if (!render)
{
// Create the image only if not yet done.
CGCTexture* texture = FModel->texture(id);
if (texture == NULL)
{
TLODList lodData;
lodData.push_back(utf8ToUtf16(filename));
texture = FModel->createTexture(lodData, id, "clamp-to-edge", "clamp-to-edge", "linear-mipmap-linear",
"linear", 2, "modulate");
};
// Make sure the handle of the texture is created before a display list is created.
texture->validateHandle();
}
else
{
float x = getFloatAttributeDef(svg, "x", 0);
float y = getFloatAttributeDef(svg, "y", 0);
float width = getFloatAttributeDef(svg, "width", 0);
float height = getFloatAttributeDef(svg, "height", 0);
// Update the accumulated bounding box.
GLfloat matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
FBoundsComputer.include(matrix, x, y);
FBoundsComputer.include(matrix, x + width, y + height);
CGCTexture* texture = FModel->texture(id);
if (texture)
texture->activateTexture();
glColor4f(1, 1, 1, 1);
glTranslatef(x, y, 0);
glBegin(GL_POLYGON);
glTexCoord2d(0, 0);
glVertex2f(0, 0);
// glNormal3f(0, 0, 1);
glTexCoord2d(1, 0);
glVertex2f(width, 0);
// glNormal3f(0, 0, 1);
glTexCoord2d(1, 1);
glVertex2f(width, height);
// glNormal3f(0, 0, 1);
glTexCoord2d(0, 1);
glVertex2f(0, height);
// glNormal3f(0, 0, 1);
glEnd();
if (texture)
texture->deactivateTexture();
};
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Parses the content of a line definition.
*
* @param svg The XML svg element containing the definition.
* @param doStroke Flag to indicate if the line is to be drawn or not.
* @param strokeColor The color to be used for the line (if set).
* @param strokeWidth The width of the line.
*/
void CSVGParser::parseLine(xmlNodePtr svg, bool doStroke, GLubyte* strokeColor, float strokeWidth)
{
float X1 = getFloatAttributeDef(svg, "x1", 0);
float Y1 = getFloatAttributeDef(svg, "y1", 0);
float X2 = getFloatAttributeDef(svg, "x2", 0);
float Y2 = getFloatAttributeDef(svg, "y2", 0);
// Update the accumulated bounding box.
GLfloat matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
FBoundsComputer.include(matrix, X1, Y1);
FBoundsComputer.include(matrix, X2, Y2);
if (doStroke)
{
if (strokeColor != NULL)
glColor4ubv(strokeColor);
glLineWidth(strokeWidth);
glBegin(GL_LINES);
glVertex2f(X1, Y1);
// glNormal3f(0, 0, 1);
glVertex2f(X2, Y2);
// glNormal3f(0, 0, 1);
glEnd();
};
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Parses the content of a polygon definition.
*
* @see parseElement for a description of the parameters.
*/
void CSVGParser::parsePolygon(xmlNodePtr svg, bool doFill, GLubyte* fillColor, bool doStroke, GLubyte* strokeColor,
float strokeWidth)
{
xmlChar* Raw = xmlGetProp(svg, (xmlChar*) "points");
if (Raw != NULL)
{
CVertexVector vertices;
parsePoints(Raw, &vertices);
xmlFree(Raw);
renderVertices(doFill, fillColor, doStroke, strokeColor, vertices, NULL, strokeWidth, true);
};
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Parses the content of a polyline definition.
*
* @see parseElement for a description of the parameters.
*/
void CSVGParser::parsePolyline(xmlNodePtr svg, bool doFill, GLubyte* fillColor, bool doStroke, GLubyte* strokeColor,
float strokeWidth)
{
xmlChar* Raw = xmlGetProp(svg, (xmlChar*) "points");
if (Raw != NULL)
{
CVertexVector vertices;
parsePoints(Raw, &vertices);
xmlFree(Raw);
renderVertices(doFill, fillColor, doStroke, strokeColor, vertices, NULL, strokeWidth, false);
};
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Parses the content of a rectangle definition.
*
* @see parseElement for a description of the parameters.
*/
void CSVGParser::parseRectangle(xmlNodePtr svg, bool doFill, GLubyte* fillColor, bool doStroke, GLubyte* strokeColor,
float strokeWidth)
{
float x, y, width, height, RX, RY;
if (!getFloatAttribute(svg, "x", x))
x = 0;
if (!getFloatAttribute(svg, "y", y))
y = 0;
if (!getFloatAttribute(svg, "width", width))
width = 1;
if (!getFloatAttribute(svg, "height", height))
height = 1;
// There are a few more things to check with the corner radii.
bool RXfound = getFloatAttribute(svg, "rx", RX);
bool RYfound = getFloatAttribute(svg, "ry", RY);
if (!RXfound && !RYfound)
{
RX = 0;
RY = 0;
}
else
{
if (!RXfound)
RX = RY;
else
if (!RYfound)
RY = RX;
if (RX > width / 2)
RX = width / 2;
if (RY > width / 2)
RY = width / 2;
};
// TODO: Consider rounded corners.
CVertexVector vertices;
TVertex v;
v.x = x;
v.y = y;
vertices.push_back(v);
v.x = x + width;
vertices.push_back(v);
v.y = y + height;
vertices.push_back(v);
v.x = x;
vertices.push_back(v);
CVertexVector TextureCoordinates;
v.x = 0;
v.y = 0;
TextureCoordinates.push_back(v);
v.x = width;
TextureCoordinates.push_back(v);
v.y = height;
TextureCoordinates.push_back(v);
v.x = 0;
TextureCoordinates.push_back(v);
renderVertices(doFill, fillColor, doStroke, strokeColor, vertices, &TextureCoordinates, strokeWidth, true);
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Takes a text node and gets all attributes for direct or dynamic rendering. This function is called recursively
* and can take either a <text> or a <tspan> node.
*
* @param svg The text or tspan node to parse.
* @param fillColor The color for the text interior.
* @param strokeColor The color of the outline.
* @param The width of the outlines.
* @param masterAlpha Only passed through because text nodes can have children.
* @return A new display list comprising all subelements.
*/
GLuint CSVGParser::parseText(xmlNodePtr svg, bool doFill, GLubyte* fillColor, bool doStroke, GLubyte* strokeColor,
float strokeWidth, float masterAlpha)
{
// Collect font information.
string fontFamily = getStringAttributeDef(svg, "font-family", defaultFontFamily);
string fontStyle = getStringAttributeDef(svg, "font-style", defaultFontStyle);
string fontWeightString = getStringAttributeDef(svg, "font-weight", defaultFontWeight);
int weight = convertFontWeight(fontWeightString);
int fontSize = getIntAttributeDef(svg, "font-size", defaultFontSize);
vector<GLuint> lists;
xmlNodePtr child = svg->children;
while (child != NULL)
{
if (XML_IS(child, "tspan"))
{
GLuint localList = parseElement(child, doFill, fillColor, doStroke, strokeColor, strokeWidth, masterAlpha);
lists.push_back(localList);
}
else
{
string source((char*) child->content);
wstring output = utf8ToUtf16(source);
// Make the font manager create all display lists before we build ours.
string fontId = fontManager()->fontIdCreate(fontFamily, fontStyle, weight, fontSize);
TBoundingBox box;
fontManager()->boundingBox(output, fontId, box);
// Update the accumulated bounding box.
GLfloat matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
FBoundsComputer.include(matrix, box);
GLuint localList = glGenLists(1);
glNewList(localList, GL_COMPILE_AND_EXECUTE);
if (fillColor != NULL)
glColor4ubv(fillColor);
// FTGL uses the lower left corner as origin, we however have the upper left corner for this task.
// Adjust the vertical coordinate accordingly.
glRasterPos2f(0, box.lower.y);
fontManager()->textOut(output, fontId);
glEndList();
lists.push_back(localList);
};
child = child->next;
};
// Create final display list.
GLuint displayList = glGenLists(1);
glNewList(displayList, GL_COMPILE);
float x = getFloatAttributeDef(svg, "x", 0);
float y = getFloatAttributeDef(svg, "y", 0);
glTranslatef(x, y, 0);
if (fillColor != NULL)
glColor4ubv(fillColor);
for (vector<GLuint>::iterator iterator = lists.begin(); iterator != lists.end(); iterator++)
glCallList(*iterator);
glEndList();
return displayList;
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Parsers the given string and interprets the content as one or more transformation of the form:
* translate(x, y, z) scale(x, y, z) etc.
*
* @param transformation The textual representation of the transformation to parse and perform.
*/
void CSVGParser::parseTransformation(char* transformation)
{
string delimiters(" ,()\t\n");
StringTokenizer tokenizer(transformation, delimiters, true);
// Each run in the outer loop is one transformation.
while (tokenizer.hasMoreTokens())
{
// Skip leading white spaces.
wstring token = tokenizer.nextToken();
int type = 0;
if (token == L"matrix")
type = 1;
else
if (token == L"translate")
type = 2;
else
if (token == L"scale")
type = 3;
else
if (token == L"rotate")
type = 4;
else
if (token == L"skewX")
type = 5;
else
if (token == L"skewY")
type = 6;
else
break;
switch (type)
{
case 1: // matrix
{
// Argument list found. Read 6 float numbers.
// | a d 0 0 |
// | b e 0 0 |
// | 0 0 1 0 |
// | c f 0 1 |
GLfloat matrix[16] = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
for (int j = 0; j < 2; j++)
for (int i = 0; i < 2; i++)
matrix[j * 4 + i] = tokenizer.nextTokenAsFloat();
matrix[12] = tokenizer.nextTokenAsFloat();
matrix[13] = tokenizer.nextTokenAsFloat();
glMultMatrixf(matrix);
break;
};
case 2: // Translation
{
// One or two floats are possible here.
float tx = tokenizer.nextTokenAsFloat();
float ty = 0;
if (tokenizer.hasMoreTokens())
ty = tokenizer.nextTokenAsFloat();
glTranslatef(tx, ty, 0);
break;
};
case 3: // Scaling
{
// One or two floats are possible here.
float sx = tokenizer.nextTokenAsFloat();
float sy = sx;
if (tokenizer.hasMoreTokens())
sy = tokenizer.nextTokenAsFloat();
glScalef(sx, sy, 0);
break;
};
case 4: // Rotation
{
// An angle (in degrees) and an optional rotation point are possible here.
float Angle = tokenizer.nextTokenAsFloat();
float x = 0;
float y = 0;
if (tokenizer.hasMoreTokens())
{
x = tokenizer.nextTokenAsFloat();
y = tokenizer.nextTokenAsFloat();
};
glTranslatef(x, y, 0);
glRotatef(Angle, 0, 0, 1);
glTranslatef(-x, -y, 0);
break;
};
case 5: // x Skewing
case 6: // y Skewing
{
// Only one value is expected, which gives the skewing angle in degrees.
GLdouble matrix[16] = {
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
int index = (type == 5) ? 4 : 1;
matrix[index] = tan(tokenizer.nextTokenAsFloat() * M_PI / 180);
glMultMatrixd(matrix);
break;
};
};
};
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Renders the given set of vertices.
*
* @param doFill Filled output is only done if this flag is true. For strokes the existance of the color as such is used
* as indicator.
* @param fillColor If not NULL then it gives the local color for this call, otherwise the current color stays untouched.
* @param strokeColor If not nULL then the vertices are render again as lines but using that color.
* @param vertices The vertex data to render.
* @param TextureCoordinates If given (can be NULL) then there must be exactly the same number of texture coordinates
* as there are vertices.
* @param strokeWidth The width of the border if one is rendered.
* @param closeShape Determines whether the border line (if strokeColor is given) is closed (that is, the last point is
* connected to the first point).
*/
void CSVGParser::renderVertices(bool doFill, GLubyte* fillColor, bool doStroke, GLubyte* strokeColor,
const CVertexVector& vertices, CVertexVector* TextureCoordinates, float strokeWidth,
bool closeShape)
{
GLfloat matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
if (doFill)
{
if (fillColor != NULL)
glColor4ubv(fillColor);
glBegin(GL_POLYGON);
for (int i = 0; i < (int) vertices.size(); i++)
{
if (TextureCoordinates != NULL)
glTexCoord2d((*TextureCoordinates)[i].x, (*TextureCoordinates)[i].y);
glVertex2f(vertices[i].x, vertices[i].y);
// glNormal3f(0, 0, 1);
// Update the accumulated bounding box.
FBoundsComputer.include(matrix, vertices[i]);
};
glEnd();
};
if (doStroke)
{
if (strokeColor != NULL)
glColor4ubv(strokeColor);
if (strokeWidth <= 1)
{
glBegin(closeShape ? GL_LINE_LOOP : GL_LINE_STRIP);
for (int i = 0; i < (int) vertices.size(); i++)
{
glVertex2f(vertices[i].x, vertices[i].y);
// glNormal3f(0, 0, 1);
};
glEnd();
}
else
{
// render small rectangles for each line.
float halfWidth = strokeWidth / 2;
vector<TVertex>::size_type index = 0;
vector<TVertex>::size_type count = vertices.size();
if (!closeShape)
count--;
while (index < count)
{
vector<TVertex>::size_type NextIndex = index + 1;
if (index == vertices.size() - 1)
NextIndex = 0;
// We need the angle of the current line relative to the horizontal axis.
float dX = vertices[NextIndex].x - vertices[index].x;
float dY = vertices[NextIndex].y - vertices[index].y;
float Angle = atan2(dY, dX);
// Compute the four corners for the rectangle. We can use symmetry to speed up computation.
dX = halfWidth * sin(Angle);
dY = halfWidth * cos(Angle);
glBegin(GL_POLYGON);
glVertex2f(vertices[index].x - dX, vertices[index].y + dY);
// glNormal3f(0, 0, 1);
FBoundsComputer.include(matrix, vertices[index]);
glVertex2f(vertices[NextIndex].x - dX, vertices[NextIndex].y + dY);
// glNormal3f(0, 0, 1);
FBoundsComputer.include(matrix, vertices[index]);
glVertex2f(vertices[NextIndex].x + dX, vertices[NextIndex].y - dY);
// glNormal3f(0, 0, 1);
FBoundsComputer.include(matrix, vertices[index]);
glVertex2f(vertices[index].x + dX, vertices[index].y - dY);
// glNormal3f(0, 0, 1);
FBoundsComputer.include(matrix, vertices[index]);
glEnd();
index++;
};
};
};
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Parses the given svg xml description and converts it to OpenGL calls. This method can be called within an active
* OpenGL display list compilation (but not between glBegin/glEnd).
*
* @param svg The svg top level element (<svg:svg> </svg:svg>).
* @param displayList The OpenGL display list to render into.
*/
void CSVGParser::convert(xmlNodePtr svg, GLuint displayList)
{
vector<GLuint> lists;
for (xmlNodePtr child = svg->children; child != NULL; child = child->next)
if (child->type == XML_ELEMENT_NODE)
{
GLuint localList = parseElement(child, false, NULL, false, NULL, 1, 1);
lists.push_back(localList);
};
// Not necessary for creating the list but used to create a clean starting point to transform bounding boxes.
glLoadIdentity();
glNewList(displayList, GL_COMPILE);
glColor4f(0, 0, 0, 1);
for (vector<GLuint>::iterator iterator = lists.begin(); iterator != lists.end(); iterator++)
glCallList(*iterator);
glEndList();
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Parses the given style definition and creates a new style, which is then added to the given model.
*
* @param definition The definition to parse.
* @model The model to which the new style is to be added.
*/
void CSVGParser::parseDefinition(xmlNodePtr definition, CGCModel* model)
{
string id;
if (getStringAttribute(definition, "id", id))
{
// Keep a reference to the model so other parts can access it.
FModel = model;
// Handle sub entries of the layout definition.
xmlNodePtr element = definition->children;
// This call will create an entry if there is none yet.
CGCStyle* style = model->style(utf8ToUtf16(id));
if (style->FDisplayList == 0)
style->FDisplayList = glGenLists(1);
while (element != NULL)
{
if (element->type == XML_ELEMENT_NODE)
{
if (XML_IS(element, "svg"))
{
FBoundsComputer.reset();
// Always add the point (0, 0, 0) to the bounding box.
FBoundsComputer.include(NULL, TVertex(0, 0, 0));
convert(element, style->FDisplayList);
style->FBoundingBox = FBoundsComputer.boundingBox();
FModel->canvas()->checkError();
};
// Ignore everything else.
break;
};
element = element->next;
};
};
}
//----------------------------------------------------------------------------------------------------------------------
|