Exemplo n.º 1
0
float Face::height(int index, float scale) const
{
  int left, top, right, bottom;
  stbtt_GetGlyphBitmapBox(m_info, index, scale, scale,
                          &left, &top, &right, &bottom);
  return float(bottom - top + 1);
}
Exemplo n.º 2
0
vec2 Face::bearing(int index, float scale) const
{
  int left, top, right, bottom;
  stbtt_GetGlyphBitmapBox(m_info, index, scale, scale,
                          &left, &top, &right, &bottom);
  return vec2(left, -bottom);
}
Exemplo n.º 3
0
 // Bake a given set of ranges of chars
 int TTFFontAsset::BakeFontBitmap(const unsigned char *data, int offset,  // font location (use offset=0 for plain .ttf)
                                  float pixel_height,                     // height of font in pixels
                                  unsigned char *pixels, int pw, int ph,  // bitmap to be filled in
                                  const GlyphRanges& ranges,              // characters to bake
                                  BakedChar *chardata)
 {
     stbtt_fontinfo f;
     stbtt_InitFont(&f, data, offset);
     STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels
     
     int x = 1, y = 1;
     int bottom_y = 1;
     
     glyphMap.clear();
     
     float scale = stbtt_ScaleForPixelHeight(&f, pixel_height);
     
     int bakedIdx = 0;
     for (GlyphRanges::const_iterator iter = ranges.begin(); iter != ranges.end(); ++iter)
     {
         const Range<int>& range = *iter;
         int numItemsInRange = range.high - range.low;
         for (int i = 0; i <= numItemsInRange; i++, bakedIdx++)
         {
             int unicodeCodepoint = i + range.low;
             glyphMap.insert(std::make_pair(unicodeCodepoint, bakedIdx));
             
             int advance, lsb, x0,y0,x1,y1,gw,gh;
             int g = stbtt_FindGlyphIndex(&f, unicodeCodepoint);
             stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
             stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1);
             
             gw = x1-x0;
             gh = y1-y0;
             if (x + gw + 1 >= pw)
                 y = bottom_y, x = 1; // advance to next row
             if (y + gh + 1 >= ph) // check if it fits vertically AFTER potentially moving to next row
                 return -i;
             
             STBTT_assert(x+gw < pw);
             STBTT_assert(y+gh < ph);
             
             stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g);
             
             chardata[bakedIdx].x0 = (stbtt_int16) x;
             chardata[bakedIdx].y0 = (stbtt_int16) y;
             chardata[bakedIdx].x1 = (stbtt_int16) (x + gw);
             chardata[bakedIdx].y1 = (stbtt_int16) (y + gh);
             chardata[bakedIdx].xadvance = scale * advance;
             chardata[bakedIdx].xoff     = (float) x0;
             chardata[bakedIdx].yoff     = (float) y0;
             
             x = x + gw + 2;
             if (y+gh+2 > bottom_y)
                 bottom_y = y+gh+2;
         }
     }
     return bottom_y;
 }
Exemplo n.º 4
0
void GlFont::InitialiseFont(const unsigned char* ttf_buffer, float pixel_height, int tex_w, int tex_h)
{
    font_height_px = pixel_height;
    this->tex_w = tex_w;
    this->tex_h = tex_h;
    font_bitmap = new unsigned char[tex_w*tex_h];
    const int offset = 0;

    stbtt_fontinfo f;
    if (!stbtt_InitFont(&f, ttf_buffer, offset)) {
       throw std::runtime_error("Unable to initialise font");
    }

    float scale = stbtt_ScaleForPixelHeight(&f, pixel_height);

    STBTT_memset(font_bitmap, 0, tex_w*tex_h);
    int x = 1;
    int y = 1;
    int bottom_y = 1;

    // Generate bitmap and char indices
    for (int i=0; i < NUM_CHARS; ++i) {
       int advance, lsb, x0,y0,x1,y1,gw,gh;
       int g = stbtt_FindGlyphIndex(&f, FIRST_CHAR + i);
       stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
       stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1);
       gw = x1-x0;
       gh = y1-y0;
       if (x + gw + 1 >= tex_w)
          y = bottom_y, x = 1; // advance to next row
       if (y + gh + 1 >= tex_h) // check if it fits vertically AFTER potentially moving to next row
          throw std::runtime_error("Unable to initialise font");
       STBTT_assert(x+gw < tex_w);
       STBTT_assert(y+gh < tex_h);
       stbtt_MakeGlyphBitmap(&f, font_bitmap+x+y*tex_w, gw,gh,tex_w, scale,scale, g);

       // Adjust offset for edges of pixels
       chardata[i] = GlChar(tex_w,tex_h, x, y, gw, gh, scale*advance, x0 -0.5, -y0 -0.5);

       x = x + gw + 1;
       if (y+gh+1 > bottom_y)
          bottom_y = y+gh+1;
    }

    // Generate kern table
    for (int i=0; i < NUM_CHARS; ++i) {
        for (int j=0; j < NUM_CHARS; ++j) {
            kern_table[i*NUM_CHARS+j] = scale * stbtt_GetCodepointKernAdvance(&f,i,j);
        }
    }
}
JNIEXPORT void JNICALL Java_com_badlogic_gdx_graphics_g2d_stbtt_StbTrueType_getGlyphBitmapBox(JNIEnv* env, jclass clazz, jlong info, jint glyph, jfloat scaleX, jfloat scaleY, jfloat shiftX, jfloat shiftY, jintArray obj_box) {
	int* box = (int*)env->GetPrimitiveArrayCritical(obj_box, 0);


//@line:162

		int x0, y0, x1, y1;
		x0 = y0 = x1 = y1 = 0;
		stbtt_GetGlyphBitmapBox((stbtt_fontinfo*)info, glyph, scaleX, scaleY, shiftX, shiftY, &x0, &y0, &x1, &y1);
		box[0] = x0;
		box[1] = y0;
		box[2] = x1;
		box[3] = y1;
	
	env->ReleasePrimitiveArrayCritical(obj_box, box, 0);

}
Exemplo n.º 6
0
Ref<Image> Face::glyph(int index, float scale) const
{
  if (stbtt_IsGlyphEmpty(m_info, index))
    return nullptr;

  int left, top, right, bottom;
  stbtt_GetGlyphBitmapBox(m_info, index, scale, scale,
                          &left, &top, &right, &bottom);

  Ref<Image> glyph = Image::create(ResourceInfo(cache()),
                                   PixelFormat::L8,
                                   right - left, bottom - top);

  stbtt_MakeGlyphBitmap(m_info, (unsigned char*) glyph->pixels(),
                        glyph->width(), glyph->height(), glyph->width(),
                        scale, scale, index);

  glyph->flipHorizontal();

  return glyph;
}
Exemplo n.º 7
0
    stash::sth_glyph* stash::sth_font::get_glyph(unsigned int codepoint, short isize)
    {
        // Find code point and size.
        std::pair<unsigned int,int> key(codepoint, isize);

        if (glyphs.find( key ) != glyphs.end() )
            return &glyphs.find( key )->second;

        // Could not find glyph, create it.
        int advance,lsb,x0,y0,x1,y1;
        int g = stbtt_FindGlyphIndex(&font, codepoint);
        float scale = stbtt_ScaleForPixelHeight(&font, isize/10.0f);
        stbtt_GetGlyphHMetrics(&font, g, &advance, &lsb);
        stbtt_GetGlyphBitmapBox(&font, g, scale,scale, &x0,&y0,&x1,&y1);

        int gw = x1-x0;
        int gh = y1-y0;

        // Find row where the glyph can be fit (vertically first, then horizontally)
        sth_row* br = 0;
        int rh = (gh+7) & ~7;
        for (size_t i = 0; i < rows->size() && !br; ++i)
        {
            if (rows->at(i).h == rh && rows->at(i).x+gw+1 <= tw)
                br = &rows->at(i);
        }

        // If no row found, add new.
        if (br == NULL)
        {
            short py = 0;
            // Check that there is enough space.
            if (rows->size())
            {
                py = rows->back().y + rows->back().h+1;
                if (py+rh > th)
                    return 0;
            }
            // Init and add row
            rows->push_back( sth_row(0,py,rh) );
            br = &rows->back();
        }

        // Init glyph.
        sth_glyph glyph;

        glyph.codepoint = codepoint;
        glyph.size = isize;
        glyph.x0 = br->x;
        glyph.y0 = br->y;
        glyph.x1 = glyph.x0+gw;
        glyph.y1 = glyph.y0+gh;
        glyph.xadv = scale * advance;
        glyph.xoff = (float)x0;
        glyph.yoff = (float)y0;

//                glyphs[ key ] = glyphs[ key ];
        glyphs[ key ] = glyph;

        // Advance row location.
        br->x += gw+1;

        // Rasterize
        std::vector< unsigned char > bmp(gw*gh);
        if( bmp.size() )
        {
            stbtt_MakeGlyphBitmap(&font, &bmp[0], gw,gh,gw, scale,scale, g);

            // Update texture
            glBindTexture(GL_TEXTURE_2D, tex);
            glPixelStorei(GL_UNPACK_ALIGNMENT,1);
            glTexSubImage2D(GL_TEXTURE_2D, 0, glyph.x0,glyph.y0, gw,gh, GL_ALPHA,GL_UNSIGNED_BYTE,&bmp[0]);
        }

        return &glyphs[ key ];
    }