Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*
- //* Shaper
- //*
- ATXTSHAPEDP aTxtShaper(ATXTPRESHAPEP span, ATXTCHUNKPARSEP chunk) {
- /* Get Freetype Font Face */
- FT_Face font = aFontGet(span->fontid, 1);
- if (font == NULL) {
- return NULL;
- }
- /* Only 1 Process per Thread */
- pthread_mutex_lock(&_atext_mutex);
- /* Init harfbuzz font */
- byte fontsize = ATC_GETFONTSIZE(chunk->curr_state.font);
- aFontSetSize(span->fontid, aFontSizePX(fontsize), 1);
- AFONTFACEP afont=aFontGetFace(span->fontid);
- hb_font_t * hb_font = afont->hb_font;
- /* Create harfbuzz buffer */
- hb_buffer_t * buf = hb_buffer_create();
- /* Init harfbuzz buffer */
- hb_buffer_set_unicode_funcs(buf, hb_ucdn_get_unicode_funcs());
- hb_buffer_set_direction(buf, chunk->rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
- hb_buffer_set_script(buf, ATXT_UCDN2HB_SCRIPT[chunk->script]);
- /* Set harfbuzz text buffer */
- hb_buffer_add_utf32(buf, (const uint32_t *) span->text, (unsigned int) span->len, 0, (unsigned int) span->len);
- hb_buffer_guess_segment_properties(buf);
- /* Run harfbuzz shaper */
- hb_shape(hb_font, buf, NULL, 0);
- /* Get harfbuzz result values */
- dword glyph_count = hb_buffer_get_length(buf);
- if (glyph_count < 1) {
- hb_buffer_destroy(buf);
- pthread_mutex_unlock(&_atext_mutex);
- return NULL;
- }
- hb_glyph_info_t * glyph_info = hb_buffer_get_glyph_infos(buf, &glyph_count);
- hb_glyph_position_t * glyph_pos = hb_buffer_get_glyph_positions(buf, &glyph_count);
- /* Prepare Return Memory */
- ATXTSHAPEDP shaped = (ATXTSHAPEDP) malloc(sizeof(ATXTSHAPED));
- shaped->coln = glyph_count;
- shaped->cols = (ATXTCOLP) malloc(sizeof(ATXTCOL) * shaped->coln);
- shaped->rtl = chunk->rtl;
- shaped->font = ATXT_FONT(span->fontid, fontsize);
- shaped->next = NULL;
- /* NOW CALCULATING :P */
- dword i = 0;
- int sizer_x = 0;
- int sizer_y = 0;
- int max_x = INT_MIN;
- int min_x = INT_MAX;
- int max_y = INT_MIN;
- int min_y = INT_MAX;
- int x = 0;
- int y = 0;
- int p = -1;
- int u = 0;
- for (i = 0; i < glyph_count; i++) {
- /* Offsets and Advances */
- int xo = glyph_pos[i].x_offset >> 6;
- int yo = glyph_pos[i].y_offset >> 6;
- int xa = glyph_pos[i].x_advance >> 6;
- int ya = glyph_pos[i].y_advance >> 6;
- /* Position */
- int gx = sizer_x + xo;
- int gy = sizer_y + yo;
- /* Retrive min & max */
- if (min_x > gx) min_x = gx;
- if (max_x < gx) max_x = gx;
- if (min_y > gy) min_y = gy;
- if (max_y < gy) max_y = gy;
- /* Save Data into Column */
- u=glyph_info[i].codepoint;;
- shaped->cols[i].id = u;
- shaped->cols[i].x = x + xo;
- shaped->cols[i].y = y - yo;
- shaped->cols[i].w = xa;
- /* Advance Position */
- sizer_x += xa;
- sizer_y += ya;
- /* Glyph Position */
- x += xa;
- y -= ya;
- }
- /* Still have to take into account last glyph's advance. Or not? */
- if (min_x>sizer_x) min_x = sizer_x;
- if (max_x<sizer_x) max_x = sizer_x;
- if (min_y>sizer_y) min_y = sizer_y;
- if (max_y<sizer_y) max_y = sizer_y;
- /* bbox */
- int bbox_w = max_x - min_x;
- int bbox_h = max_y - min_y;
- /* Get Bounding Box */
- shaped->w = max_x - min_x;
- shaped->h = max_y - min_y; /*aFontSizePX(fontsize);*/
- pthread_mutex_unlock(&_atext_mutex);
- return shaped;
- }
Advertisement
Add Comment
Please, Sign In to add comment