示例#1
0
FTFace::FTFace(const unsigned char *pBufferBytes, size_t bufferSizeInBytes,
               bool precomputeKerning)
:   numGlyphs(0),
    fontEncodingList(0),
    kerningCache(0),
    err(0)
{
    const FT_Long DEFAULT_FACE_INDEX = 0;
    ftFace = new FT_Face;

    err = FT_New_Memory_Face(*FTLibrary::Instance().GetLibrary(),
                             (FT_Byte const *)pBufferBytes, (FT_Long)bufferSizeInBytes,
                             DEFAULT_FACE_INDEX, ftFace);
    if(err)
    {
        delete ftFace;
        ftFace = 0;
        return;
    }

    numGlyphs = (*ftFace)->num_glyphs;
    hasKerningTable = (FT_HAS_KERNING((*ftFace)) != 0);

    if(hasKerningTable && precomputeKerning)
    {
        BuildKerningCache();
    }
}
示例#2
0
文件: Font.cpp 项目: 42bottles/SFML
float Font::getKerning(Uint32 first, Uint32 second, unsigned int characterSize) const
{
    // Special case where first or second is 0 (null character)
    if (first == 0 || second == 0)
        return 0.f;

    FT_Face face = static_cast<FT_Face>(m_face);

    if (face && FT_HAS_KERNING(face) && setCurrentSize(characterSize))
    {
        // Convert the characters to indices
        FT_UInt index1 = FT_Get_Char_Index(face, first);
        FT_UInt index2 = FT_Get_Char_Index(face, second);

        // Get the kerning vector
        FT_Vector kerning;
        FT_Get_Kerning(face, index1, index2, FT_KERNING_DEFAULT, &kerning);

        // X advance is already in pixels for bitmap fonts
        if (!FT_IS_SCALABLE(face))
            return static_cast<float>(kerning.x);

        // Return the X advance
        return static_cast<float>(kerning.x) / static_cast<float>(1 << 6);
    }
    else
    {
        // Invalid font, or no kerning
        return 0.f;
    }
}
示例#3
0
int  FontFreeType::getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar) const
{
    if (!_fontRef)
        return 0;

    bool hasKerning = FT_HAS_KERNING( _fontRef ) != 0;
    
    if (!hasKerning)
        return 0;
    
    // get the ID to the char we need
    int glyphIndex1 = FT_Get_Char_Index(_fontRef, firstChar);
    
    if (!glyphIndex1)
        return 0;
    
    // get the ID to the char we need
    int glyphIndex2 = FT_Get_Char_Index(_fontRef, secondChar);
    
    if (!glyphIndex2)
        return 0;
    
    FT_Vector kerning;
    
    if (FT_Get_Kerning( _fontRef, glyphIndex1, glyphIndex2,  FT_KERNING_DEFAULT,  &kerning))
        return 0;
    
    return (kerning.x >> 6);
}
示例#4
0
osg::Vec2 FreeTypeFont::getKerning(unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType kerningType)
{
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(FreeTypeLibrary::instance()->getMutex());

    if (!FT_HAS_KERNING(_face) || (kerningType == osgText::KERNING_NONE)) return osg::Vec2(0.0f,0.0f);

    FT_Kerning_Mode mode = (kerningType==osgText::KERNING_DEFAULT) ? ft_kerning_default : ft_kerning_unfitted;

    // convert character code to glyph index
    FT_UInt left = FT_Get_Char_Index( _face, leftcharcode );
    FT_UInt right = FT_Get_Char_Index( _face, rightcharcode );

    // get the kerning distances.
    FT_Vector  kerning;

    FT_Error error = FT_Get_Kerning( _face,                     // handle to face object
                                     left,                      // left glyph index
                                     right,                     // right glyph index
                                     mode,                      // kerning mode
                                     &kerning );                // target vector

    if (error)
    {
        OSG_WARN << "FT_Get_Kerning(...) returned error code " <<std::hex<<error<<std::dec<< std::endl;
        return osg::Vec2(0.0f,0.0f);
    }

    float coord_scale = getCoordScale();

    return osg::Vec2((float)kerning.x*coord_scale,(float)kerning.y*coord_scale);
}
示例#5
0
FTFace::FTFace(const char* fontFilePath, bool precomputeKerning)
:   numGlyphs(0),
    fontEncodingList(0),
    kerningCache(0),
    err(0)
{
    const FT_Long DEFAULT_FACE_INDEX = 0;
    ftFace = new FT_Face;

    err = FT_New_Face(*FTLibrary::Instance().GetLibrary(), fontFilePath,
                      DEFAULT_FACE_INDEX, ftFace);

    if(err)
    {
        delete ftFace;
        ftFace = 0;
        return;
    }

    numGlyphs = (*ftFace)->num_glyphs;
    hasKerningTable = (FT_HAS_KERNING((*ftFace)) != 0);

    if(hasKerningTable && precomputeKerning)
    {
        BuildKerningCache();
    }
}
示例#6
0
文件: Font.cpp 项目: Df458/DFEngine
void Font::draw(IScene* scene, const char* text, glm::mat4 transform, float font_size, glm::vec3 color)
{
	FT_Set_Pixel_Sizes(m_font_face, font_size, font_size);
    glUseProgram(TEXT_PROGRAM);
    checkGLError();

    glm::vec3 pen(0, 0, 0);
    char prev = 0;
    bool kern = FT_HAS_KERNING(m_font_face);
    for(const char* i = text; i[0]; ++i) {
        FT_UInt index = FT_Get_Char_Index(m_font_face, i[0]);
        Glyph glyph = renderGlyph(i[0], font_size);

        if(prev && kern && i) {
            FT_Vector delta;
            FT_Get_Kerning(m_font_face, prev, index, FT_KERNING_DEFAULT, &delta);
            pen.x += delta.x * scene->getDPU();
            //fprintf(stderr, "%ld\n", delta.x);
        }

        if(i[0] == '\n') {
            pen.x = 0;
            pen.y += font_size;
            prev = 0;
            continue;
        } else if(i[0] == ' ' || glyph.id == 0) {
            pen.x += font_size;
            prev = 0;
            continue;
        }

        glUniform3f(m_color_uniform, color.x, color.y, color.z);
        glm::vec3 offset(glyph.bearing.x, -glyph.bearing.y, 0.0f);

        glm::mat4 mvp = scene->getActiveProjectionMatrix() *
                        scene->getActiveViewMatrix() *
                        transform *
                        glm::translate(glm::mat4(1), -(pen + offset) * scene->getDPU()) *
                        glm::scale(glm::mat4(1), glm::vec3(glyph.dimensions.x * scene->getDPU(), glyph.dimensions.y * scene->getDPU(), 1.0f));
        //fprintf(stderr, "Result: %f, %f, %f, %f\n", glyph.dimensions.x, glyph.dimensions.y, glyph.dimensions.x * scene->getDPU(), glyph.dimensions.y * scene->getDPU());
        glUniformMatrix4fv(m_transform_uniform, 1, GL_FALSE, &mvp[0][0]);
        glEnableVertexAttribArray(m_vertex_position);
        glBindBuffer(GL_ARRAY_BUFFER, QUAD_BUFFER);
        glVertexAttribPointer(m_vertex_position, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, glyph.texture);
        glUniform1i(m_texture_uniform, 0);
        checkGLError();

        glDrawArrays(GL_TRIANGLES, 0, 6);
        checkGLError();

        glDisableVertexAttribArray(m_vertex_position);
        pen.x += glyph.advance;
        //fprintf(stderr, "(%d)\n", (int)glyph.advance);
        prev = index;
    }
}
//http://www.freetype.org/freetype2/docs/tutorial/step2.html
FT_Error measure_string(FT_Face face, std::string text, int size, Vector2i* size_out)
{
   int pos_x = 0;
   bool use_kerning = FT_HAS_KERNING(face) ? true : false;
   FT_UInt prev_glyph_index = 0;

   FT_BBox text_bb;
   text_bb.xMax = 0;
   text_bb.xMin = 0;
   text_bb.yMax = 0;
   text_bb.yMin = 0;
   FT_Error error;

   error = FT_Set_Char_Size(face, 0, size * 64, 72, 72);

   for(unsigned int i = 0; i < text.length(); i++)
   {
      FT_UInt glyph_index = FT_Get_Char_Index(face, text.c_str()[i]);

      if(use_kerning && prev_glyph_index)
      {
         FT_Vector delta;
         FT_Get_Kerning(face, prev_glyph_index, glyph_index, FT_KERNING_DEFAULT, &delta);
         pos_x += delta.x >> 6;
      }
      prev_glyph_index = glyph_index;

      if(error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT) != FT_Err_Ok)
      {
         Log::Error(__FILE__, "Unable to load glyph %d", glyph_index);
         return error;
      }

      FT_Glyph glyph;
      if(error = FT_Get_Glyph(face->glyph, &glyph) != FT_Err_Ok)
      {
         Log::Error(__FILE__, "Unable to get glyph %d", glyph_index);
      }

      FT_BBox bb;
      FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_pixels, &bb);
      bb.xMax += pos_x;
      bb.xMin += pos_x;


      pos_x += glyph->advance.x >> 16;

      //Grow overall bounding box
      if(bb.xMax > text_bb.xMax)
         text_bb.xMax = bb.xMax;
      if(bb.yMax > text_bb.yMax)
         text_bb.yMax = bb.yMax;
      if(bb.xMin < text_bb.xMin)
         text_bb.xMin = bb.xMin;
      if(bb.yMin < text_bb.yMin)
         text_bb.yMin = bb.yMin;

      FT_Done_Glyph(glyph);
   }
