Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
glcomptexture.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include <assert.h>
12#include <cgraph/alloc.h>
14#include <glcomp/glpangofont.h>
15#include <stddef.h>
16#include <stdbool.h>
17
18static glCompTex *glCompSetAddNewTexture(glCompSet *s, int width, int height,
19 unsigned char *data, bool is2D) {
20 int Er, offset, ind;
21 unsigned char *tarData;
22 unsigned char *srcData;
23
24 if (!data)
25 return NULL;
26
27 Er = 0;
28 glCompTex *t = gv_alloc(sizeof(glCompTex));
29 if (!is2D) { /*use opengl texture */
30 glEnable(GL_TEXTURE_2D);
31 glShadeModel(GL_FLAT);
32 glEnable(GL_DEPTH_TEST);
33 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
34 glGenTextures(1, &t->id); //get next id
35 if (glGetError() != GL_NO_ERROR) { /*for some opengl based error , texture couldnt be created */
36 /* drain the OpenGL error queue */
37 while (glGetError() != GL_NO_ERROR);
38 Er = 1;
39 } else {
40 glBindTexture(GL_TEXTURE_2D, t->id);
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
44 GL_NEAREST);
45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
46 GL_NEAREST);
47 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
48 GL_RGBA, GL_UNSIGNED_BYTE, data);
49 glDisable(GL_TEXTURE_2D);
50 }
51 }
52 if (is2D && !Er) {
53 assert(width >= 0);
54 assert(height >= 0);
55 t->data = gv_calloc(4 * (unsigned)width * (unsigned)height,
56 sizeof(unsigned char));
57 offset = 4; //RGBA mod,TO DO implement other modes
58 /*data upside down because of pango gl coord system */
59 for (ind = 0; ind < height; ind++) {
60 srcData = data + (height - 1 - ind) * offset * width;
61 tarData = t->data + ind * offset * width;
62 memcpy(tarData, srcData, 4 * (unsigned)width);
63 }
64 }
65
66 if (Er) {
67 free(data);
68 free(t);
69 return NULL;
70 }
71 t->userCount = 1;
72 t->width = (float)width;
73 t->height = (float)height;
74 if(s)
75 {
76 s->textures =
77 gv_recalloc(s->textures, s->textureCount, s->textureCount + 1, sizeof(glCompTex *));
78 s->textureCount++;
79 s->textures[s->textureCount - 1] = t;
80 }
81 return t;
82
83
84}
85
87 unsigned char *data, bool is2D) {
88
89 glCompTex *t;
90 if (!data)
91 return NULL;
92 t = glCompSetAddNewTexture(s, width, height, data, is2D);
93 if (!t)
94 return NULL;
95 t->type = glTexImage;
96 return t;
97
98}
99
100glCompTex *glCompSetAddNewTexLabel(glCompSet *s, char *def, int fs, char *text,
101 bool is2D) {
102 int Er, width, height;
103 glCompTex *t;
104 cairo_surface_t *surface = NULL;
105 unsigned char *data = NULL;
106 Er = 0;
107 if (!def)
108 return NULL;
109 /*first check if the same label with same font def created before
110 if it was , return its id
111 */
112 for (size_t ind = 0; ind < s->textureCount; ind++) {
113 if (s->textures[ind]->type == glTexLabel) {
114 if (strcmp(def, s->textures[ind]->def) == 0
115 && s->textures[ind]->type == glTexLabel
116 && strcmp(text, s->textures[ind]->text) == 0
117 && s->textures[ind]->fontSize==fs) {
118 s->textures[ind]->userCount++;
119 return s->textures[ind];
120 }
121 }
122 }
123
124
125 data = glCompCreatePangoTexture(def, fs, text, surface, &width, &height);
126 if (!data) /*pango error , */
127 return NULL;
128 t = glCompSetAddNewTexture(s, width, height, data, is2D);
129 if (!t)
130 Er = 1;
131 cairo_surface_destroy(surface);
132
133 if (Er) {
134 free(data);
135 free(t);
136 return NULL;
137 }
138
139 t->def = gv_strdup(def);
140 t->text = gv_strdup(text);
141 t->type = glTexLabel;
142 return t;
143}
144
146{
147 if (!t)
148 return;
149 t->userCount--;
150 if (!t->userCount) {
151 free(t->data);
152 free(t->def);
153 free(t->text);
154 free(t);
155 }
156}
Memory allocation wrappers that exit on failure.
static void * gv_recalloc(void *ptr, size_t old_nmemb, size_t new_nmemb, size_t size)
Definition alloc.h:73
static char * gv_strdup(const char *original)
Definition alloc.h:101
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static void * gv_alloc(size_t size)
Definition alloc.h:47
@ glTexImage
Definition glcompdefs.h:92
@ glTexLabel
Definition glcompdefs.h:92
glCompTex * glCompSetAddNewTexLabel(glCompSet *s, char *def, int fs, char *text, bool is2D)
glCompTex * glCompSetAddNewTexImage(glCompSet *s, int width, int height, unsigned char *data, bool is2D)
void glCompDeleteTexture(glCompTex *t)
static glCompTex * glCompSetAddNewTexture(glCompSet *s, int width, int height, unsigned char *data, bool is2D)
unsigned char * glCompCreatePangoTexture(char *fontdescription, int fontsize, char *txt, cairo_surface_t *surface, int *w, int *h)
Definition glpangofont.c:58
void free(void *)
node NULL
Definition grammar.y:149
swig_ptr_object_handlers offset
Definition gv_php.cpp:5915
Definition legal.c:50
char * def
Definition glcompdefs.h:163
uint32_t id
Definition glcompdefs.h:162
unsigned char * data
Definition glcompdefs.h:170
float height
Definition glcompdefs.h:166
int userCount
Definition glcompdefs.h:168
float width
Definition glcompdefs.h:165
glCompTexType type
Definition glcompdefs.h:167
char * text
Definition glcompdefs.h:164
Definition grammar.c:93