static FT_Error my_face_requester( FTC_FaceID face_id, FT_Library lib, FT_Pointer request_data, FT_Face* aface ) { PFont font = (PFont)face_id; FT_UNUSED( request_data ); if ( font->file_address != NULL ) error = FT_New_Memory_Face( lib, (const FT_Byte*)font->file_address, font->file_size, font->face_index, aface ); else error = FT_New_Face( lib, font->filepathname, font->face_index, aface ); if ( !error ) { const char* format = FT_Get_X11_Font_Format( *aface ); if ( !strcmp( format, "Type 1" ) ) { char orig[5]; char* suffix = (char*)strrchr( font->filepathname, '.' ); int has_extension = suffix && ( strcasecmp( suffix, ".pfa" ) == 0 || strcasecmp( suffix, ".pfb" ) == 0 ); if ( has_extension ) memcpy( orig, suffix, 5 ); else /* we have already allocate four more bytes */ suffix = (char*)font->filepathname + strlen( font->filepathname ); memcpy( suffix, ".afm", 5 ); if ( FT_Attach_File( *aface, font->filepathname ) ) { memcpy( suffix, ".pfm", 5 ); FT_Attach_File( *aface, font->filepathname ); } if ( has_extension ) memcpy( suffix, orig, 5 ); else suffix = '\0'; } if ( (*aface)->charmaps ) (*aface)->charmap = (*aface)->charmaps[font->cmap_index]; } return error; }
bool CFontConverter::ToOTF2(std::wstring sFontIn, unsigned int* pSymbols, int nCount, std::wstring sNameW, long nFlag, long lFaceIndex, unsigned char*& pDstData, int& nDstLen) { // функция просто скопирована и немного доработана. это все из-за нехватки времени. FT_Library pLibrary = NULL; if ( FT_Init_FreeType( &pLibrary ) ) return false; FT_Face pFace = NULL; NSFile::CFileBinary oFileBinary; if (!oFileBinary.OpenFile(sFontIn)) return false; FT_Long nFileSize = (FT_Long)oFileBinary.GetFileSize(); BYTE* pBaseAddress = new BYTE[nFileSize]; DWORD dwRead = 0; oFileBinary.ReadFile(pBaseAddress, (DWORD)nFileSize, dwRead); FT_Open_Args oOpenArgs; oOpenArgs.flags = FT_OPEN_MEMORY; oOpenArgs.memory_base = (BYTE*)pBaseAddress; oOpenArgs.memory_size = (FT_Long)nFileSize; NSFontConverter::CFontFileTrueType* pTTF = NSFontConverter::CFontFileTrueType::LoadFromFile( sFontIn.c_str() ); FT_Error oerrr; if ( oerrr = FT_Open_Face( pLibrary, &oOpenArgs, lFaceIndex, &pFace ) ) { FT_Done_FreeType( pLibrary ); RELEASEARRAYOBJECTS(pBaseAddress); return false; } std::string sFontFormat( FT_Get_X11_Font_Format( pFace ) ); // Проверим флаг конвертации и исходный формат шрифта bool bNeedConvert = false; if ( nFlag == NSFontConverter::c_lFromAll || ( "TrueType" == sFontFormat && nFlag & NSFontConverter::c_lFromTT ) || ( "CFF" == sFontFormat && nFlag & NSFontConverter::c_lFromCFF ) || ( "Type 1" == sFontFormat && nFlag & NSFontConverter::c_lFromT1 ) ) bNeedConvert = true; bool bIsGids = (NSFontConverter::c_lFlagsGids & nFlag); NSFontConverter::TCharBuffer* pCharBuffer = NULL; if ( bNeedConvert ) { pCharBuffer = new NSFontConverter::TCharBuffer(100000); // ~100Kb if ( "CFF" == sFontFormat || "Type 1" == sFontFormat ) { NSFontConverter::TCharBuffer oCFF; NSFontConverter::CFontFileType1C *pT1C = NULL; if ( "Type 1" == sFontFormat ) { // Сначала сконвертируем Type1 в CFF NSFontConverter::CFontFileType1* pT1 = NSFontConverter::CFontFileType1::LoadFromFile( sFontIn.c_str() ); pT1->ToCFF( &NSFontConverter::CharBufferWrite, &oCFF ); delete pT1; // Конвертируем CFF в OpenTypeCFF pT1C = NSFontConverter::CFontFileType1C::LoadFromBuffer( oCFF.sBuffer, oCFF.nLen ); } else { // FreeType отдает тип шрифта CFF, в случаях когда файл имеет тип OpenType(CFF). // Если так оно и есть, тогда нам с файлом ничего делать на надо. pT1C = NSFontConverter::CFontFileType1C::LoadFromFile( sFontIn.c_str() ); } if ( pT1C ) { pT1C->ToOpenTypeCFF( &NSFontConverter::CharBufferWrite, pCharBuffer, pFace ); } delete pT1C; } else if ( "TrueType" == sFontFormat && ( pSymbols != NULL || !sNameW.empty() ) ) { NSFontConverter::CFontFileTrueType* pTTF = NSFontConverter::CFontFileTrueType::LoadFromFile( sFontIn.c_str() ); if ( pTTF ) { std::string sName = U_TO_UTF8(sNameW); unsigned char *pUseGlyfs = NULL; long lGlyfsCount = pFace->num_glyphs; if ( pSymbols ) { // Сначала составим список нужных нами GID unsigned int* pUnicode = (unsigned int*)pSymbols; unsigned short* pGIDs = new unsigned short[nCount]; int nCMapIndex = 0; int nSymbolicIndex = NSFontConverter::GetSymbolicCmapIndex(pFace); if (!bIsGids) { for ( int nIndex = 0; nIndex < nCount; nIndex++ ) { pGIDs[nIndex] = NSFontConverter::SetCMapForCharCode( pFace, pUnicode[nIndex], &nCMapIndex ); if ((pGIDs[nIndex] == 0) && (-1 != nSymbolicIndex) && (pUnicode[nIndex] < 0xF000)) { pGIDs[nIndex] = NSFontConverter::SetCMapForCharCode( pFace, pUnicode[nIndex] + 0xF000, &nCMapIndex ); } } } else { for (int i = 0; i < nCount; ++i) pGIDs[i] = (unsigned short)pUnicode[i]; } pUseGlyfs = new unsigned char[lGlyfsCount]; ::memset( pUseGlyfs, 0x00, lGlyfsCount * sizeof(unsigned char) ); pUseGlyfs[0] = 1; // нулевой гид всегда записываем for ( int nGID = 1; nGID < lGlyfsCount; nGID++ ) { if ( 1 != pUseGlyfs[nGID] ) { bool bFound = false; for ( int nIndex = 0; nIndex < nCount; nIndex++ ) { if ( nGID == pGIDs[nIndex] ) { bFound = true; break; } } // Если данный символ составной (CompositeGlyf), тогда мы должны учесть все его дочерные символы (subglyfs) if ( bFound && 0 == FT_Load_Glyph( pFace, nGID, FT_LOAD_NO_SCALE | FT_LOAD_NO_RECURSE ) ) { for ( int nSubIndex = 0; nSubIndex < pFace->glyph->num_subglyphs; nSubIndex++ ) { FT_Int nSubGID; FT_UInt unFlags; FT_Int nArg1; FT_Int nArg2; FT_Matrix oMatrix; FT_Get_SubGlyph_Info( pFace->glyph, nSubIndex, &nSubGID, &unFlags, &nArg1, &nArg2, &oMatrix ); if ( nSubGID < lGlyfsCount ) pUseGlyfs[nSubGID] = 1; } } if ( bFound ) pUseGlyfs[nGID] = 1; } } } pTTF->WriteTTF( &NSFontConverter::CharBufferWrite, pCharBuffer, sName.c_str(), NULL, pUseGlyfs, lGlyfsCount ); } } LONG nSizeFont = pCharBuffer->nLen; nDstLen = (int)nSizeFont; pDstData = new BYTE[nDstLen]; memcpy(pDstData, pCharBuffer->sBuffer, nDstLen); RELEASEOBJECT(pCharBuffer); } else { nDstLen = (int)nFileSize; pDstData = new BYTE[nDstLen]; memcpy(pDstData, pBaseAddress, nDstLen); } FT_Done_Face( pFace ); FT_Done_FreeType( pLibrary ); RELEASEARRAYOBJECTS(pBaseAddress); return true; }
static int doDirectory(const char *dirname_given, int numEncodings, ListPtr encodingsToDo) { char *dirname, *fontscale_name, *filename, *encdir; FILE *fontscale, *encfile; DIR *dirp; struct dirent *entry; FT_Error ftrc; FT_Face face; ListPtr encoding, xlfd, lp; HashTablePtr entries; HashBucketPtr *array; int i, n, found, rc; int isBitmap=0,xl=0; if (exclusionSuffix) xl = strlen (exclusionSuffix); i = strlen(dirname_given); if(i == 0) dirname = dsprintf("./"); else if(dirname_given[i - 1] != '/') dirname = dsprintf("%s/", dirname_given); else dirname = dsprintf("%s", dirname_given); if(dirname == NULL) { perror("dirname"); exit(1); } if (onlyEncodings) goto encodings; entries = makeHashTable(); if(doBitmaps && !doScalable) { readFontScale(entries, dirname); } if(strcmp(outfilename, "-") == 0) fontscale_name = NULL; else { if(outfilename[0] == '/') fontscale_name = dsprintf("%s", outfilename); else fontscale_name = dsprintf("%s%s", dirname, outfilename); if(fontscale_name == NULL) { perror("fontscale_name"); exit(1); } } dirp = opendir(dirname); if(dirp == NULL) { fprintf(stderr, "%s: ", dirname); perror("opendir"); return 0; } if(fontscale_name == NULL) fontscale = stdout; else fontscale = fopen(fontscale_name, "wb"); if(fontscale == NULL) { fprintf(stderr, "%s: ", fontscale_name); perror("fopen(w)"); return 0; } while((entry = readdir(dirp)) != NULL) { int have_face = 0; char *xlfd_name = NULL; struct stat f_stat; int tprio = 1; xlfd = NULL; if (xl) { int dl = strlen (entry->d_name); if (strcmp (entry->d_name + dl - xl, exclusionSuffix) == 0) continue; } filename = dsprintf("%s%s", dirname, entry->d_name); #define PRIO(x) ((x << 1) + tprio) #ifdef DT_LNK if (entry->d_type != DT_UNKNOWN) { if (entry->d_type == DT_LNK) tprio = 0; } else #endif #ifdef S_ISLNK { if (lstat(filename, &f_stat)) goto done; if (S_ISLNK(f_stat.st_mode)) tprio = 0; } #else ; #endif if(doBitmaps) rc = bitmapIdentify(filename, &xlfd_name); else rc = 0; if(rc < 0) goto done; if(rc == 0) { ftrc = FT_New_Face(ft_library, filename, 0, &face); if(ftrc) goto done; have_face = 1; isBitmap = ((face->face_flags & FT_FACE_FLAG_SCALABLE) == 0); if(!isBitmap) { /* Workaround for bitmap-only SFNT fonts */ if(FT_IS_SFNT(face) && face->num_fixed_sizes > 0 && strcmp(FT_Get_X11_Font_Format(face), "TrueType") == 0) { TT_MaxProfile *maxp; maxp = FT_Get_Sfnt_Table(face, ft_sfnt_maxp); if(maxp != NULL && maxp->maxContours == 0) isBitmap = 1; } } if(isBitmap) { if(!doBitmaps) goto done; } else { if(!doScalable) goto done; } if(isBitmap) { BDF_PropertyRec prop; rc = FT_Get_BDF_Property(face, "FONT", &prop); if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM) { xlfd_name = strdup(prop.u.atom); if(xlfd_name == NULL) goto done; } } } if(xlfd_name) { /* We know it's a bitmap font, and we know its XLFD */ int n = strlen(xlfd_name); if(reencodeLegacy && n >= 12 && strcasecmp(xlfd_name + n - 11, "-iso10646-1") == 0) { char *s; s = malloc(n - 10); memcpy(s, xlfd_name, n - 11); s[n - 11] = '\0'; xlfd = listCons(s, xlfd); } else { /* Not a reencodable font -- skip all the rest of the loop body */ putHash(entries, xlfd_name, entry->d_name, PRIO(filePrio(entry->d_name))); goto done; } } if(!have_face) { ftrc = FT_New_Face(ft_library, filename, 0, &face); if(ftrc) goto done; have_face = 1; isBitmap = ((face->face_flags & FT_FACE_FLAG_SCALABLE) == 0); if(!isBitmap) { if(face->num_fixed_sizes > 0) { TT_MaxProfile *maxp; maxp = FT_Get_Sfnt_Table(face, ft_sfnt_maxp); if(maxp != NULL && maxp->maxContours == 0) isBitmap = 1; } } } if(xlfd == NULL) xlfd = makeXLFD(entry->d_name, face, isBitmap); found = 0; for(lp = xlfd; lp; lp = lp->next) { char buf[MAXFONTNAMELEN]; for(encoding = encodings; encoding; encoding = encoding->next) { if(checkEncoding(face, encoding->value)) { found = 1; snprintf(buf, MAXFONTNAMELEN, "%s-%s", lp->value, encoding->value); putHash(entries, buf, entry->d_name, PRIO(filePrio(entry->d_name))); } } for(encoding = extra_encodings; encoding; encoding = encoding->next) { if(checkExtraEncoding(face, encoding->value, found)) { /* Do not set found! */ snprintf(buf, MAXFONTNAMELEN, "%s-%s", lp->value, encoding->value); putHash(entries, buf, entry->d_name, PRIO(filePrio(entry->d_name))); } } } done: if(have_face) FT_Done_Face(face); deepDestroyList(xlfd); xlfd = NULL; free(filename); #undef PRIO } closedir(dirp); n = hashElements(entries); fprintf(fontscale, "%d\n", n); array = hashArray(entries, 1); for(i = 0; i < n; i++) fprintf(fontscale, "%s %s\n", array[i]->value, array[i]->key); destroyHashArray(array); entries = NULL; if(fontscale_name) { fclose(fontscale); free(fontscale_name); } encodings: encdir = dsprintf("%s%s", dirname, "encodings.dir"); if(encdir == NULL) { perror("encodings"); exit(1); } unlink(encdir); if (numEncodings) { encfile = fopen(encdir, "w"); if(encfile == NULL) { perror("open(encodings.dir)"); exit(1); } fprintf(encfile, "%d\n", numEncodings); encodingsToDo = sortList(encodingsToDo); for(lp = encodingsToDo; lp; lp = lp->next) { fprintf(encfile, "%s\n", lp->value); } fclose (encfile); } free(dirname); return 1; }