⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gui_ttf.c

📁 ucgui的矢量字库支持包,可以象windows一样使用truetype的字库了
💻 C
📖 第 1 页 / 共 2 页
字号:
*
*       _DispLine
*/
static void _DispLine(const char GUI_UNI_PTR * s, int Len) {
  U16 Char;
  int OldMode;
  /* Clear if not transparency mode has been selected */
  if (!(GUI_Context.TextMode & (LCD_DRAWMODE_TRANS | LCD_DRAWMODE_XOR))) {
    _ClearLine(s, Len);
  }
  /* Draw characters always transparent */
  OldMode = GUI_Context.TextMode;
  GUI_Context.DrawMode |= GUI_DM_TRANS;
  while (--Len >= 0) {
    Char = GUI_UC__GetCharCodeInc(&s);
    GUI_Context.pAFont->pfDispChar(Char);
  }
  GUI_Context.DrawMode = OldMode;
}

/*********************************************************************
*
*       _GetCharDistX
*/
static int _GetCharDistX(U16P c) {
  int xDist;
  xDist = _RequestGlyph(c, 0);
  return (xDist >= 0) ? xDist : 0;
}

/*********************************************************************
*
*       _GetFontInfo
*/
static void _GetFontInfo(const GUI_FONT GUI_UNI_PTR * pFont, GUI_FONTINFO * pfi) {
  pfi->Baseline = pFont->Baseline;
  pfi->LHeight  = pFont->LHeight;
  pfi->CHeight  = pFont->CHeight;
  pfi->Flags    = GUI_FONTINFO_FLAG_PROP;
}

/*********************************************************************
*
*       _IsInFont
*/
static char _IsInFont(const GUI_FONT GUI_UNI_PTR * pFont, U16 c) {
  FTC_FaceID face_id;
  FT_Face    face;
  FT_UInt    glyph_index;
  /* Get object pointer */
  face_id    = (FTC_FaceID)pFont->p.pFontData;
  /* Request face object from cache */
  if (FTC_Manager_LookupFace(_FTContext.cache_manager, face_id, &face)) {
    return 1;
  }
  glyph_index = FTC_CMapCache_Lookup(_FTContext.cmap_cache, face_id, 0, c);
  return glyph_index ? 1 : 0;
}

/*********************************************************************
*
*       _GetName
*/
static int _GetName(GUI_FONT * pFont, char * pBuffer, int NumBytes, int Index) {
  FTC_FaceID   face_id;
  FT_Face      face;
  int          Len;
  const char * pName;
  /* Get object pointer */
  face_id = (FTC_FaceID)((GUI_TTF_CS *)pFont->p.pFontData)->pTTF;
  /* Request face object from cache */
  if (FTC_Manager_LookupFace(_FTContext.cache_manager, face_id, &face)) {
    return 1;
  }
  switch (Index) {
  case FAMILY_NAME:
    pName = face->family_name;
    break;
  case STYLE_NAME:
    pName = face->style_name;
    break;
  }
  Len = strlen(pName);
  if (Len >= NumBytes) {
    Len = NumBytes - 1;
  }
  strncpy(pBuffer, pName, Len);
  *(pBuffer + Len) = 0;
  return 0;
}

