jeudi 7 juillet 2016

how to measure the font metrics in javascript? Is there any API available for text metrics like GetCellDescent in C#?

function calculateSpanHeight(testLine,font){
          var spanElement = $(document.createElement("span")).css("white-space","pre");
          spanElement[0].id = "tempSpan";
          var spanElement = $(spanElement).html(testLine);
          $(spanElement).appendTo("body");
          $(spanElement).css("font",context.font);
          var testWidth = $("#tempSpan").width();
          var height = $("#tempSpan").height();

          var block = $('<div style="display: inline-block; width: 1px; height: 0px;"></div>');

          var div = $('<div></div>');
          div.append(spanElement, block);

          var body = $('body');
          body.append(div);

          var result = {};

          block.css({ verticalAlign: 'baseline' });
          result.ascent = block.offset().top - spanElement.offset().top;

          block.css({ verticalAlign: 'bottom' });
          result.height = block.offset().top - spanElement.offset().top;

          result.descent = result.height - result.ascent;
          $(div).remove();
          return {"testWidth" : testWidth, "height" : height, "spanElement" : spanElement, "ascent" : result.ascent, "descent" : result.descent};
      }

I have used this following code. It is working fine for getting the text width and height values exactly. But i need the text ascent and descent values exactly that is in float value. Please help me and Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire