Graphviz 12.0.1~dev.20240715.2254
Loading...
Searching...
No Matches
textspan.c
Go to the documentation of this file.
1
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 <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <cdt/cdt.h>
21#include <common/render.h>
22#include <common/textspan_lut.h>
23#include <cgraph/alloc.h>
24#include <cgraph/strcasecmp.h>
25
26/* estimate_textspan_size:
27 * Estimate size of textspan, for given face and size, in points.
28 */
29static void
30estimate_textspan_size(textspan_t * span, char **fontpath)
31{
32 double fontsize;
33
34 int flags = span->font->flags;
35 bool bold = (flags & HTML_BF) != 0;
36 bool italic = (flags & HTML_IF) != 0;
37
38 fontsize = span->font->size;
39
40 span->size.x = 0.0;
41 span->size.y = fontsize * LINESPACING;
42 span->yoffset_layout = 0.0;
43 span->yoffset_centerline = 0.1 * fontsize;
44 span->layout = NULL;
45 span->free_layout = NULL;
46 span->size.x = fontsize * estimate_text_width_1pt(span->font->name, span->str, bold, italic);
47
48 if (fontpath)
49 *fontpath = "[internal hard-coded]";
50}
51
52/*
53 * This table maps standard Postscript font names to URW Type 1 fonts.
54 */
56#include "ps_font_equiv.h"
57};
58
59static int fontcmpf(const void *a, const void *b)
60{
61 return strcasecmp(a, ((const PostscriptAlias*)b)->name);
62}
63
65{
66 static char *key;
67 static PostscriptAlias *result;
68
69 if (key == NULL || strcasecmp(key, fontname)) {
70 free(key);
71 key = gv_strdup(fontname);
72 result = bsearch(key, postscript_alias,
73 sizeof(postscript_alias) / sizeof(PostscriptAlias),
74 sizeof(PostscriptAlias), fontcmpf);
75 }
76 return result;
77}
78
81{
82 char **fpp = NULL, *fontpath = NULL;
84
85 assert(span->font);
86 font = span->font;
87
88 assert(font->name);
89
90 /* only need to find alias once per font, since they are unique in dict */
91 if (! font->postscript_alias)
92 font->postscript_alias = translate_postscript_fontname(font->name);
93
94 if (Verbose && emit_once(font->name))
95 fpp = &fontpath;
96
97 if (! gvtextlayout(gvc, span, fpp))
98 estimate_textspan_size(span, fpp);
99
100 if (fpp) {
101 if (fontpath)
102 fprintf(stderr, "fontname: \"%s\" resolved to: %s\n",
103 font->name, fontpath);
104 else
105 fprintf(stderr, "fontname: unable to resolve \"%s\"\n", font->name);
106 }
107
108 return span->size;
109}
110
111static void *textfont_makef(void *obj, Dtdisc_t *disc) {
112 (void)disc;
113
114 textfont_t *f1 = obj;
115 textfont_t *f2 = calloc(1,sizeof(textfont_t));
116
117 /* key */
118 if (f1->name) f2->name = strdup(f1->name);
119 if (f1->color) f2->color = strdup(f1->color);
120 f2->flags = f1->flags;
121 f2->size = f1->size;
122
123 /* non key */
125
126 return f2;
127}
128
129static void textfont_freef(void *obj) {
130 textfont_t *f = obj;
131
132 free(f->name);
133 free(f->color);
134 free(f);
135}
136
137static int textfont_comparf(void *key1, void *key2) {
138 int rc;
139 textfont_t *f1 = key1, *f2 = key2;
140
141 if (f1->name || f2->name) {
142 if (! f1->name) return -1;
143 if (! f2->name) return 1;
144 rc = strcmp(f1->name, f2->name);
145 if (rc) return rc;
146 }
147 if (f1->color || f2->color) {
148 if (! f1->color) return -1;
149 if (! f2->color) return 1;
150 rc = strcmp(f1->color, f2->color);
151 if (rc) return rc;
152 }
153 if (f1->flags < f2->flags) return -1;
154 if (f1->flags > f2->flags) return 1;
155 if (f1->size < f2->size) return -1;
156 if (f1->size > f2->size) return 1;
157 return 0;
158}
159
164
Memory allocation wrappers that exit on failure.
static char * gv_strdup(const char *original)
Definition alloc.h:101
container data types API
#define DTDISC(dc, ky, sz, lk, mkf, frf, cmpf)
Definition cdt.h:97
CDT_API int dtclose(Dt_t *)
Definition dtclose.c:8
CDT_API Dtmethod_t * Dtoset
ordered set (self-adjusting tree)
Definition dttree.c:304
CDT_API Dt_t * dtopen(Dtdisc_t *, Dtmethod_t *)
Definition dtopen.c:9
#define LINESPACING
Definition const.h:70
bool emit_once(char *str)
Definition emit.c:3449
disc key
Definition exparse.y:214
static int flags
Definition gc.c:61
static int Verbose
Definition gml2gv.c:22
void free(void *)
node NULL
Definition grammar.y:149
bool gvtextlayout(GVC_t *gvc, textspan_t *span, char **fontpath)
GVC_t * gvc
Definition htmlparse.c:99
$2 font
Definition htmlparse.y:498
platform abstraction for case-insensitive string functions
Definition gvcint.h:80
Dt_t * textfont_dt
Definition gvcint.h:107
Dtdisc_t textfont_disc
Definition gvcint.h:106
double x
Definition geom.h:29
double y
Definition geom.h:29
char * color
Definition textspan.h:55
char * name
Definition textspan.h:54
PostscriptAlias * postscript_alias
Definition textspan.h:56
unsigned int flags
Definition textspan.h:58
double size
Definition textspan.h:57
double yoffset_layout
Definition textspan.h:69
char * str
Definition textspan.h:65
void * layout
Definition textspan.h:67
pointf size
Definition textspan.h:70
textfont_t * font
Definition textspan.h:66
double yoffset_centerline
Definition textspan.h:69
void(* free_layout)(void *layout)
Definition textspan.h:68
static void estimate_textspan_size(textspan_t *span, char **fontpath)
Definition textspan.c:30
static void * textfont_makef(void *obj, Dtdisc_t *disc)
Definition textspan.c:111
static PostscriptAlias postscript_alias[]
Definition textspan.c:55
static PostscriptAlias * translate_postscript_fontname(char *fontname)
Definition textspan.c:64
pointf textspan_size(GVC_t *gvc, textspan_t *span)
Estimates size of a textspan, in points.
Definition textspan.c:79
static void textfont_freef(void *obj)
Definition textspan.c:129
void textfont_dict_close(GVC_t *gvc)
Definition textspan.c:165
void textfont_dict_open(GVC_t *gvc)
Definition textspan.c:160
static int fontcmpf(const void *a, const void *b)
Definition textspan.c:59
static int textfont_comparf(void *key1, void *key2)
Definition textspan.c:137
#define HTML_IF
Definition textspan.h:30
#define HTML_BF
Definition textspan.h:29
double estimate_text_width_1pt(const char *font_name, const char *text, bool bold, bool italic)
lookup table for textspan