Graphviz 14.0.5~dev.20251117.1017
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 <util/alloc.h>
24#include <util/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 return bsearch(fontname, postscript_alias,
67 sizeof(postscript_alias) / sizeof(PostscriptAlias),
68 sizeof(PostscriptAlias), fontcmpf);
69}
70
73{
74 char **fpp = NULL, *fontpath = NULL;
76
77 assert(span->font);
78 font = span->font;
79
80 assert(font->name);
81
82 /* only need to find alias once per font, since they are unique in dict */
83 if (! font->postscript_alias)
84 font->postscript_alias = translate_postscript_fontname(font->name);
85
86 if (Verbose && emit_once(font->name))
87 fpp = &fontpath;
88
89 if (! gvtextlayout(gvc, span, fpp))
90 estimate_textspan_size(span, fpp);
91
92 if (fpp) {
93 if (fontpath)
94 fprintf(stderr, "fontname: \"%s\" resolved to: %s\n",
95 font->name, fontpath);
96 else
97 fprintf(stderr, "fontname: unable to resolve \"%s\"\n", font->name);
98 }
99
100 return span->size;
101}
102
103static void *textfont_makef(void *obj, Dtdisc_t *disc) {
104 (void)disc;
105
106 textfont_t *f1 = obj;
107 textfont_t *f2 = gv_alloc(sizeof(textfont_t));
108
109 /* key */
110 if (f1->name) f2->name = gv_strdup(f1->name);
111 if (f1->color) f2->color = gv_strdup(f1->color);
112 f2->flags = f1->flags;
113 f2->size = f1->size;
114
115 /* non key */
117
118 return f2;
119}
120
121static void textfont_freef(void *obj) {
122 textfont_t *f = obj;
123
124 free(f->name);
125 free(f->color);
126 free(f);
127}
128
129static int textfont_comparf(void *key1, void *key2) {
130 int rc;
131 textfont_t *f1 = key1, *f2 = key2;
132
133 if (f1->name || f2->name) {
134 if (! f1->name) return -1;
135 if (! f2->name) return 1;
136 rc = strcmp(f1->name, f2->name);
137 if (rc) return rc;
138 }
139 if (f1->color || f2->color) {
140 if (! f1->color) return -1;
141 if (! f2->color) return 1;
142 rc = strcmp(f1->color, f2->color);
143 if (rc) return rc;
144 }
145 if (f1->flags < f2->flags) return -1;
146 if (f1->flags > f2->flags) return 1;
147 if (f1->size < f2->size) return -1;
148 if (f1->size > f2->size) return 1;
149 return 0;
150}
151
156
Memory allocation wrappers that exit on failure.
static char * gv_strdup(const char *original)
Definition alloc.h:101
static void * gv_alloc(size_t size)
Definition alloc.h:47
container data types API
#define DTDISC(dc, ky, sz, lk, mkf, frf, cmpf)
Definition cdt.h:93
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:3369
static Dtdisc_t disc
Definition exparse.y:209
static int flags
Definition gc.c:61
static bool Verbose
Definition gml2gv.c:24
void free(void *)
node NULL
Definition grammar.y:181
static GVC_t * gvc
Definition gv.cpp:25
bool gvtextlayout(GVC_t *gvc, textspan_t *span, char **fontpath)
$2 font
Definition htmlparse.y:294
platform abstraction for case-insensitive string functions
Definition gvcint.h:81
Dt_t * textfont_dt
Definition gvcint.h:108
Dtdisc_t textfont_disc
Definition gvcint.h:107
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:103
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:71
static void textfont_freef(void *obj)
Definition textspan.c:121
void textfont_dict_close(GVC_t *gvc)
Definition textspan.c:157
void textfont_dict_open(GVC_t *gvc)
Definition textspan.c:152
static int fontcmpf(const void *a, const void *b)
Definition textspan.c:59
static int textfont_comparf(void *key1, void *key2)
Definition textspan.c:129
#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