void Gauge::InitFromXMLNode(XMLNode gaugeNode) { Check(gaugeNode.IsValid() && gaugeNode.GetName() == "Gauge"); double scale = globals->m_PrefManager->GetPrefD("DefaultGaugeScale"); double zoom = globals->m_PrefManager->GetPrefD("Zoom"); double x, y; // temp variables // Set the units per pixel if (gaugeNode.HasChild("UnitsPerPixel")) { SetUnitsPerPixel(gaugeNode.GetChild("UnitsPerPixel").GetTextAsDouble()); } else { SetUnitsPerPixel(globals->m_PrefManager->GetPrefD("UnitsPerPixel")); } // Set the position if (gaugeNode.HasChild("Position")) { gaugeNode.GetChild("Position").GetTextAsCoord(x, y); SetPosition(x * zoom, y * zoom); } else { SetPosition(0.0, 0.0); } // Set the scale if (gaugeNode.HasChild("Scale")) { gaugeNode.GetChild("Scale").GetTextAsCoord(x, y); SetScale(x * zoom * scale, y * zoom * scale); } else { SetScale(zoom * scale, zoom * scale); } // Set the gauge outline if (gaugeNode.HasChild("Outline")) { SetGaugeOutline(gaugeNode.GetChild("Outline").GetTextAsBool()); } CustomXMLInit(gaugeNode); }
FontRenderer* FontRenderer::LoadFromFile(const char *path, SpriteBatch *spriteBatch) { assert(spriteBatch != NULL); FontLetter texLetters[256]; XMLNode *xmlDoc = XMLLoader::LoadFromFile(path); if (xmlDoc == NULL) return NULL; std::string bmpFilename = xmlDoc->GetAttribAsString("bitmap"); if (bmpFilename.empty()) return NULL; Path _path(path); Texture *tex = LoadFontBitmap(_path.GetPath() + bmpFilename); if (tex == NULL) return NULL; if (xmlDoc->GetName() != "alphabet") { delete xmlDoc; return NULL; } for (uint32_t i = 0; i < xmlDoc->GetChildrenCount(); i++) { std::string letter = xmlDoc[i].GetAttribAsString("letter"); std::string bounds = xmlDoc[i].GetAttribAsString("bounds"); sm::Rect<int> boundsValues; if (!ParseBounds(bounds, boundsValues)) return NULL; texLetters[letter[0]].Size = sm::Point<int>(boundsValues.Width, boundsValues.Height); texLetters[letter[0]].Coords = TexPart(tex, boundsValues); } FontRenderer *fontRenderer = new FontRenderer(); fontRenderer->m_spriteBatch = spriteBatch; if (fontRenderer != NULL) { memcpy(fontRenderer ->texLetters, texLetters, sizeof(FontLetter) * 256); } return fontRenderer; }
bool ApplePropertyList::ExtractStringFromValueNode (const XMLNode &node, std::string &value) { value.clear(); #if defined( LIBXML2_DEFINED ) if (node.IsValid()) { llvm::StringRef element_name = node.GetName(); if (element_name == "true" || element_name == "false") { // The text value _is_ the element name itself... value = element_name.str(); return true; } else if (element_name == "dict" || element_name == "array") return false; // dictionaries and arrays have no text value, so we fail else return node.GetElementText(value); } #endif return false; }
/** * Recursively parse an XML element. */ static bool XML::ParseXMLElement(XMLNode &node, Parser *pXML) { bool is_declaration; const TCHAR *text = nullptr; XMLNode *pNew; enum Status status; // inside or outside a tag enum Attrib attrib = eAttribName; /* the name of the attribute that is currently being */ tstring attribute_name; assert(pXML); // If this is the first call to the function if (pXML->nFirst) { // Assume we are outside of a tag definition pXML->nFirst = false; status = eOutsideTag; } else { // If this is not the first call then we should only be called when inside a tag. status = eInsideTag; } // Iterate through the tokens in the document while (true) { // Obtain the next token NextToken token = GetNextToken(pXML); if (gcc_unlikely(token.type == eTokenError)) return false; // Check the current status switch (status) { // If we are outside of a tag definition case eOutsideTag: // Check what type of token we obtained switch (token.type) { // If we have found text or quoted text case eTokenText: case eTokenQuotedText: case eTokenEquals: if (text == nullptr) text = token.pStr; break; // If we found a start tag '<' and declarations '<?' case eTokenTagStart: case eTokenDeclaration: // Cache whether this new element is a declaration or not is_declaration = token.type == eTokenDeclaration; // If we have node text then add this to the element if (text != nullptr) { size_t length = StripRight(text, token.pStr - text); node.AddText(text, length); text = nullptr; } // Find the name of the tag token = GetNextToken(pXML); // Return an error if we couldn't obtain the next token or // it wasnt text if (token.type != eTokenText) { pXML->error = eXMLErrorMissingTagName; return false; } // If the name of the new element differs from the name of // the current element we need to add the new element to // the current one and recurse pNew = &node.AddChild(token.pStr, token.length, is_declaration); while (true) { // Callself to process the new node. If we return // FALSE this means we dont have any more // processing to do... if (!ParseXMLElement(*pNew, pXML)) { return false; } else { // If the call to recurse this function // evented in a end tag specified in XML then // we need to unwind the calls to this // function until we find the appropriate node // (the element name and end tag name must // match) if (pXML->cbEndTag) { // If we are back at the root node then we // have an unmatched end tag if (node.GetName() == nullptr) { pXML->error = eXMLErrorUnmatchedEndTag; return false; } // If the end tag matches the name of this // element then we only need to unwind // once more... if (CompareTagName(node.GetName(), pXML->lpEndTag)) { pXML->cbEndTag = 0; } return true; } else { // If we didn't have a new element to create break; } } } break; // If we found an end tag case eTokenTagEnd: // If we have node text then add this to the element if (text != nullptr) { size_t length = StripRight(text, token.pStr - text); TCHAR *text2 = FromXMLString(text, length); if (text2 == nullptr) { pXML->error = eXMLErrorUnexpectedToken; return false; } node.AddText(text2); free(text2); text = nullptr; } // Find the name of the end tag token = GetNextToken(pXML); // The end tag should be text if (token.type != eTokenText) { pXML->error = eXMLErrorMissingEndTagName; return false; } // After the end tag we should find a closing tag if (GetNextToken(pXML).type != eTokenCloseTag) { pXML->error = eXMLErrorMissingEndTagName; return false; } // We need to return to the previous caller. If the name // of the tag cannot be found we need to keep returning to // caller until we find a match if (!CompareTagName(node.GetName(), token.pStr)) { pXML->lpEndTag = token.pStr; pXML->cbEndTag = token.length; } // Return to the caller return true; // Errors... case eTokenCloseTag: /* '>' */ case eTokenShortHandClose: /* '/>' */ pXML->error = eXMLErrorUnexpectedToken; return false; default: break; } break; // If we are inside a tag definition we need to search for attributes case eInsideTag: // Check what part of the attribute (name, equals, value) we // are looking for. switch (attrib) { // If we are looking for a new attribute case eAttribName: // Check what the current token type is switch (token.type) { // If the current type is text... // Eg. 'attribute' case eTokenText: // Cache the token then indicate that we are next to // look for the equals attribute_name.assign(token.pStr, token.length); attrib = eAttribEquals; break; // If we found a closing tag... // Eg. '>' case eTokenCloseTag: // We are now outside the tag status = eOutsideTag; break; // If we found a short hand '/>' closing tag then we can // return to the caller case eTokenShortHandClose: return true; // Errors... case eTokenQuotedText: /* '"SomeText"' */ case eTokenTagStart: /* '<' */ case eTokenTagEnd: /* '</' */ case eTokenEquals: /* '=' */ case eTokenDeclaration: /* '<?' */ pXML->error = eXMLErrorUnexpectedToken; return false; default: break; } break; // If we are looking for an equals case eAttribEquals: // Check what the current token type is switch (token.type) { // If the current type is text... // Eg. 'Attribute AnotherAttribute' case eTokenText: // Add the unvalued attribute to the list node.AddAttribute(std::move(attribute_name), _T(""), 0); // Cache the token then indicate. We are next to // look for the equals attribute attribute_name.assign(token.pStr, token.length); break; // If we found a closing tag 'Attribute >' or a short hand // closing tag 'Attribute />' case eTokenShortHandClose: case eTokenCloseTag: assert(!attribute_name.empty()); // If we are a declaration element '<?' then we need // to remove extra closing '?' if it exists if (node.IsDeclaration() && attribute_name.back() == _T('?')) { attribute_name.pop_back(); } if (!attribute_name.empty()) // Add the unvalued attribute to the list node.AddAttribute(std::move(attribute_name), _T(""), 0); // If this is the end of the tag then return to the caller if (token.type == eTokenShortHandClose) return true; // We are now outside the tag status = eOutsideTag; break; // If we found the equals token... // Eg. 'Attribute =' case eTokenEquals: // Indicate that we next need to search for the value // for the attribute attrib = eAttribValue; break; // Errors... case eTokenQuotedText: /* 'Attribute "InvalidAttr"'*/ case eTokenTagStart: /* 'Attribute <' */ case eTokenTagEnd: /* 'Attribute </' */ case eTokenDeclaration: /* 'Attribute <?' */ pXML->error = eXMLErrorUnexpectedToken; return false; default: break; } break; // If we are looking for an attribute value case eAttribValue: // Check what the current token type is switch (token.type) { // If the current type is text or quoted text... // Eg. 'Attribute = "Value"' or 'Attribute = Value' or // 'Attribute = 'Value''. case eTokenText: case eTokenQuotedText: // If we are a declaration element '<?' then we need // to remove extra closing '?' if it exists if (node.IsDeclaration() && (token.pStr[token.length - 1]) == _T('?')) { token.length--; } // Add the valued attribute to the list if (token.type == eTokenQuotedText) { token.pStr++; token.length -= 2; } assert(!attribute_name.empty()); { TCHAR *value = FromXMLString(token.pStr, token.length); if (value == nullptr) { pXML->error = eXMLErrorUnexpectedToken; return false; } node.AddAttribute(std::move(attribute_name), value, _tcslen(value)); free(value); } // Indicate we are searching for a new attribute attrib = eAttribName; break; // Errors... case eTokenTagStart: /* 'Attr = <' */ case eTokenTagEnd: /* 'Attr = </' */ case eTokenCloseTag: /* 'Attr = >' */ case eTokenShortHandClose: /* "Attr = />" */ case eTokenEquals: /* 'Attr = =' */ case eTokenDeclaration: /* 'Attr = <?' */ pXML->error = eXMLErrorUnexpectedToken; return false; default: break; } } } } }
void GaugePanel::InitFromXMLNode(XMLNode gaugeNode) { Check(gaugeNode.IsValid() && gaugeNode.GetName() == "Panel"); // Create gauges as described by the XML file XMLNode::NodeList nodeList = gaugeNode.GetChildList("Gauge"); XMLNode::NodeList::iterator iter; for (iter = nodeList.begin(); iter != nodeList.end(); ++iter) { Gauge *pGauge = GaugeFactory::CreateGaugeInstance(*iter); if (pGauge != NULL) { pGauge->SetParentRenderObject(this); AddGauge(pGauge); } } double scale = globals->m_PrefManager->GetPrefD("DefaultPanelScale"); double zoom = globals->m_PrefManager->GetPrefD("Zoom"); double x, y; // temp variables // Set the units per pixel if (gaugeNode.HasChild("UnitsPerPixel")) { SetUnitsPerPixel(gaugeNode.GetChild("UnitsPerPixel").GetTextAsDouble()); } else { SetUnitsPerPixel(globals->m_PrefManager->GetPrefD("UnitsPerPixel")); } // Set the scale if (gaugeNode.HasChild("Scale")) { gaugeNode.GetChild("Scale").GetTextAsCoord(x, y); SetScale(x * zoom * scale, y * zoom * scale); } else { SetScale(zoom * scale, zoom * scale); } // Set the position if (gaugeNode.HasChild("Position")) { gaugeNode.GetChild("Position").GetTextAsCoord(x, y); SetPosition(x * zoom, y * zoom); } else { SetPosition(0.0, 0.0); } // Set the size if (gaugeNode.HasChild("Size")) { gaugeNode.GetChild("Size").GetTextAsCoord(x, y); SetSize(x, y); } else { SetSize(0.0, 0.0); } // Set the gauge outline if (gaugeNode.HasChild("Outline")) { SetPanelOutline(gaugeNode.GetChild("Outline").GetTextAsBool()); } }
//---------------------------------------------------------------------------- bool FontBitmapImpl::Initlize (int fontWidth, int fontHeight, const char *fontFilename, CharCodingType codingType, unsigned int fontExtStyle) { Font::Initlize(fontWidth, fontHeight, fontFilename, codingType, fontExtStyle); mFontFilename = fontFilename; if (CCT_UTF8 == codingType) { mCharCodingObj = new0 CharCodingUTF8(); } else if (CCT_GBK == codingType) { mCharCodingObj = new0 CharCodingGBK(); } mGlyphMap = this; std::string outPath; std::string outBaseFilename; StringHelp::SplitFilename(fontFilename, outPath, outBaseFilename); int bufferSize = 0; char *buffer = 0; if (PX2_RM.LoadBuffer(fontFilename, bufferSize, buffer)) { XMLData data; if (data.LoadBuffer(buffer, bufferSize)) { float imageWidth = 0.0f; float imageHeight = 0.0f; XMLNode rootNode = data.GetRootNode(); XMLNode child = rootNode.IterateChild(); while (!child.IsNull()) { const std::string &nodeName = child.GetName(); if ("image" == nodeName) { const char *text = child.GetText(); std::string imagePath = outPath + text; mFontTex = DynamicCast<Texture2D>(PX2_RM.BlockLoad(imagePath)); if (mFontTex) { imageWidth = (float)mFontTex->GetWidth(); imageHeight = (float)mFontTex->GetHeight(); } } else if ("symbol" == nodeName) { std::string ch = child.GetChild("char").GetText(); int x = StringHelp::StringToInt(child.GetChild("x").GetText()); int y = StringHelp::StringToInt(child.GetChild("y").GetText()); int width = StringHelp::StringToInt(child.GetChild("width").GetText()); int height = StringHelp::StringToInt(child.GetChild("height").GetText()); unsigned int unicode = mCharCodingObj->ToUnicode((const unsigned char*)ch.c_str()); BitmapFontGlyph glyph; glyph.X = x; glyph.Y = y; glyph.numWidth = width; glyph.numHeight = height; float u0 = (float)x/imageWidth; float v0 = 1.0f-(float)(y+height)/imageHeight; float u1 = (x+width)/imageWidth; float v1 = v0 + (float)height/imageHeight; glyph.RectUV = Rectf(u0, v0, u1, v1); mMapGlyph[unicode] = glyph; } child = rootNode.IterateChild(child); } } } return true; }
XMLNode* XMLParser::_Parse(wchar_t* buf) { std::wstring token; eCurState state = NONE; attrMap attributeMap; std::wstring attrKey; XMLNode* curNode = new XMLNode; while( _mParsingIndex < _mBufSize ) { switch( buf[_mParsingIndex] ) { case L'<': if( state == ELEMENT ) // After element, It could be either closing node or new child node { if( _IsTokenTab(token) ) // At first, delete the all gap (similar to trim function) { token.clear(); } if( buf[_mParsingIndex+1] == L'/' ) // In case of closing the node { curNode->SetElement( token ); token.clear(); state = CLOSING_NODE; ++_mParsingIndex; } else // In case of new child node { curNode->AddChild( _Parse(buf) ); } } else // In case of new node { ++_mParsingIndex; if( buf[_mParsingIndex] == L'?' || buf[_mParsingIndex] == L'!') // if new node is about xml information { _GetTokenWith(buf, L'>'); } else // Read the new node's name { token += buf[_mParsingIndex]; state = OPENING_NODE; } } break; case L'>': switch( state ) { case ATTR_VALUE: // In case that new node's attr setting is all set curNode->SetAttrMap(attributeMap); attributeMap.clear(); state = ELEMENT; break; case CLOSING_NODE: // In case that making node is all set if( curNode->GetName().compare(token) != 0 ) { delete curNode; wprintf(L"XML ERROR ( closing element's name isn't correct [%s] ) - line %d", curNode->GetName().c_str(), _mLine ); return nullptr; } return curNode; case OPENING_NODE: // In case that new node's name setting is all set curNode->SetName( token ); token.clear(); state = ELEMENT; break; default: std::wcerr<<L"SWITCH_DEFAULT : "<<__FILE__<<L" "<<__LINE__<<std::endl; return nullptr; } break; case L' ': switch( state ) { case OPENING_NODE: // After setting the name of new node curNode->SetName( token ); token.clear(); state = ATTR_VALUE; break; case ELEMENT : // if ' ' is used for element, then just use that token += L' '; break; default: std::wcerr<<L"SWITCH_DEFAULT : "<<__FILE__<<L" "<<__LINE__<<std::endl; return nullptr; } break; case L'=': switch( state ) { case ELEMENT: // If '=' is used for element, then just use that token += L'='; break; case ATTR_VALUE: // In case of setting the attribute, attribute's key will come next attrKey = token; token.clear(); state = ATTR_KEY; break; default: std::wcerr<<L"SWITCH_DEFAULT : "<<__FILE__<<L" "<<__LINE__<<std::endl; return nullptr; } break; case L'\"': switch( state ) { case ATTR_KEY: // Set the attribute's key ++_mParsingIndex; token = _GetTokenWith(buf, L'\"'); attributeMap[attrKey] = token; attrKey.clear(); token.clear(); state = ATTR_VALUE; break; default: std::wcerr<<L"SWITCH_DEFAULT : "<<__FILE__<<L" "<<__LINE__<<std::endl; return nullptr; } break; case L'/': // If node is closed by using '/>' if( state == ATTR_VALUE && buf[_mParsingIndex+1] == L'>' ) { curNode->SetAttrMap(attributeMap); attributeMap.clear(); ++_mParsingIndex; return curNode; } break; default: if( buf[_mParsingIndex] == L'\n' ) ++_mLine; else token += buf[_mParsingIndex]; break; } ++_mParsingIndex; } return nullptr; }