bool AP_UnixApp::makePngPreview(const char * pszInFile, const char * pszPNGFile, UT_sint32 iWidth, UT_sint32 iHeight) { cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, iWidth, iHeight); cairo_t *cr = cairo_create (surface); GR_UnixCairoAllocInfo ai(NULL, false); GR_CairoGraphics * pG = static_cast<GR_CairoGraphics*>(GR_UnixCairoGraphics::graphicsAllocator(ai)); pG->setCairo(cr); pG->beginPaint(); // needed to avoid cairo reference loss UT_Error error = UT_OK; PD_Document * pNewDoc = new PD_Document(); error = pNewDoc->readFromFile(pszInFile,IEFT_Unknown, NULL); if (error != UT_OK) { return false; } AP_Preview_Abi * pPrevAbi = new AP_Preview_Abi(pG,iWidth,iHeight,NULL, PREVIEW_ZOOMED,pNewDoc); dg_DrawArgs da; memset(&da, 0, sizeof(da)); da.pG = pG; GR_Painter * pPaint = new GR_Painter(pG); pPaint->clearArea(0,0,pG->tlu(iWidth),pG->tlu(iHeight)); pPrevAbi->getView()->draw(0, &da); pG->endPaint(); cairo_destroy(cr); DELETEP(pPaint); cairo_surface_write_to_png(surface, pszPNGFile); cairo_surface_destroy(surface); DELETEP(pG); DELETEP(pPrevAbi); // This deletes pNewDoc return true; }
gint XAP_UnixDialog_FileOpenSaveAs::previewPicture (void) { UT_ASSERT (m_FC && m_preview); UT_ASSERT(XAP_App::getApp()); const XAP_StringSet * pSS = m_pApp->getStringSet(); UT_return_val_if_fail( pSS, 0 ); // attach and clear the area immediately GR_UnixCairoAllocInfo ai(m_preview); GR_CairoGraphics* pGr = (GR_CairoGraphics*) XAP_App::getApp()->newGraphics(ai); const gchar * file_name = gtk_file_chooser_get_uri (m_FC); GR_Font * fnt = pGr->findFont("Times New Roman", "normal", "", "normal", "", "12pt", pSS->getLanguageName()); pGr->setFont(fnt); UT_UTF8String str; pSS->getValueUTF8(XAP_STRING_ID_DLG_IP_No_Picture_Label, str); int answer = 0; FG_Graphic * pGraphic = 0; GR_Image *pImage = NULL; double scale_factor = 0.0; UT_sint32 scaled_width,scaled_height; UT_sint32 iImageWidth,iImageHeight; { GR_Painter painter(pGr); painter.clearArea(0, 0, pGr->tlu(m_preview->allocation.width), pGr->tlu(m_preview->allocation.height)); if (!file_name) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); goto Cleanup; } // are we dealing with a file or directory here? struct stat st; if (!stat (file_name, &st)) { if (!S_ISREG(st.st_mode)) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); goto Cleanup; } } GsfInput * input = NULL; UT_DEBUGMSG(("file_name %s \n",file_name)); input = UT_go_file_open (file_name, NULL); if (!input) goto Cleanup; char Buf[4097] = ""; // 4096+nul ought to be enough UT_uint32 iNumbytes = UT_MIN(4096, gsf_input_size(input)); gsf_input_read(input, iNumbytes, (guint8 *)(Buf)); Buf[iNumbytes] = '\0'; IEGraphicFileType ief = IE_ImpGraphic::fileTypeForContents(Buf,4096); if((ief == IEGFT_Unknown) || (ief == IEGFT_Bogus)) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); g_object_unref (G_OBJECT (input)); goto Cleanup; } g_object_unref (G_OBJECT (input)); input = UT_go_file_open (file_name, NULL); size_t num_bytes = gsf_input_size(input); UT_Byte * bytes = (UT_Byte *) gsf_input_read(input, num_bytes,NULL ); if(bytes == NULL) { painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); g_object_unref (G_OBJECT (input)); goto Cleanup; } UT_ByteBuf * pBB = new UT_ByteBuf(); pBB->append(bytes,num_bytes); g_object_unref (G_OBJECT (input)); // // OK load the data into a GdkPixbuf // bool bLoadFailed = false; // GdkPixbuf * pixbuf = pixbufForByteBuf ( pBB); delete pBB; if(pixbuf == NULL) { // // Try a fallback loader here. // painter.drawChars (str.ucs4_str().ucs4_str(), 0, str.size(), pGr->tlu(12), pGr->tlu(static_cast<int>(m_preview->allocation.height / 2)) - pGr->getFontHeight(fnt)/2); bLoadFailed = true; goto Cleanup; } pImage = new GR_UnixImage(NULL,pixbuf); iImageWidth = gdk_pixbuf_get_width (pixbuf); iImageHeight = gdk_pixbuf_get_height (pixbuf); if (m_preview->allocation.width >= iImageWidth && m_preview->allocation.height >= iImageHeight) scale_factor = 1.0; else scale_factor = MIN( static_cast<double>(m_preview->allocation.width)/iImageWidth, static_cast<double>(m_preview->allocation.height)/iImageHeight); scaled_width = static_cast<int>(scale_factor * iImageWidth); scaled_height = static_cast<int>(scale_factor * iImageHeight); static_cast<GR_UnixImage *>(pImage)->scale(scaled_width,scaled_height); painter.drawImage(pImage, pGr->tlu(static_cast<int>((m_preview->allocation.width - scaled_width ) / 2)), pGr->tlu(static_cast<int>((m_preview->allocation.height - scaled_height) / 2))); answer = 1; } Cleanup: FREEP(file_name); DELETEP(pImage); DELETEP(pGr); DELETEP(pGraphic); return answer; }