Skip to content

Commit

Permalink
Fix so that textBox will fire promise if default font is used; also, …
Browse files Browse the repository at this point in the history
…update local samples
  • Loading branch information
conniefry committed Feb 2, 2016
1 parent cdc85d2 commit 9bec68c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/pxScene2d/src/jsbindings/fonts_xfinity_text2.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Promise.all(promises).then(function(success, failure) {
console.log("IN PROMISE n="+n);
var t = elems[n];
t.draw = true;
var fontMetrics = t.getFontMetrics();
var fontMetrics = t.font.getFontMetrics(t.pixelSize);
console.log("natural leading is "+fontMetrics.naturalLeading);
console.log("fontMetrics.height="+fontMetrics.height);
console.log("fontMetrics.baseline="+fontMetrics.baseline);
Expand Down
21 changes: 13 additions & 8 deletions examples/pxScene2d/src/jsbindings/text_measure.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@ function createRect(props) {
}

function onTextReady(text, props, bg) {

console.log("onTextReady for text "+text.text);
// measure
var metrics = text.getFontMetrics();
var metrics = text.font.getFontMetrics(text.pixelSize);
var measurements = text.measureText();
var bounds = measurements.bounds;
var charFirst = measurements.charFirst;
var charLast = measurements.charLast;
var w = bounds.x2 - bounds.x1;
var spacing = metrics.height + props.leading;
var baseline = metrics.baseline + text.y;

// show measurements
var x = 0;
var y = 0;
do {
createRect({parent:bg, fillColor:green, x:x, y:y + metrics.baseline - metrics.ascent, w:w, h:metrics.ascent});
createRect({parent:bg, fillColor:blue, y:y + metrics.baseline, w:w, h:metrics.descent});
createRect({parent:bg, fillColor:green, x:x, y:y + baseline - metrics.ascent, w:w, h:metrics.ascent});
createRect({parent:bg, fillColor:blue, y:y + baseline, w:w, h:metrics.descent});
createRect({parent:bg, lineColor:red, lineWidth:1, x:x, y:y, w:w, h:metrics.height});
y += spacing;
} while (y < bounds.y2);
Expand All @@ -40,14 +43,16 @@ function onTextReady(text, props, bg) {
function createText(x, y, props) {
var bg = scene.create({t:"object", parent:root, x:x, y:y, w:1000, h:1000, clip:false});
var container = scene.create({t:"object", parent:root, x:x, y:y, w:1000, h:1000, clip:false});
var obj = {t:"textBox", fontUrl:"FreeSans.ttf", w:1000, h:1000, parent:container, textColor:0xFFFFFFFF, pixelSize:25, x:0, y:0, leading:0, wordWrap:false, clip:false};
var obj = {t:"textBox", w:1000, h:1000, parent:container, textColor:0xFFFFFFFF, pixelSize:25, x:0, y:0, leading:0, wordWrap:false, clip:false};
for (var i in props) obj[i] = props[i];
var text2 = scene.create(obj);
text2.ready.then(function(text) { onTextReady(text2, obj, bg); });
text2.ready.then(function(text) { onTextReady(text, obj, bg); });
return text2;
}

createText(0, 0, {fontUrl:"FreeSans.ttf", text:(textA + "\n" + textA + "\n" + textA), wordWrap:false, clip:false});
createText(0, 200, {fontUrl:"http://www.w3schools.com/cssref/sansation_light.woff", text:(textB + " " + textB + " " + textB), wordWrap:true, clip:false});
createText(0, 400, {fontUrl:"DancingScript-Bold.ttf", text:(textB + "\n" + textB + "\n" + textB), wordWrap:true, clip:false, pixelSize:35, leading:20});
createText(0, 0, { text:(textA + "\n" + textA + "\n" + textA), wordWrap:false, clip:false});
createText(0, 200, {fontUrl:"http://www.w3schools.com/cssref/sansation_light.woff", text:(textB + " " + textB + " " + textB), wordWrap:true, clip:false,pixelSize:30});
createText(0, 400, {fontUrl:"http://54.146.54.142/tom/receiverdotjs/fonts/DejaVuSans.ttf", text:(textB + "\n" + textB + "\n" + textB), wordWrap:true, clip:false, pixelSize:35, leading:20});

//http://www.w3schools.com/cssref/sansation_light.woff"
//http://54.146.54.142/tom/xre2/apps/receiver/fonts/XFINITYSansTT-New-Med.ttf
6 changes: 4 additions & 2 deletions examples/pxScene2d/src/pxTextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ void pxTextBox::onInit()
{
//printf("pxTextBox::onInit. mFontLoaded=%d\n",mFontLoaded);
mInitialized = true;
if( mFontLoaded) {
// If this is using the default font, we would not get a callback
if(mFontLoaded || getFontResource()->isFontLoaded())
{
mFontLoaded = true;
setNeedsRecalc(true);

}
}

Expand Down

0 comments on commit 9bec68c

Please sign in to comment.