@@ -242,11 +242,93 @@ define(["easel","sugar-web/datastore","sugar-web/env","l10n","humane"], function
242242 this . _updatePageCounter ( ) ;
243243 } ;
244244
245- this . setData = function ( data ) {
245+ this . _scaleData = function ( data , scale ) {
246+ console . log ( "Scaling data by factor: " + scale ) ;
247+ if ( ! data [ 'boxs' ] ) return ;
248+
249+ for ( var i = 0 ; i < data [ 'boxs' ] . length ; i ++ ) {
250+ var box = data [ 'boxs' ] [ i ] ;
251+
252+ // Scale image properties
253+ if ( box [ 'img_x' ] !== undefined ) box [ 'img_x' ] *= scale ;
254+ if ( box [ 'img_y' ] !== undefined ) box [ 'img_y' ] *= scale ;
255+ if ( box [ 'img_w' ] !== undefined ) box [ 'img_w' ] *= scale ;
256+ if ( box [ 'img_h' ] !== undefined ) box [ 'img_h' ] *= scale ;
257+
258+ // Scale globes
259+ if ( box [ 'globes' ] ) {
260+ for ( var j = 0 ; j < box [ 'globes' ] . length ; j ++ ) {
261+ var globe = box [ 'globes' ] [ j ] ;
262+ globe [ 'x' ] *= scale ;
263+ globe [ 'y' ] *= scale ;
264+ globe [ 'width' ] *= scale ;
265+ globe [ 'height' ] *= scale ;
266+ globe [ 'radio' ] *= scale ;
267+
268+ if ( globe [ 'point_0' ] !== undefined ) globe [ 'point_0' ] *= scale ;
269+ if ( globe [ 'point_1' ] !== undefined ) globe [ 'point_1' ] *= scale ;
270+
271+ // Globe text scaling
272+ if ( globe [ 'text_width' ] !== undefined ) globe [ 'text_width' ] *= scale ;
273+ if ( globe [ 'text_height' ] !== undefined ) globe [ 'text_height' ] *= scale ;
274+
275+ // Font scaling
276+ if ( globe [ 'text_font_description' ] ) {
277+ // Format: "Sans 30" or "Sans bold 30"
278+ var parts = globe [ 'text_font_description' ] . split ( ' ' ) ;
279+ var sizeIndex = parts . length - 1 ;
280+ var size = parseFloat ( parts [ sizeIndex ] ) ;
281+ if ( ! isNaN ( size ) ) {
282+ parts [ sizeIndex ] = Math . max ( 1 , Math . round ( size * scale ) ) ;
283+ globe [ 'text_font_description' ] = parts . join ( ' ' ) ;
284+ }
285+ }
286+ }
287+ }
288+ }
289+ } ;
290+
291+ this . setData = function ( data ) {
246292 this . _data = data ;
293+ // Check for canvas scaling
294+ if ( this . _data [ 'canvas_width' ] && this . _data [ 'canvas_width' ] > 0 ) {
295+ var scale = this . _canvas . width / this . _data [ 'canvas_width' ] ;
296+ // Allow small epsilon difference
297+ if ( Math . abs ( scale - 1.0 ) > 0.01 ) {
298+ this . _scaleData ( this . _data , scale ) ;
299+ this . _data [ 'canvas_width' ] = this . _canvas . width ;
300+ this . _data [ 'canvas_height' ] = this . _canvas . height ;
301+ }
302+ } else {
303+ // New or legacy data - assume simple fit or do nothing.
304+ // We just set current dimensions so next save is correct.
305+ this . _data [ 'canvas_width' ] = this . _canvas . width ;
306+ this . _data [ 'canvas_height' ] = this . _canvas . height ;
307+ }
247308 this . _data [ 'previews' ] = [ ] ;
248309 this . init ( ) ;
249310 } ;
311+ this . resize = function ( width , height ) {
312+ if ( this . _canvas . width === width && this . _canvas . height === height ) {
313+ return ;
314+ }
315+ // Capture current state from screen before scaling to assume continuity
316+ this . updateData ( ) ;
317+
318+ var scale = width / this . _canvas . width ;
319+
320+ // Update canvas dimensions
321+ this . _canvas . width = width ;
322+ this . _canvas . height = height ;
323+
324+ // Update underlying data model
325+ this . _scaleData ( this . _data , scale ) ;
326+ this . _data [ 'canvas_width' ] = width ;
327+ this . _data [ 'canvas_height' ] = height ;
328+
329+ // re-init current box
330+ this . comicBox . init ( this . _data [ 'boxs' ] [ this . activeBox ] , this . _data [ 'images' ] , ( this . activeBox > 0 ) ) ;
331+ } ;
250332
251333 this . showWait = function ( ) {
252334 this . _waitMsg . style . display = 'block' ;
0 commit comments