amarullz

Text Shaper with harfbuzz-ng

Jan 16th, 2015
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.41 KB | None | 0 0
  1. //*
  2. //* Shaper
  3. //*
  4. ATXTSHAPEDP aTxtShaper(ATXTPRESHAPEP span, ATXTCHUNKPARSEP chunk) {
  5.   /* Get Freetype Font Face */
  6.   FT_Face font = aFontGet(span->fontid, 1);
  7.  
  8.   if (font == NULL) {
  9.     return NULL;
  10.   }
  11.  
  12.   /* Only 1 Process per Thread */
  13.   pthread_mutex_lock(&_atext_mutex);
  14.  
  15.   /* Init harfbuzz font */
  16.   byte fontsize             = ATC_GETFONTSIZE(chunk->curr_state.font);
  17.   aFontSetSize(span->fontid, aFontSizePX(fontsize), 1);
  18.   AFONTFACEP afont=aFontGetFace(span->fontid);
  19.   hb_font_t * hb_font       = afont->hb_font;
  20.   /* Create harfbuzz buffer */
  21.   hb_buffer_t * buf = hb_buffer_create();
  22.   /* Init harfbuzz buffer */
  23.   hb_buffer_set_unicode_funcs(buf, hb_ucdn_get_unicode_funcs());
  24.   hb_buffer_set_direction(buf, chunk->rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
  25.   hb_buffer_set_script(buf, ATXT_UCDN2HB_SCRIPT[chunk->script]);
  26.   /* Set harfbuzz text buffer */
  27.   hb_buffer_add_utf32(buf, (const uint32_t *) span->text, (unsigned int) span->len, 0, (unsigned int) span->len);
  28.   hb_buffer_guess_segment_properties(buf);
  29.  
  30.   /* Run harfbuzz shaper */
  31.   hb_shape(hb_font, buf, NULL, 0);
  32.  
  33.   /* Get harfbuzz result values */
  34.   dword glyph_count                   = hb_buffer_get_length(buf);
  35.  
  36.   if (glyph_count < 1) {
  37.     hb_buffer_destroy(buf);
  38.     pthread_mutex_unlock(&_atext_mutex);
  39.     return NULL;
  40.   }
  41.  
  42.   hb_glyph_info_t   *  glyph_info   = hb_buffer_get_glyph_infos(buf, &glyph_count);
  43.   hb_glyph_position_t * glyph_pos   = hb_buffer_get_glyph_positions(buf, &glyph_count);
  44.   /* Prepare Return Memory */
  45.   ATXTSHAPEDP shaped  = (ATXTSHAPEDP) malloc(sizeof(ATXTSHAPED));
  46.   shaped->coln        = glyph_count;
  47.   shaped->cols        = (ATXTCOLP) malloc(sizeof(ATXTCOL) * shaped->coln);
  48.   shaped->rtl         = chunk->rtl;
  49.   shaped->font        = ATXT_FONT(span->fontid, fontsize);
  50.   shaped->next        = NULL;
  51.  
  52.   /* NOW CALCULATING :P */
  53.   dword i = 0;
  54.   int   sizer_x = 0;
  55.   int   sizer_y = 0;
  56.   int   max_x   = INT_MIN;
  57.   int   min_x   = INT_MAX;
  58.   int   max_y   = INT_MIN;
  59.   int   min_y   = INT_MAX;
  60.   int   x       = 0;
  61.   int   y       = 0;
  62.   int   p = -1;
  63.   int   u = 0;
  64.   for (i = 0; i < glyph_count; i++) {
  65.     /* Offsets and Advances */
  66.     int xo = glyph_pos[i].x_offset  >> 6;
  67.     int yo = glyph_pos[i].y_offset  >> 6;
  68.     int xa = glyph_pos[i].x_advance >> 6;
  69.     int ya = glyph_pos[i].y_advance >> 6;
  70.     /* Position */
  71.     int gx = sizer_x + xo;
  72.     int gy = sizer_y + yo;
  73.    
  74.     /* Retrive min & max */
  75.     if (min_x > gx) min_x = gx;
  76.     if (max_x < gx) max_x = gx;
  77.     if (min_y > gy) min_y = gy;
  78.     if (max_y < gy) max_y = gy;
  79.    
  80.     /* Save Data into Column */
  81.     u=glyph_info[i].codepoint;;
  82.     shaped->cols[i].id  = u;
  83.     shaped->cols[i].x   = x + xo;
  84.     shaped->cols[i].y   = y - yo;
  85.     shaped->cols[i].w   = xa;
  86.    
  87.     /* Advance Position */
  88.     sizer_x += xa;
  89.     sizer_y += ya;
  90.    
  91.     /* Glyph Position */
  92.     x += xa;
  93.     y -= ya;
  94.   }
  95.  
  96.   /* Still have to take into account last glyph's advance. Or not? */
  97.   if (min_x>sizer_x) min_x = sizer_x;
  98.   if (max_x<sizer_x) max_x = sizer_x;
  99.   if (min_y>sizer_y) min_y = sizer_y;
  100.   if (max_y<sizer_y) max_y = sizer_y;
  101.  
  102.   /* bbox */
  103.   int bbox_w = max_x - min_x;
  104.   int bbox_h = max_y - min_y;
  105.            
  106.   /* Get Bounding Box */
  107.   shaped->w = max_x - min_x;
  108.   shaped->h = max_y - min_y; /*aFontSizePX(fontsize);*/
  109.  
  110.   pthread_mutex_unlock(&_atext_mutex);
  111.   return shaped;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment