1 // -*-c++-*-
2 
3 // fixg2sxd - a utility to convert fig to sxd format
4 
5 // Copyright (C) 2003-2010 Alexander Bürger, acfb@users.sourceforge.net
6 
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 #include "styles.h"
22 
23 #include "xmlwrite.h"
24 #include "check.h"
25 #include "colors.h"
26 #include <sstream>
27 #include <iomanip>
28 
29 tsset textstyles;
30 
31 string TextStyle::base = "Fig2SxdText";
32 int TextStyle::number = 10;
33 
check()34 void TextStyle::check()
35 {
36     keep_range(justification, "justification", 0, 2 );
37     keep_range(color, "color", -1, 32+512-1 );
38 }
39 
stylenumber() const40 string TextStyle::stylenumber() const
41 {
42     if( mynumber==0 )
43         mynumber = ++number;
44     ostringstream s;
45     s << base << mynumber;
46     return s.str();
47 }
48 
write(Node & out) const49 Node& TextStyle::write( Node& out ) const
50 {
51     const char* fontname = "Helvetica";
52     bool bold = false;
53     bool italic = false;
54     if( font_flags & 0x4 ) { // Postscript fonts
55         //cout << "ps fonts" << endl;
56         if( font == -1 ) {
57             // default
58         } else if( font >= 0 && font <= 31 ) {
59             const char* fontnames[] = {
60                 "Times New Roman", "AvantGarde", "Bookman", "Courier",
61                 "Helvetica",
62                 "Helvetica", // narrow by using "narrow" text style
63                 "New Century Schoolbook", "Palatino" };
64             fontname = fontnames[font/4];
65             bold   = ((font & 2) != 0);
66             italic = ((font & 1) != 0);
67         } else if( font>=32 && font<=34 ) {
68             const char* fontnames[] = { "Standard Symbols L", "Zapf Chancery",
69                                         "Zapf Dingbats" };
70             fontname = fontnames[font-32];
71         }
72     } else { // LaTeX fonts
73         if( font>=0 && font<=5 ) {
74             const char* fontnames[] = {
75                 "Helvetica", "Times New Roman", "Times New Roman",
76                 "Times New Roman", "Courier" };
77             fontname = fontnames[font];
78             bold   = (font == 2);
79             italic = (font == 3);
80         }
81     }
82     Node& style = out.subnode("style:style");
83     style["style:name"] << stylenumber();
84     style["style:family"] << "graphics"; // paragraph
85     style["style:class"] << "text";
86     style["style:parent-style-name"] << base;
87     Node& prop = style.subnode("style:properties");
88     prop["fo:font-family"] << fontname;
89     prop["draw:textarea-vertical-align"] << "middle";
90     prop["fo:font-weight"] << (bold ? "bold" : "normal" );
91     prop["fo:font-style"] << (italic ? "italic" : "normal" );
92     prop["fo:color"] << colorstring(color);
93     prop["fo:font-size"] << (font_size*(72.0/80.0)) << "pt";
94     const char* align[3] = { "left", "center", "right" };
95     prop["draw:textarea-horizontal-align"] << align[justification];
96     return out;
97 }
98 
99 #define cmp(x) if( x < o.x ) return true; if( x > o.x ) return false
operator <(TextStyle const & o) const100 bool TextStyle::operator<(TextStyle const& o) const
101 {
102     cmp( color );
103     cmp( font );
104     cmp( font_flags );
105     cmp( font_size );
106     cmp( justification );
107     return false;
108 }
109 
110