Browse Source

Gmt module: Add structure and function for representation of font parameters.

ObjPtr
Michael Uleysky 9 years ago
parent
commit
1197c50567
  1. 3
      modules/gmt/modgmt.cpp
  2. 121
      modules/gmt/modgmt_func.h
  3. 46
      modules/gmt/modgmt_objects.cpp
  4. 9
      modules/gmt/modgmt_objects.h
  5. 68
      modules/gmt/modgmt_structs.h

3
modules/gmt/modgmt.cpp

@ -17,6 +17,7 @@ int gmt_module_init(void* p)
if(0!=ret) return ret; if(0!=ret) return ret;
gmt_projection::FillProjNames(); gmt_projection::FillProjNames();
if(!gmt_font::FillFontNames()) return 1;
RegisterFunction("GMT_Header",GMT_Header); RegisterFunction("GMT_Header",GMT_Header);
RegisterFunction("GMT_Footer",GMT_Footer); RegisterFunction("GMT_Footer",GMT_Footer);
@ -26,6 +27,7 @@ int gmt_module_init(void* p)
RegisterFunction("GET",Get<ObjectGMTColor>); RegisterFunction("GET",Get<ObjectGMTColor>);
RegisterFunction("GET",Get<ObjectGMTDash>); RegisterFunction("GET",Get<ObjectGMTDash>);
RegisterFunction("GET",Get<ObjectGMTPen>); RegisterFunction("GET",Get<ObjectGMTPen>);
RegisterFunction("GET",Get<ObjectGMTFont>);
RegisterFunction("GET",Get<ObjectGMTLayer>); RegisterFunction("GET",Get<ObjectGMTLayer>);
RegisterFunction("Coord",GMT_Type<struct gmt_coord>); RegisterFunction("Coord",GMT_Type<struct gmt_coord>);
@ -37,5 +39,6 @@ int gmt_module_init(void* p)
RegisterFunction("ColorHSV",GMT_ColorHSV); RegisterFunction("ColorHSV",GMT_ColorHSV);
RegisterFunction("ColorCMYK",GMT_ColorCMYK); RegisterFunction("ColorCMYK",GMT_ColorCMYK);
RegisterFunction("Pen",GMT_Type<struct gmt_pen>); RegisterFunction("Pen",GMT_Type<struct gmt_pen>);
RegisterFunction("Font",GMT_Type<struct gmt_font>);
return 0; return 0;
} }

121
modules/gmt/modgmt_func.h

