Graphviz 13.0.0~dev.20241220.2304
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>
13#include <glcomp/glpangofont.h>
14#include <stddef.h>
15#include <stdbool.h>
16#include <util/alloc.h>
17#include <util/streq.h>
18
19static glCompTex *glCompSetAddNewTexture(glCompSet *s, int width, int height,
20 const unsigned char *data, bool is2D) {
21 int offset, ind;
22 unsigned char *tarData;
23
24 if (!data)
25 return NULL;
26
27 glCompTex *t = gv_alloc(sizeof(glCompTex));
28 if (!is2D) { /*use opengl texture */
29 glEnable(GL_TEXTURE_2D);
30 glShadeModel(GL_FLAT);
31 glEnable(GL_DEPTH_TEST);
32 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
33 glGenTextures(1, &t->id); //get next id
34 if (glGetError() != GL_NO_ERROR) { /*for some opengl based error , texture couldnt be created */
35 /* drain the OpenGL error queue */
36 while (glGetError() != GL_NO_ERROR);
37 free(t);
38 return NULL;
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) {
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 const unsigned char *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 t->userCount = 1;
67 t->width = width;
68 t->height = height;
69 if(s)
70 {
71 s->textures =
72 gv_recalloc(s->textures, s->textureCount, s->textureCount + 1, sizeof(glCompTex *));
73 s->textureCount++;
74 s->textures[s->textureCount - 1] = t;
75 }
76 return t;
77
78
79}
80
82 const unsigned char *data, bool is2D) {
83
84 glCompTex *t;
85 if (!data)
86 return NULL;
87 t = glCompSetAddNewTexture(s, width, height, data, is2D);
88 if (!t)
89 return NULL;
90 t->type = glTexImage;
91 return t;
92
93}
94
95glCompTex *glCompSetAddNewTexLabel(glCompSet *s, char *def, int fs, char *text,
96 bool is2D) {
97 int width, height;
98 glCompTex *t;
99 cairo_surface_t *surface = NULL;
100 unsigned char *data = NULL;
101 if (!def)
102 return NULL;
103 /*first check if the same label with same font def created before
104 if it was , return its id
105 */
106 for (size_t ind = 0; ind < s->textureCount; ind++) {
107 if (s->textures[ind]->type == glTexLabel) {
108 if (streq(def, s->textures[ind]->def)
109 && s->textures[ind]->type == glTexLabel
110 && streq(text, s->textures[ind]->text)
111 && s->textures[ind]->fontSize==fs) {
112 s->textures[ind]->userCount++;
113 return s->textures[ind];
114 }
115 }
116 }
117
118
119 data = glCompCreatePangoTexture(def, fs, text, &surface, &width, &height);
120 if (!data) /*pango error , */
121 return NULL;
122 t = glCompSetAddNewTexture(s, width, height, data, is2D);
123 cairo_surface_destroy(surface);
124 if (!t) {
125 free(data);
126 return NULL;
127 }
128
129 t->def = gv_strdup(def);
130 t->text = gv_strdup(text);
131 t->type = glTexLabel;
132 return t;
133}
134
136{
137 if (!t)
138 return;
139 t->userCount--;
140 if (!t->userCount) {
141 free(t->data);
142 free(t->def);
143 free(t->text);
144 free(t);
145 }
146}
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:70
@ glTexLabel
Definition glcompdefs.h:70
glCompTex * glCompSetAddNewTexLabel(glCompSet *s, char *def, int fs, char *text, bool is2D)
void glCompDeleteTexture(glCompTex *t)
glCompTex * glCompSetAddNewTexImage(glCompSet *s, int width, int height, const unsigned char *data, bool is2D)
static glCompTex * glCompSetAddNewTexture(glCompSet *s, int width, int height, const 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:50
void free(void *)
node NULL
Definition grammar.y:163
swig_ptr_object_handlers offset
Definition gv_php.cpp:5907
static bool streq(const char *a, const char *b)
are a and b equal?
Definition streq.h:11
Definition legal.c:50
char * def
Definition glcompdefs.h:135
uint32_t id
Definition glcompdefs.h:134
unsigned char * data
Definition glcompdefs.h:142
int userCount
Definition glcompdefs.h:140
glCompTexType type
Definition glcompdefs.h:139
char * text
Definition glcompdefs.h:136
Definition grammar.c:93