File: glcompfont.c

package info (click to toggle)
graphviz 14.1.2-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 139,476 kB
  • sloc: ansic: 142,288; cpp: 11,975; python: 7,883; makefile: 4,044; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,391; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (161 lines) | stat: -rw-r--r-- 4,462 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Details at https://graphviz.org
 *************************************************************************/

#include "config.h"

#include <glcomp/glcompfont.h>
#include <glcomp/glcompset.h>
#include <glcomp/glpangofont.h>
#include <glcomp/glcomptexture.h>
#include <glcomp/glutils.h>
#include <GL/glut.h>
#include <stdbool.h>
#include <stddef.h>
#include <util/alloc.h>

static void print_bitmap_string(void *font, char *s)
{
    if (s && strlen(s)) {
	while (*s) {
	    glutBitmapCharacter(font, *s);
	    s++;
	}
    }
}

void glprintfglut(void *font, float xpos, float ypos, float zpos, char *bf) {
    glRasterPos3f(xpos, ypos, zpos + 0.001f);
    print_bitmap_string(font, bf);


}

void glDeleteFont(glCompFont * f)
{
    free(f->fontdesc);
    if (f->tex)
	glCompDeleteTexture(f->tex);
    *f = (glCompFont){0};
}

glCompFont glNewFont(glCompSet *s, const char *text, glCompColor *c,
                     char *fontdesc, int fs, bool is2D) {
    glCompFont font = {0};
    font.color.R = c->R;
    font.color.G = c->G;
    font.color.B = c->B;
    font.color.A = c->A;
    font.justify.VJustify = glFontVJustifyNone;
    font.justify.HJustify = glFontHJustifyNone;
    font.is2D = is2D;

    font.glutfont = NULL;

    font.fontdesc = gv_strdup(fontdesc);
    font.size = fs;
    font.transparent = 1;
    if (text)
	font.tex =
	    glCompSetAddNewTexLabel(s, font.fontdesc, font.size, text,
				    is2D);
    return font;

}

glCompFont glNewFontFromParent(glCompObj *o, const char *text) {
    glCompCommon *parent;
    glCompFont font = {0};
    parent = o->common.parent;
    if (parent) {
	parent = o->common.parent;
	font.color.R = parent->font.color.R;
	font.color.G = parent->font.color.G;
	font.color.B = parent->font.color.B;
	font.color.A = parent->font.color.A;

	font.glutfont = parent->font.glutfont;
	font.fontdesc = gv_strdup(parent->font.fontdesc);
	font.size = parent->font.size;
	font.transparent = parent->font.transparent;
	font.justify.VJustify = parent->font.justify.VJustify;
	font.justify.HJustify = parent->font.justify.HJustify;
	font.is2D = parent->font.is2D;
	if (text) {
	    if (strlen(text))
		font.tex =
		    glCompSetAddNewTexLabel(parent->compset,
					    font.fontdesc, font.size,
					    text, parent->font.is2D);
	}
    } else {			/*no parent */

	glCompColor c;
	c.R = GLCOMPSET_FONT_COLOR_R;
	c.G = GLCOMPSET_FONT_COLOR_G;
	c.B = GLCOMPSET_FONT_COLOR_B;
	c.A = GLCOMPSET_FONT_COLOR_ALPHA;
	font = glNewFont(o->common.compset, text, &c,
		     GLCOMPSET_FONT_DESC, GLCOMPSET_FONT_SIZE, true);
    }
    return font;
}

/*texture base 3d text rendering*/
void glCompDrawText3D(glCompFont f, float x, float y, double z, float w,
                      float h) {
	glEnable(GL_BLEND);		// Turn Blending On
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_TEXTURE_2D);
	glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glBindTexture(GL_TEXTURE_2D, f.tex->id);
	glBegin(GL_QUADS);
		glTexCoord2d(0.0f, 1.0f);glVertex3d(x,y,z);
		glTexCoord2d(1.0f, 1.0f);glVertex3d(x+w,y,z);
		glTexCoord2d(1.0f, 0.0f);glVertex3d(x+w,y+h,z);
		glTexCoord2d(0.0f, 0.0f);glVertex3d(x,y+h,z);
	glEnd();

	glDisable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);

}

void glCompDrawText(glCompFont f, double x, double y) {
    glRasterPos2d(x, y);
    glDrawPixels(f.tex->width, f.tex->height, GL_RGBA, GL_UNSIGNED_BYTE,  f.tex->data);
}

/*text rendering functions, depends on a globject to retrieve stats*/
void glCompRenderText(glCompFont f, glCompObj *parentObj) {
    if (!f.tex)
	return;
    float x = 0;
    float y = 0;
    glCompCommon ref = parentObj->common;
    switch (f.justify.HJustify) {
    case glFontHJustifyNone:
	x = ref.refPos.x;
	break;
    case glFontHJustifyCenter:
	x = ref.refPos.x + (ref.width - (float)f.tex->width) / 2.0f;
	break;
    }
    switch (f.justify.VJustify) {
    case glFontVJustifyNone:
	y = ref.pos.y;
	break;
    case glFontVJustifyCenter:
	y = ref.refPos.y + (ref.height - (float)f.tex->height) / 2.0f;
	break;
    }

    glCompSetColor(f.color);
    glCompDrawText(f, x, y);
}