/* ************************ Copyright Terrain Experts Inc. Terrain Experts Inc (TERREX) reserves all rights to this source code unless otherwise specified in writing by the President of TERREX. This copyright may be updated in the future, in which case that version supercedes this one. ------------------- Terrex Experts Inc. 4400 East Broadway #314 Tucson, AZ 85711 info@terrex.com Tel: (520) 323-7990 ************************ */ /* trpage_label.cpp Methods for the trpgLable object and its associated support structure including trpgTextStyle and trpgTextStyleTable. */ #include #include #include #include #include #include // *************** Text Style implementation trpgTextStyle::trpgTextStyle(void) { Reset(); } trpgTextStyle::~trpgTextStyle(void) { } void trpgTextStyle::Reset(void) { font = ""; bold = italic = underline = false; characterSize = float32(0.0042333333333); // 12 point in meter matId = -1; } void trpgTextStyle::SetMaterial(int inMatId) { matId = inMatId; } int trpgTextStyle::GetMaterial(void) const { return matId; } void trpgTextStyle::SetFont(std::string &inFont) { font = inFont; } const std::string *trpgTextStyle::GetFont(void) const { return &font; } void trpgTextStyle::SetBold(bool inBold) { bold = inBold; } bool trpgTextStyle::GetBold(void) const { return bold; } void trpgTextStyle::SetItalic(bool inItalic) { italic = inItalic; } bool trpgTextStyle::GetItalic(void) const { return italic; } void trpgTextStyle::SetUnderline(bool inUnder) { underline = inUnder; } bool trpgTextStyle::GetUnderline(void) const { return underline; } void trpgTextStyle::SetCharacterSize(float32 inSize) { characterSize = inSize; } float32 trpgTextStyle::GetCharacterSize(void) const { return characterSize; } // Write method bool trpgTextStyle::Write(trpgWriteBuffer &buf) { buf.Begin(TRPG_TEXT_STYLE); buf.Begin(TRPG_TEXT_STYLE_BASIC); buf.Add(font); buf.Add((int32)bold); buf.Add((int32)italic); buf.Add((int32)underline); buf.Add(characterSize); buf.Add(matId); buf.End(); buf.End(); return true; } // TextStyle CB // Used to parse tokens from the text style structure. // If we do it this way it's easier to expand later. class textStyleCB : public trpgr_Callback { public: void * Parse(trpgToken,trpgReadBuffer &); trpgTextStyle *style; }; void * textStyleCB::Parse(trpgToken tok,trpgReadBuffer &buf) { std::string sVal; int iVal; float32 fVal; try { switch (tok) { case TRPG_TEXT_STYLE_BASIC: buf.Get(sVal); style->SetFont(sVal); buf.Get(iVal); style->SetBold((iVal ? true : false)); buf.Get(iVal); style->SetItalic((iVal ? true : false)); buf.Get(iVal); style->SetUnderline((iVal ? true : false)); buf.Get(fVal); style->SetCharacterSize(fVal); buf.Get(iVal); style->SetMaterial(iVal); break; default: break; } } catch (...) { return NULL; } return style; } // Read from a buffer bool trpgTextStyle::Read(trpgReadBuffer &buf) { Reset(); trpgr_Parser parse; textStyleCB textStyleCb; textStyleCb.style = this; parse.AddCallback(TRPG_TEXT_STYLE_BASIC,&textStyleCb,false); parse.Parse(buf); return isValid(); } bool trpgTextStyle::isValid(void) const { // Need to have a font designation at least if (font.size() > 0) return true; return false; } bool trpgTextStyle::operator == (const trpgTextStyle& in) const { if (font != in.font) return false; if (bold != in.bold || italic != in.italic || underline != in.underline) return false; if(fabs(double(characterSize - in.characterSize)) > 0.0001) return false; if (matId != in.matId) return false; return true; } // ******************* Text Style Table implementation trpgTextStyleTable::trpgTextStyleTable() { Reset(); } trpgTextStyleTable::~trpgTextStyleTable() { } void trpgTextStyleTable::Reset() { styleMap.clear(); } bool trpgTextStyleTable::isValid() const { //for (int i=0;isecond.isValid()) return false; return true; } int trpgTextStyleTable::AddStyle(const trpgTextStyle &style) { int handle = style.GetHandle(); if(handle==-1) { handle = styleMap.size(); } styleMap[handle] = style; return handle; } int trpgTextStyleTable::FindAddStyle(const trpgTextStyle &style) { StyleMapType::const_iterator itr = styleMap.begin(); for ( ; itr != styleMap.end( ); itr++) if (itr->second == style) return itr->first; return AddStyle(style); } int trpgTextStyleTable::GetNumStyle() const { return styleMap.size(); } const trpgTextStyle *trpgTextStyleTable::GetStyleRef(int id) const { if (id < 0) return NULL; StyleMapType::const_iterator itr = styleMap.find(id); if(itr == styleMap.end()) return NULL; return &itr->second; } bool trpgTextStyleTable::Write(trpgWriteBuffer &buf) { if (!isValid()) return false; buf.Begin(TRPG_TEXT_STYLE_TABLE); // Number of styles int numStyle = styleMap.size(); buf.Add((int32)numStyle); // Write the styles StyleMapType::iterator itr = styleMap.begin(); for ( ; itr != styleMap.end( ); itr++) itr->second.Write(buf); buf.End(); return true; } bool trpgTextStyleTable::Read(trpgReadBuffer &buf) { trpgTextStyle style; trpgToken styleTok; int32 len; bool status; int numStyle; int i; Reset(); try { buf.Get(numStyle); if (numStyle < 0) throw 1; for (i=0;iSetType(trpgSupportStyle::SupportType(iVal)); buf.Get(iVal); style->SetMaterial(iVal); break; default: break; } } catch (...) { return NULL; } return style; } // Read from a buffer bool trpgSupportStyle::Read(trpgReadBuffer &buf) { Reset(); trpgr_Parser parse; supportStyleCB supportStyleCb; supportStyleCb.style = this; parse.AddCallback(TRPG_SUPPORT_STYLE_BASIC,&supportStyleCb,false); parse.Parse(buf); return isValid(); } bool trpgSupportStyle::isValid(void) const { return true; } bool trpgSupportStyle::operator == (const trpgSupportStyle& in) const { if (type != in.type || matId != in.matId) return false; return true; } // ******************* Support Style Table implementation trpgSupportStyleTable::trpgSupportStyleTable() { Reset(); } trpgSupportStyleTable::~trpgSupportStyleTable() { } void trpgSupportStyleTable::Reset() { //styles.resize(0); supportStyleMap.clear(); } bool trpgSupportStyleTable::isValid() const { //for (int i=0;isecond.isValid()) return false; } return true; } int trpgSupportStyleTable::AddStyle(const trpgSupportStyle &style) { int handle = style.GetHandle(); if(handle==-1) { handle = supportStyleMap.size(); } supportStyleMap[handle] = style; return handle; } int trpgSupportStyleTable::FindAddStyle(const trpgSupportStyle &style) { SupportStyleMapType::const_iterator itr = supportStyleMap.begin(); for ( ; itr != supportStyleMap.end( ); itr++) if (itr->second == style) return itr->first; return AddStyle(style); } int trpgSupportStyleTable::GetNumStyle() const { return supportStyleMap.size(); } const trpgSupportStyle *trpgSupportStyleTable::GetStyleRef(int id) const { if (id < 0) return NULL; SupportStyleMapType::const_iterator itr = supportStyleMap.find(id); if(itr == supportStyleMap.end()) return NULL; return &itr->second; } bool trpgSupportStyleTable::Write(trpgWriteBuffer &buf) { if (!isValid()) return false; buf.Begin(TRPG_SUPPORT_STYLE_TABLE); // Number of styles int numStyle = supportStyleMap.size(); buf.Add((int32)numStyle); // Write the styles SupportStyleMapType::iterator itr = supportStyleMap.begin(); for ( ; itr != supportStyleMap.end( ); itr++) itr->second.Write(buf); buf.End(); return true; } bool trpgSupportStyleTable::Read(trpgReadBuffer &buf) { trpgSupportStyle style; trpgToken styleTok; int32 len; bool status; int numStyle; int i; Reset(); try { buf.Get(numStyle); if (numStyle < 0) throw 1; //styles.resize(numStyle); for (i=0;iSetFontStyle(iVal); buf.Get(iVal); property->SetSupport(iVal); buf.Get(ival); property->SetType(trpgLabelProperty::LabelType(ival)); break; default: break; } } catch (...) { return NULL; } return property; } // Read from a buffer bool trpgLabelProperty::Read(trpgReadBuffer &buf) { Reset(); trpgr_Parser parse; labelPropertyCB labelPropertyCb; labelPropertyCb.property = this; parse.AddCallback(TRPG_LABEL_PROPERTY_BASIC,&labelPropertyCb,false); parse.Parse(buf); return isValid(); } bool trpgLabelProperty::isValid(void) const { return supportId != -1 && fontId != -1 && type >=0 && type < MaxLabelType; } bool trpgLabelProperty::operator == (const trpgLabelProperty& in)const { if (fontId != in.fontId || supportId != in.supportId || type != in.type) return false; return true; } // ******************* Label Property Table implementation trpgLabelPropertyTable::trpgLabelPropertyTable() { Reset(); } trpgLabelPropertyTable::~trpgLabelPropertyTable() { } void trpgLabelPropertyTable::Reset() { labelPropertyMap.clear(); } bool trpgLabelPropertyTable::isValid() const { LabelPropertyMapType::const_iterator itr = labelPropertyMap.begin(); for ( ; itr != labelPropertyMap.end( ); itr++) { if (!itr->second.isValid()) return false; } return true; } int trpgLabelPropertyTable::AddProperty(const trpgLabelProperty &property) { int handle = property.GetHandle(); if(handle==-1) { handle = labelPropertyMap.size(); } labelPropertyMap[handle] = property; return handle; } int trpgLabelPropertyTable::FindAddProperty(const trpgLabelProperty& property) { LabelPropertyMapType::const_iterator itr = labelPropertyMap.begin(); for ( ; itr != labelPropertyMap.end( ); itr++) { if (itr->second == property) return itr->first; } return AddProperty(property); } int trpgLabelPropertyTable::GetNumProperty() const { return labelPropertyMap.size(); } const trpgLabelProperty *trpgLabelPropertyTable::GetPropertyRef(int id) const { if (id < 0) return NULL; LabelPropertyMapType::const_iterator itr = labelPropertyMap.find(id); if(itr == labelPropertyMap.end()) return NULL; return &itr->second; } bool trpgLabelPropertyTable::Write(trpgWriteBuffer &buf) { if (!isValid()) return false; buf.Begin(TRPG_LABEL_PROPERTY_TABLE); // Number of styles int numProperty = labelPropertyMap.size(); buf.Add((int32)numProperty); // Write the properties LabelPropertyMapType::iterator itr = labelPropertyMap.begin(); for ( ; itr != labelPropertyMap.end( ); itr++) { itr->second.Write(buf); } buf.End(); return true; } bool trpgLabelPropertyTable::Read(trpgReadBuffer &buf) { trpgLabelProperty property; trpgToken propertyTok; int32 len; bool status; int numProperty; int i; Reset(); try { buf.Get(numProperty); if (numProperty < 0) throw 1; //properties.resize(numProperty); for (i=0;i *trpgLabel::GetSupports() const { return &supports; } bool trpgLabel::Write(trpgWriteBuffer &buf) { unsigned int i; buf.Begin(TRPG_LABEL); buf.Add(propertyId); buf.Add(text); buf.Add(alignment); buf.Add(tabSize); buf.Add(scale); buf.Add(thickness); buf.Add(desc); buf.Add(url); buf.Add(location); buf.Add((int)supports.size()); for (i=0;i