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