@ -254,7 +254,9 @@ typedef TypeStorage<struct gmt_dash>::Base2Type Base2Dash;
// Definitions for ObjectGMTPen // Definitions for ObjectGMTPen
template<> class TypeStorage<struct gmt_pen>: public gTypeStorage<struct gmt_pen,ObjectList,ObjectString,ObjectReal,ObjectInt,ObjectGMTPen> {}; template<> class TypeStorage<struct gmt_pen>: public gTypeStorage<struct gmt_pen,ObjectList,ObjectString,ObjectReal,ObjectInt,ObjectGMTPen> {};
typedef TypeStorage<struct gmt_pen>::Base2Type Base2Pen; typedef TypeStorage<struct gmt_pen>::Base2Type Base2Pen;
// Definitions for ObjectGMTFont
template<> class TypeStorage<struct gmt_font>: public gTypeStorage<struct gmt_font,ObjectList,ObjectString,ObjectReal,ObjectInt,ObjectGMTFont> {};
typedef TypeStorage<struct gmt_font>::Base2Type Base2Font;
// Conversion from List to GMTRegion // Conversion from List to GMTRegion
/* /*
@ -1204,6 +1206,123 @@ class Convert2Struct<struct gmt_pen, ObjectList>
} }
}; };
// Converting List to GMTFont
/*
Input:
1) One argument, Font. Return copy of this argument.
2) One argument, list. Recursively calling GMT_Font.
3) Pairs list. Names are size (s), family (f) and color(c). Default values is 12pt for size, Times-Roman for family and black for color.
If pair with name font exists in list, when recursively calling GMT_Font on the value of this parameter, when modify it with specified parameters.
If argument with type Font exists in list, when copy it and modify with specified parameters.
Instead of color unnamed parameter with Color type may be used.
4) One numeric argument, interprets as size of Times-Roman black font.
5) One string argument, interprets as [size][,family][,color] or [family][,color].
6) Two arguments, interprets as size and family of black font.
7) Three arguments, interprets as size, family and color.
*/
template<>
class Convert2Struct<struct gmt_font, ObjectList>
{
public:
struct gmt_font operator()(const ObjectList* input, bool* issuc) const
{
struct gmt_font f;
auto size=input->Size();
if(1==size) // Cases 1, 2, 4 and 5
{
Base2Font font(input,0);
bool suc=true;
f=font(&suc);
if(!suc) goto fail; // Conversion failed
return f;
}
// Case 3
{
bool casevalid=false;
bool upd;
{
SearchParameter<struct gmt_font> updarg(input,"font");
bool suc=true;
f=updarg(&upd,&suc);
if(upd && !suc) goto fail; // Conversion failed or too many arguments
}
if(!upd)
{
// default font is Times-Roman, 12pt, black
f.size=gmt_font::default_size;
f.color.Convert(0);
f.family=gmt_font::default_family;
}
// Size
{
BaseM2Width s(input,"s","size");
if(s.Exist())
{
bool suc=true;
f.size=s(&suc);
if(!suc) goto fail; // Parsing error
casevalid=true;
}
}
// Family
{
SearchParameter<std::string> family(input,"family","f");
if(family.Exist())
{
bool suc=true;
f.family=family(&suc);
if(!suc) goto fail; // Parsing error
casevalid=true;
}
}
// Color
{
SearchParameter<struct gmt_color> color(input,"color","c");
if(color.Exist())
{
bool suc=true;
f.color=color(&suc);
if(!suc) goto fail; // Parsing error
casevalid=true;
}
}
if(casevalid || upd) return f; // Pen created or updated
}
// Case 6 and 7
if(2==size || 3==size)
{
Base2Width s(input,0);
Base2String fam(input,1);
Base2Color c(input,2);
bool suc=true;
if(s && fam)
{
f.size=s(&suc);
f.family=fam(&suc);
}
else goto fail; // Something wrong
if(c) f.color=c(&suc);
if(!suc) goto fail; // Something wrong
return f;
}
fail:
*issuc=false;
return f; // Something go wrong
}
};
// Template for generating GMTObject from ObjectList // Template for generating GMTObject from ObjectList
template<class Struct> template<class Struct>
ObjectBase* GMT_Type(const ObjectList* input) ObjectBase* GMT_Type(const ObjectList* input)

46
modules/gmt/modgmt_objects.cpp

@ -6,6 +6,7 @@ template<> const std::string ObjectGMTProjection::type="GMTProjection";
template<> const std::string ObjectGMTColor::type="GMTColor"; template<> const std::string ObjectGMTColor::type="GMTColor";
template<> const std::string ObjectGMTDash::type="GMTDash"; template<> const std::string ObjectGMTDash::type="GMTDash";
template<> const std::string ObjectGMTPen::type="GMTPen"; template<> const std::string ObjectGMTPen::type="GMTPen";
template<> const std::string ObjectGMTFont::type="GMTFont";
std::map<std::string,gmt_projection::projection> gmt_projection::projnames; std::map<std::string,gmt_projection::projection> gmt_projection::projnames;
@ -48,6 +49,51 @@ void gmt_projection::FillProjNames()
projnames["cylindrical stereographic"]=CYL_STERE; projnames["cylindrical stereographic"]=CYL_STERE;
} }
bool gmt_font::FillFontNames()
{
families.insert("Helvetica");
families.insert("Helvetica-Bold");
families.insert("Helvetica-Oblique");
families.insert("Helvetica-BoldOblique");
families.insert("Times-Roman");
families.insert("Times-Bold");
families.insert("Times-Italic");
families.insert("Times-BoldItalic");
families.insert("Courier");
families.insert("Courier-Bold");
families.insert("Courier-Oblique");
families.insert("Courier-BoldOblique");
families.insert("Symbol");
families.insert("AvantGarde-Book");
families.insert("AvantGarde-BookOblique");
families.insert("AvantGarde-Demi");
families.insert("AvantGarde-DemiOblique");
families.insert("Bookman-Demi");
families.insert("Bookman-DemiItalic");
families.insert("Bookman-Light");
families.insert("Bookman-LightItalic");
families.insert("Helvetica-Narrow");
families.insert("Helvetica-Narrow-Bold");
families.insert("Helvetica-Narrow-Oblique");
families.insert("Helvetica-Narrow-BoldOblique");
families.insert("NewCenturySchlbk-Roman");
families.insert("NewCenturySchlbk-Italic");
families.insert("NewCenturySchlbk-Bold");
families.insert("NewCenturySchlbk-BoldItalic");
families.insert("Palatino-Roman");
families.insert("Palatino-Italic");
families.insert("Palatino-Bold");
families.insert("Palatino-BoldItalic");
families.insert("ZapfChancery-MediumItalic");
families.insert("ZapfDingbats");
default_family="Times-Roman";
return true;
}
const double gmt_projection::default_width=10.0; const double gmt_projection::default_width=10.0;
const double gmt_pen::default_width=1.0; const double gmt_pen::default_width=1.0;
const double gmt_dash::default_width=gmt_pen::default_width; const double gmt_dash::default_width=gmt_pen::default_width;
const double gmt_font::default_size=12.0;
std::string gmt_font::default_family;
std::set<std::string> gmt_font::families;

9
modules/gmt/modgmt_objects.h

@ -118,6 +118,15 @@ template<> inline ObjectBase* ObjectGMTPen::OGet(const std::string& name) const
return 0; return 0;
} }
// GMTFont
typedef ObjectGMTClass<struct gmt_font> ObjectGMTFont;
template<> inline ObjectBase* ObjectGMTFont::OGet(const std::string& name) const
{
if("s"==name || "size"==name) return new ObjectReal(s.size);
if("f"==name || "family"==name)return new ObjectString(s.family);
if("c"==name || "color"==name) return new ObjectGMTColor(s.color);
return 0;
}
// GMTLayer // GMTLayer
class ObjectGMTLayer: public ObjectBase class ObjectGMTLayer: public ObjectBase

68
modules/gmt/modgmt_structs.h

@ -727,4 +727,72 @@ struct gmt_pen: public gmt_struct
return true; return true;
} }
}; };
// Font
struct gmt_font: public gmt_struct
{
double size;
struct gmt_color color;
std::string family;
static std::string default_family;
static std::set<std::string> families;
static const double default_size;
std::string Value() const {return ToString(size)+"p,"+family+","+color.Value();}
// Interpret one numeric argument as size of default black font
bool Convert(double in)
{
Value2Width s(in);
bool suc=true;
color.Convert(0); // Black
size=s(&suc);
family=default_family;
return suc;
}
// Convert from string
bool Convert(const std::string& str)
{
WordList::const_iterator ci;
WordList wl=Split(str,",",true);
// Defaults
size=default_size;
family=default_family;
color.Convert(0); // Black
if(wl.size()>3) return false; // String is [size][,family][,color] or [family][,color]
ci=wl.begin();
if(wl.end()!=ci && 0!=ci->size())
{
Value2Width s(*ci);
bool suc=true;
size=s(&suc);
if(!suc) // Parse error. check if argument is font name
{
if(0==families.count(*ci)) return false; // No, argument is not allowed font name
family=*ci;
if(wl.size()>2) return false; // If first word is font name, then words count is 1 or 2.
goto read_color;
}
}
if(wl.end()!=ci) ci++;
if(wl.end()!=ci && 0!=ci->size())
{
if(0==families.count(*ci)) return false; // Argument is not allowed font name
family=*ci;
}
read_color:
if(wl.end()!=ci) ci++;
if(wl.end()!=ci && 0!=ci->size())
{
if(!color.Convert(*ci)) return false; // Parse error
}
return true;
}
static bool FillFontNames();
};
#endif #endif

Loading…
Cancel
Save