Home | History | Annotate | Download | only in font
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 /**
     18  * @author Ilya S. Okomin
     19  * @version $Revision$
     20  */
     21 package org.apache.harmony.awt.gl.font;
     22 
     23 import java.awt.font.LineMetrics;
     24 
     25 import org.apache.harmony.awt.internal.nls.Messages;
     26 
     27 /**
     28  *
     29  * LineMetrics implementation class.
     30  */
     31 
     32 public class LineMetricsImpl extends LineMetrics implements Cloneable{
     33 
     34     // array of baseline offsets
     35     float[] baselineOffsets;
     36 
     37     // the number of characters to measure
     38     int numChars;
     39 
     40     // baseline index of the font corresponding to this line metrics
     41     int baseLineIndex;
     42 
     43     // underline thickness
     44     float underlineThickness;
     45 
     46     // underline offset
     47     float underlineOffset;
     48 
     49     // strikethrough thickness
     50     float strikethroughThickness;
     51 
     52     // strikethrough offset
     53     float strikethroughOffset;
     54 
     55     // External leading
     56     float leading;
     57 
     58     // Height of the font ( == (ascent+descent+leading))
     59     float height;
     60 
     61     // Ascent of the font
     62     float ascent;
     63 
     64     // Descent of the font
     65     float descent;
     66 
     67     // Width of the widest char in the font
     68     float maxCharWidth;
     69 
     70     // underline thickness (in pixels)
     71     int lUnderlineThickness;
     72 
     73     // underline offset (in pixels)
     74     int lUnderlineOffset;
     75 
     76     // strikethrough thickness (in pixels)
     77     int lStrikethroughThickness;
     78 
     79     // strikethrough offset (in pixels)
     80     int lStrikethroughOffset;
     81 
     82     // External leading (in pixels)
     83     int lLeading;
     84 
     85     // Height of the font ( == (ascent+descent+leading)) (in pixels)
     86     int lHeight;
     87 
     88     // Ascent of the font (in pixels)
     89     int lAscent;
     90 
     91     // Descent of the font (in pixels)
     92     int lDescent;
     93 
     94     //  Width of the widest char in the font (in pixels)
     95     int lMaxCharWidth;
     96 
     97     // units per EM square in font value
     98     int units_per_EM = 0;
     99 
    100     /**
    101      * Creates LineMetricsImpl object from specified parameters. If baseline data parameter
    102      * is null than {0, (-ascent+descent)/2, -ascent} values are used for baseline offsets.
    103      *
    104      * @param len a number of characters
    105      * @param metrics an array of 16 elements with metrics values that can be
    106      * initialized in native code.<p>
    107      * metrics[0] - ascent<p>
    108      * metrics[1] - descent<p>
    109      * metrics[2] - external leading<p>
    110      * metrics[3] - underline thickness<p>
    111      * -metrics[4] - underline offset<p>
    112      * metrics[5] - strikethrough thickness<p>
    113      * -metrics[6] - strikethrough offset<p>
    114      * metrics[7] - maximum char width<p>
    115      * metrics[8] - ascent in pixels<p>
    116      * metrics[9] - descent in pixles<p>
    117      * metrics[10] - external leading in pixels<p>
    118      * metrics[11] - underline thickness in pixels<p>
    119      * -metrics[12] - underline offset in pixels<p>
    120      * metrics[13] - strikethrough thickness in pixels<p>
    121      * -metrics[14] - strikethrough offset in pixels<p>
    122      * metrics[15] - maximum char width in pixels<p>
    123 
    124      * @param _baselineData an array of 3 elements with baseline offsets metrics<p>
    125      * _baselineData[0] - roman baseline offset<p>
    126      * _baselineData[1] - center baseline offset<p>
    127      * _baselineData[2] - hanging baseline offset<p>
    128      */
    129     public LineMetricsImpl(int len, float[] metrics, float[] _baselineData){
    130         numChars = len;
    131 
    132         ascent = metrics[0];    // Ascent of the font
    133         descent = metrics[1];   // Descent of the font
    134         leading = metrics[2];  // External leading
    135         height = metrics[0] + metrics[1] + metrics[2];  // Height of the font ( == (ascent + descent + leading))
    136     }
    137 
    138     /**
    139      * Creates LineMetricsImpl object from specified parameters. If baseline data parameter
    140      * is null than {0, (-ascent+descent)/2, -ascent} values are used for baseline offsets.
    141      *
    142      * @param _numChars number of chars
    143      * @param _baseLineIndex index of the baseline offset
    144      * @param _baselineOffsets an array of baseline offsets
    145      * @param _underlineThickness underline thickness
    146      * @param _underlineOffset underline offset
    147      * @param _strikethroughThickness strikethrough thickness
    148      * @param _strikethroughOffset strinkethrough offset
    149      * @param _leading leading of the font
    150      * @param _height font height
    151      * @param _ascent ascent of the font
    152      * @param _descent descent of the font
    153      * @param _maxCharWidth max char width
    154      */
    155     public LineMetricsImpl(int _numChars, int _baseLineIndex,
    156             float[] _baselineOffsets, float _underlineThickness,
    157             float _underlineOffset, float _strikethroughThickness,
    158             float _strikethroughOffset, float _leading, float _height,
    159             float _ascent, float _descent, float _maxCharWidth) {
    160 
    161         numChars = _numChars;
    162         baseLineIndex = _baseLineIndex;
    163         underlineThickness = _underlineThickness;
    164         underlineOffset = _underlineOffset;
    165         strikethroughThickness = _strikethroughThickness;
    166         strikethroughOffset = _strikethroughOffset;
    167         leading = _leading;
    168         height = _height;
    169         ascent = _ascent;
    170         descent = _descent;
    171         baselineOffsets = _baselineOffsets;
    172         lUnderlineThickness = (int) underlineThickness;
    173         lUnderlineOffset = (int) underlineOffset;
    174         lStrikethroughThickness = (int) strikethroughThickness;
    175         lStrikethroughOffset = (int) strikethroughOffset;
    176         lLeading = (int) leading;
    177         lHeight = (int) height;
    178         lAscent = (int) ascent;
    179         lDescent = (int) descent;
    180         maxCharWidth = _maxCharWidth;
    181     }
    182 
    183     public LineMetricsImpl(){
    184 
    185     }
    186 
    187     /**
    188      * All metrics are scaled according to scaleX and scaleY values.
    189      * This function helps to recompute metrics according to the scale factors
    190      * of desired AffineTransform.
    191      *
    192      * @param scaleX scale X factor
    193      * @param scaleY scale Y factor
    194      */
    195     public void scale(float scaleX, float scaleY){
    196         float absScaleX = Math.abs(scaleX);
    197         float absScaleY = Math.abs(scaleY);
    198 
    199         underlineThickness *= absScaleY;
    200         underlineOffset *= scaleY;
    201         strikethroughThickness *= absScaleY;
    202         strikethroughOffset *= scaleY;
    203         leading *= absScaleY;
    204         height *= absScaleY;
    205         ascent *= absScaleY;
    206         descent *= absScaleY;
    207 
    208         if(baselineOffsets == null) {
    209             getBaselineOffsets();
    210         }
    211 
    212         for (int i=0; i< baselineOffsets.length; i++){
    213             baselineOffsets[i] *= scaleY;
    214         }
    215 
    216         lUnderlineThickness *= absScaleY;
    217         lUnderlineOffset *= scaleY;
    218         lStrikethroughThickness *= absScaleY;
    219         lStrikethroughOffset *= scaleY;
    220         lLeading  *= absScaleY;
    221         lHeight *= absScaleY;
    222         lAscent *= absScaleY;
    223         lDescent *= absScaleY;
    224         maxCharWidth *= absScaleX;
    225 
    226     }
    227 
    228 
    229     /**
    230      * Returns offset of the baseline.
    231      */
    232     @Override
    233     public float[] getBaselineOffsets() {
    234         // XXX: at the moment there only horizontal metrics are taken into
    235         // account. If there is no baseline information in TrueType font
    236         // file default values used: {0, -ascent, (-ascent+descent)/2}
    237 
    238         return baselineOffsets;
    239     }
    240 
    241     /**
    242      * Returns a number of chars in specified text
    243      */
    244     @Override
    245     public int getNumChars() {
    246         return numChars;
    247     }
    248 
    249     /**
    250      * Returns index of the baseline, one of predefined constants.
    251      */
    252     @Override
    253     public int getBaselineIndex() {
    254         // Baseline index is the deafult baseline index value
    255         // taken from the TrueType table "BASE".
    256         return baseLineIndex;
    257     }
    258 
    259     /**
    260      * Returns thickness of the Underline.
    261      */
    262     @Override
    263     public float getUnderlineThickness() {
    264         return underlineThickness;
    265     }
    266 
    267     /**
    268      * Returns offset of the Underline.
    269      */
    270     @Override
    271     public float getUnderlineOffset() {
    272         return underlineOffset;
    273     }
    274 
    275     /**
    276      * Returns thickness of the Strikethrough line.
    277      */
    278     @Override
    279     public float getStrikethroughThickness() {
    280         return strikethroughThickness;
    281     }
    282 
    283     /**
    284      * Returns offset of the Strikethrough line.
    285      */
    286     @Override
    287     public float getStrikethroughOffset() {
    288         return strikethroughOffset;
    289     }
    290 
    291     /**
    292      * Returns the leading.
    293      */
    294     @Override
    295     public float getLeading() {
    296         return leading;
    297     }
    298 
    299     /**
    300      * Returns the height of the font.
    301      */
    302     @Override
    303     public float getHeight() {
    304         //return height; // equals to (ascent + descent + leading);
    305     	return ascent + descent + leading;
    306     }
    307 
    308     /**
    309      * Returns the descent.
    310      */
    311     @Override
    312     public float getDescent() {
    313         return descent;
    314     }
    315 
    316     /**
    317      * Returns the ascent.
    318      */
    319     @Override
    320     public float getAscent() {
    321         return ascent;
    322     }
    323 
    324     /**
    325      * Returns logical thickness of the Underline.
    326      */
    327     public int getLogicalUnderlineThickness() {
    328         return lUnderlineThickness;
    329     }
    330 
    331     /**
    332      * Returns logical offset of the Underline.
    333      */
    334     public int getLogicalUnderlineOffset() {
    335         return lUnderlineOffset;
    336     }
    337 
    338     /**
    339      * Returns logical thickness of the Strikethrough line.
    340      */
    341     public int getLogicalStrikethroughThickness() {
    342         return lStrikethroughThickness;
    343     }
    344 
    345     /**
    346      * Returns logical offset of the Strikethrough line.
    347      */
    348     public int getLogicalStrikethroughOffset() {
    349         return lStrikethroughOffset;
    350     }
    351 
    352     /**
    353      * Returns the logical leading.
    354      */
    355     public int getLogicalLeading() {
    356         return lLeading;
    357     }
    358 
    359     /**
    360      * Returns the logical height of the font.
    361      */
    362     public int getLogicalHeight() {
    363         return lHeight; // equals to (ascent + descent + leading);
    364     }
    365 
    366     /**
    367      * Returns the logical descent.
    368      */
    369     public int getLogicalDescent() {
    370         return lDescent;
    371     }
    372 
    373     /**
    374      * Returns the logical ascent.
    375      */
    376     public int getLogicalAscent() {
    377         return lAscent;
    378     }
    379 
    380     /**
    381      * Returns the logical size of the widest char.
    382      */
    383     public int getLogicalMaxCharWidth() {
    384         return lMaxCharWidth;
    385     }
    386 
    387     /**
    388      * Returns the size of the widest char.
    389      */
    390     public float getMaxCharWidth() {
    391         return maxCharWidth;
    392     }
    393 
    394     /**
    395      * Set num chars to the desired value.
    396      *
    397      * @param num specified number of chars
    398      */
    399     public void setNumChars(int num){
    400         numChars = num;
    401     }
    402 
    403     @Override
    404     public Object clone(){
    405         try{
    406             return super.clone();
    407         }catch (CloneNotSupportedException e){
    408             return null;
    409         }
    410     }
    411 
    412 }