Skip to content

Commit

Permalink
InlineManager: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Mariotto committed Jan 21, 2022
1 parent 2d49e50 commit 05b5aae
Showing 1 changed file with 45 additions and 71 deletions.
116 changes: 45 additions & 71 deletions src/components/core/InlineManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,19 @@ export default function InlineManager( Base = class {} ) {

// computed by BoxComponent
const INNER_WIDTH = this.getWidth() - (this.padding * 2 || 0);

// Compute lines
const lines = this.computeLines();

/////////////////////////////////////////////////////////////////
// Position lines according to justifyContent and contentAlign
/////////////////////////////////////////////////////////////////

// got by BoxComponent
const INNER_HEIGHT = this.getHeight() - (this.padding * 2 || 0);

// got by MeshUIComponent
const JUSTIFICATION = this.getJustifyContent();
const ALIGNMENT = this.getAlignContent();
const INTERLINE = this.getInterLine();

// Compute lines
const lines = this.computeLines();

/////////////////////////////////////////////////////////////////
// Position lines according to justifyContent and contentAlign
/////////////////////////////////////////////////////////////////

// individual vertical offset

Expand Down Expand Up @@ -110,19 +107,16 @@ export default function InlineManager( Base = class {} ) {
}


calculateBestFit(bestFit) {
calculateBestFit( bestFit ) {

const inlineChildren = this.children.filter( (child)=> {

return child.isInline ? true : false

});

if(inlineChildren.length == 0)
{
return;
}
if ( inlineChildren.length == 0 ) return;

console.log("BEST FIT: " + bestFit);
switch(bestFit) {
case 'grow':
this.calculateGrowFit(inlineChildren);
Expand All @@ -137,13 +131,14 @@ export default function InlineManager( Base = class {} ) {

}

calculateGrowFit(inlineChildren) {
calculateGrowFit( inlineChildren ) {

const INNER_HEIGHT = this.getHeight() - (this.padding * 2 || 0);

//Iterative method to find a fontSize of text children that text will fit into container
let iterations = 1;
const heightTolerance = 0.075;
const firstText = inlineChildren.find(inlineComponent => inlineComponent.isText);
const firstText = inlineChildren.find( inlineComponent => inlineComponent.isText );

let minFontMultiplier = 1;
let maxFontMultiplier = 2;
Expand All @@ -154,16 +149,17 @@ export default function InlineManager( Base = class {} ) {

textHeight = this.calculateHeight(inlineChildren, fontMultiplier);

if(textHeight > INNER_HEIGHT) {
if ( textHeight > INNER_HEIGHT ) {

if(fontMultiplier <= minFontMultiplier) { // can't shrink text
if ( fontMultiplier <= minFontMultiplier ) { // can't shrink text

inlineChildren.forEach(inlineComponent => {

if ( inlineComponent.isInlineBlock ) return;

// ensure fontSize does not shrink
inlineComponent._fitFontSize = inlineComponent.getFontSize();

});

break;
Expand All @@ -173,29 +169,25 @@ export default function InlineManager( Base = class {} ) {
maxFontMultiplier = fontMultiplier;
fontMultiplier -= (maxFontMultiplier - minFontMultiplier) / 2;

}
else
{
if (Math.abs(INNER_HEIGHT - textHeight) < heightTolerance) {
break;
}
} else {

if(Math.abs(fontMultiplier - maxFontMultiplier) < 5e-10)
{
maxFontMultiplier *= 2;
}
if ( Math.abs(INNER_HEIGHT - textHeight) < heightTolerance ) break;

if ( Math.abs(fontMultiplier - maxFontMultiplier) < 5e-10 ) maxFontMultiplier *= 2;

minFontMultiplier = fontMultiplier;
fontMultiplier += (maxFontMultiplier - minFontMultiplier) / 2;
}

}while(++iterations <= 10 );
} while ( ++ iterations <= 10 );

}

calculateShrinkFit(inlineChildren) {
calculateShrinkFit( inlineChildren ) {

const INNER_HEIGHT = this.getHeight() - (this.padding * 2 || 0);

//Iterative method to find a fontSize of text children that text will fit into container
// Iterative method to find a fontSize of text children that text will fit into container
let iterations = 1;
const heightTolerance = 0.075;
const firstText = inlineChildren.find(inlineComponent => inlineComponent.isText);
Expand All @@ -209,43 +201,40 @@ export default function InlineManager( Base = class {} ) {

textHeight = this.calculateHeight(inlineChildren, fontMultiplier);

if(textHeight > INNER_HEIGHT)
{
if ( textHeight > INNER_HEIGHT ) {

maxFontMultiplier = fontMultiplier;
fontMultiplier -= (maxFontMultiplier - minFontMultiplier) / 2;

}
else
{
} else {

if(fontMultiplier >= maxFontMultiplier) { // can't grow text
if ( fontMultiplier >= maxFontMultiplier ) { // can't grow text

inlineChildren.forEach(inlineComponent => {

if ( inlineComponent.isInlineBlock ) return;

// ensure fontSize does not grow
inlineComponent._fitFontSize = inlineComponent.getFontSize();

});

break;

}

if (Math.abs(INNER_HEIGHT - textHeight) < heightTolerance) {
break;
}
if ( Math.abs(INNER_HEIGHT - textHeight) < heightTolerance ) break;

minFontMultiplier = fontMultiplier;
fontMultiplier += (maxFontMultiplier - minFontMultiplier) / 2;

}

}while(++iterations <= 10 );
} while ( ++ iterations <= 10 );
}

calculateAutoFit(inlineChildren) {
calculateAutoFit( inlineChildren ) {

const INNER_HEIGHT = this.getHeight() - (this.padding * 2 || 0);

//Iterative method to find a fontSize of text children that text will fit into container
Expand All @@ -262,27 +251,21 @@ export default function InlineManager( Base = class {} ) {

textHeight = this.calculateHeight(inlineChildren, fontMultiplier);

if(textHeight > INNER_HEIGHT)
{
if ( textHeight > INNER_HEIGHT ) {
maxFontMultiplier = fontMultiplier;
fontMultiplier -= (maxFontMultiplier - minFontMultiplier) / 2;
}
else
{
if (Math.abs(INNER_HEIGHT - textHeight) < heightTolerance) {
break;
}
} else {

if(Math.abs(fontMultiplier - maxFontMultiplier) < 5e-10)
{
maxFontMultiplier *= 2;
}
if ( Math.abs(INNER_HEIGHT - textHeight) < heightTolerance ) break;

if ( Math.abs(fontMultiplier - maxFontMultiplier) < 5e-10 ) maxFontMultiplier *= 2;

minFontMultiplier = fontMultiplier;
fontMultiplier += (maxFontMultiplier - minFontMultiplier) / 2;

}

}while(++iterations <= 10 );
} while ( ++ iterations <= 10 );
}

/**
Expand Down Expand Up @@ -323,8 +306,6 @@ export default function InlineManager( Base = class {} ) {
const xadvance = inline.xadvance ? inline.xadvance : inline.width;

// Line break
// const xoffset = inline.xadvance - inline.width;
// const xoffset = 0;

const nextBreak = this.distanceToNextBreak( inlines, i , letterSpacing );

Expand Down Expand Up @@ -392,7 +373,8 @@ export default function InlineManager( Base = class {} ) {

line.width = 0;
const lineHasInlines = line[0];
if( lineHasInlines ) {

if ( lineHasInlines ) {
// starts by processing whitespace, it will return a collapsed left offset
const WHITE_SPACE = this.getWhiteSpace();
const whiteSpaceOffset = Whitespace.collapseInlines(line, WHITE_SPACE);
Expand All @@ -413,7 +395,7 @@ export default function InlineManager( Base = class {} ) {

calculateHeight( inlineChildren, fontMultiplier ) {

inlineChildren.forEach(inlineComponent => {
inlineChildren.forEach( inlineComponent => {

if ( inlineComponent.isInlineBlock ) return;

Expand All @@ -432,10 +414,9 @@ export default function InlineManager( Base = class {} ) {

}, 0 ) + INTERLINE;

//

return Math.abs( textHeight );
}

/**
* Compute the width of a line
* @param line
Expand All @@ -448,11 +429,10 @@ export default function InlineManager( Base = class {} ) {

const lastInline = line[line.length-1];

return Math.abs(firstInline.offsetX - (lastInline.offsetX+lastInline.width));
return Math.abs( firstInline.offsetX - (lastInline.offsetX+lastInline.width) );

}


/**
* get the distance in world coord to the next glyph defined
* as break-line-safe ( like whitespace for instance )
Expand All @@ -471,22 +451,16 @@ export default function InlineManager( Base = class {} ) {
const xadvance = inline.xadvance ? inline.xadvance : inline.width ;

// if inline.lineBreak is set, it is 'mandatory' or 'possible'
if ( inline.lineBreak ) {

return accu + xadvance
if ( inline.lineBreak ) return accu + xadvance

// no line break is possible on this character
}

return this.distanceToNextBreak(
inlines,
currentIdx + 1,
letterSpacing,
accu + xadvance + letterSpacing + xoffset + kerning
);



}

/**
Expand Down

0 comments on commit 05b5aae

Please sign in to comment.