| 1 |
/** |
| 2 |
* @file |
| 3 |
* @ingroup common_render |
| 4 |
* @brief @ref textspan_size |
| 5 |
*/ |
| 6 |
|
| 7 |
/************************************************************************* |
| 8 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 9 |
* All rights reserved. This program and the accompanying materials |
| 10 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 11 |
* which accompanies this distribution, and is available at |
| 12 |
* https://www.eclipse.org/legal/epl-v10.html |
| 13 |
* |
| 14 |
* Contributors: Details at https://graphviz.org |
| 15 |
*************************************************************************/ |
| 16 |
|
| 17 |
#include "config.h" |
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <cdt/cdt.h> |
| 23 |
#include <common/render.h> |
| 24 |
#include <common/textspan_lut.h> |
| 25 |
#include <util/alloc.h> |
| 26 |
#include <util/strcasecmp.h> |
| 27 |
|
| 28 |
/* estimate_textspan_size: |
| 29 |
* Estimate size of textspan, for given face and size, in points. |
| 30 |
*/ |
| 31 |
static void |
| 32 |
estimate_textspan_size(textspan_t * span, char **fontpath) |
| 33 |
{ |
| 34 |
double fontsize; |
| 35 |
|
| 36 |
int flags = span->font->flags; |
| 37 |
bool bold = (flags & HTML_BF) != 0; |
| 38 |
bool italic = (flags & HTML_IF) != 0; |
| 39 |
|
| 40 |
fontsize = span->font->size; |
| 41 |
|
| 42 |
span->size.x = 0.0; |
| 43 |
span->size.y = fontsize * LINESPACING; |
| 44 |
span->yoffset_layout = 0.0; |
| 45 |
span->yoffset_centerline = 0.1 * fontsize; |
| 46 |
span->layout = NULL; |
| 47 |
span->free_layout = NULL; |
| 48 |
span->size.x = fontsize * estimate_text_width_1pt(span->font->name, span->str, bold, italic); |
| 49 |
|
| 50 |
if (fontpath) |
| 51 |
*fontpath = "[internal hard-coded]"; |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
* This table maps standard Postscript font names to URW Type 1 fonts. |
| 56 |
*/ |
| 57 |
static PostscriptAlias postscript_alias[] = { |
| 58 |
#include "ps_font_equiv.h" |
| 59 |
}; |
| 60 |
|
| 61 |
static int fontcmpf(const void *a, const void *b) |
| 62 |
{ |
| 63 |
return strcasecmp(a, ((const PostscriptAlias*)b)->name); |
| 64 |
} |
| 65 |
|
| 66 |
static PostscriptAlias* translate_postscript_fontname(char* fontname) |
| 67 |
{ |
| 68 |
return bsearch(fontname, postscript_alias, |
| 69 |
sizeof(postscript_alias) / sizeof(PostscriptAlias), |
| 70 |
sizeof(PostscriptAlias), fontcmpf); |
| 71 |
} |
| 72 |
|
| 73 |
pointf textspan_size(GVC_t *gvc, textspan_t * span) |
| 74 |
/// Estimates size of a textspan, in points. |
| 75 |
{ |
| 76 |
char **fpp = NULL, *fontpath = NULL; |
| 77 |
textfont_t *font; |
| 78 |
|
| 79 |
assert(span->font); |
| 80 |
font = span->font; |
| 81 |
|
| 82 |
assert(font->name); |
| 83 |
|
| 84 |
/* only need to find alias once per font, since they are unique in dict */ |
| 85 |
if (! font->postscript_alias) |
| 86 |
font->postscript_alias = translate_postscript_fontname(font->name); |
| 87 |
|
| 88 |
if (Verbose && emit_once(font->name)) |
| 89 |
fpp = &fontpath; |
| 90 |
|
| 91 |
if (! gvtextlayout(gvc, span, fpp)) |
| 92 |
estimate_textspan_size(span, fpp); |
| 93 |
|
| 94 |
if (fpp) { |
| 95 |
if (fontpath) |
| 96 |
fprintf(stderr, "fontname: \"%s\" resolved to: %s\n", |
| 97 |
font->name, fontpath); |
| 98 |
else |
| 99 |
fprintf(stderr, "fontname: unable to resolve \"%s\"\n", font->name); |
| 100 |
} |
| 101 |
|
| 102 |
return span->size; |
| 103 |
} |
| 104 |
|
| 105 |
static void *textfont_makef(void *obj, Dtdisc_t *disc) { |
| 106 |
(void)disc; |
| 107 |
|
| 108 |
textfont_t *f1 = obj; |
| 109 |
textfont_t *f2 = gv_alloc(sizeof(textfont_t)); |
| 110 |
|
| 111 |
/* key */ |
| 112 |
if (f1->name) f2->name = gv_strdup(f1->name); |
| 113 |
if (f1->color) f2->color = gv_strdup(f1->color); |
| 114 |
f2->flags = f1->flags; |
| 115 |
f2->size = f1->size; |
| 116 |
|
| 117 |
/* non key */ |
| 118 |
f2->postscript_alias = f1->postscript_alias; |
| 119 |
|
| 120 |
return f2; |
| 121 |
} |
| 122 |
|
| 123 |
static void textfont_freef(void *obj) { |
| 124 |
textfont_t *f = obj; |
| 125 |
|
| 126 |
free(f->name); |
| 127 |
free(f->color); |
| 128 |
free(f); |
| 129 |
} |
| 130 |
|
| 131 |
static int textfont_comparf(void *key1, void *key2) { |
| 132 |
int rc; |
| 133 |
textfont_t *f1 = key1, *f2 = key2; |
| 134 |
|
| 135 |
if (f1->name || f2->name) { |
| 136 |
if (! f1->name) return -1; |
| 137 |
if (! f2->name) return 1; |
| 138 |
rc = strcmp(f1->name, f2->name); |
| 139 |
if (rc) return rc; |
| 140 |
} |
| 141 |
if (f1->color || f2->color) { |
| 142 |
if (! f1->color) return -1; |
| 143 |
if (! f2->color) return 1; |
| 144 |
rc = strcmp(f1->color, f2->color); |
| 145 |
if (rc) return rc; |
| 146 |
} |
| 147 |
if (f1->flags < f2->flags) return -1; |
| 148 |
if (f1->flags > f2->flags) return 1; |
| 149 |
if (f1->size < f2->size) return -1; |
| 150 |
if (f1->size > f2->size) return 1; |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
void textfont_dict_open(GVC_t *gvc) { |
| 155 |
DTDISC(&gvc->textfont_disc, 0, sizeof(textfont_t), -1, textfont_makef, textfont_freef, textfont_comparf); |
| 156 |
gvc->textfont_dt = dtopen(&(gvc->textfont_disc), Dtoset); |
| 157 |
} |
| 158 |
|
| 159 |
void textfont_dict_close(GVC_t *gvc) |
| 160 |
{ |
| 161 |
dtclose(gvc->textfont_dt); |
| 162 |
} |