/*********************************************************************
*
*       _APIList
*/
static const tGUI_ENC_APIList _APIList = {
  NULL,
  NULL,
  _DispLine
};

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       GUI_TTF_CreateFont
*
* Purpose:
*   Creates a new GUI_FONT object by a given TTF font file available in memory.
*
* Parameters:
*   pFont      - Pointer to a GUI_FONT structure to be initialized by this function.
*   pCS        - Pointer to a GUI_TTF_CS structure which contains information about 
*                file location, file size, pixel size and face index.
*/
int GUI_TTF_CreateFont(GUI_FONT * pFont, GUI_TTF_CS * pCS) {
  FTC_ImageTypeRec * pImageType;
  FTC_FaceID         face_id;
  FTC_ScalerRec      scaler;
  FT_Face            face;
  FT_Size            size;
  FT_GlyphSlot       slot;
  FT_Glyph           glyph;
  FT_UInt            glyph_index;
  /* Check initialization */
  _CheckInit();
  /* face_id is nothing but the address of the font file */
  face_id = (FTC_FaceID)pCS->pTTF;
  /* Request face object from cache */
  if (FTC_Manager_LookupFace(_FTContext.cache_manager, face_id, &face)) {
    return 1;
  }
  /* Set image type attributes */
  pImageType          = (FTC_ImageTypeRec *)pCS->aImageTypeBuffer;
  pImageType->face_id = face_id;
  pImageType->width   = 0;
  pImageType->height  = pCS->PixelHeight;
  pImageType->flags   = FT_LOAD_MONOCHROME;
  /* Request size object from cache */
  scaler.face_id = face_id;
  scaler.width   = pImageType->width;
  scaler.height  = pImageType->height;
  scaler.pixel   = 1;
  if (FTC_Manager_LookupSize(_FTContext.cache_manager, &scaler, &size)) {
    return 1;
  }
  /* Magnification is always 1 */
  pFont->XMag = 1;
  pFont->YMag = 1;
  /* Set function pointers */
  pFont->p.pFontData    = pCS;
  pFont->pfDispChar     = _DispChar;
  pFont->pfGetCharDistX = _GetCharDistX;
  pFont->pfGetFontInfo  = _GetFontInfo;
  pFont->pfIsInFont     = _IsInFont;
  pFont->pafEncode      = &_APIList;
  /* Calculate baseline and vertical size */
  pFont->Baseline = face->size->metrics.ascender >> 6;
  pFont->YSize    = pFont->Baseline - (face->size->metrics.descender >> 6);
  pFont->YDist    = pFont->YSize;
  slot            = face->glyph;
  /* Calculate lowercase height */
  glyph_index = FTC_CMapCache_Lookup(_FTContext.cmap_cache, face_id, 0, 'g');
  if (FTC_ImageCache_Lookup(_FTContext.image_cache, pImageType, glyph_index, &glyph, NULL)) {
    return 1;
  }
  pFont->LHeight = slot->metrics.horiBearingY >> 6;
  /* Calculate capital height */
  glyph_index = FTC_CMapCache_Lookup(_FTContext.cmap_cache, face_id, 0, 'M');
  if (FTC_ImageCache_Lookup(_FTContext.image_cache, pImageType, glyph_index, &glyph, NULL)) {
    return 1;
  }
  pFont->CHeight = slot->metrics.height >> 6;
  /* Select the currently created font */
  GUI_SetFont(pFont);
  return 0;
}

/*********************************************************************
*
*       GUI_TTF_GetFamilyName
*/
int GUI_TTF_GetFamilyName(GUI_FONT * pFont, char * pBuffer, int NumBytes) {
  return _GetName(pFont, pBuffer, NumBytes, FAMILY_NAME);
}

/*********************************************************************
*
*       GUI_TTF_GetStyleName
*/
int GUI_TTF_GetStyleName(GUI_FONT * pFont, char * pBuffer, int NumBytes) {
  return _GetName(pFont, pBuffer, NumBytes, STYLE_NAME);
}

/*********************************************************************
*
*       GUI_TTF_SetCacheSize
*
* Purpose:
*   Sets the maximum number of font faces, size objects and bytes used by the cache
*/
void GUI_TTF_SetCacheSize(unsigned MaxFaces, unsigned MaxSizes, U32 MaxBytes) {
  _FTCacheSize.MaxFaces = MaxFaces;
  _FTCacheSize.MaxSizes = MaxSizes;
  _FTCacheSize.MaxBytes = MaxBytes;
}

/*********************************************************************
*
*       GUI_TTF_DestroyCache
*
* Purpose:
*   Destroys the FreeType font cache after emptying it.
*/
void GUI_TTF_DestroyCache(void) {
  if (_FTContext.cache_manager) {
    FTC_Manager_Done(_FTContext.cache_manager);
    _FTContext.cache_manager = 0;
  }
}

/*********************************************************************
*
*       GUI_TTF_Done
*/
void GUI_TTF_Done(void) {
  GUI_TTF_DestroyCache();
  if (_FTContext.library) {
    FT_Done_Library(_FTContext.library);
    _FTContext.library = 0;
  }
}

#else

void GUI_TTF_C(void);
void GUI_TTF_C(void) {} /* Avoid empty object files */

#endif

/*************************** End of file ****************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -