Add label map
Signed-off-by: ez_lcw <ez_lcw@foxmail.com>
This commit is contained in:
parent
b0552bf240
commit
34c6526186
@ -19,9 +19,9 @@ string Xdir="right";
|
||||
string Ydir="down";
|
||||
string output_file="g2s_output.svg";
|
||||
string output_shape="default";
|
||||
map<string,string> mp;
|
||||
map<string,string> color_map,label_map;
|
||||
|
||||
const map<string,float*> float_map{
|
||||
const map<string,float*> float_config_item{
|
||||
{"OutputScale",&output_scale}, //输出比例
|
||||
{"XL",&XL}, //左边框留白比例
|
||||
{"XR",&XR}, //右边框留白比例
|
||||
@ -30,7 +30,7 @@ const map<string,float*> float_map{
|
||||
{"R",&vertex_R}, //点的半径
|
||||
{"Thickness",&border_width} //点的边框粗细
|
||||
};
|
||||
const map<string,string*> string_map{
|
||||
const map<string,string*> string_config_item{
|
||||
{"BorderColor",&border_color}, //点的边框和边的颜色
|
||||
{"Label",&label}, // 标签格式
|
||||
{"FontFamily",&font_family}, // 标签字体
|
||||
@ -40,7 +40,7 @@ const map<string,string*> string_map{
|
||||
{"OutputFile",&output_file}, //输出文件名
|
||||
{"OutputShape",&output_shape} //输出形状
|
||||
};
|
||||
const map<string,vector<string>> string_configuration{
|
||||
const map<string,vector<string>> string_config_item_options{
|
||||
{"Label",{"a","A","1","none"}},
|
||||
{"Xaxis",{"left","right"}},
|
||||
{"Yaxis",{"up","down"}},
|
||||
@ -56,6 +56,15 @@ void getstring(string &s)
|
||||
while(ch!='\"') s+=ch,ch=getchar();
|
||||
}
|
||||
|
||||
void printlist(vector<string> vec)
|
||||
{
|
||||
fprintf(stderr,R"("%s")",vec[0].c_str());
|
||||
for(size_t i=1;i+1<vec.size();i++)
|
||||
fprintf(stderr,R"(, "%s")",vec[i].c_str());
|
||||
if(vec.size()>1)
|
||||
fprintf(stderr,R"( and "%s")",vec.back().c_str());
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
for(int line=1;;line++)
|
||||
@ -70,27 +79,24 @@ void init()
|
||||
string s;
|
||||
while(isalpha(ch)) s+=ch,ch=getchar();
|
||||
|
||||
if(float_map.find(s)!=float_map.end()) cin>>*(float_map.at(s));
|
||||
else if(string_map.find(s)!=string_map.end())
|
||||
if(float_config_item.find(s)!=float_config_item.end()) cin>>*(float_config_item.at(s));
|
||||
else if(string_config_item.find(s)!=string_config_item.end())
|
||||
{
|
||||
getstring(*(string_map.at(s)));
|
||||
if(string_configuration.find(s)!=string_configuration.end())
|
||||
getstring(*(string_config_item.at(s)));
|
||||
if(string_config_item_options.find(s)!=string_config_item_options.end())
|
||||
{
|
||||
bool bel=0;
|
||||
auto vec=string_configuration.at(s);
|
||||
s=*string_map.at(s);
|
||||
auto vec=string_config_item_options.at(s);
|
||||
string ss=s;
|
||||
s=*string_config_item.at(s);
|
||||
for(string t:vec) bel|=(s==t);
|
||||
if(!bel)
|
||||
{
|
||||
fprintf(stderr,R"(Error: Line %d.
|
||||
Expected = "%s")",line,vec[0].c_str());
|
||||
for(size_t i=1;i+1<vec.size();i++)
|
||||
fprintf(stderr,R"(, "%s")",vec[i].c_str());
|
||||
if(vec.size()>1)
|
||||
fprintf(stderr,R"( or "%s")",vec.back().c_str());
|
||||
fprintf(stderr,R"(Error on line %d: "%s" is not a valid option of "%s"
|
||||
Valid options: )",line,s.c_str(),ss.c_str());
|
||||
printlist(vec);
|
||||
fprintf(stderr,".\n");
|
||||
fprintf(stderr,R"(found = "%s".)",s.c_str());
|
||||
abort();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -99,14 +105,25 @@ Expected = "%s")",line,vec[0].c_str());
|
||||
s.clear();
|
||||
ch=getchar();
|
||||
while(ch!='=') s+=ch,ch=getchar();
|
||||
getstring(mp[s]);
|
||||
getstring(color_map[s]);
|
||||
}
|
||||
else if(s=="LabelMap")
|
||||
{
|
||||
// TODO
|
||||
} else {
|
||||
fprintf(stderr,R"(Error: Line %d. "%s" is not a valid option.)",line,s.c_str());
|
||||
abort();
|
||||
s.clear();
|
||||
ch=getchar();
|
||||
while(ch!='=') s+=ch,ch=getchar();
|
||||
getstring(label_map[s]);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr,R"(Error on line %d: "%s" is not a valid config item.
|
||||
Valid config items: )",line,s.c_str());
|
||||
vector<string> vec;
|
||||
for(const auto &it:float_config_item) vec.push_back(it.first);
|
||||
for(const auto &it:string_config_item) vec.push_back(it.first);
|
||||
printlist(vec);
|
||||
fprintf(stderr,".\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -121,7 +138,7 @@ struct vec
|
||||
vec operator - (vec a) const {vec b=(*this);b-=a;return b;}
|
||||
vec operator * (float a) const {vec b=(*this);b*=a;return b;}
|
||||
float norm() const {return sqrt(x*x+y*y);}
|
||||
}p[N];
|
||||
};
|
||||
|
||||
vec calc(vec a,vec b)
|
||||
{
|
||||
@ -130,9 +147,17 @@ vec calc(vec a,vec b)
|
||||
return a+c;
|
||||
}
|
||||
|
||||
void printlabel(vec p,string s)
|
||||
{
|
||||
if(label_map.find(s)!=label_map.end()) s=label_map.at(s);
|
||||
printf(R"(<text font-family="%s" fill="%s" font-size="%f" style="text-anchor: middle;"><tspan x="%f" y="%f">%s</tspan></text>
|
||||
)", font_family.c_str(), font_color.c_str(), vertex_R, p.x, p.y+vertex_R*0.3, s.c_str());
|
||||
}
|
||||
|
||||
int n,m;
|
||||
float minX,minY,maxX,maxY;
|
||||
string pc[N];
|
||||
vec p[N];
|
||||
|
||||
void printhelp(char* name)
|
||||
{
|
||||
@ -161,7 +186,7 @@ int main(int argc,char** argv)
|
||||
p[i].x*=output_scale,p[i].y*=output_scale;
|
||||
if(Xdir=="left") p[i].x=-p[i].x;
|
||||
if(Ydir=="up") p[i].y=-p[i].y;
|
||||
if(mp.find(pc[i])!=mp.end()) pc[i]=mp.find(pc[i])->second;
|
||||
if(color_map.find(pc[i])!=color_map.end()) pc[i]=color_map.find(pc[i])->second;
|
||||
|
||||
if(i==1) minX=maxX=p[i].x,minY=maxY=p[i].y;
|
||||
if(p[i].x<minX) minX=p[i].x;
|
||||
@ -206,15 +231,13 @@ int main(int argc,char** argv)
|
||||
|
||||
if (label == "A" || label == "a") {
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
printf(R"(<text font-family="%s" fill="%s" font-size="%f" style="text-anchor: middle;dominant-baseline: middle;"><tspan x="%f" y="%f">%c</tspan></text>
|
||||
)", font_family.c_str(), font_color.c_str(), vertex_R, p[i].x, p[i].y, i + label[0] - 1);
|
||||
printlabel(p[i],string{char(i + label[0] - 1)});
|
||||
}
|
||||
}
|
||||
else if(label=="1")
|
||||
{
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
printf(R"(<text font-family="%s" fill="%s" font-size="%f" style="text-anchor: middle;dominant-baseline: middle;"><tspan x="%f" y="%f">%d</tspan></text>
|
||||
)", font_family.c_str(), font_color.c_str(), vertex_R, p[i].x, p[i].y, i);
|
||||
printlabel(p[i],to_string(i));
|
||||
}
|
||||
}
|
||||
puts("</svg>");
|
||||
|
Loading…
x
Reference in New Issue
Block a user