| 1 |
/// @file |
| 2 |
/// @ingroup common_render |
| 3 |
/************************************************************************* |
| 4 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 5 |
* All rights reserved. This program and the accompanying materials |
| 6 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
* which accompanies this distribution, and is available at |
| 8 |
* https://www.eclipse.org/legal/epl-v10.html |
| 9 |
* |
| 10 |
* Contributors: Details at https://graphviz.org |
| 11 |
*************************************************************************/ |
| 12 |
|
| 13 |
// Implementation of HTML-like tables. |
| 14 |
// |
| 15 |
// The (now purged) CodeGen graphics model, especially with integral |
| 16 |
// coordinates, is not adequate to handle this as we would like. In particular, |
| 17 |
// it is difficult to handle notions of adjacency and correct rounding to |
| 18 |
// pixels. For example, if 2 adjacent boxes bb1.UR.x == bb2.LL.x, the rectangles |
| 19 |
// may be drawn overlapping. However, if we use bb1.UR.x+1 == bb2.LL.x |
| 20 |
// there may or may not be a gap between them, even in the same device |
| 21 |
// depending on their positions. When CELLSPACING > 1, this isn't as much |
| 22 |
// of a problem. |
| 23 |
// |
| 24 |
// We allow negative spacing as a hack to allow overlapping cell boundaries. |
| 25 |
// For the reasons discussed above, this is difficult to get correct. |
| 26 |
// This is an important enough case we should extend the table model to |
| 27 |
// support it correctly. This could be done by allowing a table attribute, |
| 28 |
// e.g., CELLGRID=n, which sets CELLBORDER=0 and has the border drawing |
| 29 |
// handled correctly by the table. |
| 30 |
|
| 31 |
#include "config.h" |
| 32 |
|
| 33 |
#include <assert.h> |
| 34 |
#include <common/render.h> |
| 35 |
#include <common/htmltable.h> |
| 36 |
#include <common/pointset.h> |
| 37 |
#include <cdt/cdt.h> |
| 38 |
#include <float.h> |
| 39 |
#include <inttypes.h> |
| 40 |
#include <limits.h> |
| 41 |
#include <math.h> |
| 42 |
#include <stddef.h> |
| 43 |
#include <stdbool.h> |
| 44 |
#include <stdint.h> |
| 45 |
#include <stdio.h> |
| 46 |
#include <util/agxbuf.h> |
| 47 |
#include <util/alloc.h> |
| 48 |
#include <util/bitarray.h> |
| 49 |
#include <util/exit.h> |
| 50 |
#include <util/gv_math.h> |
| 51 |
#include <util/prisize_t.h> |
| 52 |
#include <util/strcasecmp.h> |
| 53 |
#include <util/streq.h> |
| 54 |
#include <util/unreachable.h> |
| 55 |
|
| 56 |
#define DEFAULT_BORDER 1 |
| 57 |
#define DEFAULT_CELLPADDING 2 |
| 58 |
#define DEFAULT_CELLSPACING 2 |
| 59 |
|
| 60 |
typedef struct { |
| 61 |
char *url; |
| 62 |
char *tooltip; |
| 63 |
char *target; |
| 64 |
char *id; |
| 65 |
bool explicit_tooltip; |
| 66 |
point LL; |
| 67 |
point UR; |
| 68 |
} htmlmap_data_t; |
| 69 |
|
| 70 |
#ifdef DEBUG |
| 71 |
static void printCell(htmlcell_t * cp, int ind); |
| 72 |
#endif |
| 73 |
|
| 74 |
/* Replace current font attributes in env with ones from fp, |
| 75 |
* storing old attributes in savp. We only deal with attributes |
| 76 |
* set in env. The attributes are restored via popFontInfo. |
| 77 |
*/ |
| 78 |
static void |
| 79 |
pushFontInfo(htmlenv_t * env, textfont_t * fp, textfont_t * savp) |
| 80 |
{ |
| 81 |
if (env->finfo.name) { |
| 82 |
if (fp->name) { |
| 83 |
savp->name = env->finfo.name; |
| 84 |
env->finfo.name = fp->name; |
| 85 |
} else |
| 86 |
savp->name = NULL; |
| 87 |
} |
| 88 |
if (env->finfo.color) { |
| 89 |
if (fp->color) { |
| 90 |
savp->color = env->finfo.color; |
| 91 |
env->finfo.color = fp->color; |
| 92 |
} else |
| 93 |
savp->color = NULL; |
| 94 |
} |
| 95 |
if (env->finfo.size >= 0) { |
| 96 |
if (fp->size >= 0) { |
| 97 |
savp->size = env->finfo.size; |
| 98 |
env->finfo.size = fp->size; |
| 99 |
} else |
| 100 |
savp->size = -1.0; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/* Restore saved font attributes. |
| 105 |
* Copy only set values. |
| 106 |
*/ |
| 107 |
static void popFontInfo(htmlenv_t * env, textfont_t * savp) |
| 108 |
{ |
| 109 |
if (savp->name) |
| 110 |
env->finfo.name = savp->name; |
| 111 |
if (savp->color) |
| 112 |
env->finfo.color = savp->color; |
| 113 |
if (savp->size >= 0.0) |
| 114 |
env->finfo.size = savp->size; |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
emit_htextspans(GVJ_t *job, size_t nspans, htextspan_t *spans, pointf p, |
| 119 |
double halfwidth_x, textfont_t finfo, boxf b, int simple) |
| 120 |
{ |
| 121 |
double center_x, left_x, right_x; |
| 122 |
textspan_t tl; |
| 123 |
textfont_t tf; |
| 124 |
pointf p_ = { 0.0, 0.0 }; |
| 125 |
textspan_t *ti; |
| 126 |
|
| 127 |
center_x = p.x; |
| 128 |
left_x = center_x - halfwidth_x; |
| 129 |
right_x = center_x + halfwidth_x; |
| 130 |
|
| 131 |
/* Initial p is in center of text block; set initial baseline |
| 132 |
* to top of text block. |
| 133 |
*/ |
| 134 |
p_.y = p.y + (b.UR.y - b.LL.y) / 2.0; |
| 135 |
|
| 136 |
gvrender_begin_label(job, LABEL_HTML); |
| 137 |
for (size_t i = 0; i < nspans; i++) { |
| 138 |
/* set p.x to leftmost point where the line of text begins */ |
| 139 |
switch (spans[i].just) { |
| 140 |
case 'l': |
| 141 |
p.x = left_x; |
| 142 |
break; |
| 143 |
case 'r': |
| 144 |
p.x = right_x - spans[i].size; |
| 145 |
break; |
| 146 |
default: |
| 147 |
case 'n': |
| 148 |
p.x = center_x - spans[i].size / 2.0; |
| 149 |
break; |
| 150 |
} |
| 151 |
p_.y -= spans[i].lfsize; /* move to current base line */ |
| 152 |
|
| 153 |
ti = spans[i].items; |
| 154 |
for (size_t j = 0; j < spans[i].nitems; j++) { |
| 155 |
if (ti->font && ti->font->size > 0) |
| 156 |
tf.size = ti->font->size; |
| 157 |
else |
| 158 |
tf.size = finfo.size; |
| 159 |
if (ti->font && ti->font->name) |
| 160 |
tf.name = ti->font->name; |
| 161 |
else |
| 162 |
tf.name = finfo.name; |
| 163 |
if (ti->font && ti->font->color) |
| 164 |
tf.color = ti->font->color; |
| 165 |
else |
| 166 |
tf.color = finfo.color; |
| 167 |
if (ti->font && ti->font->flags) |
| 168 |
tf.flags = ti->font->flags; |
| 169 |
else |
| 170 |
tf.flags = 0; |
| 171 |
|
| 172 |
gvrender_set_pencolor(job, tf.color); |
| 173 |
|
| 174 |
tl.str = ti->str; |
| 175 |
tl.font = &tf; |
| 176 |
tl.yoffset_layout = ti->yoffset_layout; |
| 177 |
if (simple) |
| 178 |
tl.yoffset_centerline = ti->yoffset_centerline; |
| 179 |
else |
| 180 |
tl.yoffset_centerline = 1; |
| 181 |
tl.font->postscript_alias = ti->font->postscript_alias; |
| 182 |
tl.layout = ti->layout; |
| 183 |
tl.size.x = ti->size.x; |
| 184 |
tl.size.y = spans[i].lfsize; |
| 185 |
tl.just = 'l'; |
| 186 |
|
| 187 |
p_.x = p.x; |
| 188 |
gvrender_textspan(job, p_, &tl); |
| 189 |
p.x += ti->size.x; |
| 190 |
ti++; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
gvrender_end_label(job); |
| 195 |
} |
| 196 |
|
| 197 |
static void emit_html_txt(GVJ_t * job, htmltxt_t * tp, htmlenv_t * env) |
| 198 |
{ |
| 199 |
double halfwidth_x; |
| 200 |
pointf p; |
| 201 |
|
| 202 |
/* make sure that there is something to do */ |
| 203 |
if (tp->nspans < 1) |
| 204 |
return; |
| 205 |
|
| 206 |
halfwidth_x = (tp->box.UR.x - tp->box.LL.x) / 2.0; |
| 207 |
p.x = env->pos.x + (tp->box.UR.x + tp->box.LL.x) / 2.0; |
| 208 |
p.y = env->pos.y + (tp->box.UR.y + tp->box.LL.y) / 2.0; |
| 209 |
|
| 210 |
emit_htextspans(job, tp->nspans, tp->spans, p, halfwidth_x, env->finfo, |
| 211 |
tp->box, tp->simple); |
| 212 |
} |
| 213 |
|
| 214 |
static void doSide(GVJ_t * job, pointf p, double wd, double ht) |
| 215 |
{ |
| 216 |
boxf BF; |
| 217 |
|
| 218 |
BF.LL = p; |
| 219 |
BF.UR.x = p.x + wd; |
| 220 |
BF.UR.y = p.y + ht; |
| 221 |
gvrender_box(job, BF, 1); |
| 222 |
} |
| 223 |
|
| 224 |
/* Convert boxf into four corner points |
| 225 |
* If border is > 1, inset the points by half the border. |
| 226 |
* It is assumed AF is pointf[4], so the data is store there |
| 227 |
* and AF is returned. |
| 228 |
*/ |
| 229 |
static pointf *mkPts(pointf * AF, boxf b, int border) |
| 230 |
{ |
| 231 |
AF[0] = b.LL; |
| 232 |
AF[2] = b.UR; |
| 233 |
if (border > 1) { |
| 234 |
const double delta = border / 2.0; |
| 235 |
AF[0].x += delta; |
| 236 |
AF[0].y += delta; |
| 237 |
AF[2].x -= delta; |
| 238 |
AF[2].y -= delta; |
| 239 |
} |
| 240 |
AF[1].x = AF[2].x; |
| 241 |
AF[1].y = AF[0].y; |
| 242 |
AF[3].x = AF[0].x; |
| 243 |
AF[3].y = AF[2].y; |
| 244 |
|
| 245 |
return AF; |
| 246 |
} |
| 247 |
|
| 248 |
/* Draw a rectangular border for the box b. |
| 249 |
* Handles dashed and dotted styles, rounded corners. |
| 250 |
* Also handles thick lines. |
| 251 |
* Assume dp->border > 0 |
| 252 |
*/ |
| 253 |
static void doBorder(GVJ_t * job, htmldata_t * dp, boxf b) |
| 254 |
{ |
| 255 |
pointf AF[7]; |
| 256 |
char *sptr[2]; |
| 257 |
char *color = dp->pencolor ? dp->pencolor : DEFAULT_COLOR; |
| 258 |
unsigned short sides; |
| 259 |
|
| 260 |
gvrender_set_pencolor(job, color); |
| 261 |
if (dp->style.dashed || dp->style.dotted) { |
| 262 |
sptr[0] = sptr[1] = NULL; |
| 263 |
if (dp->style.dashed) |
| 264 |
sptr[0] = "dashed"; |
| 265 |
else if (dp->style.dotted) |
| 266 |
sptr[0] = "dotted"; |
| 267 |
gvrender_set_style(job, sptr); |
| 268 |
} else |
| 269 |
gvrender_set_style(job, job->gvc->defaultlinestyle); |
| 270 |
gvrender_set_penwidth(job, dp->border); |
| 271 |
|
| 272 |
if (dp->style.rounded) |
| 273 |
round_corners(job, mkPts(AF, b, dp->border), 4, |
| 274 |
(graphviz_polygon_style_t){.rounded = true}, 0); |
| 275 |
else if ((sides = (dp->flags & BORDER_MASK))) { |
| 276 |
mkPts (AF+1, b, dp->border); /* AF[1-4] has LL=SW,SE,UR=NE,NW */ |
| 277 |
switch (sides) { |
| 278 |
case BORDER_BOTTOM : |
| 279 |
gvrender_polyline(job, AF+1, 2); |
| 280 |
break; |
| 281 |
case BORDER_RIGHT : |
| 282 |
gvrender_polyline(job, AF+2, 2); |
| 283 |
break; |
| 284 |
case BORDER_TOP : |
| 285 |
gvrender_polyline(job, AF+3, 2); |
| 286 |
break; |
| 287 |
case BORDER_LEFT : |
| 288 |
AF[0] = AF[4]; |
| 289 |
gvrender_polyline(job, AF, 2); |
| 290 |
break; |
| 291 |
case BORDER_BOTTOM|BORDER_RIGHT : |
| 292 |
gvrender_polyline(job, AF+1, 3); |
| 293 |
break; |
| 294 |
case BORDER_RIGHT|BORDER_TOP : |
| 295 |
gvrender_polyline(job, AF+2, 3); |
| 296 |
break; |
| 297 |
case BORDER_TOP|BORDER_LEFT : |
| 298 |
AF[5] = AF[1]; |
| 299 |
gvrender_polyline(job, AF+3, 3); |
| 300 |
break; |
| 301 |
case BORDER_LEFT|BORDER_BOTTOM : |
| 302 |
AF[0] = AF[4]; |
| 303 |
gvrender_polyline(job, AF, 3); |
| 304 |
break; |
| 305 |
case BORDER_BOTTOM|BORDER_RIGHT|BORDER_TOP : |
| 306 |
gvrender_polyline(job, AF+1, 4); |
| 307 |
break; |
| 308 |
case BORDER_RIGHT|BORDER_TOP|BORDER_LEFT : |
| 309 |
AF[5] = AF[1]; |
| 310 |
gvrender_polyline(job, AF+2, 4); |
| 311 |
break; |
| 312 |
case BORDER_TOP|BORDER_LEFT|BORDER_BOTTOM : |
| 313 |
AF[5] = AF[1]; |
| 314 |
AF[6] = AF[2]; |
| 315 |
gvrender_polyline(job, AF+3, 4); |
| 316 |
break; |
| 317 |
case BORDER_LEFT|BORDER_BOTTOM|BORDER_RIGHT : |
| 318 |
AF[0] = AF[4]; |
| 319 |
gvrender_polyline(job, AF, 4); |
| 320 |
break; |
| 321 |
case BORDER_TOP|BORDER_BOTTOM : |
| 322 |
gvrender_polyline(job, AF+1, 2); |
| 323 |
gvrender_polyline(job, AF+3, 2); |
| 324 |
break; |
| 325 |
case BORDER_LEFT|BORDER_RIGHT : |
| 326 |
AF[0] = AF[4]; |
| 327 |
gvrender_polyline(job, AF, 2); |
| 328 |
gvrender_polyline(job, AF+2, 2); |
| 329 |
break; |
| 330 |
default: |
| 331 |
break; |
| 332 |
} |
| 333 |
} else { |
| 334 |
if (dp->border > 1) { |
| 335 |
const double delta = dp->border / 2.0; |
| 336 |
b.LL.x += delta; |
| 337 |
b.LL.y += delta; |
| 338 |
b.UR.x -= delta; |
| 339 |
b.UR.y -= delta; |
| 340 |
} |
| 341 |
gvrender_box(job, b, 0); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
/* Set up fill values from given color; make pen transparent. |
| 346 |
* Return type of fill required. |
| 347 |
*/ |
| 348 |
static int setFill(GVJ_t *job, char *color, int angle, htmlstyle_t style, |
| 349 |
char *clrs[2]) { |
| 350 |
int filled; |
| 351 |
double frac; |
| 352 |
if (findStopColor(color, clrs, &frac)) { |
| 353 |
gvrender_set_fillcolor(job, clrs[0]); |
| 354 |
if (clrs[1]) |
| 355 |
gvrender_set_gradient_vals(job, clrs[1], angle, frac); |
| 356 |
else |
| 357 |
gvrender_set_gradient_vals(job, DEFAULT_COLOR, angle, frac); |
| 358 |
if (style.radial) |
| 359 |
filled = RGRADIENT; |
| 360 |
else |
| 361 |
filled = GRADIENT; |
| 362 |
} else { |
| 363 |
gvrender_set_fillcolor(job, color); |
| 364 |
filled = FILL; |
| 365 |
} |
| 366 |
gvrender_set_pencolor(job, "transparent"); |
| 367 |
return filled; |
| 368 |
} |
| 369 |
|
| 370 |
/* Save current map values. |
| 371 |
* Initialize fields in job->obj pertaining to anchors. |
| 372 |
* In particular, this also sets the output rectangle. |
| 373 |
* If there is something to do, |
| 374 |
* start the anchor and returns 1. |
| 375 |
* Otherwise, it returns 0. |
| 376 |
* |
| 377 |
* FIX: Should we provide a tooltip if none is set, as is done |
| 378 |
* for nodes, edges, etc. ? |
| 379 |
*/ |
| 380 |
static int |
| 381 |
initAnchor(GVJ_t * job, htmlenv_t * env, htmldata_t * data, boxf b, |
| 382 |
htmlmap_data_t * save) |
| 383 |
{ |
| 384 |
obj_state_t *obj = job->obj; |
| 385 |
char *id; |
| 386 |
static int anchorId; |
| 387 |
agxbuf xb = {0}; |
| 388 |
|
| 389 |
save->url = obj->url; |
| 390 |
save->tooltip = obj->tooltip; |
| 391 |
save->target = obj->target; |
| 392 |
save->id = obj->id; |
| 393 |
save->explicit_tooltip = obj->explicit_tooltip != 0; |
| 394 |
id = data->id; |
| 395 |
if (!id || !*id) { /* no external id, so use the internal one */ |
| 396 |
if (!env->objid) { |
| 397 |
env->objid = gv_strdup(getObjId(job, obj->u.n, &xb)); |
| 398 |
env->objid_set = true; |
| 399 |
} |
| 400 |
agxbprint(&xb, "%s_%d", env->objid, anchorId++); |
| 401 |
id = agxbuse(&xb); |
| 402 |
} |
| 403 |
const bool changed = |
| 404 |
initMapData(job, NULL, data->href, data->title, data->target, id, |
| 405 |
obj->u.g); |
| 406 |
agxbfree(&xb); |
| 407 |
|
| 408 |
if (changed) { |
| 409 |
if (obj->url || obj->explicit_tooltip) { |
| 410 |
emit_map_rect(job, b); |
| 411 |
gvrender_begin_anchor(job, |
| 412 |
obj->url, obj->tooltip, obj->target, |
| 413 |
obj->id); |
| 414 |
} |
| 415 |
} |
| 416 |
return changed; |
| 417 |
} |
| 418 |
|
| 419 |
#define RESET(fld) \ |
| 420 |
if(obj->fld != save->fld) {free(obj->fld); obj->fld = save->fld;} |
| 421 |
|
| 422 |
/* Pop context pushed by initAnchor. |
| 423 |
* This is done by ending current anchor, restoring old values and |
| 424 |
* freeing new. |
| 425 |
* |
| 426 |
* NB: We don't save or restore geometric map info. This is because |
| 427 |
* this preservation of map context is only necessary for SVG-like |
| 428 |
* systems where graphical items are wrapped in an anchor, and we map |
| 429 |
* top-down. For ordinary map anchors, this is all done bottom-up, so |
| 430 |
* the geometric map info at the higher level hasn't been emitted yet. |
| 431 |
*/ |
| 432 |
static void endAnchor(GVJ_t * job, htmlmap_data_t * save) |
| 433 |
{ |
| 434 |
obj_state_t *obj = job->obj; |
| 435 |
|
| 436 |
if (obj->url || obj->explicit_tooltip) |
| 437 |
gvrender_end_anchor(job); |
| 438 |
RESET(url); |
| 439 |
RESET(tooltip); |
| 440 |
RESET(target); |
| 441 |
RESET(id); |
| 442 |
obj->explicit_tooltip = save->explicit_tooltip; |
| 443 |
} |
| 444 |
|
| 445 |
static void emit_html_cell(GVJ_t * job, htmlcell_t * cp, htmlenv_t * env); |
| 446 |
|
| 447 |
/* place vertical and horizontal lines between adjacent cells and |
| 448 |
* extend the lines to intersect the rounded table boundary |
| 449 |
*/ |
| 450 |
static void |
| 451 |
emit_html_rules(GVJ_t * job, htmlcell_t * cp, htmlenv_t * env, char *color, htmlcell_t* nextc) |
| 452 |
{ |
| 453 |
pointf rule_pt; |
| 454 |
double rule_length; |
| 455 |
double base; |
| 456 |
pointf pos = env->pos; |
| 457 |
|
| 458 |
if (!color) |
| 459 |
color = DEFAULT_COLOR; |
| 460 |
gvrender_set_fillcolor(job, color); |
| 461 |
gvrender_set_pencolor(job, color); |
| 462 |
|
| 463 |
boxf pts = cp->data.box; |
| 464 |
pts.LL.x += pos.x; |
| 465 |
pts.UR.x += pos.x; |
| 466 |
pts.LL.y += pos.y; |
| 467 |
pts.UR.y += pos.y; |
| 468 |
|
| 469 |
//Determine vertical line coordinate and length |
| 470 |
if (cp->vruled && cp->col + cp->colspan < cp->parent->column_count) { |
| 471 |
if (cp->row == 0) { // first row |
| 472 |
// extend to center of table border and add half cell spacing |
| 473 |
base = cp->parent->data.border + cp->parent->data.space / 2; |
| 474 |
rule_pt.y = pts.LL.y - cp->parent->data.space / 2; |
| 475 |
} else if (cp->row + cp->rowspan == cp->parent->row_count) { // bottom row |
| 476 |
// extend to center of table border and add half cell spacing |
| 477 |
base = cp->parent->data.border + cp->parent->data.space / 2; |
| 478 |
rule_pt.y = pts.LL.y - cp->parent->data.space / 2 - base; |
| 479 |
} else { |
| 480 |
base = 0; |
| 481 |
rule_pt.y = pts.LL.y - cp->parent->data.space / 2; |
| 482 |
} |
| 483 |
rule_pt.x = pts.UR.x + cp->parent->data.space / 2; |
| 484 |
rule_length = base + pts.UR.y - pts.LL.y + cp->parent->data.space; |
| 485 |
doSide(job, rule_pt, 0, rule_length); |
| 486 |
} |
| 487 |
//Determine the horizontal coordinate and length |
| 488 |
if (cp->hruled && cp->row + cp->rowspan < cp->parent->row_count) { |
| 489 |
if (cp->col == 0) { // first column |
| 490 |
// extend to center of table border and add half cell spacing |
| 491 |
base = cp->parent->data.border + cp->parent->data.space / 2; |
| 492 |
rule_pt.x = pts.LL.x - base - cp->parent->data.space / 2; |
| 493 |
if (cp->col + cp->colspan == cp->parent->column_count) // also last column |
| 494 |
base *= 2; |
| 495 |
/* incomplete row of cells; extend line to end */ |
| 496 |
else if (nextc && nextc->row != cp->row) { |
| 497 |
base += cp->parent->data.box.UR.x + pos.x - (pts.UR.x + cp->parent->data.space / 2); |
| 498 |
} |
| 499 |
} else if (cp->col + cp->colspan == cp->parent->column_count) { // last column |
| 500 |
// extend to center of table border and add half cell spacing |
| 501 |
base = cp->parent->data.border + cp->parent->data.space / 2; |
| 502 |
rule_pt.x = pts.LL.x - cp->parent->data.space / 2; |
| 503 |
} else { |
| 504 |
base = 0; |
| 505 |
rule_pt.x = pts.LL.x - cp->parent->data.space / 2; |
| 506 |
/* incomplete row of cells; extend line to end */ |
| 507 |
if (nextc && nextc->row != cp->row) { |
| 508 |
base += cp->parent->data.box.UR.x + pos.x - (pts.UR.x + cp->parent->data.space / 2); |
| 509 |
} |
| 510 |
} |
| 511 |
rule_pt.y = pts.LL.y - cp->parent->data.space / 2; |
| 512 |
rule_length = base + pts.UR.x - pts.LL.x + cp->parent->data.space; |
| 513 |
doSide(job, rule_pt, rule_length, 0); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
static void emit_html_tbl(GVJ_t * job, htmltbl_t * tbl, htmlenv_t * env) |
| 518 |
{ |
| 519 |
boxf pts = tbl->data.box; |
| 520 |
pointf pos = env->pos; |
| 521 |
htmlcell_t **cells = tbl->cells; |
| 522 |
htmlcell_t *cp; |
| 523 |
static textfont_t savef; |
| 524 |
htmlmap_data_t saved; |
| 525 |
int anchor; /* if true, we need to undo anchor settings. */ |
| 526 |
const bool doAnchor = tbl->data.href || tbl->data.target || tbl->data.title; |
| 527 |
pointf AF[4]; |
| 528 |
|
| 529 |
if (tbl->font) |
| 530 |
pushFontInfo(env, tbl->font, &savef); |
| 531 |
|
| 532 |
pts.LL.x += pos.x; |
| 533 |
pts.UR.x += pos.x; |
| 534 |
pts.LL.y += pos.y; |
| 535 |
pts.UR.y += pos.y; |
| 536 |
|
| 537 |
if (doAnchor && !(job->flags & EMIT_CLUSTERS_LAST)) |
| 538 |
anchor = initAnchor(job, env, &tbl->data, pts, &saved); |
| 539 |
else |
| 540 |
anchor = 0; |
| 541 |
|
| 542 |
if (!tbl->data.style.invisible) { |
| 543 |
|
| 544 |
/* Fill first */ |
| 545 |
if (tbl->data.bgcolor) { |
| 546 |
char *clrs[2] = {0}; |
| 547 |
int filled = |
| 548 |
setFill(job, tbl->data.bgcolor, tbl->data.gradientangle, |
| 549 |
tbl->data.style, clrs); |
| 550 |
if (tbl->data.style.rounded) { |
| 551 |
round_corners(job, mkPts(AF, pts, tbl->data.border), 4, |
| 552 |
(graphviz_polygon_style_t){.rounded = true}, filled); |
| 553 |
} else |
| 554 |
gvrender_box(job, pts, filled); |
| 555 |
free(clrs[0]); |
| 556 |
free(clrs[1]); |
| 557 |
} |
| 558 |
|
| 559 |
while (*cells) { |
| 560 |
emit_html_cell(job, *cells, env); |
| 561 |
cells++; |
| 562 |
} |
| 563 |
|
| 564 |
/* Draw table rules and border. |
| 565 |
* Draw after cells so we can draw over any fill. |
| 566 |
* At present, we set the penwidth to 1 for rules until we provide the calculations to take |
| 567 |
* into account wider rules. |
| 568 |
*/ |
| 569 |
cells = tbl->cells; |
| 570 |
gvrender_set_penwidth(job, 1.0); |
| 571 |
while ((cp = *cells++)) { |
| 572 |
if (cp->hruled || cp->vruled) |
| 573 |
emit_html_rules(job, cp, env, tbl->data.pencolor, *cells); |
| 574 |
} |
| 575 |
|
| 576 |
if (tbl->data.border) |
| 577 |
doBorder(job, &tbl->data, pts); |
| 578 |
|
| 579 |
} |
| 580 |
|
| 581 |
if (anchor) |
| 582 |
endAnchor(job, &saved); |
| 583 |
|
| 584 |
if (doAnchor && (job->flags & EMIT_CLUSTERS_LAST)) { |
| 585 |
if (initAnchor(job, env, &tbl->data, pts, &saved)) |
| 586 |
endAnchor(job, &saved); |
| 587 |
} |
| 588 |
|
| 589 |
if (tbl->font) |
| 590 |
popFontInfo(env, &savef); |
| 591 |
} |
| 592 |
|
| 593 |
/* The image will be centered in the given box. |
| 594 |
* Scaling is determined by either the image's scale attribute, |
| 595 |
* or the imagescale attribute of the graph object being drawn. |
| 596 |
*/ |
| 597 |
static void emit_html_img(GVJ_t * job, htmlimg_t * cp, htmlenv_t * env) |
| 598 |
{ |
| 599 |
pointf A[4]; |
| 600 |
boxf bb = cp->box; |
| 601 |
char *scale; |
| 602 |
|
| 603 |
bb.LL.x += env->pos.x; |
| 604 |
bb.LL.y += env->pos.y; |
| 605 |
bb.UR.x += env->pos.x; |
| 606 |
bb.UR.y += env->pos.y; |
| 607 |
|
| 608 |
A[0] = bb.UR; |
| 609 |
A[2] = bb.LL; |
| 610 |
A[1].x = A[2].x; |
| 611 |
A[1].y = A[0].y; |
| 612 |
A[3].x = A[0].x; |
| 613 |
A[3].y = A[2].y; |
| 614 |
|
| 615 |
if (cp->scale) |
| 616 |
scale = cp->scale; |
| 617 |
else |
| 618 |
scale = env->imgscale; |
| 619 |
assert(cp->src); |
| 620 |
assert(cp->src[0]); |
| 621 |
gvrender_usershape(job, cp->src, A, 4, true, scale, "mc"); |
| 622 |
} |
| 623 |
|
| 624 |
static void emit_html_cell(GVJ_t * job, htmlcell_t * cp, htmlenv_t * env) |
| 625 |
{ |
| 626 |
htmlmap_data_t saved; |
| 627 |
boxf pts = cp->data.box; |
| 628 |
pointf pos = env->pos; |
| 629 |
int inAnchor; |
| 630 |
const bool doAnchor = cp->data.href || cp->data.target || cp->data.title; |
| 631 |
pointf AF[4]; |
| 632 |
|
| 633 |
pts.LL.x += pos.x; |
| 634 |
pts.UR.x += pos.x; |
| 635 |
pts.LL.y += pos.y; |
| 636 |
pts.UR.y += pos.y; |
| 637 |
|
| 638 |
if (doAnchor && !(job->flags & EMIT_CLUSTERS_LAST)) |
| 639 |
inAnchor = initAnchor(job, env, &cp->data, pts, &saved); |
| 640 |
else |
| 641 |
inAnchor = 0; |
| 642 |
|
| 643 |
if (!cp->data.style.invisible) { |
| 644 |
if (cp->data.bgcolor) { |
| 645 |
char *clrs[2]; |
| 646 |
int filled = |
| 647 |
setFill(job, cp->data.bgcolor, cp->data.gradientangle, |
| 648 |
cp->data.style, clrs); |
| 649 |
if (cp->data.style.rounded) { |
| 650 |
round_corners(job, mkPts(AF, pts, cp->data.border), 4, |
| 651 |
(graphviz_polygon_style_t){.rounded = true}, filled); |
| 652 |
} else |
| 653 |
gvrender_box(job, pts, filled); |
| 654 |
free(clrs[0]); |
| 655 |
} |
| 656 |
|
| 657 |
if (cp->data.border) |
| 658 |
doBorder(job, &cp->data, pts); |
| 659 |
|
| 660 |
if (cp->child.kind == HTML_TBL) |
| 661 |
emit_html_tbl(job, cp->child.u.tbl, env); |
| 662 |
else if (cp->child.kind == HTML_IMAGE) |
| 663 |
emit_html_img(job, cp->child.u.img, env); |
| 664 |
else |
| 665 |
emit_html_txt(job, cp->child.u.txt, env); |
| 666 |
} |
| 667 |
|
| 668 |
if (inAnchor) |
| 669 |
endAnchor(job, &saved); |
| 670 |
|
| 671 |
if (doAnchor && (job->flags & EMIT_CLUSTERS_LAST)) { |
| 672 |
if (initAnchor(job, env, &cp->data, pts, &saved)) |
| 673 |
endAnchor(job, &saved); |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
/* Push new obj on stack to be used in common by all |
| 678 |
* html elements with anchors. |
| 679 |
* This inherits the type, emit_state, and object of the |
| 680 |
* parent, as well as the url, explicit, target and tooltip. |
| 681 |
*/ |
| 682 |
static void allocObj(GVJ_t * job) |
| 683 |
{ |
| 684 |
obj_state_t *obj; |
| 685 |
obj_state_t *parent; |
| 686 |
|
| 687 |
obj = push_obj_state(job); |
| 688 |
parent = obj->parent; |
| 689 |
obj->type = parent->type; |
| 690 |
obj->emit_state = parent->emit_state; |
| 691 |
switch (obj->type) { |
| 692 |
case NODE_OBJTYPE: |
| 693 |
obj->u.n = parent->u.n; |
| 694 |
break; |
| 695 |
case ROOTGRAPH_OBJTYPE: |
| 696 |
obj->u.g = parent->u.g; |
| 697 |
break; |
| 698 |
case CLUSTER_OBJTYPE: |
| 699 |
obj->u.sg = parent->u.sg; |
| 700 |
break; |
| 701 |
case EDGE_OBJTYPE: |
| 702 |
obj->u.e = parent->u.e; |
| 703 |
break; |
| 704 |
default: |
| 705 |
UNREACHABLE(); |
| 706 |
} |
| 707 |
obj->url = parent->url; |
| 708 |
obj->tooltip = parent->tooltip; |
| 709 |
obj->target = parent->target; |
| 710 |
obj->explicit_tooltip = parent->explicit_tooltip; |
| 711 |
} |
| 712 |
|
| 713 |
static void freeObj(GVJ_t * job) |
| 714 |
{ |
| 715 |
obj_state_t *obj = job->obj; |
| 716 |
|
| 717 |
obj->url = NULL; |
| 718 |
obj->tooltip = NULL; |
| 719 |
obj->target = NULL; |
| 720 |
obj->id = NULL; |
| 721 |
pop_obj_state(job); |
| 722 |
} |
| 723 |
|
| 724 |
static double |
| 725 |
heightOfLbl (htmllabel_t * lp) |
| 726 |
{ |
| 727 |
double sz = 0.0; |
| 728 |
|
| 729 |
switch (lp->kind) { |
| 730 |
case HTML_TBL: |
| 731 |
sz = lp->u.tbl->data.box.UR.y - lp->u.tbl->data.box.LL.y; |
| 732 |
break; |
| 733 |
case HTML_IMAGE: |
| 734 |
sz = lp->u.img->box.UR.y - lp->u.img->box.LL.y; |
| 735 |
break; |
| 736 |
case HTML_TEXT: |
| 737 |
sz = lp->u.txt->box.UR.y - lp->u.txt->box.LL.y; |
| 738 |
break; |
| 739 |
default: |
| 740 |
UNREACHABLE(); |
| 741 |
} |
| 742 |
return sz; |
| 743 |
} |
| 744 |
|
| 745 |
void emit_html_label(GVJ_t * job, htmllabel_t * lp, textlabel_t * tp) |
| 746 |
{ |
| 747 |
htmlenv_t env; |
| 748 |
pointf p; |
| 749 |
|
| 750 |
allocObj(job); |
| 751 |
|
| 752 |
p = tp->pos; |
| 753 |
switch (tp->valign) { |
| 754 |
case 't': |
| 755 |
p.y = tp->pos.y + (tp->space.y - heightOfLbl(lp))/ 2.0 - 1; |
| 756 |
break; |
| 757 |
case 'b': |
| 758 |
p.y = tp->pos.y - (tp->space.y - heightOfLbl(lp))/ 2.0 - 1; |
| 759 |
break; |
| 760 |
default: |
| 761 |
/* no-op */ |
| 762 |
break; |
| 763 |
} |
| 764 |
env.pos = p; |
| 765 |
env.finfo.color = tp->fontcolor; |
| 766 |
env.finfo.name = tp->fontname; |
| 767 |
env.finfo.size = tp->fontsize; |
| 768 |
env.imgscale = agget(job->obj->u.n, "imagescale"); |
| 769 |
env.objid = job->obj->id; |
| 770 |
env.objid_set = false; |
| 771 |
if (env.imgscale == NULL || env.imgscale[0] == '\0') |
| 772 |
env.imgscale = "false"; |
| 773 |
if (lp->kind == HTML_TBL) { |
| 774 |
htmltbl_t *tbl = lp->u.tbl; |
| 775 |
|
| 776 |
/* set basic graphics context */ |
| 777 |
/* Need to override line style set by node. */ |
| 778 |
gvrender_set_style(job, job->gvc->defaultlinestyle); |
| 779 |
if (tbl->data.pencolor) |
| 780 |
gvrender_set_pencolor(job, tbl->data.pencolor); |
| 781 |
else |
| 782 |
gvrender_set_pencolor(job, DEFAULT_COLOR); |
| 783 |
emit_html_tbl(job, tbl, &env); |
| 784 |
} else { |
| 785 |
emit_html_txt(job, lp->u.txt, &env); |
| 786 |
} |
| 787 |
if (env.objid_set) |
| 788 |
free(env.objid); |
| 789 |
freeObj(job); |
| 790 |
} |
| 791 |
|
| 792 |
void free_html_data(htmldata_t * dp) |
| 793 |
{ |
| 794 |
free(dp->href); |
| 795 |
free(dp->port); |
| 796 |
free(dp->target); |
| 797 |
free(dp->id); |
| 798 |
free(dp->title); |
| 799 |
free(dp->bgcolor); |
| 800 |
free(dp->pencolor); |
| 801 |
} |
| 802 |
|
| 803 |
void free_html_text(htmltxt_t * t) |
| 804 |
{ |
| 805 |
if (!t) |
| 806 |
return; |
| 807 |
|
| 808 |
htextspan_t *const tl = t->spans; |
| 809 |
for (size_t i = 0; i < t->nspans; i++) { |
| 810 |
textspan_t *const ti = tl[i].items; |
| 811 |
for (size_t j = 0; j < tl[i].nitems; j++) { |
| 812 |
free(ti[j].str); |
| 813 |
if (ti[j].layout && ti[j].free_layout) |
| 814 |
ti[j].free_layout(ti[j].layout); |
| 815 |
} |
| 816 |
free(ti); |
| 817 |
} |
| 818 |
free(tl); |
| 819 |
free(t); |
| 820 |
} |
| 821 |
|
| 822 |
static void free_html_img(htmlimg_t * ip) |
| 823 |
{ |
| 824 |
free(ip->src); |
| 825 |
free(ip); |
| 826 |
} |
| 827 |
|
| 828 |
static void free_html_cell(htmlcell_t * cp) |
| 829 |
{ |
| 830 |
free_html_label(&cp->child, 0); |
| 831 |
free_html_data(&cp->data); |
| 832 |
free(cp); |
| 833 |
} |
| 834 |
|
| 835 |
/* If tbl->row_count is `SIZE_MAX`, table is in initial state from |
| 836 |
* HTML parse, with data stored in `tbl->prev` and `tbl->rows`. Once run through |
| 837 |
* processTbl, data is stored in `tbl->parent` and `tbl->cells`, and |
| 838 |
* tbl->row_count is < `SIZE_MAX`. |
| 839 |
*/ |
| 840 |
static void free_html_tbl(htmltbl_t * tbl) |
| 841 |
{ |
| 842 |
htmlcell_t **cells; |
| 843 |
|
| 844 |
if (tbl->row_count == SIZE_MAX) { // raw, parsed table |
| 845 |
LIST_FREE(&tbl->rows); |
| 846 |
} else { |
| 847 |
cells = tbl->cells; |
| 848 |
|
| 849 |
free(tbl->heights); |
| 850 |
free(tbl->widths); |
| 851 |
while (*cells) { |
| 852 |
free_html_cell(*cells); |
| 853 |
cells++; |
| 854 |
} |
| 855 |
free(tbl->cells); |
| 856 |
} |
| 857 |
free_html_data(&tbl->data); |
| 858 |
free(tbl); |
| 859 |
} |
| 860 |
|
| 861 |
void free_html_label(htmllabel_t * lp, int root) |
| 862 |
{ |
| 863 |
if (lp->kind == HTML_TBL) |
| 864 |
free_html_tbl(lp->u.tbl); |
| 865 |
else if (lp->kind == HTML_IMAGE) |
| 866 |
free_html_img(lp->u.img); |
| 867 |
else |
| 868 |
free_html_text(lp->u.txt); |
| 869 |
if (root) |
| 870 |
free(lp); |
| 871 |
} |
| 872 |
|
| 873 |
static htmldata_t *portToTbl(htmltbl_t *, char *); |
| 874 |
|
| 875 |
static htmldata_t *portToCell(htmlcell_t * cp, char *id) |
| 876 |
{ |
| 877 |
htmldata_t *rv; |
| 878 |
|
| 879 |
if (cp->data.port && strcasecmp(cp->data.port, id) == 0) |
| 880 |
rv = &cp->data; |
| 881 |
else if (cp->child.kind == HTML_TBL) |
| 882 |
rv = portToTbl(cp->child.u.tbl, id); |
| 883 |
else |
| 884 |
rv = NULL; |
| 885 |
|
| 886 |
return rv; |
| 887 |
} |
| 888 |
|
| 889 |
/* See if tp or any of its child cells has the given port id. |
| 890 |
* If true, return corresponding box. |
| 891 |
*/ |
| 892 |
static htmldata_t *portToTbl(htmltbl_t * tp, char *id) |
| 893 |
{ |
| 894 |
htmldata_t *rv; |
| 895 |
htmlcell_t **cells; |
| 896 |
htmlcell_t *cp; |
| 897 |
|
| 898 |
if (tp->data.port && strcasecmp(tp->data.port, id) == 0) |
| 899 |
rv = &tp->data; |
| 900 |
else { |
| 901 |
rv = NULL; |
| 902 |
cells = tp->cells; |
| 903 |
while ((cp = *cells++)) { |
| 904 |
if ((rv = portToCell(cp, id))) |
| 905 |
break; |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
return rv; |
| 910 |
} |
| 911 |
|
| 912 |
/* See if edge port corresponds to part of the html node. |
| 913 |
* If successful, return pointer to port's box. |
| 914 |
* Else return NULL. |
| 915 |
*/ |
| 916 |
boxf *html_port(node_t *n, char *pname, unsigned char *sides){ |
| 917 |
assert(pname != NULL && !streq(pname, "")); |
| 918 |
htmldata_t *tp; |
| 919 |
htmllabel_t *lbl = ND_label(n)->u.html; |
| 920 |
boxf *rv = NULL; |
| 921 |
|
| 922 |
if (lbl->kind == HTML_TEXT) |
| 923 |
return NULL; |
| 924 |
|
| 925 |
tp = portToTbl(lbl->u.tbl, pname); |
| 926 |
if (tp) { |
| 927 |
rv = &tp->box; |
| 928 |
*sides = tp->sides; |
| 929 |
} |
| 930 |
return rv; |
| 931 |
|
| 932 |
} |
| 933 |
|
| 934 |
static int size_html_txt(GVC_t *gvc, htmltxt_t * ftxt, htmlenv_t * env) |
| 935 |
{ |
| 936 |
double xsize = 0.0; /* width of text block */ |
| 937 |
double ysize = 0.0; /* height of text block */ |
| 938 |
double lsize; /* height of current line */ |
| 939 |
double mxfsize = 0.0; /* max. font size for the current line */ |
| 940 |
double curbline = 0.0; /* dist. of current base line from top */ |
| 941 |
pointf sz; |
| 942 |
double width; |
| 943 |
textspan_t lp; |
| 944 |
textfont_t tf = {NULL,NULL,NULL,0.0,0,0}; |
| 945 |
double maxoffset, mxysize = 0.0; |
| 946 |
bool simple = true; // one item per span, same font size/face, no flags |
| 947 |
double prev_fsize = -1; |
| 948 |
char* prev_fname = NULL; |
| 949 |
|
| 950 |
for (size_t i = 0; i < ftxt->nspans; i++) { |
| 951 |
if (ftxt->spans[i].nitems > 1) { |
| 952 |
simple = false; |
| 953 |
break; |
| 954 |
} |
| 955 |
if (ftxt->spans[i].items[0].font) { |
| 956 |
if (ftxt->spans[i].items[0].font->flags) { |
| 957 |
simple = false; |
| 958 |
break; |
| 959 |
} |
| 960 |
if (ftxt->spans[i].items[0].font->size > 0) |
| 961 |
tf.size = ftxt->spans[i].items[0].font->size; |
| 962 |
else |
| 963 |
tf.size = env->finfo.size; |
| 964 |
if (ftxt->spans[i].items[0].font->name) |
| 965 |
tf.name = ftxt->spans[i].items[0].font->name; |
| 966 |
else |
| 967 |
tf.name = env->finfo.name; |
| 968 |
} |
| 969 |
else { |
| 970 |
tf.size = env->finfo.size; |
| 971 |
tf.name = env->finfo.name; |
| 972 |
} |
| 973 |
if (i == 0) |
| 974 |
prev_fsize = tf.size; |
| 975 |
else if (tf.size != prev_fsize) { |
| 976 |
simple = false; |
| 977 |
break; |
| 978 |
} |
| 979 |
if (prev_fname == NULL) |
| 980 |
prev_fname = tf.name; |
| 981 |
else if (strcmp(tf.name,prev_fname)) { |
| 982 |
simple = false; |
| 983 |
break; |
| 984 |
} |
| 985 |
} |
| 986 |
ftxt->simple = simple; |
| 987 |
|
| 988 |
for (size_t i = 0; i < ftxt->nspans; i++) { |
| 989 |
width = 0; |
| 990 |
mxysize = maxoffset = mxfsize = 0; |
| 991 |
for (size_t j = 0; j < ftxt->spans[i].nitems; j++) { |
| 992 |
lp.str = |
| 993 |
strdup_and_subst_obj(ftxt->spans[i].items[j].str, |
| 994 |
env->obj); |
| 995 |
if (ftxt->spans[i].items[j].font) { |
| 996 |
if (ftxt->spans[i].items[j].font->flags) |
| 997 |
tf.flags = ftxt->spans[i].items[j].font->flags; |
| 998 |
else if (env->finfo.flags > 0) |
| 999 |
tf.flags = env->finfo.flags; |
| 1000 |
else |
| 1001 |
tf.flags = 0; |
| 1002 |
if (ftxt->spans[i].items[j].font->size > 0) |
| 1003 |
tf.size = ftxt->spans[i].items[j].font->size; |
| 1004 |
else |
| 1005 |
tf.size = env->finfo.size; |
| 1006 |
if (ftxt->spans[i].items[j].font->name) |
| 1007 |
tf.name = ftxt->spans[i].items[j].font->name; |
| 1008 |
else |
| 1009 |
tf.name = env->finfo.name; |
| 1010 |
if (ftxt->spans[i].items[j].font->color) |
| 1011 |
tf.color = ftxt->spans[i].items[j].font->color; |
| 1012 |
else |
| 1013 |
tf.color = env->finfo.color; |
| 1014 |
} else { |
| 1015 |
tf.size = env->finfo.size; |
| 1016 |
tf.name = env->finfo.name; |
| 1017 |
tf.color = env->finfo.color; |
| 1018 |
tf.flags = env->finfo.flags; |
| 1019 |
} |
| 1020 |
lp.font = dtinsert(gvc->textfont_dt, &tf); |
| 1021 |
sz = textspan_size(gvc, &lp); |
| 1022 |
free(ftxt->spans[i].items[j].str); |
| 1023 |
ftxt->spans[i].items[j].str = lp.str; |
| 1024 |
ftxt->spans[i].items[j].size.x = sz.x; |
| 1025 |
ftxt->spans[i].items[j].yoffset_layout = lp.yoffset_layout; |
| 1026 |
ftxt->spans[i].items[j].yoffset_centerline = lp.yoffset_centerline; |
| 1027 |
ftxt->spans[i].items[j].font = lp.font; |
| 1028 |
ftxt->spans[i].items[j].layout = lp.layout; |
| 1029 |
ftxt->spans[i].items[j].free_layout = lp.free_layout; |
| 1030 |
width += sz.x; |
| 1031 |
mxfsize = MAX(tf.size, mxfsize); |
| 1032 |
mxysize = MAX(sz.y, mxysize); |
| 1033 |
maxoffset = MAX(lp.yoffset_centerline, maxoffset); |
| 1034 |
} |
| 1035 |
/* lsize = mxfsize * LINESPACING; */ |
| 1036 |
ftxt->spans[i].size = width; |
| 1037 |
/* ysize - curbline is the distance from the previous |
| 1038 |
* baseline to the bottom of the previous line. |
| 1039 |
* Then, in the current line, we set the baseline to |
| 1040 |
* be 5/6 of the max. font size. Thus, lfsize gives the |
| 1041 |
* distance from the previous baseline to the new one. |
| 1042 |
*/ |
| 1043 |
/* ftxt->spans[i].lfsize = 5*mxfsize/6 + ysize - curbline; */ |
| 1044 |
if (simple) { |
| 1045 |
lsize = mxysize; |
| 1046 |
if (i == 0) |
| 1047 |
ftxt->spans[i].lfsize = mxfsize; |
| 1048 |
else |
| 1049 |
ftxt->spans[i].lfsize = mxysize; |
| 1050 |
} |
| 1051 |
else { |
| 1052 |
lsize = mxfsize; |
| 1053 |
if (i == 0) |
| 1054 |
ftxt->spans[i].lfsize = mxfsize - maxoffset; |
| 1055 |
else |
| 1056 |
ftxt->spans[i].lfsize = mxfsize + ysize - curbline - maxoffset; |
| 1057 |
} |
| 1058 |
curbline += ftxt->spans[i].lfsize; |
| 1059 |
xsize = MAX(width, xsize); |
| 1060 |
ysize += lsize; |
| 1061 |
} |
| 1062 |
ftxt->box.UR.x = xsize; |
| 1063 |
if (ftxt->nspans == 1) |
| 1064 |
ftxt->box.UR.y = mxysize; |
| 1065 |
else |
| 1066 |
ftxt->box.UR.y = ysize; |
| 1067 |
return 0; |
| 1068 |
} |
| 1069 |
|
| 1070 |
static int size_html_tbl(graph_t * g, htmltbl_t * tbl, htmlcell_t * parent, |
| 1071 |
htmlenv_t * env); |
| 1072 |
|
| 1073 |
static int size_html_img(htmlimg_t * img, htmlenv_t * env) |
| 1074 |
{ |
| 1075 |
box b; |
| 1076 |
int rv; |
| 1077 |
|
| 1078 |
b.LL.x = b.LL.y = 0; |
| 1079 |
b.UR = gvusershape_size(env->g, img->src); |
| 1080 |
if (b.UR.x == -1 && b.UR.y == -1) { |
| 1081 |
rv = 1; |
| 1082 |
b.UR.x = b.UR.y = 0; |
| 1083 |
agerrorf("No or improper image file=\"%s\"\n", img->src); |
| 1084 |
} else { |
| 1085 |
rv = 0; |
| 1086 |
GD_has_images(env->g) = true; |
| 1087 |
} |
| 1088 |
|
| 1089 |
B2BF(b, img->box); |
| 1090 |
return rv; |
| 1091 |
} |
| 1092 |
|
| 1093 |
static int |
| 1094 |
size_html_cell(graph_t * g, htmlcell_t * cp, htmltbl_t * parent, |
| 1095 |
htmlenv_t * env) |
| 1096 |
{ |
| 1097 |
int rv; |
| 1098 |
pointf sz, child_sz; |
| 1099 |
int margin; |
| 1100 |
|
| 1101 |
cp->parent = parent; |
| 1102 |
if (!(cp->data.flags & PAD_SET)) { |
| 1103 |
if (parent->data.flags & PAD_SET) |
| 1104 |
cp->data.pad = parent->data.pad; |
| 1105 |
else |
| 1106 |
cp->data.pad = DEFAULT_CELLPADDING; |
| 1107 |
} |
| 1108 |
if (!(cp->data.flags & BORDER_SET)) { |
| 1109 |
if (parent->cellborder >= 0) |
| 1110 |
cp->data.border = (unsigned char)parent->cellborder; |
| 1111 |
else if (parent->data.flags & BORDER_SET) |
| 1112 |
cp->data.border = parent->data.border; |
| 1113 |
else |
| 1114 |
cp->data.border = DEFAULT_BORDER; |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (cp->child.kind == HTML_TBL) { |
| 1118 |
rv = size_html_tbl(g, cp->child.u.tbl, cp, env); |
| 1119 |
child_sz = cp->child.u.tbl->data.box.UR; |
| 1120 |
} else if (cp->child.kind == HTML_IMAGE) { |
| 1121 |
rv = size_html_img(cp->child.u.img, env); |
| 1122 |
child_sz = cp->child.u.img->box.UR; |
| 1123 |
} else { |
| 1124 |
rv = size_html_txt(GD_gvc(g), cp->child.u.txt, env); |
| 1125 |
child_sz = cp->child.u.txt->box.UR; |
| 1126 |
} |
| 1127 |
|
| 1128 |
margin = 2 * (cp->data.pad + cp->data.border); |
| 1129 |
sz.x = child_sz.x + margin; |
| 1130 |
sz.y = child_sz.y + margin; |
| 1131 |
|
| 1132 |
if (cp->data.flags & FIXED_FLAG) { |
| 1133 |
if (cp->data.width && cp->data.height) { |
| 1134 |
if ((cp->data.width < sz.x || cp->data.height < sz.y) && cp->child.kind != HTML_IMAGE) { |
| 1135 |
agwarningf("cell size too small for content\n"); |
| 1136 |
rv = 1; |
| 1137 |
} |
| 1138 |
sz.x = sz.y = 0; |
| 1139 |
|
| 1140 |
} else { |
| 1141 |
agwarningf( |
| 1142 |
"fixed cell size with unspecified width or height\n"); |
| 1143 |
rv = 1; |
| 1144 |
} |
| 1145 |
} |
| 1146 |
cp->data.box.UR.x = MAX(sz.x, cp->data.width); |
| 1147 |
cp->data.box.UR.y = MAX(sz.y, cp->data.height); |
| 1148 |
return rv; |
| 1149 |
} |
| 1150 |
|
| 1151 |
static uint16_t findCol(PointSet *ps, int row, int col, htmlcell_t *cellp) { |
| 1152 |
int notFound = 1; |
| 1153 |
int lastc; |
| 1154 |
int i, j, c; |
| 1155 |
int end = cellp->colspan - 1; |
| 1156 |
|
| 1157 |
while (notFound) { |
| 1158 |
lastc = col + end; |
| 1159 |
for (c = lastc; c >= col; c--) { |
| 1160 |
if (isInPS(ps, c, row)) |
| 1161 |
break; |
| 1162 |
} |
| 1163 |
if (c >= col) /* conflict : try column after */ |
| 1164 |
col = c + 1; |
| 1165 |
else |
| 1166 |
notFound = 0; |
| 1167 |
} |
| 1168 |
for (j = col; j < col + cellp->colspan; j++) { |
| 1169 |
for (i = row; i < row + cellp->rowspan; i++) { |
| 1170 |
addPS(ps, j, i); |
| 1171 |
} |
| 1172 |
} |
| 1173 |
assert(col >= 0 && col <= UINT16_MAX); |
| 1174 |
return (uint16_t)col; |
| 1175 |
} |
| 1176 |
|
| 1177 |
/* Convert parser representation of cells into final form. |
| 1178 |
* Find column and row positions of cells. |
| 1179 |
* Recursively size cells. |
| 1180 |
* Return 1 if problem sizing a cell. |
| 1181 |
*/ |
| 1182 |
static int processTbl(graph_t * g, htmltbl_t * tbl, htmlenv_t * env) |
| 1183 |
{ |
| 1184 |
htmlcell_t **cells; |
| 1185 |
rows_t rows = tbl->rows; |
| 1186 |
int rv = 0; |
| 1187 |
size_t n_rows = 0; |
| 1188 |
size_t n_cols = 0; |
| 1189 |
PointSet *ps = newPS(); |
| 1190 |
bitarray_t is = bitarray_new((size_t)UINT16_MAX + 1); |
| 1191 |
|
| 1192 |
size_t cnt = 0; |
| 1193 |
for (uint16_t r = 0; r < LIST_SIZE(&rows); ++r) { |
| 1194 |
row_t *rp = LIST_GET(&rows, r); |
| 1195 |
cnt += LIST_SIZE(&rp->rp); |
| 1196 |
if (rp->ruled) { |
| 1197 |
bitarray_set(&is, r + 1, true); |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
cells = tbl->cells = gv_calloc(cnt + 1, sizeof(htmlcell_t *)); |
| 1202 |
for (uint16_t r = 0; r < LIST_SIZE(&rows); ++r) { |
| 1203 |
row_t *rp = LIST_GET(&rows, r); |
| 1204 |
uint16_t c = 0; |
| 1205 |
for (size_t i = 0; i < LIST_SIZE(&rp->rp); ++i) { |
| 1206 |
htmlcell_t *cellp = LIST_GET(&rp->rp, i); |
| 1207 |
*cells++ = cellp; |
| 1208 |
rv |= size_html_cell(g, cellp, tbl, env); |
| 1209 |
c = findCol(ps, r, c, cellp); |
| 1210 |
cellp->row = r; |
| 1211 |
cellp->col = c; |
| 1212 |
c += cellp->colspan; |
| 1213 |
n_cols = MAX(c, n_cols); |
| 1214 |
n_rows = MAX(r + cellp->rowspan, n_rows); |
| 1215 |
if (bitarray_get(is, r + cellp->rowspan)) |
| 1216 |
cellp->hruled = true; |
| 1217 |
} |
| 1218 |
} |
| 1219 |
tbl->row_count = n_rows; |
| 1220 |
tbl->column_count = n_cols; |
| 1221 |
LIST_FREE(&rows); |
| 1222 |
bitarray_reset(&is); |
| 1223 |
freePS(ps); |
| 1224 |
return rv; |
| 1225 |
} |
| 1226 |
|
| 1227 |
/// set the widths of HTML table cells |
| 1228 |
/// |
| 1229 |
/// Graphviz HTML tables were implemented prior to HTML standardization, but |
| 1230 |
/// the subsequent RFC 1942¹ provides some guidance on “Recommended Layout |
| 1231 |
/// Algorithms.” Following this, the W3C gave a slightly clearer articulation of |
| 1232 |
/// essentially the same guidance when specifying CSS.² Many features discussed |
| 1233 |
/// in this algorithm are not present in Graphviz’ variant of HTML (e.g. widths |
| 1234 |
/// as percentages) or are not relevant (e.g. the derivation of _maximum_ column |
| 1235 |
/// widths seems only relevant if you are laying out a table within another |
| 1236 |
/// elastically sized container like a browser window), but the algorithm is |
| 1237 |
/// useful nonetheless. This function implements an adapted version of the CSS |
| 1238 |
/// specification algorithm. Comments that include quotes are quoting the CSS |
| 1239 |
/// specification. |
| 1240 |
/// |
| 1241 |
/// ¹ https://www.ietf.org/rfc/rfc1942.txt |
| 1242 |
/// ² https://www.w3.org/TR/CSS2/tables.html#width-layout |
| 1243 |
/// |
| 1244 |
/// @param table Table to update |
| 1245 |
static void set_cell_widths(htmltbl_t *table) { |
| 1246 |
|
| 1247 |
// `processTbl` has already done step 1, “1. Calculate the minimum content |
| 1248 |
// width (MCW) of each cell”, and stored it in `.data.box.UR.x`. |
| 1249 |
|
| 1250 |
// Allocate space for minimum column widths. Note that we add an extra entry |
| 1251 |
// to allow later code to make references like `table->widths[col + colspan]`. |
| 1252 |
assert(table->widths == NULL && "table widths computed twice"); |
| 1253 |
table->widths = gv_calloc(table->column_count + 1, sizeof(double)); |
| 1254 |
|
| 1255 |
// “2. For each column, determine a … minimum column width from the cells that |
| 1256 |
// span only that column. The minimum is that required by the cell with the |
| 1257 |
// largest minimum cell width …”. Note that this loop and the following one |
| 1258 |
// could be fused, but this would make the implementation harder to relate |
| 1259 |
// back to the specification. |
| 1260 |
for (htmlcell_t **i = table->cells; *i != NULL; ++i) { |
| 1261 |
const htmlcell_t cell = **i; |
| 1262 |
if (cell.colspan > 1) { |
| 1263 |
continue; |
| 1264 |
} |
| 1265 |
assert(cell.col < table->column_count && "out of range cell"); |
| 1266 |
table->widths[cell.col] = fmax(table->widths[cell.col], cell.data.box.UR.x); |
| 1267 |
} |
| 1268 |
|
| 1269 |
// “3. For each cell that spans more than one column, increase the minimum |
| 1270 |
// widths of the columns it spans so that together, they are at least as wide |
| 1271 |
// as the cell. … If possible, widen all spanned columns by approximately the |
| 1272 |
// same amount.” |
| 1273 |
for (htmlcell_t **i = table->cells; *i != NULL; ++i) { |
| 1274 |
const htmlcell_t cell = **i; |
| 1275 |
if (cell.colspan == 1) { |
| 1276 |
continue; |
| 1277 |
} |
| 1278 |
|
| 1279 |
// what is the current width of this cell’s column(s)’ span? |
| 1280 |
double span_width = 0; |
| 1281 |
assert(cell.col + cell.colspan <= table->column_count && |
| 1282 |
"cell spans wider than containing table"); |
| 1283 |
for (size_t j = 0; j < cell.colspan; ++j) { |
| 1284 |
span_width += table->widths[cell.col + j]; |
| 1285 |
} |
| 1286 |
|
| 1287 |
// if it is wider than its span (including cell spacing), widen the span |
| 1288 |
const double spacing = (cell.colspan - 1) * table->data.space; |
| 1289 |
if (span_width + spacing < cell.data.box.UR.x) { |
| 1290 |
const double widen_by = |
| 1291 |
(cell.data.box.UR.x - spacing - span_width) / cell.colspan; |
| 1292 |
for (size_t j = 0; j < cell.colspan; ++j) { |
| 1293 |
table->widths[cell.col + j] += widen_by; |
| 1294 |
} |
| 1295 |
} |
| 1296 |
} |
| 1297 |
|
| 1298 |
// take the minimum width for each column and apply it to its contained cells |
| 1299 |
for (htmlcell_t **i = table->cells; *i != NULL; ++i) { |
| 1300 |
htmlcell_t *cell = *i; |
| 1301 |
|
| 1302 |
// what is the current width of this cell’s column(s)’ span? |
| 1303 |
double min_width = 0; |
| 1304 |
assert(cell->col + cell->colspan <= table->column_count && |
| 1305 |
"cell spans wider than containing table"); |
| 1306 |
for (size_t j = 0; j < cell->colspan; ++j) { |
| 1307 |
min_width += table->widths[cell->col + j]; |
| 1308 |
} |
| 1309 |
|
| 1310 |
// widen the cell if necessary |
| 1311 |
const double spacing = (cell->colspan - 1) * table->data.space; |
| 1312 |
cell->data.box.UR.x = fmax(cell->data.box.UR.x, min_width + spacing); |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
/// set the heights of HTML table cells |
| 1317 |
/// |
| 1318 |
/// This recapitulates the logic of `set_cell_widths` on rows. |
| 1319 |
/// |
| 1320 |
/// @param table Table to update |
| 1321 |
static void set_cell_heights(htmltbl_t *table) { |
| 1322 |
|
| 1323 |
assert(table->heights == NULL && "table heights computed twice"); |
| 1324 |
table->heights = gv_calloc(table->row_count + 1, sizeof(double)); |
| 1325 |
|
| 1326 |
for (htmlcell_t **i = table->cells; *i != NULL; ++i) { |
| 1327 |
const htmlcell_t cell = **i; |
| 1328 |
if (cell.rowspan > 1) { |
| 1329 |
continue; |
| 1330 |
} |
| 1331 |
assert(cell.row < table->row_count && "out of range cell"); |
| 1332 |
table->heights[cell.row] = |
| 1333 |
fmax(table->heights[cell.row], cell.data.box.UR.y); |
| 1334 |
} |
| 1335 |
|
| 1336 |
for (htmlcell_t **i = table->cells; *i != NULL; ++i) { |
| 1337 |
const htmlcell_t cell = **i; |
| 1338 |
if (cell.rowspan == 1) { |
| 1339 |
continue; |
| 1340 |
} |
| 1341 |
|
| 1342 |
double span_height = 0; |
| 1343 |
assert(cell.row + cell.rowspan <= table->row_count && |
| 1344 |
"cell spans higher than containing table"); |
| 1345 |
for (size_t j = 0; j < cell.rowspan; ++j) { |
| 1346 |
span_height += table->heights[cell.row + j]; |
| 1347 |
} |
| 1348 |
|
| 1349 |
const double spacing = (cell.rowspan - 1) * table->data.space; |
| 1350 |
if (span_height + spacing < cell.data.box.UR.y) { |
| 1351 |
const double heighten_by = |
| 1352 |
(cell.data.box.UR.y - spacing - span_height) / cell.rowspan; |
| 1353 |
for (size_t j = 0; j < cell.rowspan; ++j) { |
| 1354 |
table->heights[cell.row + j] += heighten_by; |
| 1355 |
} |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
for (htmlcell_t **i = table->cells; *i != NULL; ++i) { |
| 1360 |
htmlcell_t *cell = *i; |
| 1361 |
|
| 1362 |
double min_height = 0; |
| 1363 |
assert(cell->row + cell->rowspan <= table->row_count && |
| 1364 |
"cell spans higher than containing table"); |
| 1365 |
for (size_t j = 0; j < cell->rowspan; ++j) { |
| 1366 |
min_height += table->heights[cell->row + j]; |
| 1367 |
} |
| 1368 |
|
| 1369 |
const double spacing = (cell->rowspan - 1) * table->data.space; |
| 1370 |
cell->data.box.UR.y = fmax(cell->data.box.UR.y, min_height + spacing); |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
static void pos_html_tbl(htmltbl_t *, boxf, unsigned char); |
| 1375 |
|
| 1376 |
/* Place image in cell |
| 1377 |
* storing allowed space handed by parent cell. |
| 1378 |
* How this space is used is handled in emit_html_img. |
| 1379 |
*/ |
| 1380 |
static void pos_html_img(htmlimg_t * cp, boxf pos) |
| 1381 |
{ |
| 1382 |
cp->box = pos; |
| 1383 |
} |
| 1384 |
|
| 1385 |
/// Set default alignment. |
| 1386 |
static void pos_html_txt(htmltxt_t * ftxt, char c) |
| 1387 |
{ |
| 1388 |
for (size_t i = 0; i < ftxt->nspans; i++) { |
| 1389 |
if (ftxt->spans[i].just == UNSET_ALIGN) /* unset */ |
| 1390 |
ftxt->spans[i].just = c; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
|
| 1394 |
static void pos_html_cell(htmlcell_t *cp, boxf pos, unsigned char sides) { |
| 1395 |
double delx, dely; |
| 1396 |
pointf oldsz; |
| 1397 |
boxf cbox; |
| 1398 |
|
| 1399 |
if (!cp->data.pencolor && cp->parent->data.pencolor) |
| 1400 |
cp->data.pencolor = gv_strdup(cp->parent->data.pencolor); |
| 1401 |
|
| 1402 |
/* If fixed, align cell */ |
| 1403 |
if (cp->data.flags & FIXED_FLAG) { |
| 1404 |
oldsz = cp->data.box.UR; |
| 1405 |
delx = pos.UR.x - pos.LL.x - oldsz.x; |
| 1406 |
if (delx > 0) { |
| 1407 |
switch (cp->data.flags & HALIGN_MASK) { |
| 1408 |
case HALIGN_LEFT: |
| 1409 |
pos.UR.x = pos.LL.x + oldsz.x; |
| 1410 |
break; |
| 1411 |
case HALIGN_RIGHT: |
| 1412 |
pos.UR.x += delx; |
| 1413 |
pos.LL.x += delx; |
| 1414 |
break; |
| 1415 |
default: |
| 1416 |
pos.LL.x += delx / 2; |
| 1417 |
pos.UR.x -= delx / 2; |
| 1418 |
break; |
| 1419 |
} |
| 1420 |
} |
| 1421 |
dely = pos.UR.y - pos.LL.y - oldsz.y; |
| 1422 |
if (dely > 0) { |
| 1423 |
switch (cp->data.flags & VALIGN_MASK) { |
| 1424 |
case VALIGN_BOTTOM: |
| 1425 |
pos.UR.y = pos.LL.y + oldsz.y; |
| 1426 |
break; |
| 1427 |
case VALIGN_TOP: |
| 1428 |
pos.UR.y += dely; |
| 1429 |
pos.LL.y += dely; |
| 1430 |
break; |
| 1431 |
default: |
| 1432 |
pos.LL.y += dely / 2; |
| 1433 |
pos.UR.y -= dely / 2; |
| 1434 |
break; |
| 1435 |
} |
| 1436 |
} |
| 1437 |
} |
| 1438 |
cp->data.box = pos; |
| 1439 |
cp->data.sides = sides; |
| 1440 |
|
| 1441 |
/* set up child's position */ |
| 1442 |
cbox.LL.x = pos.LL.x + cp->data.border + cp->data.pad; |
| 1443 |
cbox.LL.y = pos.LL.y + cp->data.border + cp->data.pad; |
| 1444 |
cbox.UR.x = pos.UR.x - cp->data.border - cp->data.pad; |
| 1445 |
cbox.UR.y = pos.UR.y - cp->data.border - cp->data.pad; |
| 1446 |
|
| 1447 |
if (cp->child.kind == HTML_TBL) { |
| 1448 |
pos_html_tbl(cp->child.u.tbl, cbox, sides); |
| 1449 |
} else if (cp->child.kind == HTML_IMAGE) { |
| 1450 |
/* Note that alignment trumps scaling */ |
| 1451 |
oldsz = cp->child.u.img->box.UR; |
| 1452 |
delx = cbox.UR.x - cbox.LL.x - oldsz.x; |
| 1453 |
if (delx > 0) { |
| 1454 |
switch (cp->data.flags & HALIGN_MASK) { |
| 1455 |
case HALIGN_LEFT: |
| 1456 |
cbox.UR.x -= delx; |
| 1457 |
break; |
| 1458 |
case HALIGN_RIGHT: |
| 1459 |
cbox.LL.x += delx; |
| 1460 |
break; |
| 1461 |
default: |
| 1462 |
break; |
| 1463 |
} |
| 1464 |
} |
| 1465 |
|
| 1466 |
dely = cbox.UR.y - cbox.LL.y - oldsz.y; |
| 1467 |
if (dely > 0) { |
| 1468 |
switch (cp->data.flags & VALIGN_MASK) { |
| 1469 |
case VALIGN_BOTTOM: |
| 1470 |
cbox.UR.y -= dely; |
| 1471 |
break; |
| 1472 |
case VALIGN_TOP: |
| 1473 |
cbox.LL.y += dely; |
| 1474 |
break; |
| 1475 |
default: |
| 1476 |
break; |
| 1477 |
} |
| 1478 |
} |
| 1479 |
pos_html_img(cp->child.u.img, cbox); |
| 1480 |
} else { |
| 1481 |
char dfltalign; |
| 1482 |
int af; |
| 1483 |
|
| 1484 |
oldsz = cp->child.u.txt->box.UR; |
| 1485 |
delx = cbox.UR.x - cbox.LL.x - oldsz.x; |
| 1486 |
/* If the cell is larger than the text block and alignment is |
| 1487 |
* done at textblock level, the text box is shrunk accordingly. |
| 1488 |
*/ |
| 1489 |
if (delx > 0 && (af = (cp->data.flags & HALIGN_MASK)) != HALIGN_TEXT) { |
| 1490 |
switch (af) { |
| 1491 |
case HALIGN_LEFT: |
| 1492 |
cbox.UR.x -= delx; |
| 1493 |
break; |
| 1494 |
case HALIGN_RIGHT: |
| 1495 |
cbox.LL.x += delx; |
| 1496 |
break; |
| 1497 |
default: |
| 1498 |
cbox.LL.x += delx / 2; |
| 1499 |
cbox.UR.x -= delx / 2; |
| 1500 |
break; |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
dely = cbox.UR.y - cbox.LL.y - oldsz.y; |
| 1505 |
if (dely > 0) { |
| 1506 |
switch (cp->data.flags & VALIGN_MASK) { |
| 1507 |
case VALIGN_BOTTOM: |
| 1508 |
cbox.UR.y -= dely; |
| 1509 |
break; |
| 1510 |
case VALIGN_TOP: |
| 1511 |
cbox.LL.y += dely; |
| 1512 |
break; |
| 1513 |
default: |
| 1514 |
cbox.LL.y += dely / 2; |
| 1515 |
cbox.UR.y -= dely / 2; |
| 1516 |
break; |
| 1517 |
} |
| 1518 |
} |
| 1519 |
cp->child.u.txt->box = cbox; |
| 1520 |
|
| 1521 |
/* Set default text alignment |
| 1522 |
*/ |
| 1523 |
switch (cp->data.flags & BALIGN_MASK) { |
| 1524 |
case BALIGN_LEFT: |
| 1525 |
dfltalign = 'l'; |
| 1526 |
break; |
| 1527 |
case BALIGN_RIGHT: |
| 1528 |
dfltalign = 'r'; |
| 1529 |
break; |
| 1530 |
default: |
| 1531 |
dfltalign = 'n'; |
| 1532 |
break; |
| 1533 |
} |
| 1534 |
pos_html_txt(cp->child.u.txt, dfltalign); |
| 1535 |
} |
| 1536 |
} |
| 1537 |
|
| 1538 |
/* Position table given its box, then calculate |
| 1539 |
* the position of each cell. In addition, set the sides |
| 1540 |
* attribute indicating which external sides of the node |
| 1541 |
* are accessible to the table. |
| 1542 |
*/ |
| 1543 |
static void pos_html_tbl(htmltbl_t *tbl, boxf pos, unsigned char sides) { |
| 1544 |
int plus; |
| 1545 |
htmlcell_t **cells = tbl->cells; |
| 1546 |
htmlcell_t *cp; |
| 1547 |
boxf cbox; |
| 1548 |
|
| 1549 |
if (tbl->parent && tbl->parent->data.pencolor && !tbl->data.pencolor) |
| 1550 |
tbl->data.pencolor = gv_strdup(tbl->parent->data.pencolor); |
| 1551 |
|
| 1552 |
double oldsz = tbl->data.box.UR.x; |
| 1553 |
double delx = fmax(pos.UR.x - pos.LL.x - oldsz, 0); |
| 1554 |
oldsz = tbl->data.box.UR.y; |
| 1555 |
double dely = fmax(pos.UR.y - pos.LL.y - oldsz, 0); |
| 1556 |
|
| 1557 |
/* If fixed, align box */ |
| 1558 |
if (tbl->data.flags & FIXED_FLAG) { |
| 1559 |
if (delx > 0) { |
| 1560 |
switch (tbl->data.flags & HALIGN_MASK) { |
| 1561 |
case HALIGN_LEFT: |
| 1562 |
pos.UR.x = pos.LL.x + oldsz; |
| 1563 |
break; |
| 1564 |
case HALIGN_RIGHT: |
| 1565 |
pos.UR.x += delx; |
| 1566 |
pos.LL.x += delx; |
| 1567 |
break; |
| 1568 |
default: |
| 1569 |
pos.LL.x += delx / 2; |
| 1570 |
pos.UR.x -= delx / 2; |
| 1571 |
break; |
| 1572 |
} |
| 1573 |
delx = 0; |
| 1574 |
} |
| 1575 |
if (dely > 0) { |
| 1576 |
switch (tbl->data.flags & VALIGN_MASK) { |
| 1577 |
case VALIGN_BOTTOM: |
| 1578 |
pos.UR.y = pos.LL.y + oldsz; |
| 1579 |
break; |
| 1580 |
case VALIGN_TOP: |
| 1581 |
pos.LL.y += dely; |
| 1582 |
pos.UR.y = pos.LL.y + oldsz; |
| 1583 |
break; |
| 1584 |
default: |
| 1585 |
pos.LL.y += dely / 2; |
| 1586 |
pos.UR.y -= dely / 2; |
| 1587 |
break; |
| 1588 |
} |
| 1589 |
dely = 0; |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
/* change sizes to start positions and distribute extra space */ |
| 1594 |
double x = pos.LL.x + tbl->data.border + tbl->data.space; |
| 1595 |
assert(tbl->column_count <= DBL_MAX); |
| 1596 |
double extra = delx / (double)tbl->column_count; |
| 1597 |
plus = ROUND(delx - extra * (double)tbl->column_count); |
| 1598 |
for (size_t i = 0; i <= tbl->column_count; i++) { |
| 1599 |
delx = tbl->widths[i] + extra + ((i <= INT_MAX && (int)i < plus) ? 1 : 0); |
| 1600 |
tbl->widths[i] = x; |
| 1601 |
x += delx + tbl->data.space; |
| 1602 |
} |
| 1603 |
double y = pos.UR.y - tbl->data.border - tbl->data.space; |
| 1604 |
assert(tbl->row_count <= DBL_MAX); |
| 1605 |
extra = dely / (double)tbl->row_count; |
| 1606 |
plus = ROUND(dely - extra * (double)tbl->row_count); |
| 1607 |
for (size_t i = 0; i <= tbl->row_count; i++) { |
| 1608 |
dely = tbl->heights[i] + extra + ((i <= INT_MAX && (int)i < plus) ? 1 : 0); |
| 1609 |
tbl->heights[i] = y; |
| 1610 |
y -= dely + tbl->data.space; |
| 1611 |
} |
| 1612 |
|
| 1613 |
while ((cp = *cells++)) { |
| 1614 |
unsigned char mask = 0; |
| 1615 |
if (sides) { |
| 1616 |
if (cp->col == 0) |
| 1617 |
mask |= LEFT; |
| 1618 |
if (cp->row == 0) |
| 1619 |
mask |= TOP; |
| 1620 |
if (cp->col + cp->colspan == tbl->column_count) |
| 1621 |
mask |= RIGHT; |
| 1622 |
if (cp->row + cp->rowspan == tbl->row_count) |
| 1623 |
mask |= BOTTOM; |
| 1624 |
} |
| 1625 |
cbox.LL.x = tbl->widths[cp->col]; |
| 1626 |
cbox.UR.x = tbl->widths[cp->col + cp->colspan] - tbl->data.space; |
| 1627 |
cbox.UR.y = tbl->heights[cp->row]; |
| 1628 |
cbox.LL.y = tbl->heights[cp->row + cp->rowspan] + tbl->data.space; |
| 1629 |
pos_html_cell(cp, cbox, sides & mask); |
| 1630 |
} |
| 1631 |
|
| 1632 |
tbl->data.sides = sides; |
| 1633 |
tbl->data.box = pos; |
| 1634 |
} |
| 1635 |
|
| 1636 |
/* Determine the size of a table by first determining the |
| 1637 |
* size of each cell. |
| 1638 |
*/ |
| 1639 |
static int |
| 1640 |
size_html_tbl(graph_t * g, htmltbl_t * tbl, htmlcell_t * parent, |
| 1641 |
htmlenv_t * env) |
| 1642 |
{ |
| 1643 |
int rv = 0; |
| 1644 |
static textfont_t savef; |
| 1645 |
|
| 1646 |
if (tbl->font) |
| 1647 |
pushFontInfo(env, tbl->font, &savef); |
| 1648 |
tbl->parent = parent; |
| 1649 |
rv = processTbl(g, tbl, env); |
| 1650 |
|
| 1651 |
/* Set up border and spacing */ |
| 1652 |
if (!(tbl->data.flags & SPACE_SET)) { |
| 1653 |
tbl->data.space = DEFAULT_CELLSPACING; |
| 1654 |
} |
| 1655 |
if (!(tbl->data.flags & BORDER_SET)) { |
| 1656 |
tbl->data.border = DEFAULT_BORDER; |
| 1657 |
} |
| 1658 |
|
| 1659 |
set_cell_widths(tbl); |
| 1660 |
set_cell_heights(tbl); |
| 1661 |
|
| 1662 |
assert(tbl->column_count <= DBL_MAX); |
| 1663 |
double wd = ((double)tbl->column_count + 1) * tbl->data.space + 2 * tbl->data.border; |
| 1664 |
assert(tbl->row_count <= DBL_MAX); |
| 1665 |
double ht = ((double)tbl->row_count + 1) * tbl->data.space + 2 * tbl->data.border; |
| 1666 |
for (size_t i = 0; i < tbl->column_count; i++) |
| 1667 |
wd += tbl->widths[i]; |
| 1668 |
for (size_t i = 0; i < tbl->row_count; i++) |
| 1669 |
ht += tbl->heights[i]; |
| 1670 |
|
| 1671 |
if (tbl->data.flags & FIXED_FLAG) { |
| 1672 |
if (tbl->data.width && tbl->data.height) { |
| 1673 |
if (tbl->data.width < wd || tbl->data.height < ht) { |
| 1674 |
agwarningf("table size too small for content\n"); |
| 1675 |
rv = 1; |
| 1676 |
} |
| 1677 |
wd = 0; |
| 1678 |
ht = 0; |
| 1679 |
} else { |
| 1680 |
agwarningf( |
| 1681 |
"fixed table size with unspecified width or height\n"); |
| 1682 |
rv = 1; |
| 1683 |
} |
| 1684 |
} |
| 1685 |
tbl->data.box.UR.x = fmax(wd, tbl->data.width); |
| 1686 |
tbl->data.box.UR.y = fmax(ht, tbl->data.height); |
| 1687 |
|
| 1688 |
if (tbl->font) |
| 1689 |
popFontInfo(env, &savef); |
| 1690 |
return rv; |
| 1691 |
} |
| 1692 |
|
| 1693 |
static char *nameOf(void *obj, agxbuf * xb) |
| 1694 |
{ |
| 1695 |
Agedge_t *ep; |
| 1696 |
switch (agobjkind(obj)) { |
| 1697 |
case AGRAPH: |
| 1698 |
agxbput(xb, agnameof(obj)); |
| 1699 |
break; |
| 1700 |
case AGNODE: |
| 1701 |
agxbput(xb, agnameof(obj)); |
| 1702 |
break; |
| 1703 |
case AGEDGE: |
| 1704 |
ep = obj; |
| 1705 |
agxbput(xb, agnameof(agtail(ep))); |
| 1706 |
agxbput(xb, agnameof(aghead(ep))); |
| 1707 |
if (agisdirected(agraphof(aghead(ep)))) |
| 1708 |
agxbput(xb, "->"); |
| 1709 |
else |
| 1710 |
agxbput(xb, "--"); |
| 1711 |
break; |
| 1712 |
} |
| 1713 |
return agxbuse(xb); |
| 1714 |
} |
| 1715 |
|
| 1716 |
#ifdef DEBUG |
| 1717 |
void indent(int i) |
| 1718 |
{ |
| 1719 |
while (i--) |
| 1720 |
fprintf(stderr, " "); |
| 1721 |
} |
| 1722 |
|
| 1723 |
void printBox(boxf b) |
| 1724 |
{ |
| 1725 |
fprintf(stderr, "(%f,%f)(%f,%f)", b.LL.x, b.LL.y, b.UR.x, b.UR.y); |
| 1726 |
} |
| 1727 |
|
| 1728 |
void printImage(htmlimg_t * ip, int ind) |
| 1729 |
{ |
| 1730 |
indent(ind); |
| 1731 |
fprintf(stderr, "img: %s\n", ip->src); |
| 1732 |
} |
| 1733 |
|
| 1734 |
void printTxt(htmltxt_t * txt, int ind) |
| 1735 |
{ |
| 1736 |
indent(ind); |
| 1737 |
fprintf(stderr, "txt spans = %" PRISIZE_T " \n", txt->nspans); |
| 1738 |
for (size_t i = 0; i < txt->nspans; i++) { |
| 1739 |
indent(ind + 1); |
| 1740 |
fprintf(stderr, "[%" PRISIZE_T "] %" PRISIZE_T " items\n", i, |
| 1741 |
txt->spans[i].nitems); |
| 1742 |
for (size_t j = 0; j < txt->spans[i].nitems; j++) { |
| 1743 |
indent(ind + 2); |
| 1744 |
fprintf(stderr, "[%" PRISIZE_T "] (%f,%f) \"%s\" ", |
| 1745 |
j, txt->spans[i].items[j].size.x, |
| 1746 |
txt->spans[i].items[j].size.y, |
| 1747 |
txt->spans[i].items[j].str); |
| 1748 |
if (txt->spans[i].items[j].font) |
| 1749 |
fprintf(stderr, "font %s color %s size %f\n", |
| 1750 |
txt->spans[i].items[j].font->name, |
| 1751 |
txt->spans[i].items[j].font->color, |
| 1752 |
txt->spans[i].items[j].font->size); |
| 1753 |
else |
| 1754 |
fprintf(stderr, "\n"); |
| 1755 |
} |
| 1756 |
} |
| 1757 |
} |
| 1758 |
|
| 1759 |
void printData(htmldata_t * dp) |
| 1760 |
{ |
| 1761 |
unsigned char flags = dp->flags; |
| 1762 |
char c; |
| 1763 |
|
| 1764 |
fprintf(stderr, "s%d(%d) ", dp->space, (flags & SPACE_SET ? 1 : 0)); |
| 1765 |
fprintf(stderr, "b%d(%d) ", dp->border, (flags & BORDER_SET ? 1 : 0)); |
| 1766 |
fprintf(stderr, "p%d(%d) ", dp->pad, (flags & PAD_SET ? 1 : 0)); |
| 1767 |
switch (flags & HALIGN_MASK) { |
| 1768 |
case HALIGN_RIGHT: |
| 1769 |
c = 'r'; |
| 1770 |
break; |
| 1771 |
case HALIGN_LEFT: |
| 1772 |
c = 'l'; |
| 1773 |
break; |
| 1774 |
default: |
| 1775 |
c = 'n'; |
| 1776 |
break; |
| 1777 |
} |
| 1778 |
fprintf(stderr, "%c", c); |
| 1779 |
switch (flags & VALIGN_MASK) { |
| 1780 |
case VALIGN_TOP: |
| 1781 |
c = 't'; |
| 1782 |
break; |
| 1783 |
case VALIGN_BOTTOM: |
| 1784 |
c = 'b'; |
| 1785 |
break; |
| 1786 |
default: |
| 1787 |
c = 'c'; |
| 1788 |
break; |
| 1789 |
} |
| 1790 |
fprintf(stderr, "%c ", c); |
| 1791 |
printBox(dp->box); |
| 1792 |
} |
| 1793 |
|
| 1794 |
void printTbl(htmltbl_t * tbl, int ind) |
| 1795 |
{ |
| 1796 |
htmlcell_t **cells = tbl->cells; |
| 1797 |
indent(ind); |
| 1798 |
fprintf(stderr, "tbl (%p) %" PRISIZE_T " %" PRISIZE_T " ", tbl, tbl->column_count, tbl->row_count); |
| 1799 |
printData(&tbl->data); |
| 1800 |
fputs("\n", stderr); |
| 1801 |
while (*cells) |
| 1802 |
printCell(*cells++, ind + 1); |
| 1803 |
} |
| 1804 |
|
| 1805 |
static void printCell(htmlcell_t * cp, int ind) |
| 1806 |
{ |
| 1807 |
indent(ind); |
| 1808 |
fprintf(stderr, "cell %" PRIu16 " %" PRIu16 " %" PRIu16 " %" PRIu16 " ", cp->colspan, |
| 1809 |
cp->colspan, cp->rowspan, cp->col, cp->row); |
| 1810 |
printData(&cp->data); |
| 1811 |
fputs("\n", stderr); |
| 1812 |
switch (cp->child.kind) { |
| 1813 |
case HTML_TBL: |
| 1814 |
printTbl(cp->child.u.tbl, ind + 1); |
| 1815 |
break; |
| 1816 |
case HTML_TEXT: |
| 1817 |
printTxt(cp->child.u.txt, ind + 1); |
| 1818 |
break; |
| 1819 |
case HTML_IMAGE: |
| 1820 |
printImage(cp->child.u.img, ind + 1); |
| 1821 |
break; |
| 1822 |
default: |
| 1823 |
break; |
| 1824 |
} |
| 1825 |
} |
| 1826 |
|
| 1827 |
void printLbl(htmllabel_t * lbl) |
| 1828 |
{ |
| 1829 |
if (lbl->kind == HTML_TBL) |
| 1830 |
printTbl(lbl->u.tbl, 0); |
| 1831 |
else |
| 1832 |
printTxt(lbl->u.txt, 0); |
| 1833 |
} |
| 1834 |
#endif /* DEBUG */ |
| 1835 |
|
| 1836 |
static char *getPenColor(void *obj) |
| 1837 |
{ |
| 1838 |
char *str; |
| 1839 |
|
| 1840 |
if ((str = agget(obj, "pencolor")) != 0 && str[0]) |
| 1841 |
return str; |
| 1842 |
else if ((str = agget(obj, "color")) != 0 && str[0]) |
| 1843 |
return str; |
| 1844 |
else |
| 1845 |
return NULL; |
| 1846 |
} |
| 1847 |
|
| 1848 |
/// Return non-zero if problem parsing HTML. In this case, use object name. |
| 1849 |
int make_html_label(void *obj, textlabel_t * lp) |
| 1850 |
{ |
| 1851 |
int rv; |
| 1852 |
double wd2, ht2; |
| 1853 |
graph_t *g; |
| 1854 |
htmllabel_t *lbl; |
| 1855 |
htmlenv_t env; |
| 1856 |
char *s; |
| 1857 |
|
| 1858 |
env.obj = obj; |
| 1859 |
switch (agobjkind(obj)) { |
| 1860 |
case AGRAPH: |
| 1861 |
env.g = ((Agraph_t *) obj)->root; |
| 1862 |
break; |
| 1863 |
case AGNODE: |
| 1864 |
env.g = agraphof(obj); |
| 1865 |
break; |
| 1866 |
case AGEDGE: |
| 1867 |
env.g = agraphof(aghead(((Agedge_t *) obj))); |
| 1868 |
break; |
| 1869 |
} |
| 1870 |
g = env.g->root; |
| 1871 |
|
| 1872 |
env.finfo.size = lp->fontsize; |
| 1873 |
env.finfo.name = lp->fontname; |
| 1874 |
env.finfo.color = lp->fontcolor; |
| 1875 |
env.finfo.flags = 0; |
| 1876 |
lbl = parseHTML(lp->text, &rv, &env); |
| 1877 |
if (!lbl) { |
| 1878 |
if (rv == 3) { |
| 1879 |
// fatal error; `parseHTML` will have printed detail of it |
| 1880 |
lp->html = false; |
| 1881 |
lp->text = gv_strdup(lp->text); |
| 1882 |
return rv; |
| 1883 |
} |
| 1884 |
/* Parse of label failed; revert to simple text label */ |
| 1885 |
agxbuf xb = {0}; |
| 1886 |
lp->html = false; |
| 1887 |
lp->text = gv_strdup(nameOf(obj, &xb)); |
| 1888 |
switch (lp->charset) { |
| 1889 |
case CHAR_LATIN1: |
| 1890 |
s = latin1ToUTF8(lp->text); |
| 1891 |
break; |
| 1892 |
default: /* UTF8 */ |
| 1893 |
s = htmlEntityUTF8(lp->text, env.g); |
| 1894 |
break; |
| 1895 |
} |
| 1896 |
free(lp->text); |
| 1897 |
lp->text = s; |
| 1898 |
make_simple_label(GD_gvc(g), lp); |
| 1899 |
agxbfree(&xb); |
| 1900 |
return rv; |
| 1901 |
} |
| 1902 |
|
| 1903 |
if (lbl->kind == HTML_TBL) { |
| 1904 |
if (!lbl->u.tbl->data.pencolor && getPenColor(obj)) |
| 1905 |
lbl->u.tbl->data.pencolor = gv_strdup(getPenColor(obj)); |
| 1906 |
rv |= size_html_tbl(g, lbl->u.tbl, NULL, &env); |
| 1907 |
wd2 = lbl->u.tbl->data.box.UR.x / 2; |
| 1908 |
ht2 = lbl->u.tbl->data.box.UR.y / 2; |
| 1909 |
boxf b = {{-wd2, -ht2}, {wd2, ht2}}; |
| 1910 |
pos_html_tbl(lbl->u.tbl, b, BOTTOM | RIGHT | TOP | LEFT); |
| 1911 |
lp->dimen.x = b.UR.x - b.LL.x; |
| 1912 |
lp->dimen.y = b.UR.y - b.LL.y; |
| 1913 |
} else { |
| 1914 |
rv |= size_html_txt(GD_gvc(g), lbl->u.txt, &env); |
| 1915 |
wd2 = lbl->u.txt->box.UR.x / 2; |
| 1916 |
ht2 = lbl->u.txt->box.UR.y / 2; |
| 1917 |
boxf b = {{-wd2, -ht2}, {wd2, ht2}}; |
| 1918 |
lbl->u.txt->box = b; |
| 1919 |
lp->dimen.x = b.UR.x - b.LL.x; |
| 1920 |
lp->dimen.y = b.UR.y - b.LL.y; |
| 1921 |
} |
| 1922 |
|
| 1923 |
lp->u.html = lbl; |
| 1924 |
|
| 1925 |
/* If the label is a table, replace label text because this may |
| 1926 |
* be used for the title and alt fields in image maps. |
| 1927 |
*/ |
| 1928 |
if (lbl->kind == HTML_TBL) { |
| 1929 |
free(lp->text); |
| 1930 |
lp->text = gv_strdup("<TABLE>"); |
| 1931 |
} |
| 1932 |
|
| 1933 |
return rv; |
| 1934 |
} |