| 1 |
/* |
| 2 |
* SPDX-License-Identifier: BSD-3-Clause |
| 3 |
* Copyright © 2019-2021 Inria. All rights reserved. |
| 4 |
* See COPYING in top-level directory. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "private/autogen/config.h" |
| 8 |
#include "hwloc.h" |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
#include <errno.h> |
| 14 |
|
| 15 |
#include "lstopo.h" |
| 16 |
|
| 17 |
#define SVG_TEXT_WIDTH(length, fontsize) (((length) * (fontsize))/2) |
| 18 |
#define SVG_FONTSIZE_SCALE(size) (((size) * 11) / 9) |
| 19 |
|
| 20 |
static void |
| 21 |
native_svg_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) |
| 22 |
{ |
| 23 |
FILE *file = loutput->file; |
| 24 |
unsigned cpukind_style = lstopo_obj_cpukind_style(loutput, obj); |
| 25 |
unsigned thickness = loutput->thickness; |
| 26 |
int r = lcolor->r, g = lcolor->g, b = lcolor->b; |
| 27 |
char id[128] = ""; |
| 28 |
char class[128] = ""; |
| 29 |
char complement[12] = ""; |
| 30 |
char dash[32] = ""; |
| 31 |
|
| 32 |
if (box_id) |
| 33 |
snprintf(complement, sizeof complement, "_%u", box_id); |
| 34 |
|
| 35 |
if (obj) { |
| 36 |
char type[64]; |
| 37 |
hwloc_obj_type_snprintf(type, sizeof(type), obj, 0); |
| 38 |
snprintf(id, sizeof id, " id='%s_%u_rect%s'", type, obj->logical_index, complement); |
| 39 |
snprintf(class, sizeof class, " class='%s'", type); |
| 40 |
} else { |
| 41 |
snprintf(id, sizeof id, " id='anon_rect%s'", complement); |
| 42 |
} |
| 43 |
|
| 44 |
if (cpukind_style) { |
| 45 |
snprintf(dash, sizeof(dash), " stroke-dasharray=\"%u\"", 1U << cpukind_style); |
| 46 |
thickness *= cpukind_style; |
| 47 |
} |
| 48 |
|
| 49 |
fprintf(file,"\t<rect%s%s x='%u' y='%u' width='%u' height='%u' fill='rgb(%d,%d,%d)' stroke='rgb(0,0,0)' stroke-width='%u'%s/>\n", |
| 50 |
id, class, x, y, width, height, r, g, b, thickness, dash); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
static void |
| 55 |
native_svg_line(struct lstopo_output *loutput, unsigned depth __hwloc_attribute_unused, unsigned x1, unsigned y1, unsigned x2, unsigned y2, hwloc_obj_t obj, unsigned line_id) |
| 56 |
{ |
| 57 |
FILE *file = loutput->file; |
| 58 |
char id[128] = ""; |
| 59 |
char class[128] = ""; |
| 60 |
char complement[12] = ""; |
| 61 |
if (line_id) |
| 62 |
snprintf(complement, sizeof complement, "_%u", line_id); |
| 63 |
|
| 64 |
if (obj) { |
| 65 |
char type[64]; |
| 66 |
hwloc_obj_type_snprintf(type, sizeof(type), obj, 0); |
| 67 |
snprintf(id, sizeof id, " id='%s_%u_line%s'", type, obj->logical_index, complement); |
| 68 |
snprintf(class, sizeof class, " class='%s'", type); |
| 69 |
} else { |
| 70 |
snprintf(id, sizeof id, " id='anon_line%s'", complement); |
| 71 |
} |
| 72 |
|
| 73 |
fprintf(file,"\t<line%s%s x1='%u' y1='%u' x2='%u' y2='%u' stroke='rgb(0,0,0)' stroke-width='%u'/>\n", |
| 74 |
id, class, x1, y1, x2, y2, loutput->thickness); |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
native_svg_textsize(struct lstopo_output *loutput __hwloc_attribute_unused, const char *text __hwloc_attribute_unused, unsigned textlength, unsigned fontsize, unsigned *width) |
| 79 |
{ |
| 80 |
fontsize = SVG_FONTSIZE_SCALE(fontsize); |
| 81 |
*width = SVG_TEXT_WIDTH(textlength, fontsize); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
static void |
| 86 |
native_svg_text(struct lstopo_output *loutput, const struct lstopo_color *lcolor, int size, unsigned depth __hwloc_attribute_unused, unsigned x, unsigned y, const char *text, hwloc_obj_t obj, unsigned text_id) |
| 87 |
{ |
| 88 |
FILE *file = loutput->file; |
| 89 |
unsigned cpukind_style = lstopo_obj_cpukind_style(loutput, obj); |
| 90 |
int r = lcolor->r, g = lcolor->g, b = lcolor->b; |
| 91 |
char id[128] = ""; |
| 92 |
char class[128] = ""; |
| 93 |
char complement[12] = ""; |
| 94 |
const char *fontweight = ""; |
| 95 |
|
| 96 |
if (text_id) |
| 97 |
snprintf(complement, sizeof complement, "_%u", text_id); |
| 98 |
|
| 99 |
if (obj) { |
| 100 |
char type[64]; |
| 101 |
hwloc_obj_type_snprintf(type, sizeof(type), obj, 0); |
| 102 |
snprintf(id, sizeof id, " id='%s_%u_text%s'", type, obj->logical_index, complement); |
| 103 |
snprintf(class, sizeof class, " class='%s'", type); |
| 104 |
} else { |
| 105 |
snprintf(id, sizeof id, " id='anon_text%s'", complement); |
| 106 |
} |
| 107 |
|
| 108 |
if (cpukind_style % 2) |
| 109 |
fontweight = " font-weight='bold'"; |
| 110 |
|
| 111 |
fprintf(file,"\t<text%s%s font-family='Monospace'%s x='%u' y='%u' fill='rgb(%d,%d,%d)' font-size='%dpx'>%s</text>\n", |
| 112 |
id, class, |
| 113 |
fontweight, |
| 114 |
x, y+size, r, g, b, size, text); |
| 115 |
} |
| 116 |
|
| 117 |
static struct draw_methods native_svg_draw_methods = { |
| 118 |
NULL, |
| 119 |
NULL, |
| 120 |
native_svg_box, |
| 121 |
native_svg_line, |
| 122 |
native_svg_text, |
| 123 |
native_svg_textsize, |
| 124 |
}; |
| 125 |
|
| 126 |
int output_nativesvg(struct lstopo_output * loutput, const char *filename) |
| 127 |
{ |
| 128 |
FILE *output = open_output(filename, loutput->overwrite); |
| 129 |
if (!output) { |
| 130 |
fprintf(stderr, "Failed to open %s for writing (%s)\n", filename, strerror(errno)); |
| 131 |
return -1; |
| 132 |
} |
| 133 |
|
| 134 |
loutput->file = output; |
| 135 |
loutput->methods = &native_svg_draw_methods; |
| 136 |
loutput->backend_flags |= LSTOPO_BACKEND_FLAG_APPROXIMATIVE_TEXTWIDTH; |
| 137 |
|
| 138 |
/* recurse once for preparing sizes and positions */ |
| 139 |
loutput->drawing = LSTOPO_DRAWING_PREPARE; |
| 140 |
output_draw(loutput); |
| 141 |
loutput->drawing = LSTOPO_DRAWING_DRAW; |
| 142 |
|
| 143 |
fprintf(output, "<?xml version='1.0' encoding='UTF-8'?>\n"); |
| 144 |
fprintf(output, "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='%upx' height='%upx' viewBox='0 0 %upx %upx' version='1.1'>\n", |
| 145 |
loutput->width, loutput->height, loutput->width, loutput->height); |
| 146 |
|
| 147 |
/* ready */ |
| 148 |
declare_colors(loutput); |
| 149 |
lstopo_prepare_custom_styles(loutput); |
| 150 |
|
| 151 |
output_draw(loutput); |
| 152 |
fprintf(output,"</svg>\n"); |
| 153 |
|
| 154 |
if (output != stdout) |
| 155 |
fclose(output); |
| 156 |
|
| 157 |
destroy_colors(loutput); |
| 158 |
return 0; |
| 159 |
} |