@@ -15,8 +15,9 @@ var TRAILING = /(\s)+$/g;
1515 * @constructor
1616 * @param {FontProvider } fontProvider
1717 */
18- function TextTools ( fontProvider ) {
18+ function TextTools ( fontProvider , docMeasure ) {
1919 this . fontProvider = fontProvider ;
20+ this . docMeasure = docMeasure ;
2021}
2122
2223/**
@@ -28,20 +29,20 @@ function TextTools(fontProvider) {
2829 * @return {Object } collection of inlines, minWidth, maxWidth
2930 */
3031TextTools . prototype . buildInlines = function ( textArray , styleContextStack ) {
31- var measured = measure ( this . fontProvider , textArray , styleContextStack ) ;
32+ var measured = measure ( this . fontProvider , textArray , styleContextStack , this . docMeasure ) ;
3233
3334 var minWidth = 0 ,
3435 maxWidth = 0 ,
3536 currentLineWidth ;
3637
3738 measured . forEach ( function ( inline ) {
38- minWidth = Math . max ( minWidth , inline . width - inline . leadingCut - inline . trailingCut ) ;
39+ minWidth = Math . max ( minWidth , ( inline . width || inline . _width ) - inline . leadingCut - inline . trailingCut ) ;
3940
4041 if ( ! currentLineWidth ) {
4142 currentLineWidth = { width : 0 , leadingCut : inline . leadingCut , trailingCut : 0 } ;
4243 }
4344
44- currentLineWidth . width += inline . width ;
45+ currentLineWidth . width += inline . width || inline . _width ;
4546 currentLineWidth . trailingCut = inline . trailingCut ;
4647
4748 maxWidth = Math . max ( maxWidth , getTrimmedWidth ( currentLineWidth ) ) ;
@@ -62,7 +63,7 @@ TextTools.prototype.buildInlines = function (textArray, styleContextStack) {
6263 } ;
6364
6465 function getTrimmedWidth ( item ) {
65- return Math . max ( 0 , item . width - item . leadingCut - item . trailingCut ) ;
66+ return Math . max ( 0 , ( item . width || item . _width ) - item . leadingCut - item . trailingCut ) ;
6667 }
6768} ;
6869
@@ -164,6 +165,11 @@ function normalizeTextArray(array, styleContextStack) {
164165 var style = null ;
165166 var words ;
166167
168+ if ( item . image ) {
169+ results . push ( item )
170+ continue
171+ }
172+
167173 var noWrap = getStyleProperty ( item || { } , styleContextStack , 'noWrap' , false ) ;
168174 if ( isObject ( item ) ) {
169175 words = splitWords ( normalizeString ( item . text ) , noWrap ) ;
@@ -225,7 +231,7 @@ function getStyleProperty(item, styleContextStack, property, defaultValue) {
225231 }
226232}
227233
228- function measure ( fontProvider , textArray , styleContextStack ) {
234+ function measure ( fontProvider , textArray , styleContextStack , docMeasure ) {
229235 var normalized = normalizeTextArray ( textArray , styleContextStack ) ;
230236
231237 if ( normalized . length ) {
@@ -257,10 +263,14 @@ function measure(fontProvider, textArray, styleContextStack) {
257263
258264 var font = fontProvider . provideFont ( fontName , bold , italics ) ;
259265
260- item . width = widthOfString ( item . text , font , fontSize , characterSpacing , fontFeatures ) ;
261- item . height = font . lineHeight ( fontSize ) * lineHeight ;
266+ if ( item . image ) {
267+ docMeasure . measureImage ( item )
268+ } else {
269+ item . width = widthOfString ( item . text , font , fontSize , characterSpacing , fontFeatures ) ;
270+ item . height = font . lineHeight ( fontSize ) * lineHeight ;
271+ }
262272
263- var leadingSpaces = item . text . match ( LEADING ) ;
273+ var leadingSpaces = item . text ? item . text . match ( LEADING ) : [ ' ' ] ;
264274
265275 if ( ! item . leadingCut ) {
266276 item . leadingCut = 0 ;
@@ -270,7 +280,7 @@ function measure(fontProvider, textArray, styleContextStack) {
270280 item . leadingCut += widthOfString ( leadingSpaces [ 0 ] , font , fontSize , characterSpacing , fontFeatures ) ;
271281 }
272282
273- var trailingSpaces = item . text . match ( TRAILING ) ;
283+ var trailingSpaces = item . text ? item . text . match ( TRAILING ) : [ ' ' ] ;
274284 if ( trailingSpaces ) {
275285 item . trailingCut = widthOfString ( trailingSpaces [ 0 ] , font , fontSize , characterSpacing , fontFeatures ) ;
276286 } else {
0 commit comments