openFrameworks icon indicating copy to clipboard operation
openFrameworks copied to clipboard

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

Open dimitre opened this issue 2 years ago • 4 comments

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: https://github.com/openframeworks/openFrameworks/issues/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();
}


dimitre avatar Apr 21 '22 22:04 dimitre

I like this idea.

roymacdonald avatar Apr 22 '22 12:04 roymacdonald

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 avatar Apr 22 '22 13:04 dimitre

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:

  • https://github.com/openframeworks/openFrameworks/pull/6969

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

dimitre avatar May 12 '22 01:05 dimitre

Good idea!

On Thu, 12 May 2022 at 11:38, Dimitre @.***> wrote:

Hey @ofTheo https://github.com/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)

— Reply to this email directly, view it on GitHub https://github.com/openframeworks/openFrameworks/issues/6947#issuecomment-1124444068, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGK2HFEAJO6F57GIVVNXDLVJROIFANCNFSM5UARW45A . You are receiving this because you are subscribed to this thread.Message ID: @.***>

danoli3 avatar May 12 '22 02:05 danoli3

closed by https://github.com/openframeworks/openFrameworks/pull/6985

dimitre avatar Feb 14 '23 22:02 dimitre