| 1 |
/* |
| 2 |
* SPDX-License-Identifier: BSD-3-Clause |
| 3 |
* Copyright © 2020 Hewlett Packard Enterprise. All rights reserved. |
| 4 |
* Copyright © 2020-2021 Inria. All rights reserved. |
| 5 |
* See COPYING in top-level directory. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "private/autogen/config.h" |
| 9 |
#include "hwloc.h" |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <string.h> |
| 14 |
#include <errno.h> |
| 15 |
|
| 16 |
#include "lstopo.h" |
| 17 |
|
| 18 |
#define TIKZ_TEXT_WIDTH(length, fontsize) (((length) * (fontsize) * 10) / 22) |
| 19 |
#define TIKZ_FONTSIZE_SCALE(size) (((size) * 13) / 9) |
| 20 |
|
| 21 |
#define TIKZ_FONTFAMILY_ENV "LSTOPO_TIKZ_FONTFAMILY" |
| 22 |
|
| 23 |
static const char * |
| 24 |
tikz_get_font_family(void) |
| 25 |
{ |
| 26 |
/* Authorized values (case insensitive): sf, rm, tt */ |
| 27 |
char *font_request = getenv(TIKZ_FONTFAMILY_ENV); |
| 28 |
if (!font_request || !(*font_request)) |
| 29 |
return ""; /* use latex default font family */ |
| 30 |
else if (!strcasecmp(font_request, "sf")) |
| 31 |
return "\\sffamily"; |
| 32 |
else if (!strcasecmp(font_request, "rm")) |
| 33 |
return "\\rmfamily"; |
| 34 |
else if (!strcasecmp(font_request, "tt")) |
| 35 |
return "\\ttfamily"; |
| 36 |
else |
| 37 |
return NULL; |
| 38 |
} |
| 39 |
|
| 40 |
static int |
| 41 |
tikz_declare_color(struct lstopo_output *loutput, struct lstopo_color *lcolor) |
| 42 |
{ |
| 43 |
int r = lcolor->r, g = lcolor->g, b = lcolor->b; |
| 44 |
FILE *file = loutput->backend_data; |
| 45 |
|
| 46 |
/* TODO: add random seed in name to avoid collisions |
| 47 |
* name format proposed: hwloc-color-<seed>-<r>-<g>-<b> |
| 48 |
*/ |
| 49 |
fprintf(file, "\\definecolor{hwloc-color-%d-%d-%d}{RGB}{%d,%d,%d}\n", |
| 50 |
r, g, b, r, g, b); |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
static void |
| 55 |
tikz_box(struct lstopo_output *loutput, const struct lstopo_color *lcolor, unsigned depth __hwloc_attribute_unused, unsigned x, unsigned width, unsigned y, unsigned height, hwloc_obj_t obj, unsigned box_id __hwloc_attribute_unused) |
| 56 |
{ |
| 57 |
FILE *file = loutput->file; |
| 58 |
int r = lcolor->r, g = lcolor->g, b = lcolor->b; |
| 59 |
unsigned cpukind_style = lstopo_obj_cpukind_style(loutput, obj); |
| 60 |
char linestyle[64] = "solid"; |
| 61 |
unsigned thickness = loutput->thickness; |
| 62 |
float dashspace = 1.15f; /* default dash size: 1.15pt */ |
| 63 |
|
| 64 |
if (cpukind_style) { |
| 65 |
char dashsize[20], *comma = NULL; |
| 66 |
thickness *= cpukind_style; |
| 67 |
dashspace *= 1U << cpukind_style; |
| 68 |
snprintf(dashsize, 20, "%.4f", dashspace); |
| 69 |
comma = strchr(dashsize, ','); |
| 70 |
if (comma) |
| 71 |
*comma = '.'; /* Use decimal dot despite the locale's opinion. */ |
| 72 |
snprintf(linestyle, sizeof(linestyle), "dash pattern=on %spt off %spt", |
| 73 |
dashsize, dashsize); |
| 74 |
} |
| 75 |
|
| 76 |
fprintf(file, "\t\\filldraw [fill=hwloc-color-%d-%d-%d,draw=black,line width=%upt,%s] (%u,%u) rectangle ++(%u,%u);\n", |
| 77 |
r, g, b, thickness, linestyle, x, y, width, height); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
static void |
| 82 |
tikz_line(struct lstopo_output *loutput, unsigned depth __hwloc_attribute_unused, unsigned x1, unsigned y1, unsigned x2, unsigned y2, hwloc_obj_t obj __hwloc_attribute_unused, unsigned line_id __hwloc_attribute_unused) |
| 83 |
{ |
| 84 |
FILE *file = loutput->file; |
| 85 |
|
| 86 |
fprintf(file, "\t\\draw [draw=black,line width=%upt] (%u,%u) -- (%u,%u);\n", |
| 87 |
loutput->thickness, x1, y1, x2, y2); |
| 88 |
} |
| 89 |
|
| 90 |
static void |
| 91 |
tikz_textsize(struct lstopo_output *loutput __hwloc_attribute_unused, const char *text __hwloc_attribute_unused, unsigned textlength, unsigned fontsize, unsigned *width) |
| 92 |
{ |
| 93 |
fontsize = TIKZ_FONTSIZE_SCALE(fontsize); |
| 94 |
*width = TIKZ_TEXT_WIDTH(textlength, fontsize); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
static void |
| 99 |
tikz_text(struct lstopo_output *loutput, const struct lstopo_color *lcolor, int size __hwloc_attribute_unused, unsigned depth __hwloc_attribute_unused, unsigned x, unsigned y, const char *text, hwloc_obj_t obj, unsigned text_id __hwloc_attribute_unused) |
| 100 |
{ |
| 101 |
FILE *file = loutput->file; |
| 102 |
int r = lcolor->r, g = lcolor->g, b = lcolor->b; |
| 103 |
unsigned cpukind_style = lstopo_obj_cpukind_style(loutput, obj); |
| 104 |
const char *bf_style = ""; |
| 105 |
|
| 106 |
const char *tikzdelim = "{}%&#"; |
| 107 |
|
| 108 |
if (cpukind_style % 2) |
| 109 |
bf_style = "-bold"; |
| 110 |
|
| 111 |
fprintf(file, "\t\\node [hwloc-label%s,text=hwloc-color-%d-%d-%d] at (%u,%u) {", |
| 112 |
bf_style, r, g, b, x, y); |
| 113 |
while (*text) { |
| 114 |
size_t chunksize = strcspn(text, tikzdelim), n_delim; |
| 115 |
fprintf(file, "%.*s", (int) chunksize, text); |
| 116 |
text += chunksize; |
| 117 |
for (n_delim = strspn(text, tikzdelim); *text && n_delim; ++text, --n_delim) |
| 118 |
fprintf(file, "\\%c", *text); |
| 119 |
} |
| 120 |
fprintf(file, "};\n"); |
| 121 |
} |
| 122 |
|
| 123 |
static struct draw_methods tikz_draw_methods = { |
| 124 |
tikz_declare_color, |
| 125 |
NULL, |
| 126 |
tikz_box, |
| 127 |
tikz_line, |
| 128 |
tikz_text, |
| 129 |
tikz_textsize, |
| 130 |
}; |
| 131 |
|
| 132 |
int output_tikz(struct lstopo_output * loutput, const char *filename) |
| 133 |
{ |
| 134 |
const char *font_family; |
| 135 |
FILE *output = open_output(filename, loutput->overwrite); |
| 136 |
if (!output) { |
| 137 |
fprintf(stderr, "Failed to open %s for writing (%s)\n", filename, strerror(errno)); |
| 138 |
return -1; |
| 139 |
} |
| 140 |
|
| 141 |
font_family = tikz_get_font_family(); |
| 142 |
if (!font_family) { |
| 143 |
fprintf(stderr, "Invalid value for %s. The only accepted values are \"rm\", \"sf\" and \"tt\".\n", |
| 144 |
TIKZ_FONTFAMILY_ENV); |
| 145 |
if (output != stdout) |
| 146 |
fclose(output); |
| 147 |
return -1; |
| 148 |
} |
| 149 |
|
| 150 |
loutput->file = output; |
| 151 |
loutput->methods = &tikz_draw_methods; |
| 152 |
loutput->backend_data = output; |
| 153 |
loutput->backend_flags |= LSTOPO_BACKEND_FLAG_APPROXIMATIVE_TEXTWIDTH; |
| 154 |
|
| 155 |
/* recurse once for preparing sizes and positions */ |
| 156 |
loutput->drawing = LSTOPO_DRAWING_PREPARE; |
| 157 |
output_draw(loutput); |
| 158 |
loutput->drawing = LSTOPO_DRAWING_DRAW; |
| 159 |
|
| 160 |
/* ready */ |
| 161 |
|
| 162 |
/* Write LaTeX header */ |
| 163 |
fprintf(output, "\\documentclass{standalone}\n"); |
| 164 |
|
| 165 |
/* Write required LaTeX preambule */ |
| 166 |
fprintf(output, "\n%%%%%%%%%% If inserting in another document, the following lines below must be copied before \\begin{document} %%%%%%%%%%\n\n"); |
| 167 |
fprintf(output, "\\usepackage{tikz}\n\\usepackage{xcolor}\n"); |
| 168 |
declare_colors(loutput); |
| 169 |
lstopo_prepare_custom_styles(loutput); /* Add custom colors to the preambule */ |
| 170 |
fprintf(output, "\n%%%%%%%%%% End of lines needed before \\begin{document} %%%%%%%%%%\n\n"); |
| 171 |
|
| 172 |
fprintf(output, "\\begin{document}\n"); |
| 173 |
|
| 174 |
/* Write actual image code */ |
| 175 |
fprintf(output, "\n%%%%%%%%%% If inserting in another document, this is the actual source code of the picture %%%%%%%%%%\n\n"); |
| 176 |
fprintf(output, "\\begin{tikzpicture}[x=1pt,y=1pt,yscale=-1," |
| 177 |
"hwloc-label/.style={fill=none,draw=none,text=black,align=left,anchor=north west," |
| 178 |
"outer sep=0pt,inner sep=0pt,font=\\fontsize{%u}{%u}\\selectfont%s}," |
| 179 |
"hwloc-label-bold/.style={hwloc-label,font=\\fontsize{%u}{%u}\\selectfont%s\\bfseries}]\n", |
| 180 |
loutput->fontsize, loutput->fontsize + loutput->linespacing, font_family, |
| 181 |
loutput->fontsize, loutput->fontsize + loutput->linespacing, font_family); |
| 182 |
fprintf(output, "\t\\clip (0,0) rectangle (%u,%u);\n", loutput->width, loutput->height); |
| 183 |
output_draw(loutput); |
| 184 |
fprintf(output,"\\end{tikzpicture}\n"); |
| 185 |
fprintf(output, "\n%%%%%%%%%% End of actual source code of the picture to insert in another document %%%%%%%%%%\n\n"); |
| 186 |
|
| 187 |
/* Write LaTeX footer */ |
| 188 |
fprintf(output, "\\end{document}\n"); |
| 189 |
|
| 190 |
if (output != stdout) |
| 191 |
fclose(output); |
| 192 |
|
| 193 |
destroy_colors(loutput); |
| 194 |
return 0; |
| 195 |
} |