• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 18. Fonts > Class FontMetrics

18.4. Class FontMetrics

If you look at the implementation of the DrawText example, you can see that it uses the rather primitive strategy of splitting the vertical space in the shell into 10 equal-sized chunks (see the variable yAdvance), then aligns each of the text-drawing calls with the start of a chunk. Obviously, this is less than ideal because if the example window is resized to be much smaller, the text lines will draw on top of each other. To fix this problem, the code would need to measure the height of the text being drawn and move down an appropriate amount for each line. This kind of operation, called a measuring operation, is extremely common when building user interfaces that do text drawing. Some other examples of where this might be important are as follows.

  • Displaying lines of text using multiple fonts or styles

  • Wrapping text to fit within a fixed size area

  • Drawing the I-beam between characters

  • Aligning text with other drawings[8]

    [8] This includes things such as centering captions under images in graphs and drawing the “red squiggle” underline used as an error indicator in the Eclipse Java editor.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint