[gbrainy] Generate solutions page in PDF export



commit 7a463d7644455f0280a8b520dbe9cfeb73a68433
Author: Jordi Mas <jmas softcatala org>
Date:   Tue Nov 23 01:33:15 2010 +0100

    Generate solutions page in PDF export

 po/POTFILES.in               |    7 ++-
 src/Core/Main/PdfExporter.cs |  116 +++++++++++++++++++++++++++++++++--------
 2 files changed, 97 insertions(+), 26 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 059cf46..873cfe6 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -14,16 +14,17 @@ src/Core/Main/GameManager.cs
 src/Core/Main/GameSession.cs
 src/Core/Main/GameTips.cs
 src/Core/Main/GameTypes.cs
+src/Core/Main/Memory.cs
+src/Core/Main/PdfExporter.cs
 src/Core/Main/PlayerHistory.cs
 src/Core/Main/Preferences.cs
-src/Core/Main/Memory.cs
 src/Core/Main/Verbal/Analogies.cs
-src/Core/Main/Verbal/Analogy.cs
+src/Core/Main/Verbal/AnalogiesFactory.cs
 src/Core/Main/Verbal/AnalogiesMultipleOptions.cs
 src/Core/Main/Verbal/AnalogiesPairOfWordsCompare.cs
 src/Core/Main/Verbal/AnalogiesPairOfWordsOptions.cs
 src/Core/Main/Verbal/AnalogiesQuestionAnswer.cs
-src/Core/Main/Verbal/AnalogiesFactory.cs
+src/Core/Main/Verbal/Analogy.cs
 src/Core/Main/Xml/GameXml.cs
 src/Clients/Classical/gbrainy.cs
 src/Clients/Classical/CommandLine.cs
diff --git a/src/Core/Main/PdfExporter.cs b/src/Core/Main/PdfExporter.cs
index f550d5d..195f152 100644
--- a/src/Core/Main/PdfExporter.cs
+++ b/src/Core/Main/PdfExporter.cs
@@ -25,46 +25,60 @@ using gbrainy.Core.Libraries;
 
 namespace gbrainy.Core.Main
 {
-	// Generates a single PDF document with the selected games		
+	// Generates a single PDF document with the selected games
 	static public class PdfExporter
 	{
+		static readonly int width = 400, height = 400, margin = 20, question_height = 100;
+		static readonly int columns = 2, rows = 2;
+		static readonly int page_margin = 20; // space between vertical and hortizontal pages
+		static readonly int page_width = width + page_margin;
+		static readonly int page_height = height + question_height + page_margin;
+
 		static public void GeneratePdf (Game [] games, int games_page, string file)
 		{
-			const int width = 400, height = 400, margin = 20, question_height = 100;
-			int x, y, cnt;
-			Game puzzle;
-			
-			PdfSurface pdf = new PdfSurface (file, (width + margin) * 2, 
-				(height + margin + question_height) * games_page / 2);
-
+			PdfSurface pdf = new PdfSurface (file, page_width * columns, page_height * rows);
 			CairoContextEx cr = new CairoContextEx (pdf, "sans 12", 72);
-			x = y = cnt = 0;
 
-			// TODO:
-			//	- Solution page
-			//	- Puzzles that are not shown correctly
+			GenerateQuestions (cr, games);
+			GenerateAnswers (cr, games);
+
+			pdf.Finish ();
+			((IDisposable)cr).Dispose();
+			return;
+		}
+
+		static void GenerateQuestions (CairoContextEx cr, Game [] games)
+		{
+			int x, y, page;
+			Game puzzle;
+			string str;
+
+			x = y = page = 0;
 			for (int i = 0; i < games.Length; i++)
 			{
 				puzzle = games [i];
 				puzzle.Begin ();
-
-				cnt++;
+				page++;
 
 				cr.Save ();
 				cr.Translate (x, y);
 				cr.Rectangle (0, 0, width, height + question_height);
 				cr.Clip ();
 
-				// Draw question				
+				// Translators: {0} is the game number and {1} the game question
+				// The number is used as reference when looking for the game solution in the PDF
+				str = String.Format (Catalog.GetString ("Game {0}. {1}"), i + 1, puzzle.Question);
+
+				// Draw question
 				cr.SetPangoFontSize (12);
 				cr.UseMarkup = true;
-				cr.DrawStringWithWrapping (20, 10, puzzle.Question, width - 20);
+				cr.DrawStringWithWrapping (margin, 10, str, width - margin);
 				cr.Stroke ();
 				cr.UseMarkup = false;
 
 				// Draw from question_height up height since from 0 to question_height is the question
 				// Translate adds always to previous matrix's transformation
-				cr.Translate (0, question_height);						
+				cr.Translate (0, question_height);
 				puzzle.DrawPreview (cr, width, height, false);
 				x += width + margin;
 				if (x > width + margin) {
@@ -74,19 +88,75 @@ namespace gbrainy.Core.Main
 				cr.Restore ();
 				cr.Stroke ();
 
-				if (cnt >= games_page) {
+				if (page >= columns * rows) {
 					cr.ShowPage ();
-					cnt = x = y = 0;
+					page = x = y = 0;
 				}
 			}
 
-			if (y > 0) {
+			if (y > 0)
 				cr.ShowPage ();
+		}
+
+		static void GenerateAnswers (CairoContextEx cr, Game [] games)
+		{
+			int x, y, page;
+			Game puzzle;
+			string str;
+			int column, row;
+
+			x = y = page = 0;
+			column = row = 0;
+
+			// Draw solution title
+			cr.SetPangoFontSize (20);
+			cr.DrawStringWithWrapping (x + margin, y + margin,
+				Catalog.GetString ("Solutions"), width - margin);
+			y += 75;
+			cr.Stroke ();
+
+			cr.SetPangoFontSize (12);
+			cr.UseMarkup = true;
+			for (int i = 0; i < games.Length; i++)
+			{
+				// Translators: {0} is the game number and {1} the game answer
+				// The number is used as reference to find the question to which this solution refers in the PDF
+				str = String.Format (Catalog.GetString ("Game {0}. {1} "), i + 1, games[i].Answer);
+
+				// Draw Solution
+				cr.DrawStringWithWrapping (x + margin, y + margin, str, width - margin);
+				cr.Stroke ();
+
+				y += 75;
+
+				// Next lateral page (right)
+				if (y >= page_height * (row + 1) && x + page_width < page_width * columns) {
+					column++;
+
+					x = column * page_width;
+					y = row * page_height;
+					page++;
+				} else {
+					// No more space (right), new row
+					if (y >= page_height * (row + 1) && x + page_width >= page_width * columns) {
+						row++;
+						column = 0;
+
+						x = column * page_width;
+						y = row * page_height;
+						page++;
+					}
+				}
+
+				if (page >= rows * columns) {
+					cr.ShowPage ();
+					page = x = y = 0;
+					column = row = 0;
+				}
 			}
 
-			pdf.Finish ();
-			((IDisposable)cr).Dispose();
-			return;
+			if (y > 0)
+				cr.ShowPage ();
 		}
 	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]