greatwar93 / gwt-g2d

Automatically exported from code.google.com/p/gwt-g2d
0 stars 0 forks source link

I need a method to measure text height in advance #3

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
It's a feature request. I need a method to measure text height in advance. 
Something like the one for width:
double width = surface.measureText("Hello geeks!");

Or even better - to get separate sizes above baseline and below baseline:
double height = surface.measureTextHeight("Hello geeks!");
double heightAbove = surface.measureTextAbove("Hello geeks!");
double heightBelow = surface.measureTextBelow("Hello geeks!");

Original issue reported on code.google.com by Bulatju@gmail.com on 10 Jan 2010 at 6:53

GoogleCodeExporter commented 8 years ago
Since measure text height is not part of the spec, I doubt if there is an 
efficient way 
to measure it in JavaScript alone. I will look into it to see if there is 
anything that 
can be done.

Original comment by hao1...@gmail.com on 10 Jan 2010 at 8:12

GoogleCodeExporter commented 8 years ago
This is how it's done in AbstractCanvas 
(http://code.google.com/p/abstractcanvas/)
for their implementation of text rendering by adding HTML widgets:
        private LinkedList<HTML> m_texts = new LinkedList<HTML>(); 
        private HTML m_textmetrics = new HTML();

        public void strokeText(String text, int x, int y)
        {
                String val = "<span style=\"font-family: " + m_fontname+"; color: " +
m_canvas.getStrokeStyleColor().toString()+"; font-size:
"+m_fontsize+"px;\">"+text+"</span>";
                AlignedHtml h = new AlignedHtml(val, this, m_align);
                Text t = new Text();
                t.x = x;
                t.y = y;
                m_texts.add(h);
                this.add(h,x,y);
                this.setWidgetPosition(h, x - h.getElement().getOffsetWidth(), y);
        }

        public TextMetrics measureText(String text) 
        {
                m_textmetrics.setHTML("<div style='font-size: " + m_fontsize + "px;
font-family: " + m_fontname +";'>"+text+"</div>");
                m_textmetrics.setVisible(true);
                double w = m_textmetrics.getOffsetWidth();
                double h = m_textmetrics.getOffsetHeight();
                m_textmetrics.setVisible(false);

                return new TextMetrics(w,h);
        }

Maybe there's a way to manage this similarly for other text rendering 
techniques that
you use.

Thanks a lot for the work you do!

Original comment by Bulatju@gmail.com on 10 Jan 2010 at 9:21

GoogleCodeExporter commented 8 years ago
Maybe you can use HTML widget for getting text metrics in your widget also ? I 
think
browsers render text in a 'canvas' element the very same way they do it in any 
other
type of element ('div', 'span', etc.).

Original comment by Bulatju@gmail.com on 10 Jan 2010 at 9:43

GoogleCodeExporter commented 8 years ago
I have confirmed that the AbstractCanvas way of measuring text does indeed 
work. I 
have uploaded the helper class TextMeasurer to svn 
(http://code.google.com/p/gwt-
g2d/source/browse/trunk/gwt-g2d/src/gwt/g2d/client/graphics/TextMeasurer.java). 

One thing to note is that all characters in the same font will give the same 
height, 
so a string of "m" may give the same height as a string of "M", even though "m" 
touches less pixels than "M". Another thing to note is that measuring text 
(both the 
measure text width supported natively and measure text height using 
AbstractCanvas 
method) ignores the current transformation state of the canvas. So for example, 
if 
you applied a scaling factor to canvas, you may need to manually scale the 
measured 
width/height to get the correct result.

Original comment by hao1...@gmail.com on 11 Jan 2010 at 2:18

GoogleCodeExporter commented 8 years ago
Thanks a lot!

About native measureText:
Maybe that is a bug in a browser ? Or is that explicitly specified in a spec ?
The work around could be to store applied transformation in a variable (or get 
it
from the context) and apply it to text metrics calculations. I don't need 
scaling
feature right now, but that would look more consistent. :)

Original comment by Bulatju@gmail.com on 11 Jan 2010 at 3:53

GoogleCodeExporter commented 8 years ago
The spec implies that measureText is based on the font property and makes no 
mention 
about the current transformation, so it may be assumed that measureText should 
ignore 
the current transformation.

I have switched the status for this bug issue to Fixed. Feel free to reopen it 
if 
further changes are needed.

Original comment by hao1...@gmail.com on 12 Jan 2010 at 1:13