123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- /******************************************************************************
- Copyright (C) 2014 by Nibbles
- 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, see <http://www.gnu.org/licenses/>.
- ******************************************************************************/
- #include <obs-module.h>
- #include <util/platform.h>
- #include <ft2build.h>
- #include FT_FREETYPE_H
- #include <sys/stat.h>
- #include "text-freetype2.h"
- #include "obs-convenience.h"
- float offsets[16] = { -2.0f, 0.0f, 0.0f, -2.0f, 2.0f, 0.0f, 2.0f, 0.0f,
- 0.0f, 2.0f, 0.0f, 2.0f, -2.0f, 0.0f, -2.0f, 0.0f };
- extern uint32_t texbuf_w, texbuf_h;
- void draw_outlines(struct ft2_source *srcdata)
- {
- // Horrible (hopefully temporary) solution for outlines.
- uint32_t *tmp;
- struct gs_vb_data *vdata = gs_vertexbuffer_get_data(srcdata->vbuf);
- tmp = vdata->colors;
- vdata->colors = srcdata->colorbuf;
- gs_matrix_push();
- for (int32_t i = 0; i < 8; i++) {
- gs_matrix_translate3f(offsets[i * 2], offsets[(i * 2) + 1],
- 0.0f);
- draw_uv_vbuffer(srcdata->vbuf, srcdata->tex,
- srcdata->draw_effect,
- (uint32_t)wcslen(srcdata->text) * 6);
- }
- gs_matrix_identity();
- gs_matrix_pop();
- vdata->colors = tmp;
- }
- void draw_drop_shadow(struct ft2_source *srcdata)
- {
- // Horrible (hopefully temporary) solution for drop shadow.
- uint32_t *tmp;
- struct gs_vb_data *vdata = gs_vertexbuffer_get_data(srcdata->vbuf);
- tmp = vdata->colors;
- vdata->colors = srcdata->colorbuf;
- gs_matrix_push();
- gs_matrix_translate3f(4.0f, 4.0f, 0.0f);
- draw_uv_vbuffer(srcdata->vbuf, srcdata->tex,
- srcdata->draw_effect, (uint32_t)wcslen(srcdata->text) * 6);
- gs_matrix_identity();
- gs_matrix_pop();
- vdata->colors = tmp;
- }
- void set_up_vertex_buffer(struct ft2_source *srcdata)
- {
- FT_UInt glyph_index = 0;
- uint32_t x = 0, space_pos = 0, word_width = 0;
- if (srcdata->custom_width >= 100)
- srcdata->cx = srcdata->custom_width;
- else
- srcdata->cx = get_ft2_text_width(srcdata->text, srcdata);
- srcdata->cy = srcdata->max_h;
- obs_enter_graphics();
- if (srcdata->vbuf != NULL) {
- gs_vertbuffer_t tmpvbuf = srcdata->vbuf;
- srcdata->vbuf = NULL;
- gs_vertexbuffer_destroy(tmpvbuf);
- }
- srcdata->vbuf = create_uv_vbuffer((uint32_t)wcslen(srcdata->text) * 6,
- true);
- if (srcdata->custom_width <= 100) goto skip_word_wrap;
- if (!srcdata->word_wrap) goto skip_word_wrap;
- for (uint32_t i = 0; i <= wcslen(srcdata->text); i++) {
- if (i == wcslen(srcdata->text)) goto eos_check;
- if (srcdata->text[i] != L' ' && srcdata->text[i] != L'\n')
- goto next_char;
- eos_check:;
- if (x + word_width > srcdata->custom_width) {
- if (space_pos != 0)
- srcdata->text[space_pos] = L'\n';
- x = 0;
- }
- if (i == wcslen(srcdata->text)) goto eos_skip;
- x += word_width;
- word_width = 0;
- if (srcdata->text[i] == L'\n')
- x = 0;
- if (srcdata->text[i] == L' ')
- space_pos = i;
- next_char:;
- glyph_index = FT_Get_Char_Index(srcdata->font_face,
- srcdata->text[i]);
- word_width += src_glyph->xadv;
- eos_skip:;
- }
- skip_word_wrap:;
- fill_vertex_buffer(srcdata);
- obs_leave_graphics();
- }
- void fill_vertex_buffer(struct ft2_source *srcdata)
- {
- struct gs_vb_data *vdata = gs_vertexbuffer_get_data(srcdata->vbuf);
- if (vdata == NULL) return;
- struct vec2 *tvarray = (struct vec2 *)vdata->tvarray[0].array;
- uint32_t *col = (uint32_t *)vdata->colors;
- FT_UInt glyph_index = 0;
- uint32_t dx = 0, dy = srcdata->max_h, max_y = dy;
- uint32_t cur_glyph = 0;
- if (srcdata->colorbuf != NULL) {
- bfree(srcdata->colorbuf);
- srcdata->colorbuf = NULL;
- }
- srcdata->colorbuf = bzalloc(sizeof(uint32_t)*wcslen(srcdata->text) * 6);
- for (uint32_t i = 0; i < wcslen(srcdata->text) * 6; i++) {
- srcdata->colorbuf[i] = 0xFF000000;
- }
- for (uint32_t i = 0; i < wcslen(srcdata->text); i++) {
- add_linebreak:;
- if (srcdata->text[i] != L'\n') goto draw_glyph;
- dx = 0; i++;
- dy += srcdata->max_h + 4;
- if (i == wcslen(srcdata->text)) goto skip_glyph;
- if (srcdata->text[i] == L'\n') goto add_linebreak;
- draw_glyph:;
- // Skip filthy dual byte Windows line breaks
- if (srcdata->text[i] == L'\r') goto skip_glyph;
- glyph_index = FT_Get_Char_Index(srcdata->font_face,
- srcdata->text[i]);
- if (src_glyph == NULL)
- goto skip_glyph;
- if (srcdata->custom_width < 100) goto skip_custom_width;
- if (dx + src_glyph->xadv > srcdata->custom_width) {
- dx = 0;
- dy += srcdata->max_h + 4;
- }
- skip_custom_width:;
- set_v3_rect(vdata->points + (cur_glyph * 6),
- (float)dx + (float)src_glyph->xoff,
- (float)dy - (float)src_glyph->yoff,
- (float)src_glyph->w,
- (float)src_glyph->h);
- set_v2_uv(tvarray + (cur_glyph * 6),
- src_glyph->u,
- src_glyph->v,
- src_glyph->u2,
- src_glyph->v2);
- set_rect_colors2(col + (cur_glyph * 6),
- srcdata->color[0],
- srcdata->color[1]);
- dx += src_glyph->xadv;
- if (dy - (float)src_glyph->yoff + src_glyph->h > max_y)
- max_y = dy - src_glyph->yoff + src_glyph->h;
- cur_glyph++;
- skip_glyph:;
- }
- srcdata->cy = max_y;
- }
- void cache_standard_glyphs(struct ft2_source *srcdata)
- {
- for (uint32_t i = 0; i < num_cache_slots; i++) {
- if (srcdata->cacheglyphs[i] != NULL) {
- bfree(srcdata->cacheglyphs[i]);
- srcdata->cacheglyphs[i] = NULL;
- }
- }
- srcdata->texbuf_x = 0;
- srcdata->texbuf_y = 0;
- cache_glyphs(srcdata, L"abcdefghijklmnopqrstuvwxyz" \
- L"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" \
- L"!@#$%^&*()-_=+,<.>/?\\|[]{}`~ \'\"\0");
- }
- #define glyph_pos x + (y*slot->bitmap.pitch)
- #define buf_pos (dx + x) + ((dy + y) * texbuf_w)
- void cache_glyphs(struct ft2_source *srcdata, wchar_t *cache_glyphs)
- {
- FT_GlyphSlot slot = srcdata->font_face->glyph;
- FT_UInt glyph_index = 0;
- uint32_t dx = srcdata->texbuf_x, dy = srcdata->texbuf_y;
- uint8_t alpha;
- int32_t g_pitch = 0;
- int32_t cached_glyphs = 0;
- for (uint32_t i = 0; i < wcslen(cache_glyphs); i++) {
- glyph_index = FT_Get_Char_Index(srcdata->font_face,
- cache_glyphs[i]);
- if (src_glyph != NULL)
- goto skip_glyph;
- FT_Load_Glyph(srcdata->font_face, glyph_index, FT_LOAD_DEFAULT);
- FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
- uint32_t g_w = slot->bitmap.width;
- uint32_t g_h = slot->bitmap.rows;
- g_pitch = slot->bitmap.pitch;
- if (srcdata->max_h < g_h) srcdata->max_h = g_h;
- if (dx + g_w >= texbuf_w) {
- dx = 0;
- dy += srcdata->max_h + 1;
- }
- src_glyph = bzalloc(sizeof(struct glyph_info));
- src_glyph->u = (float)dx / (float)texbuf_w;
- src_glyph->u2 = (float)(dx + g_w) / (float)texbuf_w;
- src_glyph->v = (float)dy / (float)texbuf_h;
- src_glyph->v2 = (float)(dy + g_h) / (float)texbuf_h;
- src_glyph->w = g_w;
- src_glyph->h = g_h;
- src_glyph->yoff = slot->bitmap_top;
- src_glyph->xoff = slot->bitmap_left;
- src_glyph->xadv = slot->advance.x >> 6;
- for (uint32_t y = 0; y < g_h; y++) {
- for (uint32_t x = 0; x < g_w; x++) {
- alpha = slot->bitmap.buffer[glyph_pos];
- srcdata->texbuf[buf_pos] =
- 0x00FFFFFF ^ (alpha << 24);
- }
- }
- dx += (g_w + 1);
- if (dx >= texbuf_w) {
- dx = 0;
- dy += srcdata->max_h;
- }
- cached_glyphs++;
- skip_glyph:;
- }
- srcdata->texbuf_x = dx;
- srcdata->texbuf_y = dy;
- if (cached_glyphs > 0) {
- obs_enter_graphics();
- if (srcdata->tex != NULL) {
- gs_texture_t tmp_texture = NULL;
- tmp_texture = srcdata->tex;
- srcdata->tex = NULL;
- gs_texture_destroy(tmp_texture);
- }
- srcdata->tex = gs_texture_create(texbuf_w, texbuf_h,
- GS_RGBA, 1, (const uint8_t **)&srcdata->texbuf, 0);
- obs_leave_graphics();
- }
- }
- time_t get_modified_timestamp(char *filename)
- {
- struct stat stats;
- // stat is apparently terrifying and horrible, but we only call it once
- // every second at most.
- stat(filename, &stats);
- return stats.st_mtime;
- }
- void load_text_from_file(struct ft2_source *srcdata, const char *filename)
- {
- FILE *tmp_file = NULL;
- uint32_t filesize = 0;
- char *tmp_read = NULL;
- uint16_t header = 0;
- tmp_file = fopen(filename, "rb");
- if (tmp_file == NULL) {
- blog(LOG_WARNING, "Failed to open file %s", filename);
- return;
- }
- fseek(tmp_file, 0, SEEK_END);
- filesize = (uint32_t)ftell(tmp_file);
- fseek(tmp_file, 0, SEEK_SET);
- fread(&header, 2, 1, tmp_file);
- if (header == 0xFEFF) {
- // File is already in UTF-16 format
- if (srcdata->text != NULL) {
- bfree(srcdata->text);
- srcdata->text = NULL;
- }
- srcdata->text = bzalloc(filesize);
- fread(srcdata->text, filesize - 2, 1, tmp_file);
- srcdata->m_timestamp =
- get_modified_timestamp(srcdata->text_file);
- bfree(tmp_read);
- fclose(tmp_file);
- return;
- }
- fseek(tmp_file, 0, SEEK_SET);
- srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file);
- tmp_read = bzalloc(filesize + 1);
- fread(tmp_read, filesize, 1, tmp_file);
- fclose(tmp_file);
- if (srcdata->text != NULL) {
- bfree(srcdata->text);
- srcdata->text = NULL;
- }
- srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t));
- os_utf8_to_wcs(tmp_read, strlen(tmp_read),
- srcdata->text, (strlen(tmp_read) + 1));
- bfree(tmp_read);
- }
- void read_from_end(struct ft2_source *srcdata, const char *filename)
- {
- FILE *tmp_file = NULL;
- uint32_t filesize = 0, cur_pos = 0;
- char *tmp_read = NULL;
- uint16_t value = 0, line_breaks = 0;
- char bvalue;
- bool utf16 = false;
- tmp_file = fopen(filename, "rb");
- if (tmp_file == NULL) {
- blog(LOG_WARNING, "Failed to open file %s", filename);
- return;
- }
- fread(&value, 2, 1, tmp_file);
- if (value == 0xFEFF)
- utf16 = true;
- fseek(tmp_file, 0, SEEK_END);
- filesize = (uint32_t)ftell(tmp_file);
- cur_pos = filesize;
- while (line_breaks <= 6 && cur_pos != 0) {
- if (!utf16) cur_pos--;
- else cur_pos -= 2;
- fseek(tmp_file, cur_pos, SEEK_SET);
- if (!utf16) {
- fread(&bvalue, 1, 1, tmp_file);
- if (bvalue == '\n')
- line_breaks++;
- }
- else {
- fread(&value, 2, 1, tmp_file);
- if (value == L'\n')
- line_breaks++;
- }
- }
- if (cur_pos != 0)
- cur_pos += (utf16) ? 2 : 1;
- fseek(tmp_file, cur_pos, SEEK_SET);
- if (utf16) {
- if (srcdata->text != NULL) {
- bfree(srcdata->text);
- srcdata->text = NULL;
- }
- srcdata->text = bzalloc(filesize - cur_pos);
- fread(srcdata->text, (filesize - cur_pos), 1, tmp_file);
- srcdata->m_timestamp =
- get_modified_timestamp(srcdata->text_file);
- bfree(tmp_read);
- fclose(tmp_file);
- return;
- }
- tmp_read = bzalloc((filesize - cur_pos) + 1);
- fread(tmp_read, filesize - cur_pos, 1, tmp_file);
- fclose(tmp_file);
- if (srcdata->text != NULL) {
- bfree(srcdata->text);
- srcdata->text = NULL;
- }
- srcdata->text = bzalloc((strlen(tmp_read) + 1)*sizeof(wchar_t));
- os_utf8_to_wcs(tmp_read, strlen(tmp_read),
- srcdata->text, (strlen(tmp_read) + 1));
- srcdata->m_timestamp = get_modified_timestamp(srcdata->text_file);
- bfree(tmp_read);
- }
- uint32_t get_ft2_text_width(wchar_t *text, struct ft2_source *srcdata)
- {
- FT_GlyphSlot slot = srcdata->font_face->glyph;
- FT_UInt glyph_index = 0;
- uint32_t w = 0, max_w = 0;
- for (uint32_t i = 0; i < (uint32_t)wcslen(text); i++) {
- glyph_index = FT_Get_Char_Index(srcdata->font_face, text[i]);
- FT_Load_Glyph(srcdata->font_face, glyph_index, FT_LOAD_DEFAULT);
- if (text[i] == L'\n') w = 0;
- else {
- w += slot->advance.x >> 6;
- if (w > max_w) max_w = w;
- }
- }
- return max_w;
- }
|