示例#8
0
/**
 * Default constructor for the FreeTypeGX class.
 *
 * @param textureFormat	Optional format (GX_TF_*) of the texture as defined by the libogc gx.h header file. If not specified default value is GX_TF_RGBA8.
 * @param vertexIndex	Optional vertex format index (GX_VTXFMT*) of the glyph textures as defined by the libogc gx.h header file. If not specified default value is GX_VTXFMT1.
 */
FreeTypeGX::FreeTypeGX(FT_UInt pixelSize, uint8_t textureFormat, uint8_t vertexIndex)
{
	this->textureFormat = textureFormat;
	this->setVertexFormat(vertexIndex);
	this->setCompatibilityMode(FTGX_COMPATIBILITY_DEFAULT_TEVOP_GX_PASSCLR | FTGX_COMPATIBILITY_DEFAULT_VTXDESC_GX_NONE);
	this->ftPointSize = pixelSize;
	this->ftKerningEnabled = FT_HAS_KERNING(ftFace);
}
//-----------------------------------------------------------
int ofTrueTypeFont::getKerning(uint32_t c, uint32_t prevC) const{
    if(FT_HAS_KERNING( face )){
        FT_Vector kerning;
        FT_Get_Kerning(face.get(), FT_Get_Char_Index(face.get(), c), FT_Get_Char_Index(face.get(), prevC), FT_KERNING_UNFITTED, &kerning);
        return kerning.x * fontUnitScale;
    }else{
        return 0;
    }
}
JNIEXPORT jboolean JNICALL Java_com_badlogic_gdx_graphics_g2d_freetype_FreeType_hasKerning(JNIEnv* env, jclass clazz, jlong face) {


//@line:656

   	return FT_HAS_KERNING(((FT_Face)face));
   

}
示例#11
0
/**
 * Default constructor for the FreeTypeGX class.
 *
 * @param vertexIndex	Optional vertex format index (GX_VTXFMT*) of the glyph textures as defined by the libogc gx.h header file. If not specified default value is GX_VTXFMT1.
 */
