• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright (C) 2013-2020 Red Hat, Inc.
5 
6 #include <stdexcept>
7 #include <fstream>
8 
9 #include "abg-internal.h"
10 // <headers defining libabigail's API go under here>
11 ABG_BEGIN_EXPORT_DECLARATIONS
12 
13 #include "abg-viz-svg.h"
14 
15 ABG_END_EXPORT_DECLARATIONS
16 // </headers defining libabigail's API>
17 
18 namespace abigail
19 {
20 
21 using std::ostream;
22 using std::ostringstream;
23 
24 // Constants.
25 
26 // Using pixels, units vs. representation
27 // const canvas ansi_letter_mm = { units::millimeter, 215.9, 279.4 };
28 // const canvas iso_a4_mm = { units::millimeter, 210, 297 };
29 // const canvas ansi_letter_px = { units::pixel, 765, 990 };
30 // const canvas iso_a4_px = { units::pixel, 765, 990 };
31 const canvas ansi_letter_canvas = { units::pixel, 765, 990 };
32 const canvas iso_a4_canvas = { units::pixel, 765, 990 };
33 
34 const typography arial_typo = \
35   { "'ArialMT'", 12, color::black, R"(text-anchor="middle")"};
36 
37 const typography source_code_pro_typo = \
38   { "Source Code Pro Light", 12, color::black, R"(text-anchor="middle")"};
39 
40 const typography roboto_typo = \
41   { "Roboto Light", 12, color::black, R"(text-anchor="middle")"};
42 
43 const style primary_row_sty = { color::white, color::black, "" };
44 const style base_row_sty = { color::white, color::gray75, "" };
45 const style member_row_sty = { color::black, color::gray25, "" };
46 const style implementation_row_sty = { color::black, color::white, "" };
47 
48 
49 // XXX Do not export.
50 void
string_replace(std::string & target,const std::string & match,const std::string & replace)51 string_replace(std::string& target, const std::string& match,
52 	       const std::string& replace)
53 {
54   size_t pos = 0;
55   while((pos = target.find(match, pos)) != std::string::npos)
56     {
57       target.replace(pos, match.length(), replace);
58       pos += replace.length();
59     }
60 }
61 
62 std::string
units_to_string(units __val)63 units_to_string(units __val)
64 {
65   std::string ret;
66   switch (__val)
67     {
68     case units::millimeter:
69       ret = "mm";
70       break;
71     case units::pixel:
72       ret = "px";
73       break;
74     default:
75       throw std::logic_error("abigail::units_to_string units not recognized");
76       break;
77     }
78   return ret;
79 }
80 
81 std::string
color_to_string(color __val)82 color_to_string(color __val)
83 {
84   std::string ret;
85   switch (__val)
86     {
87     case color::white:
88       ret = "white";
89       break;
90     case color::gray25:
91       ret = "gainsboro";
92       break;
93     case color::gray75:
94       ret = "slategray";
95       break;
96     case color::black:
97       ret = "black";
98       break;
99     default:
100       throw std::logic_error("abigail::color_to_string color not recognized");
101       break;
102     }
103   return ret;
104 }
105 
106 std::string
anchor_to_string(anchor __val) const107 typography::anchor_to_string(anchor __val) const
108 {
109   std::string ret;
110   switch (__val)
111     {
112     case start:
113       ret = "start";
114       break;
115     case middle:
116       ret = "middle";
117       break;
118     default:
119       throw std::logic_error("abigail::anchor_to_string anchor not recognized");
120       break;
121     }
122   return ret;
123 }
124 
125 
126 std::string
to_attribute(anchor __a) const127 typography::to_attribute(anchor __a) const
128 {
129   const std::string name("__name");
130   const std::string size("__size");
131   const std::string anchor("__anchor");
132   std::string strip = R"(font-family="__name" font-size="__size" text-anchor="__anchor")";
133   string_replace(strip, name, _M_face);
134   string_replace(strip, size, std::to_string(_M_size));
135   string_replace(strip, anchor, anchor_to_string(__a));
136 
137   // NB: Add in extra _M_attributes if necessary.
138   return strip;
139 }
140 
141 }//end namespace abigail
142