@@ -43,6 +43,7 @@ export type Shaping = {
4343 lineCount : number ,
4444 text : string ,
4545 yOffset : number ,
46+ hasBaseline : Boolean ,
4647} ;
4748
4849export type SymbolAnchor = 'center' | 'left' | 'right' | 'top' | 'bottom' | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' ;
@@ -209,7 +210,8 @@ function shapeText(text: Formatted,
209210 right : translate [ 0 ] ,
210211 writingMode,
211212 lineCount : lines . length ,
212- yOffset : - 17 // the y offset *should* be part of the font metadata
213+ yOffset : - 17 , // the y offset *should* be part of the font metadata
214+ hasBaseline : false
213215 } ;
214216
215217 shapeLines ( shaping , glyphs , lines , lineHeight , textAnchor , textJustify , writingMode , spacing , allowVerticalPlacement ) ;
@@ -498,16 +500,14 @@ function shapeLines(shaping: Shaping,
498500 const glyph = positions && positions [ codePoint ] ;
499501 if ( ! glyph ) continue ;
500502
501- // The rects have an addditional buffer that is not included in their size.
502- const glyphPadding = 1.0 ;
503- const rectBuffer = 3 + glyphPadding ;
504- // Each glyph's baseline is starting from its acsender, which is the vertical distance
505- // from the horizontal baseline to the highest ‘character’ coordinate in a font face.
506- // If ascender is applied, the shaping rect buffer needs to be counted.
507- // If ascender is not applicable, fall back to use a default baseline yOffset.
508- // Since we're laying out at 24 points, we need also calculate how much it will move
509- // when we scale up or down.
510- const baselineOffset = ( hasBaseline ? ( ( - glyph . metrics . ascender + rectBuffer * 2 ) * section . scale ) : shaping . yOffset ) + ( lineMaxScale - section . scale ) * 24 ;
503+ // In order to make different fonts aligned, they must share a general baseline that starts from the midline
504+ // of each font face. Baseline offset is the vertical distance from font face's baseline to its top most
505+ // position, which is the half size of the sum (ascender + descender). Since glyph's position is counted
506+ // from the top left corner, the negative shift is needed. So different fonts shares the same baseline but
507+ // with different offset shift. If font's baseline is not applicable, fall back to use a default baseline
508+ // offset, see shaping.yOffset. Since we're laying out at 24 points, we need also calculate how much it will
509+ // move when we scale up or down.
510+ const baselineOffset = ( hasBaseline ? ( ( - glyph . metrics . ascender + glyph . metrics . descender ) / 2 * section . scale ) : shaping . yOffset ) + ( lineMaxScale - section . scale ) * 24 ;
511511
512512 if ( writingMode === WritingMode . horizontal ||
513513 // Don't verticalize glyphs that have no upright orientation if vertical placement is disabled.
@@ -545,6 +545,7 @@ function shapeLines(shaping: Shaping,
545545 shaping . bottom = shaping . top + height ;
546546 shaping . left += - horizontalAlign * maxLineLength ;
547547 shaping . right = shaping . left + maxLineLength ;
548+ shaping . hasBaseline = hasBaseline ;
548549}
549550
550551// justify right = 1, left = 0, center = 0.5
0 commit comments