Welcome to mirror list, hosted at ThFree Co, Russian Federation.

draw_manager_text.c « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 977374a00c51a032d7c74024eac581bc25bee5cc (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright 2016, Blender Foundation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/** \file blender/draw/intern/draw_manager_text.c
 *  \ingroup draw
 */

#include "MEM_guardedalloc.h"

#include "BLI_listbase.h"
#include "BLI_string.h"
#include "BLI_math.h"

#include "BIF_gl.h"

#include "GPU_matrix.h"

#include "ED_screen.h"
#include "ED_view3d.h"

#include "UI_resources.h"
#include "UI_interface.h"

#include "WM_api.h"
#include "BLF_api.h"

#include "draw_manager_text.h"

typedef struct ViewCachedString {
	struct ViewCachedString *next, *prev;
	float vec[3];
	union {
		uchar ub[4];
		int pack;
	} col;
	short sco[2];
	short xoffs;
	short flag;
	int str_len;

	/* str is allocated past the end */
	char str[0];
} ViewCachedString;

typedef struct DRWTextStore {
	ListBase list;
} DRWTextStore;

DRWTextStore *DRW_text_cache_create(void)
{
	DRWTextStore *dt = MEM_callocN(sizeof(*dt), __func__);
	return dt;
}

void DRW_text_cache_destroy(struct DRWTextStore *dt)
{
	BLI_freelistN(&dt->list);
	MEM_freeN(dt);
}

void DRW_text_cache_add(
        DRWTextStore *dt,
        const float co[3],
        const char *str, const int str_len,
        short xoffs, short flag,
        const uchar col[4])
{
	int alloc_len;
	ViewCachedString *vos;

	if (flag & DRW_TEXT_CACHE_STRING_PTR) {
		BLI_assert(str_len == strlen(str));
		alloc_len = sizeof(void *);
	}
	else {
		alloc_len = str_len + 1;
	}

	vos = MEM_mallocN(sizeof(ViewCachedString) + alloc_len, __func__);

	BLI_addtail(&dt->list, vos);

	copy_v3_v3(vos->vec, co);
	copy_v4_v4_uchar(vos->col.ub, col);
	vos->xoffs = xoffs;
	vos->flag = flag;
	vos->str_len = str_len;

	/* allocate past the end */
	if (flag & DRW_TEXT_CACHE_STRING_PTR) {
		memcpy(vos->str, &str, alloc_len);
	}
	else {
		memcpy(vos->str, str, alloc_len);
	}
}

void DRW_text_cache_draw(DRWTextStore *dt, ARegion *ar)
{
	RegionView3D *rv3d = ar->regiondata;
	ViewCachedString *vos;
	int tot = 0;

	/* project first and test */
	for (vos = dt->list.first; vos; vos = vos->next) {
		if (ED_view3d_project_short_ex(
		        ar,
		        (vos->flag & DRW_TEXT_CACHE_GLOBALSPACE) ? rv3d->persmat : rv3d->persmatob,
		        (vos->flag & DRW_TEXT_CACHE_LOCALCLIP) != 0,
		        vos->vec, vos->sco,
		        V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_WIN | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK)
		{
			tot++;
		}
		else {
			vos->sco[0] = IS_CLIPPED;
		}
	}

	if (tot) {
		int col_pack_prev = 0;

		if (rv3d->rflag & RV3D_CLIPPING) {
			ED_view3d_clipping_disable();
		}

		float original_proj[4][4];
		gpuGetProjectionMatrix(original_proj);
		wmOrtho2_region_pixelspace(ar);

		gpuPushMatrix();
		gpuLoadIdentity();

		const int font_id = BLF_default();

		const uiStyle *style = UI_style_get();

		BLF_size(font_id, style->widget.points * U.pixelsize, U.dpi);

		for (vos = dt->list.first; vos; vos = vos->next) {
			if (vos->sco[0] != IS_CLIPPED) {
				if (col_pack_prev != vos->col.pack) {
					BLF_color4ubv(font_id, vos->col.ub);
					col_pack_prev = vos->col.pack;
				}

				BLF_position(
				        font_id,
				        (float)(vos->sco[0] + vos->xoffs), (float)(vos->sco[1]), 2.0f);

				((vos->flag & DRW_TEXT_CACHE_ASCII) ?
				 BLF_draw_ascii :
				 BLF_draw
				 )(font_id,
				   (vos->flag & DRW_TEXT_CACHE_STRING_PTR) ? *((const char **)vos->str) : vos->str,
				   vos->str_len);
			}
		}

		gpuPopMatrix();
		gpuLoadProjectionMatrix(original_proj);

		if (rv3d->rflag & RV3D_CLIPPING) {
			ED_view3d_clipping_enable();
		}
	}
}