FreeTypeGX::FreeTypeGX(FT_UInt pixelSize, uint8_t vertexIndex) {
        this->setVertexFormat(vertexIndex);
        this->setCompatibilityMode(FTGX_COMPATIBILITY_DEFAULT_TEVOP_GX_PASSCLR | FTGX_COMPATIBILITY_DEFAULT_VTXDESC_GX_NONE);
        this->ftPointSize = pixelSize;
        this->ftKerningEnabled = FT_HAS_KERNING(ftFace);

        if (glyphData == NULL)
                glyphData = (uint8_t *) malloc(256 * 256 * 4);
}
示例#12
0
/**
 * Enables or disables kerning of the output text.
 *
 * This routine enables or disables kerning of the output text only if kerning is supported by the font.
 * Note that by default kerning is enabled if it is supported by the font.
 *
 * @param enabled       The enabled state of the font kerning.
 * @return The resultant enabled state of the font kerning.
 */
bool FreeTypeGX::setKerningEnabled(bool enabled) {
        if(!enabled) {
                return this->ftKerningEnabled = false;
        }

        if(FT_HAS_KERNING(this->ftFace)) {
                return this->ftKerningEnabled = true;
        }

        return false;
}
示例#13
0
文件: Font.cpp 项目: roman5566/NoRSX
Font::Font(u32 Size, const char *Font_Path, Minimum *min){
	FontColor = COLOR_BLACK;
	FontSize = Size;
	Lenght = 0;
	m = min;
	FT_Init_FreeType(&library);
	FT_New_Face(library,Font_Path,0,&face);
	FT_Stroker_New(library,&stroker);
	Kerning = FT_HAS_KERNING(face);
	FT_Set_Pixel_Sizes(face,0,FontSize);
	font=0;
}
void gfx_font_adapter::add_kerning(unsigned int first, unsigned int second, scalar* x, scalar* y)
{
    if (m_impl->font && first && second && FT_HAS_KERNING(m_impl->font)) {
        FT_Vector delta;
        FT_Get_Kerning(m_impl->font, first, second, FT_KERNING_DEFAULT, &delta);
        scalar dx = int26p6_to_flt(delta.x);
        scalar dy = int26p6_to_flt(delta.y);
        if (m_impl->font->glyph->format != FT_GLYPH_FORMAT_BITMAP)
            m_impl->matrix.transform_2x2(&dx, &dy);
        *x += dx;
        *y += dy;
    }
}
示例#15
0
static void glyphstring_create(FT_Face face, Text *text, FT_Glyph *glyph_string,
		FT_Vector *pos)
{
	const uint8_t *string = text->string;
	FT_Bool has_kerning;
	FT_UInt glyph_index, previous;
	FT_Vector pen, delta;
	uint32_t charcode;
	int i;

	has_kerning = FT_HAS_KERNING(face);
	previous = 0;
	i = 0;
	pen.x = pen.y = 0;
	while (string[0] != '\0')
	{
		charcode = utf8_next(&string);
		glyph_index = FT_Get_Char_Index(face, charcode);
		if (has_kerning && previous && glyph_index)
			FT_Get_Kerning(face, previous, glyph_index, FT_KERNING_DEFAULT,
					&delta);
		else
			delta.x = 0;

		if (glyph_index == 0)
			log_err("Glyph for character U+%X missing\n", charcode);

		if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER) != 0)
		{
			log_err("Error loading glyph for character U+%X\n", charcode);
			continue;
		}
		if (FT_Get_Glyph(face->glyph, &glyph_string[i]) != 0)
		{
			log_err("Error copying glyph for character U+%X\n", charcode);
			continue;
		}

		pen.x += delta.x;

		pos[i] = pen;

		pen.x += face->glyph->advance.x;
		pen.y += face->glyph->advance.y;

		previous = glyph_index;
		i++;
	}

	text->num_glyphs = i;
}
示例#16
0
ts::Vector2i ts::resources::impl::Font_face::kerning(utf8::uint32_t first, utf8::uint32_t second, std::uint32_t character_size)
{
    if (first && second && FT_HAS_KERNING(face))
    {
        set_character_size(character_size);

        auto first_index = FT_Get_Char_Index(face, first);
        auto second_index = FT_Get_Char_Index(face, second);

        FT_Vector kerning;
        FT_Get_Kerning(face, first_index, second_index, FT_KERNING_DEFAULT, &kerning);
        
        return Vector2i(kerning.x >> 6, kerning.y >> 6);            
    }
示例#17
0
void 
FT2Font::load_glyphs() {
  _VERBOSE("FT2Font::load_glyphs");
  
  /* a small shortcut */ 
  
  FT_Bool use_kerning = FT_HAS_KERNING( face ); 
  FT_UInt previous = 0; 
  
  glyphs.resize(0);
  pen.x = 0;
  pen.y = 0;
  
  for ( unsigned int n = 0; n < text.size(); n++ ) { 
    FT_UInt glyph_index = FT_Get_Char_Index( face, text[n] );
    /* retrieve kerning distance and move pen position */ 
    if ( use_kerning && previous && glyph_index ) { 
      FT_Vector delta;
      FT_Get_Kerning( face, previous, glyph_index,
		      ft_kerning_default, &delta );
      pen.x += delta.x;
    }
    error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT ); 
    if ( error ) {
      std::cerr << "\tcould not load glyph for " << text[n] << std::endl;
      continue; 
    }
    /* ignore errors, jump to next glyph */ 
    
    /* extract glyph image and store it in our table */

    FT_Glyph thisGlyph; 
    error = FT_Get_Glyph( face->glyph, &thisGlyph ); 

    if ( error ) {
      std::cerr << "\tcould not get glyph for " << text[n] << std::endl;
      continue; 
    }
    /* ignore errors, jump to next glyph */ 

    FT_Glyph_Transform( thisGlyph, 0, &pen);
    pen.x += face->glyph->advance.x;
    
    previous = glyph_index; 
    glyphs.push_back(thisGlyph);
  }
  // now apply the rotation
  for (unsigned int n=0; n<glyphs.size(); n++) 
    FT_Glyph_Transform(glyphs[n], &matrix, 0);
}
示例#18
0
文件: Font.cpp 项目: roman5566/NoRSX
Font::Font(const void *MemFont, u32 MemFont_size, Minimum *min){
	FontColor = COLOR_BLACK;
	FontSize = DEFAULT_FONT_SIZE;
	Pointer = (FT_Byte*)MemFont;
	Lenght = MemFont_size;
	m = min;

	FT_Init_FreeType(&library);
	FT_New_Memory_Face(library,Pointer,Lenght,0,&face);
	FT_Stroker_New(library,&stroker);
	Kerning = FT_HAS_KERNING(face);
	FT_Set_Pixel_Sizes(face,0,FontSize);
	
	font=0;
}
示例#19
0
FTFace::FTFace (const char* fontFilePath)
	: numGlyphs(0), fontEncodingList(0), err(0) {
	const FT_Long DEFAULT_FACE_INDEX = 0;
	ftFace = new FT_Face;

	err = FT_New_Face (*FTLibrary::Instance().GetLibrary(), fontFilePath, DEFAULT_FACE_INDEX, ftFace);
	if (err) {
		Message ("error FT_New_Face");
		delete ftFace;
		ftFace = 0;
	} else {
		numGlyphs = (*ftFace)->num_glyphs;
		hasKerningTable = FT_HAS_KERNING((*ftFace)) != 0;
	}
}
示例#20
0
/* load a glyph into the slot and compute bearing */
static FT_Error set_glyph(FT_Face face, FT_UInt codepoint, FT_UInt *previous,
                          FT_Vector *pen, FT_Bool vertical, FT_Matrix *rotation,
                          FT_Vector *bearing, FT_Int halign) {
  FT_Error error;
  FT_UInt glyph_index;

  glyph_index = FT_Get_Char_Index(face, codepoint);
  if (FT_HAS_KERNING(face) && *previous && !vertical && glyph_index) {
    FT_Vector delta;
    FT_Get_Kerning(face, *previous, glyph_index, FT_KERNING_DEFAULT,
                   &delta);
    FT_Vector_Transform(&delta, rotation);
    pen->x += delta.x;
    pen->y += delta.y;
  }
  error = FT_Load_Glyph(face, glyph_index, vertical ?
                        FT_LOAD_VERTICAL_LAYOUT : FT_LOAD_DEFAULT);
  if (error) {
    gks_perror("glyph could not be loaded: %c", codepoint);
    return 1;
  }
  error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
  if (error) {
    gks_perror("glyph could not be rendered: %c", codepoint);
    return 1;
  }
  *previous = glyph_index;

  bearing->x = face->glyph->metrics.horiBearingX;
  bearing->y = 0;
  if (vertical) {
    if (halign == GKS_K_TEXT_HALIGN_RIGHT) {
      bearing->x += face->glyph->metrics.width;
    } else if (halign == GKS_K_TEXT_HALIGN_CENTER) {
      bearing->x += face->glyph->metrics.width / 2;
    }
    if (bearing->x != 0) FT_Vector_Transform(bearing, rotation);
    bearing->x = 64 * face->glyph->bitmap_left - bearing->x;
    bearing->y = 64 * face->glyph->bitmap_top  - bearing->y;
  } else {
    if (bearing->x != 0) FT_Vector_Transform(bearing, rotation);
    pen->x -= bearing->x;
    pen->y -= bearing->y;
    bearing->x = 64 * face->glyph->bitmap_left;
    bearing->y = 64 * face->glyph->bitmap_top;
  }
  return 0;
}
示例#21
0
/**
 * Loads and processes a specified true type font buffer to a specific point size.
 * 
 * This routine takes a precompiled true type font buffer and loads the necessary processed data into memory. This routine should be called before drawText will succeed. 
 * 
 * @param fontBuffer	A pointer in memory to a precompiled true type font buffer.
 * @param bufferSize	Size of the true type font buffer in bytes.
 * @param pointSize	The desired point size this wrapper's configured font face.
 * @param cacheAll	Optional flag to specify if all font characters should be cached when the class object is created. If specified as false the characters only become cached the first time they are used. If not specified default value is false.
 */
