void simple_element::write(const std::wstring & RootPath) { std::wstring name_ = RootPath + FILE_SEPARATOR_STR + file_name_; NSFile::CFileBinary file; if ( file.CreateFileW(name_) == true) { if (bXml) { std::string root = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; file.WriteFile((BYTE*)root.c_str(), root.length()); } file.WriteFile((BYTE*)content_utf8_.c_str(), content_utf8_.length()); file.CloseFile(); } }
bool CJBig2File::MemoryToJBig2(unsigned char* pBufferBGRA ,int BufferSize, int nWidth, int nHeight, std::wstring sDstFileName) { // check for valid input parameters /////////////////////////////////////////////////////////// if ( NULL == pBufferBGRA ) return false; int lBufferSize = BufferSize; unsigned char *pSourceBuffer = pBufferBGRA; PIX *pSource = pixCreate( nWidth, nHeight, 32 ); if ( !pSource ) return false; for ( int nY = 0; nY < nHeight; nY++ ) { for ( int nX = 0; nX < nWidth; nX++, pSourceBuffer += 3 )//todooo сделать 3 ? 4 { pixSetRGBPixel( pSource, nX, nY, pSourceBuffer[ 2 ], pSourceBuffer[ 1 ], pSourceBuffer[ 0 ] ); } } jbig2ctx *pContext = jbig2_init( m_dTreshold, 0.5, 0, 0, ! m_bPDFMode, m_bRefine ? 10 : -1 ); // Пока сделаем запись одной картинки в JBig2 // TO DO: надо будет сделать запись нескольких картинок в 1 JBig2 файл // Убираем ColorMap PIX *pPixL = NULL; if ( NULL == ( pPixL = pixRemoveColormap( pSource, REMOVE_CMAP_BASED_ON_SRC ) ) ) { pixDestroy( &pSource ); jbig2_destroy( pContext ); return false; } pixDestroy( &pSource ); PIX *pPixT = NULL; if ( pPixL->d > 1 ) { PIX *pGray = NULL; if ( pPixL->d > 8 ) { pGray = pixConvertRGBToGrayFast( pPixL ); if ( !pGray ) { pixDestroy( &pSource ); jbig2_destroy( pContext ); return false; } } else { pGray = pixClone( pPixL ); } if ( m_bUpscale2x ) { pPixT = pixScaleGray2xLIThresh( pGray, m_nBwTreshold ); } else if ( m_bUpscale4x ) { pPixT = pixScaleGray4xLIThresh( pGray, m_nBwTreshold ); } else { pPixT = pixThresholdToBinary( pGray, m_nBwTreshold ); } pixDestroy( &pGray ); } else { pPixT = pixClone( pPixL ); } if ( m_sOutputTreshold.length() > 0 ) { pixWrite( m_sOutputTreshold.c_str(), pPixT, IFF_BMP ); } if ( m_bSegment && pPixL->d > 1 ) { PIX *pGraphics = segment_image( pPixT, pPixL ); if ( pGraphics ) { char *sFilename; asprintf( &sFilename, "%s.%04d.%s", m_sBaseName.c_str(), 0, ".bmp" ); pixWrite( sFilename, pGraphics, IFF_BMP ); free( sFilename ); } if ( !pPixT ) { // Ничего не делаем return true; } } pixDestroy( &pPixL ); if ( !m_bSymbolMode ) { int nLength = 0; uint8_t *pBuffer = jbig2_encode_generic( pPixT, !m_bPDFMode, 0, 0, m_bDuplicateLineRemoval, &nLength ); bool bRes = true; NSFile::CFileBinary file; if (file.CreateFileW(sDstFileName ) == true ) { file.WriteFile(pBuffer, nLength); file.CloseFile(); bRes = true; } else bRes = false; pixDestroy( &pPixT ); if ( pBuffer ) free( pBuffer ); jbig2_destroy( pContext ); return bRes; } int nNumPages = 1; jbig2_add_page( pContext, pPixT ); pixDestroy( &pPixT ); int nLength = 0; uint8_t *pBuffer = jbig2_pages_complete( pContext, &nLength ); if ( !pBuffer ) { jbig2_destroy( pContext ); return false; } if ( m_bPDFMode ) { std::wstring sFileName = sDstFileName;//m_sBaseName + _T(".sym"); NSFile::CFileBinary file; if ( file.CreateFileW(sFileName) == false) { free( pBuffer ); jbig2_destroy( pContext ); return false; } file.WriteFile( pBuffer, nLength ); file.CloseFile(); } free( pBuffer ); for ( int nIndex = 0; nIndex < nNumPages; ++nIndex ) { pBuffer = jbig2_produce_page( pContext, nIndex, -1, -1, &nLength ); if ( m_bPDFMode ) { std::wstring sFileName = m_sBaseName + L".0000"; NSFile::CFileBinary file; if ( file.CreateFileW(sFileName) ==false) { free( pBuffer ); jbig2_destroy( pContext ); return false; } file.WriteFile( pBuffer, nLength ); file.CloseFile(); } free( pBuffer ); } jbig2_destroy( pContext ); return true; }
static int do_extract_currentfile( unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password ) { char filename_inzipA[256]; wchar_t filename_inzip[256]; wchar_t* filename_withoutpath; wchar_t* p; int err=UNZ_OK; NSFile::CFileBinary oFile; FILE *fout=NULL; void* buf; uInt size_buf; unz_file_info file_info; uLong ratio=0; err = unzGetCurrentFileInfo(uf,&file_info,filename_inzipA,sizeof(filename_inzipA),NULL,0,NULL,0); std::wstring filenameW = codepage_issue_fixFromOEM(filename_inzipA); wcscpy(filename_inzip , filenameW.c_str()); if (err!=UNZ_OK) { return err; } size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if (buf==NULL) { return UNZ_INTERNALERROR; } p = filename_withoutpath = filename_inzip; while ((*p) != '\0') { if (((*p)=='/') || ((*p)=='\\')) filename_withoutpath = p+1; p++; } if ((*filename_withoutpath)=='\0') { if ((*popt_extract_without_path)==0) { mymkdir(filename_inzip); } } else { const wchar_t* write_filename; int skip=0; if ((*popt_extract_without_path)==0) write_filename = filename_inzip; else write_filename = filename_withoutpath; err = unzOpenCurrentFilePassword(uf,password); if (((*popt_overwrite)==0) && (err==UNZ_OK)) { char rep=0; NSFile::CFileBinary oFileTemp; if (oFileTemp.OpenFile(write_filename)) { oFileTemp.CloseFile(); } if (rep == 'N') skip = 1; if (rep == 'A') *popt_overwrite=1; } if ((skip==0) && (err==UNZ_OK)) { if(oFile.CreateFileW(write_filename)) fout = oFile.GetFileNative(); // some zipfile don't contain directory alone before file if ((fout==NULL) && ((*popt_extract_without_path)==0) && (filename_withoutpath!=(wchar_t*)filename_inzip)) { char c=*(filename_withoutpath-1); *(filename_withoutpath-1)='\0'; makedir(write_filename); *(filename_withoutpath-1)=c; if(oFile.CreateFileW(write_filename)) fout = oFile.GetFileNative(); } } if (fout!=NULL) { do { err = unzReadCurrentFile(uf, buf, size_buf); if (err<0) { break; } if (err>0) if (fwrite(buf,err,1,fout)!=1) { err=UNZ_ERRNO; break; } } while (err>0); //close вызовется в oFile //if (fout) // fclose(fout); if (err==0) change_file_date(write_filename,file_info.dosDate, file_info.tmu_date); } if (err==UNZ_OK) { err = unzCloseCurrentFile (uf); } else unzCloseCurrentFile(uf); // don't lose the error } free(buf); return err; }
bool CFontConverter::ToOTF(std::wstring sFontIn, std::wstring sFontOut, unsigned int* pSymbols, int nCount, std::wstring sNameW, long nFlag) { 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 = nFileSize; NSFontConverter::CFontFileTrueType* pTTF = NSFontConverter::CFontFileTrueType::LoadFromFile( sFontIn.c_str() ); FT_Error oerrr; if ( oerrr = FT_Open_Face( pLibrary, &oOpenArgs, 0, &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); if ( bNeedConvert ) { 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 ) { NSFile::CFileBinary oWriteFile; oWriteFile.CreateFileW(sFontOut); pT1C->ToOpenTypeCFF( &NSFontConverter::FileWrite, oWriteFile.GetFileNative(), pFace ); oWriteFile.CloseFile(); } 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 = 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; } } } NSFile::CFileBinary oWriteFile; oWriteFile.CreateFileW(sFontOut); pTTF->WriteTTF( &NSFontConverter::FileWrite, oWriteFile.GetFileNative(), sName.c_str(), NULL, pUseGlyfs, lGlyfsCount ); oWriteFile.CloseFile(); } else { // error parse font // Просто копируем файл NSFile::CFileBinary::Copy(sFontIn, sFontOut); } } } else { // Просто копируем файл NSFile::CFileBinary::Copy(sFontIn, sFontOut); } FT_Done_Face( pFace ); FT_Done_FreeType( pLibrary ); RELEASEARRAYOBJECTS(pBaseAddress); return true; }
inline Global::_BlipType SaveImageToFileFromDIB(unsigned char* data, int size, const std::wstring& file_name)//without ext { Global::_BlipType result = Global::msoblipERROR; CBgraFrame oFrame; int offset = 0, biSizeImage = 0; __BITMAPINFOHEADER * header = (__BITMAPINFOHEADER*)data; if (!header) return result; result = Global::msoblipDIB; if (header->biWidth > 100000 || header->biHeight > 100000 || header->biSize != 40) { __BITMAPCOREHEADER * header_core = (__BITMAPCOREHEADER *)data; if (header_core->bcSize != 12) { result = Global::msoblipWMF; } else { offset = 12; //sizeof(BITMAPCOREHEADER) oFrame.put_Height (header_core->bcHeight ); oFrame.put_Width (header_core->bcWidth ); int sz_bitmap = header_core->bcHeight * header_core->bcWidth * header_core->bcBitCount/ 8; //if (header_core->bcWidth % 2 != 0 && sz_bitmap < size - offset) // header_core->bcWidth++; ///???? todooo непонятно .. в biff5 нужно флипать картинку, в biff8 не ясно ( - int stride = -(size - offset) / header_core->bcHeight; oFrame.put_Stride (stride/*header_core->bcBitCount * header_core->bcWidth /8 */); biSizeImage = size - offset; if (-stride >= header_core->bcWidth && header_core->bcBitCount >=24 ) { result = Global::msoblipPNG; } } } else { offset = 40; //sizeof(BITMAPINFOHEADER) oFrame.put_Height (header->biHeight ); oFrame.put_Width (header->biWidth ); int sz_bitmap = header->biHeight * header->biWidth * header->biBitCount/ 8; //if (header->biWidth % 2 != 0 && sz_bitmap < size -offset) // header->biWidth++; int stride = -(size - offset) / header->biHeight; if (-stride >= header->biWidth && header->biBitCount >= 24) { result = Global::msoblipPNG; } oFrame.put_Stride (stride/*header->biBitCount * header->biWidth /8*/); biSizeImage = header->biSizeImage > 0 ? header->biSizeImage : (size - offset); } //------------------------------------------------------------------------------------------ if (result == Global::msoblipPNG) { oFrame.put_Data((unsigned char*)data + offset); if (!oFrame.SaveFile(file_name + L".png", 4/*CXIMAGE_FORMAT_PNG*/)) result = Global::msoblipERROR; oFrame.put_Data(NULL); } else if (result == Global::msoblipWMF) { NSFile::CFileBinary file; if (file.CreateFileW(file_name + L".wmf")) { file.WriteFile((BYTE*)data, size); file.CloseFile(); } } else if (biSizeImage > 0) { NSFile::CFileBinary file; if (file.CreateFileW(file_name + L".bmp")) { _UINT16 vtType = 0x4D42; file.WriteFile((BYTE*)&vtType, 2); _UINT32 dwLen = biSizeImage; file.WriteFile((BYTE*)&dwLen, 4); _UINT32 dwRes = 0; file.WriteFile((BYTE*)&dwRes, 4); _UINT32 dwOffset = 2; file.WriteFile((BYTE*)&dwOffset, 4); file.WriteFile((BYTE*)data, size); file.CloseFile(); } } return result; }