Beispiel #1
0
static const char*
mime_type(const char *file)
{
	//TODO: for now only a single mime type
	if (strcmp(file_ext(file), "css") == 0) {
		return "text/css";
	}

	return "text/html";
}
Beispiel #2
0
void DIALOG_PLOT::Plot( wxCommandEvent& event )
{
    applyPlotSettings();

    // If no layer selected, we have nothing plotted.
    // Prompt user if it happens because he could think there is a bug in Pcbnew.
    if( !m_plotOpts.GetLayerSelection().any() )
    {
        DisplayError( this, _( "No layer selected, Nothing to plot" ) );
        return;
    }

    // Create output directory if it does not exist (also transform it in
    // absolute form). Bail if it fails
    wxFileName  outputDir = wxFileName::DirName( m_plotOpts.GetOutputDirectory() );
    wxString    boardFilename = m_parent->GetBoard()->GetFileName();
    REPORTER&   reporter = m_messagesPanel->Reporter();

    if( !EnsureFileDirectoryExists( &outputDir, boardFilename, &reporter ) )
    {
        wxString msg;
        msg.Printf( _( "Could not write plot files to folder \"%s\"." ),
                    GetChars( outputDir.GetPath() ) );
        DisplayError( this, msg );
        return;
    }

    m_plotOpts.SetAutoScale( false );
    m_plotOpts.SetScale( 1 );

    switch( m_plotOpts.GetScaleSelection() )
    {
    default:
        break;

    case 0:     // Autoscale option
        m_plotOpts.SetAutoScale( true );
        break;

    case 2:     // 3:2 option
        m_plotOpts.SetScale( 1.5 );
        break;

    case 3:     // 2:1 option
        m_plotOpts.SetScale( 2 );
        break;

    case 4:     // 3:1 option
        m_plotOpts.SetScale( 3 );
        break;
    }

    /* If the scale factor edit controls are disabled or the scale value
     * is 0, don't adjust the base scale factor. This fixes a bug when
     * the default scale adjust is initialized to 0 and saved in program
     * settings resulting in a divide by zero fault.
     */
    if( m_fineAdjustXscaleOpt->IsEnabled()  && m_XScaleAdjust != 0.0 )
        m_plotOpts.SetFineScaleAdjustX( m_XScaleAdjust );

    if( m_fineAdjustYscaleOpt->IsEnabled() && m_YScaleAdjust != 0.0 )
        m_plotOpts.SetFineScaleAdjustY( m_YScaleAdjust );

    if( m_PSFineAdjustWidthOpt->IsEnabled() )
        m_plotOpts.SetWidthAdjust( m_PSWidthAdjust );

    wxString file_ext( GetDefaultPlotExtension( m_plotOpts.GetFormat() ) );

    // Test for a reasonable scale value
    // XXX could this actually happen? isn't it constrained in the apply
    // function?
    if( m_plotOpts.GetScale() < PLOT_MIN_SCALE )
        DisplayInfoMessage( this,
                            _( "Warning: Scale option set to a very small value" ) );

    if( m_plotOpts.GetScale() > PLOT_MAX_SCALE )
        DisplayInfoMessage( this,
                            _( "Warning: Scale option set to a very large value" ) );

    GERBER_JOBFILE_WRITER jobfile_writer( m_board, &reporter );

    // Save the current plot options in the board
    m_parent->SetPlotSettings( m_plotOpts );

    wxBusyCursor dummy;

    for( LSEQ seq = m_plotOpts.GetLayerSelection().UIOrder();  seq;  ++seq )
    {
        PCB_LAYER_ID layer = *seq;

        // All copper layers that are disabled are actually selected
        // This is due to wonkyness in automatically selecting copper layers
        // for plotting when adding more than two layers to a board.
        // If plot options become accessible to the layers setup dialog
        // please move this functionality there!
        // This skips a copper layer if it is actually disabled on the board.
        if( ( LSET::AllCuMask() & ~m_board->GetEnabledLayers() )[layer] )
            continue;

        // Pick the basename from the board file
        wxFileName fn( boardFilename );

        // Use Gerber Extensions based on layer number
        // (See http://en.wikipedia.org/wiki/Gerber_File)
        if( m_plotOpts.GetFormat() == PLOT_FORMAT_GERBER && m_useGerberExtensions->GetValue() )
            file_ext = GetGerberProtelExtension( layer );

        BuildPlotFileName( &fn, outputDir.GetPath(), m_board->GetLayerName( layer ), file_ext );
        wxString fullname = fn.GetFullName();
        jobfile_writer.AddGbrFile( layer, fullname );

        LOCALE_IO toggle;

        BOARD*      board = m_parent->GetBoard();
        PLOTTER*    plotter = StartPlotBoard( board, &m_plotOpts, layer, fn.GetFullPath(), wxEmptyString );

        // Print diags in messages box:
        wxString msg;

        if( plotter )
        {
            PlotOneBoardLayer( board, plotter, layer, m_plotOpts );
            plotter->EndPlot();
            delete plotter;

            msg.Printf( _( "Plot file \"%s\" created." ), GetChars( fn.GetFullPath() ) );
            reporter.Report( msg, REPORTER::RPT_ACTION );
        }
        else
        {
            msg.Printf( _( "Unable to create file \"%s\"." ), GetChars( fn.GetFullPath() ) );
            reporter.Report( msg, REPORTER::RPT_ERROR );
        }
    }

    if( m_plotOpts.GetFormat() == PLOT_FORMAT_GERBER && m_plotOpts.GetCreateGerberJobFile() )
    {
        // Pick the basename from the board file
        wxFileName fn( boardFilename );
        // Build gerber job file from basename
        BuildPlotFileName( &fn, outputDir.GetPath(), "job", GerberJobFileExtension );
        jobfile_writer.CreateJobFile( fn.GetFullPath() );
    }
}
bool AdobeThumbnail(const char* adobe_filename , const char* savejpeg_filename)
{
    string file_ext(adobe_filename);
    string rs = "(.+)(\\.(?:ai|AI|indd|INDD|Indd|eps|EPS|Eps))";  // 正则字符串,exp开始的单词
    std::regex expression(rs);                   // 字符串传递给构造函数,建立正则表达式
    bool ret = std::regex_match(file_ext, expression);
    if (!ret) {
        //      cout << "文件格式不对!\n";
        return ret ;
    }

    if (!IsFileExist(adobe_filename))
        return false ;     // 文件不存在


    char* pch = NULL;
    const char* flag = "pGImg:image";  // AI 和 Indd 稍微不同

    /// ************* 获取 ID或者AI文档 的预览图 **************** ///
    FILE* adobe_file = fopen(adobe_filename, "rb");
    size_t file_size = get_fileSize(adobe_filename); // 获得文件大小

    size_t bufsize = 1 * MBsize;        // AI 和EPS 预览图在开头,INDD文件在末位
    char* filebuf = new char[bufsize];  // 文件读到缓冲

    //  文件小于2M 整个文件读,否则遍历读最后2M
    if (file_size < bufsize) {
        bufsize = file_size;
        fread(filebuf, 1, bufsize, adobe_file);
        if (0xF5ED0606 == *(DWORD*)filebuf) { // indd 文件开头好像都这样
            pch = memfind(filebuf, flag , bufsize);   // INDD 可能不只一个预览图
            if ((pch != NULL))
                while ((pch != NULL) && (strlen(pch) < 10 * 1024))
                    pch = memfind(pch + 1, flag , bufsize - (pch - filebuf));

        } else
            pch = memfind(filebuf, flag , bufsize);

    } else {
        fread(filebuf, 1, bufsize, adobe_file);
//        00000000h: 06 06 ED F5 D8 1D 46 E5 BD 31 EF E7 FE 74 B7 1D ; ..眭?F褰1镧?
//        00000010h: 44 4F 43 55 4D 45 4E 54 01 70 0F 00 00 05 00 00 ; DOCUMENT.p......
        if (0xF5ED0606 == *(DWORD*)filebuf) { // indd 文件开头好像都这样
            fseek(adobe_file, (file_size - bufsize), SEEK_SET);
            fread(filebuf, 1, bufsize, adobe_file);

            pch = memfind(filebuf, flag , bufsize);   // INDD 可能不只一个预览图
            if ((pch != NULL))
                while ((pch != NULL) && (strlen(pch) < 10 * 1024))
                    pch = memfind(pch + 1, flag , bufsize - (pch - filebuf));

        } else
            pch = memfind(filebuf, flag , bufsize);   // AI 应该只有一个预览信息,
    }
    // 读取文件结束,关闭
    fclose(adobe_file);

    if (pch == NULL) {
        flag = "%AI7_Thumbnail:";
        size_t width, height, bitCount, Hexsize;
        char AI7_Thumbnail[64]; char BeginData[64]; char Hex_Bytes[64];

        pch = memfind(filebuf, flag , bufsize);
        // 检测到AI低版本预览图标记
        if (pch != NULL) {
            sscanf(pch, "%s %d %d %d\n%s %d %s\n", AI7_Thumbnail, &width, &height, &bitCount , BeginData, &Hexsize , Hex_Bytes);

            pch = memfind(filebuf, "Hex Bytes" , bufsize);
        }

        if (pch != NULL) {  // 解码 AI7_Thumbnail 为 图片
            char savepng_filename[MAX_PATH]={0};    // 源图是 BMP,保存png 失真少一点
            strncpy(savepng_filename , savejpeg_filename, strlen(savejpeg_filename) - 4);
            strcat(savepng_filename, ".png");

            string AI7Thumb(pch + 10 , Hexsize + 1);
            decode_Ai7Thumb_toPng(AI7Thumb , width, height , savepng_filename);
            delete[] filebuf; // 释放文件缓冲
            return true;
        }
    };

    if (pch == NULL)   ret = false;
    if (!ret) {  // 没有找到,返回前
        delete[] filebuf; // 释放文件缓冲
        return ret;
    }


    strtok(pch, "\r\n");
    string Base64_str(pch);

    std::regex ex("pGImg:image>|<\\/x\\wpGImg:image>|pGImg:image=\"");
    std::regex en("&#xA;");
    // 正则删除 xmpGImg 标记和 转意换行替换回来
    Base64_str = std::regex_replace(Base64_str, ex, string(""));
    Base64_str = std::regex_replace(Base64_str, en, string("\n"));

#if(AITEST)
        printf( "pGImg:image标记偏移: %d 在文件%s\n" , pch - filebuf , adobe_filename);
#endif
/// =============================== 解码一个Base64 的JPEG文件 ==============================////

    int b64len = Base64_str.size();
    int jpglen = fromBase64_Decode(Base64_str.c_str() , b64len , filebuf , b64len);

    FILE* jpeg_file = fopen(savejpeg_filename, "wb");
    fwrite(filebuf, 1 , jpglen , jpeg_file);


    delete[] filebuf; // 释放文件缓冲

    fclose(jpeg_file);

    return true;
}