uint16_t FreeTypeGX::loadFont(uint8_t* fontBuffer, FT_Long bufferSize, FT_UInt pointSize, bool cacheAll) {
	this->unloadFont();
	this->ftPointSize = pointSize;
	
	FT_New_Memory_Face(this->ftLibrary, (FT_Byte *)fontBuffer, bufferSize, 0, &this->ftFace);
	FT_Set_Pixel_Sizes(this->ftFace, 0, this->ftPointSize);

	this->ftSlot = this->ftFace->glyph;
	this->ftKerningEnabled = FT_HAS_KERNING(this->ftFace);
	
	if (cacheAll) {
		return this->cacheGlyphDataComplete();
	}
	
	return 0;
}
示例#22
0
文件: say_font.c 项目: Spooner/ray
size_t say_font_get_kerning(say_font *font, uint32_t first, uint32_t second,
                            size_t size) {
  if (first == 0 || second == 0)
    return 0;

  if (font->face && FT_HAS_KERNING(font->face) &&
      say_font_set_size(font, size)) {
    size_t first_index = FT_Get_Char_Index(font->face, first);
    size_t sec_index   = FT_Get_Char_Index(font->face, second);

    FT_Vector kerning;
    FT_Get_Kerning(font->face, first_index, sec_index, FT_KERNING_DEFAULT,
                   &kerning);

    return kerning.x >> 6;
  }
void rtText::ConvertStringToTex(std::wstring str, unsigned char *&texMemory, Rec &texBoundingBox) {

	FT_GlyphSlot  slot = fFontFace->glyph;  /* a small shortcut */
	FT_UInt       glyph_index;
	FT_Bool       use_kerning = FT_HAS_KERNING(fFontFace);
	FT_UInt       previous;
	int           pen_x, pen_y;
	FT_Vector     delta;
	int           baseY;
	bool          error = false;

	// todo: 加入转义解析

	// 第一趟:确定包围盒大小
	pen_x = pen_y = 0; //任意基点
	previous = 0;
	int xMin, xMax, yMin, yMax;
	xMin = xMax = yMin = yMax = 0;
	for (int n = 0; n < str.length(); n++) {
		/* convert character code to glyph index */
		glyph_index = FT_Get_Char_Index(fFontFace, str[n]);
		/* retrieve kerning distance and move pen position */
		if (use_kerning && previous && glyph_index) {
			FT_Get_Kerning(fFontFace, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
			pen_x += delta.x >> 6;
		}
		/* load glyph image into the slot (erase previous one) */
		error = FT_Load_Glyph(fFontFace, glyph_index, FT_LOAD_RENDER);
		if (error) continue;  /* ignore errors */

		// a single glyph bouncing box is given by the following:
		// xMin = pen_x + slot->bitmap_left, yMax = pen_y + slot->bitmap_top;
		// xMax = xMin + slot->bitmap.width, yMin = yMax - slot->bitmap.rows;

		// grow overall string bouncing box
		xMin = std::min(pen_x + slot->bitmap_left, xMin); xMax = std::max(pen_x + slot->bitmap_left + slot->bitmap.width, xMax);
		yMin = std::min(pen_y + slot->bitmap_top - slot->bitmap.rows, yMin); yMax = std::max(pen_y + slot->bitmap_top, yMax);

		/* increment pen position */
		pen_x += slot->advance.x >> 6;
		/* record current glyph index */
		previous = glyph_index;
	}
示例#24
0
	void kernAdvance(Font font, unsigned int index1, unsigned int index2, float& x, float& y)
	{
		x=0.0f;
		y=0.0f;

		if (FT_HAS_KERNING(font->ftFace) && index1 && index2)
		{
			FT_Vector kernAdvance;

			kernAdvance.x = kernAdvance.y = 0;

			FT_Error err = FT_Get_Kerning(font->ftFace, index1, index2, ft_kerning_unfitted, &kernAdvance);
			if (!err)
			{   
				x = static_cast<float>(kernAdvance.x)/64.0f;
				y = static_cast<float>(kernAdvance.y)/64.0f;
			}
		}
	}
示例#25
0
/**
 * \brief Get kerning for the pair of glyphs.
 **/
FT_Vector ass_font_get_kerning(ass_font_t* font, uint32_t c1, uint32_t c2)
{
	FT_Vector v = {0, 0};
	int i;

	for (i = 0; i < font->n_faces; ++i) {
		FT_Face face = font->faces[i];
		int i1 = FT_Get_Char_Index(face, c1);
		int i2 = FT_Get_Char_Index(face, c2);
		if (i1 && i2) {
			if (FT_HAS_KERNING(face))
				FT_Get_Kerning(face, i1, i2, FT_KERNING_DEFAULT, &v);
			return v;
		}
		if (i1 || i2) // these glyphs are from different font faces, no kerning information
			return v;
	}
	return v;
}
示例#26
0
文件: Font.cpp 项目: dorgonman/DEP
bool Font::Load(const std::string& Filename)
{

    if (FT_New_Face(mLibrary, Filename.c_str(), 0, &mFace) != 0)
        return false;


    //We only support scalable Unicode fonts
    if (!mFace->charmap || !FT_IS_SCALABLE(mFace))
    {
        FT_Done_Face(mFace);
        return false;
    }
    mFilename = Filename;
    if (FT_HAS_KERNING(mFace))
        mHaveKerning = true;

    return true;
}
示例#27
0
static unsigned int DrawText(const wchar_t *string, unsigned int fontSize, void *buffer) {
	unsigned int error = 0;
	int penX = 0;
	int penY = fontSize;
	FT_GlyphSlot slot = ftFace->glyph;
	FT_UInt glyphIndex = 0;
	FT_UInt previousGlyph = 0;
	FT_Bool hasKerning = FT_HAS_KERNING(ftFace);
	
	/* Convert the string to UTF32 */
	//size_t length = strlen(string);
	const wchar_t *utf32 =  string;//(wchar_t*)malloc(length * sizeof(wchar_t)); 
	size_t length = wcslen(utf32);//mbstowcs(utf32, string, length);
	
	/* Loop over each character, drawing it on to the buffer, until the 
	 * end of the string is reached, or until the pixel width is too wide */
	unsigned int loop = 0;
	for (loop = 0; loop < length; ++loop) {
		glyphIndex = FT_Get_Char_Index(ftFace, utf32[ loop ]);
		
		/* To the best of my knowledge, none of the other freetype 
		 * implementations use kerning, so my method ends up looking
		 * slightly better :) */
		if (hasKerning && previousGlyph && glyphIndex) {
			FT_Vector delta;
			FT_Get_Kerning(ftFace, previousGlyph, glyphIndex, FT_KERNING_DEFAULT, &delta);
			
			penX += delta.x >> 6;
		}
		
		error = FT_Load_Glyph(ftFace, glyphIndex, FT_LOAD_RENDER);
		if (error) {
			/* Whoops, something went wrong trying to load the glyph 
			 * for this character... you should handle this better */
			continue;
		}
		
		if (BlitGlyph(&slot->bitmap, penX + slot->bitmap_left, penY - slot->bitmap_top, buffer) == true) {
			/* The glyph was successfully blitted to the buffer, move the pen forwards */
			penX += slot->advance.x >> 6;
			previousGlyph = glyphIndex;
		} else {
示例#28
0
文件: FTFace.cpp 项目: CJFocke/vsxu
FTPoint FTFace::KernAdvance( unsigned int index1, unsigned int index2)
{
    float x, y;
    x = y = 0.0f;

    if( FT_HAS_KERNING((*ftFace)) && index1 && index2)
    {
        FT_Vector kernAdvance;
        kernAdvance.x = kernAdvance.y = 0;

        err = FT_Get_Kerning( *ftFace, index1, index2, ft_kerning_unfitted, &kernAdvance);
        if( !err)
        {   
            x = static_cast<float>( kernAdvance.x) / 64.0f;
            y = static_cast<float>( kernAdvance.y) / 64.0f;
        }
    }
    
    return FTPoint( x, y, 0.0);
}
示例#29
0
文件: Font.cpp 项目: vidjogamer/SFML
int Font::GetKerning(Uint32 first, Uint32 second, unsigned int characterSize) const
{
    // Special case where first or second is 0 (null character)
    if (first == 0 || second == 0)
        return 0;

    FT_Face face = static_cast<FT_Face>(myFace);

    if (face && FT_HAS_KERNING(face) && SetCurrentSize(characterSize))
    {
        // Convert the characters to indices
        FT_UInt index1 = FT_Get_Char_Index(face, first);
        FT_UInt index2 = FT_Get_Char_Index(face, second);

        // Get the kerning vector
        FT_Vector kerning;
        FT_Get_Kerning(face, index1, index2, FT_KERNING_DEFAULT, &kerning);

        // Return the X advance
        return kerning.x >> 6;
    }
示例#30
0
/**
 * Loads and processes a specified true type font buffer to a specific point size.
 * 
 * This routine takes a precompiled true type font buffer and loads the necessary processed data into memory. This routine should be called before drawText will succeed. 
 * 
 * @param fontBuffer    A pointer in memory to a precompiled true type font buffer.
 * @param bufferSize    Size of the true type font buffer in bytes.
 * @param pointSize     The desired point size this wrapper's configured font face.
 * @param cacheAll      Optional flag to specify if all font characters should be cached when the class object is created. If specified as false the characters only become cached the first time they are used. If not specified default value is false.
 */
uint16_t FreeTypeGX::loadFont(uint8_t* fontBuffer, FT_Long bufferSize, FT_UInt pointSize, bool cacheAll) {
        uint16_t numCached = 0;

        this->unloadFont();
        this->ftFontBuffer = (FT_Byte *)fontBuffer;
        this->ftFontBufferSize = bufferSize;
        this->ftPointSize = pointSize;

        FT_New_Memory_Face(this->ftLibrary, this->ftFontBuffer, this->ftFontBufferSize, 0, &this->ftFace);
        FT_Set_Pixel_Sizes(this->ftFace, 0, this->ftPointSize);

        this->ftKerningEnabled = FT_HAS_KERNING(this->ftFace);
        this->ftAscender = this->ftPointSize * this->ftFace->ascender / this->ftFace->units_per_EM;
        this->ftDescender = this->ftPointSize * this->ftFace->descender / this->ftFace->units_per_EM;

        if (cacheAll) {
                numCached = this->cacheGlyphDataComplete();
        }
        
        return numCached;
}