<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Vertical centering of svg text, regardless of font: is there a way to measure glyph height? in Custom Visuals Development Discussion</title>
    <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Vertical-centering-of-svg-text-regardless-of-font-is-there-a-way/m-p/3683996#M8887</link>
    <description>&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/679652"&gt;@VizDataLtd&lt;/a&gt;&amp;nbsp; In order to vertically center a svg text to a svg rect, this should be the config&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;const text = svg.append('text')
	.each(
		function(_, i) {
			// taget element for alignment 
			const rect = /*get the rect node either with vanilla or d3*/ document.querySelector('rect');
			//get x+ half of width
			const midX = parseFloat(rect.getAttribute('x')) + parseFloat(rect.getAttribute('width')) / 2;
			//get y+ half of height
			const midY = parseFloat(rect.getAttribute('y')) + parseFloat(rect.getAttribute('height')) / 2;

			d3.select(this)
				.attr('x', midX)
				.attr('y', midY)
				// aligns as per the text length
				.attr('text-anchor', 'middle')
				.attr('dominant-baseline', 'middle')
				.text('Lorem Ipsum')
				.attr('font-size', '140px')
		}
	)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="smpa01_0-1707250649033.png" style="width: 400px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/1038429iE795167EF043F0B1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="smpa01_0-1707250649033.png" alt="smpa01_0-1707250649033.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;&amp;lt;div id="viz"&amp;gt;
  &amp;lt;svg id="svg" viewBox="0 0 1280 600"&amp;gt;
    &amp;lt;rect class="svg_container" width="1280" height="600" fill="#EFEFEF" stroke="black"&amp;gt;&amp;lt;/rect&amp;gt;
   &amp;lt;text x="640" y="300" text-anchor="middle" dominant-baseline="middle" font-size="140px"&amp;gt;Lorem Ipsum&amp;lt;/text&amp;gt;
  &amp;lt;/svg&amp;gt;
&amp;lt;/div&amp;gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 06 Feb 2024 20:34:36 GMT</pubDate>
    <dc:creator>smpa01</dc:creator>
    <dc:date>2024-02-06T20:34:36Z</dc:date>
    <item>
      <title>Vertical centering of svg text, regardless of font: is there a way to measure glyph height?</title>
      <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Vertical-centering-of-svg-text-regardless-of-font-is-there-a-way/m-p/3683617#M8884</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Setting dominant-baseline to middle isn't working for all fonts in cases where the text height exceeds svg height. I am trying to vertically center text on top of a rect element. Some fonts appear too low and others too high, while some appear just right. In short, some fonts are squat and some are tall. Is there a way for me to measure the actual glyphs, without the line height and bounding box of the whole text element?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Emma&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;kpiTextHeight = Math.round(textMeasurementService.measureSvgTextHeight(kpiTextProperties));&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried playing with baselineDelta but it doesn't seem to change much with the font.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;        this.kpiText
            .attr('text-anchor','middle')
            .attr('dominant-baseline','central')
            .attr('font-size',kpiFontSize+'pt')
            .attr('y', 0 )
            .attr('dy', 0)
            .attr('y', kpiRectHeight/2 )
            // .attr('dy', -kpiFontSize/2)
            .attr('font-family',kpiFont)
            .attr('x',svgWidth/2)
            .attr('fill', settings.kpiTextCard.kpiFontColorSlice.value.value)
            .attr('class','kpiNumber')
            .text(kpiValue);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this.kpiText sits within a group, which also contains a rect element of height kpiRectHeight.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2024 19:27:08 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Vertical-centering-of-svg-text-regardless-of-font-is-there-a-way/m-p/3683617#M8884</guid>
      <dc:creator>VizDataLtd</dc:creator>
      <dc:date>2024-02-06T19:27:08Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical centering of svg text, regardless of font: is there a way to measure glyph height?</title>
      <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Vertical-centering-of-svg-text-regardless-of-font-is-there-a-way/m-p/3683996#M8887</link>
      <description>&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/679652"&gt;@VizDataLtd&lt;/a&gt;&amp;nbsp; In order to vertically center a svg text to a svg rect, this should be the config&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;const text = svg.append('text')
	.each(
		function(_, i) {
			// taget element for alignment 
			const rect = /*get the rect node either with vanilla or d3*/ document.querySelector('rect');
			//get x+ half of width
			const midX = parseFloat(rect.getAttribute('x')) + parseFloat(rect.getAttribute('width')) / 2;
			//get y+ half of height
			const midY = parseFloat(rect.getAttribute('y')) + parseFloat(rect.getAttribute('height')) / 2;

			d3.select(this)
				.attr('x', midX)
				.attr('y', midY)
				// aligns as per the text length
				.attr('text-anchor', 'middle')
				.attr('dominant-baseline', 'middle')
				.text('Lorem Ipsum')
				.attr('font-size', '140px')
		}
	)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="smpa01_0-1707250649033.png" style="width: 400px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/1038429iE795167EF043F0B1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="smpa01_0-1707250649033.png" alt="smpa01_0-1707250649033.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;&amp;lt;div id="viz"&amp;gt;
  &amp;lt;svg id="svg" viewBox="0 0 1280 600"&amp;gt;
    &amp;lt;rect class="svg_container" width="1280" height="600" fill="#EFEFEF" stroke="black"&amp;gt;&amp;lt;/rect&amp;gt;
   &amp;lt;text x="640" y="300" text-anchor="middle" dominant-baseline="middle" font-size="140px"&amp;gt;Lorem Ipsum&amp;lt;/text&amp;gt;
  &amp;lt;/svg&amp;gt;
