Comparing version 0.0.1 to 0.0.2
@@ -119,4 +119,10 @@ module.exports = { | ||
'object-curly-newline': [ 'error' , { | ||
'consistent': true , | ||
'minProperties': 3 | ||
'ObjectExpression' : { | ||
'consistent': true , | ||
'minProperties': 3 | ||
} , | ||
'ObjectPattern' : { | ||
'consistent': true , | ||
'minProperties': 5 | ||
} | ||
} ] , | ||
@@ -123,0 +129,0 @@ 'object-curly-spacing': [ 'error' , 'always' ] , |
@@ -45,2 +45,4 @@ /* | ||
this.height = options.height || 0 ; | ||
this.contentWidth = options.contentWidth || 0 ; | ||
this.contentHeight = options.contentHeight || 0 ; | ||
this.align = options.align && Box.align[ options.align ] ? options.align : 'topLeft' ; | ||
@@ -70,3 +72,3 @@ } | ||
Box.align.topLeft = function topLeft( absolute , self , parent ) { | ||
Box.align.topLeft = function topLeft( absolute , self /*, parent*/ ) { | ||
absolute.x += self.x ; | ||
@@ -76,16 +78,41 @@ absolute.y += self.y ; | ||
Box.align.top = function top( absolute , self , parent ) { | ||
absolute.x += ( parent.width - ( parent.contentWidth || self.width ) ) / 2 + self.x ; | ||
absolute.y += self.y ; | ||
} ; | ||
Box.align.topRight = function topRight( absolute , self , parent ) { | ||
absolute.x += parent.width - self.width + self.x ; | ||
absolute.x += parent.width - ( parent.contentWidth || self.width ) + self.x ; | ||
absolute.y += self.y ; | ||
} ; | ||
Box.align.left = function left( absolute , self , parent ) { | ||
absolute.x += self.x ; | ||
absolute.y += ( parent.height - ( parent.contentHeight || self.height ) ) / 2 + self.y ; | ||
} ; | ||
Box.align.center = function center( absolute , self , parent ) { | ||
absolute.x += ( parent.width - ( parent.contentWidth || self.width ) ) / 2 + self.x ; | ||
absolute.y += ( parent.height - ( parent.contentHeight || self.height ) ) / 2 + self.y ; | ||
} ; | ||
Box.align.top = function top( absolute , self , parent ) { | ||
absolute.x += ( parent.width - self.width ) / 2 + self.x ; | ||
absolute.y += self.y ; | ||
Box.align.right = function right( absolute , self , parent ) { | ||
absolute.x += parent.width - ( parent.contentWidth || self.width ) + self.x ; | ||
absolute.y += ( parent.height - ( parent.contentHeight || self.height ) ) / 2 + self.y ; | ||
} ; | ||
Box.align.bottomLeft = function bottomLeft( absolute , self , parent ) { | ||
absolute.x += self.x ; | ||
absolute.y += parent.height - ( parent.contentHeight || self.height ) + self.y ; | ||
} ; | ||
Box.align.bottom = function bottom( absolute , self , parent ) { | ||
absolute.x += ( parent.width - ( parent.contentWidth || self.width ) ) / 2 + self.x ; | ||
absolute.y += parent.height - ( parent.contentHeight || self.height ) + self.y ; | ||
} ; | ||
Box.align.bottomRight = function bottomRight( absolute , self , parent ) { | ||
absolute.x += parent.width - ( parent.contentWidth || self.width ) + self.x ; | ||
absolute.y += parent.height - ( parent.contentHeight || self.height ) + self.y ; | ||
} ; | ||
@@ -33,4 +33,6 @@ /* | ||
function noop() {} | ||
function ContentBox( doc , options ) { | ||
@@ -54,1 +56,11 @@ Box.call( this , doc , options ) ; | ||
// Should be typically subclassed | ||
ContentBox.prototype.spaceCount = () => 0 ; // spaceCount( level2 ) | ||
ContentBox.prototype.resizeSpaces = noop ; // resizeSpaces( spaceWidth , level2 ) | ||
ContentBox.prototype.trim = noop ; | ||
ContentBox.prototype.trimLeft = noop ; | ||
ContentBox.prototype.trimRight = noop ; | ||
//ContentBox.prototype.resizeWidth = function resizeWidth( width ) { this.width = width ; } ; |
@@ -32,3 +32,4 @@ /* | ||
var Box = require( './Box.js' ) ; | ||
var ContentBox = require( './ContentBox.js' ) ; | ||
//var ContentBox = require( './ContentBox.js' ) ; | ||
var TextContentBox = require( './TextContentBox.js' ) ; | ||
var Line = require( './Line.js' ) ; | ||
@@ -45,2 +46,3 @@ | ||
this.align = 'topLeft' ; | ||
this.justify = !! options.justify ; | ||
@@ -59,3 +61,3 @@ this.currentLine = null ; | ||
FlowBlock.prototype.appendText = function appendText( text , attr ) { | ||
var length , font , textWidth , textHeight , fontSpaceWidth , remainingWidth , remainderText , found ; | ||
var length , font , textWidth , textHeight , remainingWidth , remainderText , found ; | ||
@@ -77,6 +79,20 @@ length = text.length ; | ||
found = this.wordsUpToWidth( text , font , attr.fontSize , remainingWidth ) ; | ||
if ( ! found ) { return ; } | ||
( { | ||
text , textWidth , remainderText | ||
} = found ) ; | ||
if ( ! found ) { | ||
if ( this.currentLine.contentBoxes.length ) { | ||
// Retry on a new line | ||
this.currentLine = null ; | ||
this.appendText( text , attr ) ; | ||
return ; | ||
} | ||
found = this.wordsUpToWidth( text , font , attr.fontSize , remainingWidth , true ) ; | ||
if ( ! found ) { | ||
console.error( "Cannot even add a single char..." ) ; | ||
return ; | ||
} | ||
} | ||
( { text , textWidth , remainderText } = found ) ; | ||
} | ||
@@ -122,6 +138,7 @@ | ||
if ( ! lastValidText ) { | ||
if ( charByChar ) { return null ; } | ||
return null ; | ||
//if ( charByChar ) { return null ; } | ||
// Retry char by char | ||
return this.wordsUpToWidth( text , font , fontSize , maxWidth , true ) ; | ||
//return this.wordsUpToWidth( text , font , fontSize , maxWidth , true ) ; | ||
} | ||
@@ -140,2 +157,3 @@ | ||
var lastLine = this.lines[ this.lines.length - 1 ] ; | ||
this.currentLine = new Line( this , { | ||
@@ -145,3 +163,11 @@ y: lastLine ? lastLine.y + lastLine.height : 0 , | ||
align: this.lineAlign | ||
// Give justify only when there is another line? | ||
//justify: this.justify | ||
} ) ; | ||
if ( lastLine && this.justify ) { | ||
lastLine.justify = this.justify ; | ||
} | ||
this.lines.push( this.currentLine ) ; | ||
@@ -156,6 +182,5 @@ } ; | ||
var contentBox = new ContentBox( this , { | ||
var contentBox = new TextContentBox( this , { | ||
width: textWidth , | ||
height: textHeight , | ||
type: 'text' , | ||
content: content | ||
@@ -162,0 +187,0 @@ } ) ; |
@@ -33,8 +33,10 @@ /* | ||
function noop() {} | ||
function Line( doc , options ) { | ||
Box.call( this , doc , options ) ; | ||
this.contentWidth = 0 ; | ||
this.contentBoxes = [] ; | ||
this.justify = !! options.justify ; | ||
} | ||
@@ -51,3 +53,5 @@ | ||
contentBox.parent = this ; | ||
contentBox.x = this.contentWidth ; | ||
if ( this.height < contentBox.height ) { this.height = contentBox.height ; } | ||
this.contentWidth += contentBox.width ; | ||
this.contentBoxes.push( contentBox ) ; | ||
@@ -58,5 +62,58 @@ } ; | ||
// Recompute content position with inner adjustements | ||
Line.prototype.reflow = function reflow( fn ) { | ||
fn = fn || noop ; | ||
this.contentWidth = 0 ; | ||
this.contentBoxes.forEach( contentBox => { | ||
contentBox.x = this.contentWidth ; | ||
fn( contentBox ) ; | ||
this.contentWidth += contentBox.width ; | ||
} ) ; | ||
} ; | ||
Line.prototype.justifyContent = function justifyContent() { | ||
var spaceCount , spaceWidth ; | ||
if ( ! this.justify || this.contentWidth >= this.width ) { return ; } | ||
spaceCount = this.contentBoxes.reduce( ( spaceCount_ , contentBox ) => spaceCount_ + contentBox.spaceCount() , 0 ) ; | ||
if ( spaceCount ) { | ||
spaceWidth = ( this.width - this.contentWidth ) / spaceCount ; | ||
this.reflow( contentBox => contentBox.resizeSpaces( spaceWidth ) ) ; | ||
} | ||
if ( this.contentWidth + 0.001 < this.width && this.contentBoxes.length === 1 ) { | ||
spaceCount = this.contentBoxes.reduce( ( spaceCount_ , contentBox ) => spaceCount_ + contentBox.spaceCount( true ) , 0 ) ; | ||
if ( spaceCount ) { | ||
spaceWidth = ( this.width - this.contentWidth ) / spaceCount ; | ||
this.reflow( contentBox => contentBox.resizeSpaces( spaceWidth , true ) ) ; | ||
} | ||
} | ||
} ; | ||
Line.prototype.trim = function trim() { | ||
if ( this.contentBoxes.length === 1 ) { | ||
this.contentBoxes[ 0 ].trim() ; | ||
this.reflow() ; | ||
} | ||
else if ( this.contentBoxes.length > 1 ) { | ||
this.contentBoxes[ 0 ].trimLeft() ; | ||
this.contentBoxes[ this.contentBoxes.length - 1 ].trimRight() ; | ||
this.reflow() ; | ||
} | ||
} ; | ||
Line.prototype.render = function render() { | ||
this.trim() ; | ||
this.justifyContent() ; | ||
this.contentBoxes.forEach( contentBox => contentBox.render() ) ; | ||
} ; | ||
{ | ||
"name": "bobbox", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Agnostic box-model and text-flow", | ||
@@ -12,3 +12,5 @@ "main": "lib/bobbox.js", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"string-kit": "^0.6.7" | ||
}, | ||
"devDependencies": { | ||
@@ -15,0 +17,0 @@ "expect.js": "^0.3.1", |
28162
15
591
1
+ Addedstring-kit@^0.6.7
+ Added@babel/runtime-corejs3@7.26.0(transitive)
+ Addedcore-js-pure@3.40.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
+ Addedstring-kit@0.6.18(transitive)
+ Addedxregexp@4.4.1(transitive)