Debian Codegraph @

utils/lstopo/lstopo-fig.c

raw
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2009 CNRS
4 * Copyright © 2009-2021 Inria. All rights reserved.
5 * Copyright © 2009-2010 Université Bordeaux
6 * Copyright © 2011 Cisco Systems, Inc. All rights reserved.
7 * See COPYING in top-level directory.
8 */
9
10 #include "private/autogen/config.h"
11 #include "hwloc.h"
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <errno.h>
17
18 #include "lstopo.h"
19
20 /* .fig back-end. */
21
22 #define FIG_FACTOR 20
23
24 static int fig_color_index = 32;
25
26 static int
27 fig_declare_color(struct lstopo_output *loutput, struct lstopo_color *lcolor)
28 {
29 FILE *file = loutput->file;
30 int r = lcolor->r, g = lcolor->g, b = lcolor->b;
31
32 if (r == 0xff && g == 0xff && b == 0xff) {
33 lcolor->private.fig.color = 7;
34 return 0;
35 } else if (!r && !g && !b) {
36 lcolor->private.fig.color = 0;
37 return 0;
38 } else {
39 lcolor->private.fig.color = fig_color_index++;
40 }
41
42 fprintf(file, "0 %d #%02x%02x%02x\n", lcolor->private.fig.color, (unsigned) r, (unsigned) g, (unsigned) b);
43 return 0;
44 }
45
46 static void
47 fig_box(struct lstopo_output *loutput, const struct lstopo_color *lcolor, unsigned depth, unsigned x, unsigned width, unsigned y, unsigned height, hwloc_obj_t obj __hwloc_attribute_unused, unsigned box_id __hwloc_attribute_unused)
48 {
49 FILE *file = loutput->file;
50 unsigned cpukind_style = lstopo_obj_cpukind_style(loutput, obj);
51 unsigned linestyle = 0; /* solid */
52 unsigned dashspace = 0; /* no dash */
53 unsigned thickness = loutput->thickness;
54
55 if (!width || !height)
56 return;
57
58 if (cpukind_style) {
59 linestyle = 1; /* dash */
60 dashspace = 1U << cpukind_style;
61 thickness *= (1 + cpukind_style);
62 }
63
64 x *= FIG_FACTOR;
65 y *= FIG_FACTOR;
66 width *= FIG_FACTOR;
67 height *= FIG_FACTOR;
68 fprintf(file, "2 2 %u %u 0 %d %u -1 20 %u.0 0 0 -1 0 0 5\n\t",
69 linestyle, thickness, lcolor->private.fig.color, depth, dashspace);
70 fprintf(file, " %u %u", x, y);
71 fprintf(file, " %u %u", x + width, y);
72 fprintf(file, " %u %u", x + width, y + height);
73 fprintf(file, " %u %u", x, y + height);
74 fprintf(file, " %u %u", x, y);
75 fprintf(file, "\n");
76 }
77
78 static void
79 fig_line(struct lstopo_output *loutput, unsigned depth, unsigned x1, unsigned y1, unsigned x2, unsigned y2, hwloc_obj_t obj __hwloc_attribute_unused, unsigned line_id __hwloc_attribute_unused)
80 {
81 FILE *file = loutput->file;
82
83 x1 *= FIG_FACTOR;
84 y1 *= FIG_FACTOR;
85 x2 *= FIG_FACTOR;
86 y2 *= FIG_FACTOR;
87 fprintf(file, "2 1 0 %u 0 0 %u -1 -1 0.0 0 0 -1 0 0 2\n\t", loutput->thickness, depth);
88 fprintf(file, " %u %u", x1, y1);
89 fprintf(file, " %u %u", x2, y2);
90 fprintf(file, "\n");
91 }
92
93 #define FIG_FONTSIZE_SCALE(size) (((size) * 11) / 10)
94 /* assume character width is half their height on average */
95 #define FIG_TEXT_WIDTH(length, fontsize) (((length) * (fontsize))/2)
96
97 static void
98 fig_text(struct lstopo_output *loutput, const struct lstopo_color *lcolor, int size, unsigned depth, unsigned x, unsigned y, const char *text, hwloc_obj_t obj __hwloc_attribute_unused, unsigned text_id __hwloc_attribute_unused)
99 {
100 FILE *file = loutput->file;
101 unsigned cpukind_style = lstopo_obj_cpukind_style(loutput, obj);
102 unsigned fontflags = 0; /* normal */
103 int len = (int)strlen(text);
104 int color;
105
106 if (cpukind_style % 2)
107 fontflags = 2; /* bold for odd styles */
108
109 color = lcolor->private.fig.color;
110 x *= FIG_FACTOR;
111 y *= FIG_FACTOR;
112 /* move the origin of the text away from the box corner */
113 x += (size * FIG_FACTOR * 2) / 10;
114 y += (size * FIG_FACTOR * 4) / 10;
115
116 size = FIG_FONTSIZE_SCALE(size);
117 fprintf(file, "4 0 %d %u -1 %u %d 0.0 4 %d %d %u %u %s\\001\n",
118 color, depth, fontflags, size, size * FIG_FACTOR, FIG_TEXT_WIDTH(len, size) * FIG_FACTOR, x, y + size * 10, text);
119 }
120
121 static void
122 fig_textsize(struct lstopo_output *loutput __hwloc_attribute_unused, const char *text __hwloc_attribute_unused, unsigned textlength, unsigned fontsize, unsigned *width)
123 {
124 fontsize = FIG_FONTSIZE_SCALE(fontsize);
125 *width = FIG_TEXT_WIDTH(textlength, fontsize);
126 }
127
128 static struct draw_methods fig_draw_methods = {
129 fig_declare_color,
130 NULL,
131 fig_box,
132 fig_line,
133 fig_text,
134 fig_textsize,
135 };
136
137 int
138 output_fig (struct lstopo_output *loutput, const char *filename)
139 {
140 FILE *output = open_output(filename, loutput->overwrite);
141 if (!output) {
142 fprintf(stderr, "Failed to open %s for writing (%s)\n", filename, strerror(errno));
143 return -1;
144 }
145
146 loutput->file = output;
147 loutput->methods = &fig_draw_methods;
148 loutput->backend_flags |= LSTOPO_BACKEND_FLAG_APPROXIMATIVE_TEXTWIDTH;
149
150 /* recurse once for preparing sizes and positions */
151 loutput->drawing = LSTOPO_DRAWING_PREPARE;
152 output_draw(loutput);
153 loutput->drawing = LSTOPO_DRAWING_DRAW;
154
155 fprintf(output, "#FIG 3.2 Produced by hwloc's lstopo\n");
156 fprintf(output, "Landscape\n");
157 fprintf(output, "Center\n");
158 fprintf(output, "Inches\n");
159 fprintf(output, "letter\n");
160 fprintf(output, "100.00\n"); /* magnification */
161 fprintf(output, "Single\n"); /* single page */
162 fprintf(output, "-2\n"); /* no transparent color */
163 fprintf(output, "1200 2\n"); /* 1200 ppi resolution, upper left origin */
164
165 /* ready */
166 declare_colors(loutput);
167 lstopo_prepare_custom_styles(loutput);
168
169 output_draw(loutput);
170
171 if (output != stdout)
172 fclose(output);
173
174 destroy_colors(loutput);
175 return 0;
176 }