&amp;lt;/div&amp;gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2024 20:34:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Vertical-centering-of-svg-text-regardless-of-font-is-there-a-way/m-p/3683996#M8887</guid>
      <dc:creator>smpa01</dc:creator>
      <dc:date>2024-02-06T20:34:36Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical centering of svg text, regardless of font: is there a way to measure glyph height?</title>
      <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Vertical-centering-of-svg-text-regardless-of-font-is-there-a-way/m-p/3684040#M8888</link>
      <description>&lt;P&gt;Thanks, yes, that should work but it wasn't working at the extremes. I added a red line at the vertical midpoint and compared fonts. For anyone struggling with the same thing, I worked out that some fonts just sit lower. Constantia, for example, sits below the others. So while my visual works brilliantly with Arial and Times New Roman, I don't think there is a fix for some fonts.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2024 20:41:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Vertical-centering-of-svg-text-regardless-of-font-is-there-a-way/m-p/3684040#M8888</guid>
      <dc:creator>VizDataLtd</dc:creator>
      <dc:date>2024-02-06T20:41:52Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical centering of svg text, regardless of font: is there a way to measure glyph height?</title>
      <link>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Vertical-centering-of-svg-text-regardless-of-font-is-there-a-way/m-p/3684046#M8889</link>
      <description>&lt;P&gt;If you are using an external font (which I always do) it might be worth to wrap your calculations in &lt;A href="https://developer.mozilla.org/en-US/docs/Web/API/Document/fonts" target="_self"&gt;Document: fonts&amp;nbsp; &lt;/A&gt;promise and use &lt;CODE style="box-sizing: border-box; border-radius: var(--elem-radius); font-family: var(--font-code); font-size: var(--type-smaller-font-size); background: none; padding: 0px; width: fit-content; color: var(--text-primary);"&gt;&lt;SPAN class="token property"&gt;font-display&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;:&lt;/SPAN&gt; block&lt;SPAN class="token punctuation"&gt;; &lt;STRONG&gt;to ensure that the coordinates are only applied once the external fonts are loaded and not before that.&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2024 20:52:20 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Custom-Visuals-Development/Vertical-centering-of-svg-text-regardless-of-font-is-there-a-way/m-p/3684046#M8889</guid>
      <dc:creator>smpa01</dc:creator>
      <dc:date>2024-02-06T20:52:20Z</dc:date>
    </item>
  </channel>
</rss>

