📄 trpage_label.cpp
字号:
/* ************************ 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 <stdlib.h>#include <stdio.h>#include <string>#include <math.h>#include <trpage_geom.h>#include <trpage_read.h>// *************** Text Style implementationtrpgTextStyle::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 methodbool 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 bufferbool 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 implementationtrpgTextStyleTable::trpgTextStyleTable(){ Reset();}trpgTextStyleTable::~trpgTextStyleTable(){}void trpgTextStyleTable::Reset(){ styleMap.clear();}bool trpgTextStyleTable::isValid() const{ //for (int i=0;i<styles.size();i++) StyleMapType::const_iterator itr = styleMap.begin(); for ( ; itr != styleMap.end( ); itr++) if (!itr->second.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;i<numStyle;i++) { buf.GetToken(styleTok,len); if (styleTok != TRPG_TEXT_STYLE) throw 1; buf.PushLimit(len); style.Reset(); status = style.Read(buf); buf.PopLimit(); if (!status) throw 1; AddStyle(style); } } catch (...) { return false; } return isValid();}// *************** Support Style implementationtrpgSupportStyle::trpgSupportStyle(void){ Reset();}trpgSupportStyle::~trpgSupportStyle(void){}void trpgSupportStyle::Reset(void){ type = Line; matId = -1; handle = -1; writeHandle = false;}void trpgSupportStyle::SetType(SupportType s){ type = s;}trpgSupportStyle::SupportType trpgSupportStyle::GetType() const{ return type;}void trpgSupportStyle::SetMaterial(int inMatId){ matId = inMatId;}int trpgSupportStyle::GetMaterial(void) const{ return matId;}// Write methodbool trpgSupportStyle::Write(trpgWriteBuffer &buf){ buf.Begin(TRPG_SUPPORT_STYLE); buf.Begin(TRPG_SUPPORT_STYLE_BASIC); buf.Add(type); buf.Add(matId); buf.End(); buf.End(); return true;}// SupportStyle CB// Used to parse tokens from the support style structure.// If we do it this way it's easier to expand later.class supportStyleCB : public trpgr_Callback{public: void * Parse(trpgToken,trpgReadBuffer &); trpgSupportStyle *style;};void * supportStyleCB::Parse(trpgToken tok,trpgReadBuffer &buf){ int iVal; try { switch (tok) { case TRPG_SUPPORT_STYLE_BASIC: buf.Get(iVal); style->SetType(trpgSupportStyle::SupportType(iVal)); buf.Get(iVal); style->SetMaterial(iVal); break; default: break; } } catch (...) { return NULL; } return style;}// Read from a bufferbool 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 implementationtrpgSupportStyleTable::trpgSupportStyleTable(){ Reset();}trpgSupportStyleTable::~trpgSupportStyleTable(){}void trpgSupportStyleTable::Reset(){ //styles.resize(0); supportStyleMap.clear();}bool trpgSupportStyleTable::isValid() const{ //for (int i=0;i<styles.size();i++) SupportStyleMapType::const_iterator itr = supportStyleMap.begin(); for ( ; itr != supportStyleMap.end( ); itr++) { if (!itr->second.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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -