Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal : ofGraphicsCairo.h - cairo functions outside ofGraphics.h #6947

Closed
dimitre opened this issue Apr 21, 2022 · 5 comments
Closed

Proposal : ofGraphicsCairo.h - cairo functions outside ofGraphics.h #6947

dimitre opened this issue Apr 21, 2022 · 5 comments
Labels

Comments

@dimitre
Copy link
Member

dimitre commented Apr 21, 2022

I'm proposing here a separation of all Cairo functionalities to a file outside of ofGraphics.h
Already tested here and works great. I've called ofGraphicsCairo.h and ofGraphicsCairo.cpp

This paves the way of a modular OF where we can trim down unneeded functionalities per project.
I'm already testing some proof of concept with separate .xcconfig

//THIS HAS ALL THE HEADER AND LIBS FOR OF CORE
#include "../../../libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig"
GCC_PREPROCESSOR_DEFINITIONS=OFLIB_USECAIRO
#include "../../../libs/openFrameworksCompiled/project/osx/Cairo.xcconfig"

related: #6948

ofGraphicsCairo.h

#pragma once

#include "ofRectangle.h"

/// \}
/// \name Screen Saving
/// \{

/// \brief Begin rendering to a PDF file.
///
/// openFrameworks allows rendering of 2D graphics to pdf via the
/// ofCairoRenderer. ofBeginSaveScreenAsPDF() is called before drawing. When
/// done drawing call ofEndSaveScreenAsPDF() to output the file.
///
/// ~~~~{.cpp}
/// void ofApp::setup(){
///     ofBeginSaveScreenAsPDF("screenshot.pdf", false);
///     ofSetColor(54,54,54);
///     ofDrawEllipse(100,100,200,200);
///     ofEndSaveScreenAsPDF();
/// }
/// ~~~~
/// \sa End drawing with ofEndSaveScreenAsPDF()
///
void ofBeginSaveScreenAsPDF(std::string filename, bool bMultipage = false, bool b3D = false, ofRectangle outputsize = ofRectangle(0,0,0,0));

/// \brief Terminates draw to PDF through ofCairoRenderer and outputs the file.
/// \sa ofBeginSaveScreenAsPDF()
void ofEndSaveScreenAsPDF();

/// \brief Begin rendering to a SVG file.
/// \sa ofEndSaveScreenAsSVG(), ofBeginSaveScreenAsPDF()
void ofBeginSaveScreenAsSVG(std::string filename, bool bMultipage = false, bool b3D = false, ofRectangle outputsize = ofRectangle(0,0,0,0));

/// \brief Terminates draw to SVG and outputs the file.
/// \sa ofBeginSaveScreenAsSVG()
void ofEndSaveScreenAsSVG();

/// \}

ofGraphicsCairo.cpp

#include "ofGraphicsCairo.h"
#include "ofRendererCollection.h"
#include "ofCairoRenderer.h"

static std::shared_ptr<ofCairoRenderer> cairoScreenshot;
static std::shared_ptr<ofBaseRenderer> storedRenderer;
static std::shared_ptr<ofRendererCollection> rendererCollection;
static bool bScreenShotStarted = false;


static void ofEndSaveScreen(){
	if( bScreenShotStarted ){

		if( cairoScreenshot ){
			cairoScreenshot->close();
			rendererCollection.reset();
			cairoScreenshot.reset();
		}
		if( storedRenderer ){
			ofSetCurrentRenderer(storedRenderer,true);
			storedRenderer.reset();
		}

		bScreenShotStarted = false;
	}

}

static void ofBeginSaveScreen(std::string filename, ofCairoRenderer::Type type, bool bMultipage, bool b3D, ofRectangle outputsize){
	if( bScreenShotStarted ) ofEndSaveScreen();
	
	storedRenderer = ofGetCurrentRenderer();
	
	cairoScreenshot = std::make_unique<ofCairoRenderer>();
	cairoScreenshot->setup(filename, type, bMultipage, b3D, outputsize);

	rendererCollection = std::make_shared<ofRendererCollection>();
	rendererCollection->renderers.push_back(storedRenderer);
	rendererCollection->renderers.push_back(cairoScreenshot);
	
	ofSetCurrentRenderer(rendererCollection, true);
	cairoScreenshot->background(cairoScreenshot->getStyle().bgColor);
	bScreenShotStarted = true;
}

//-----------------------------------------------------------------------------------
void ofBeginSaveScreenAsPDF(std::string filename, bool bMultipage, bool b3D, ofRectangle outputsize){
	ofBeginSaveScreen(filename, ofCairoRenderer::PDF, bMultipage, b3D, outputsize);
}

//-----------------------------------------------------------------------------------
void ofEndSaveScreenAsPDF(){
	ofEndSaveScreen();
}

//-----------------------------------------------------------------------------------
void ofBeginSaveScreenAsSVG(std::string filename, bool bMultipage, bool b3D, ofRectangle outputsize){
	ofBeginSaveScreen(filename, ofCairoRenderer::SVG, bMultipage, b3D, outputsize);
}

//-----------------------------------------------------------------------------------
void ofEndSaveScreenAsSVG(){
	ofEndSaveScreen();
}

@roymacdonald
Copy link
Member

I like this idea.

@dimitre
Copy link
Member Author

dimitre commented Apr 22, 2022

Great! I know xcode (ios and macos) will need this new files added. not sure which platforms needs the specific files added to their templates too.

@dimitre
Copy link
Member Author

dimitre commented May 12, 2022

Hey @ofTheo
I would love to separate includes and headers from Cairo too and move to Project.xcconfig, uncommented of course, but the same way of this Boost libraries here:

This way it can be disabled by project if needed (improving compiling time)

The complete PR would be:

  • editing ofGraphics.h and ofGraphics.cpp - moving Cairo functions
  • creating ofGraphicsCairo.h and ofGraphicsCairo.cpp - Cairo is moved here
  • adding two new files to pbxproj (navigator and build phases)
  • removing from CoreOF.xcconfig
  • adding to Project.xcconfig

@danoli3
Copy link
Member

danoli3 commented May 12, 2022 via email

@dimitre
Copy link
Member Author

dimitre commented Feb 14, 2023

closed by #6985

@dimitre dimitre closed this as completed Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants