summaryrefslogtreecommitdiffstats
path: root/awt/org/apache/harmony/awt/gl/font/CommonGlyphVector.java
blob: 4040a60d9207a0b210736f3476312b7f4b8ee69f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Ilya S. Okomin
 * @version $Revision$
 */
package org.apache.harmony.awt.gl.font;

import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphJustificationInfo;
import java.awt.font.GlyphMetrics;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import org.apache.harmony.awt.internal.nls.Messages;

/**
 * GlyphVector implementation
 */
public class CommonGlyphVector extends GlyphVector {

    // array of transforms of glyphs in GlyphVector
    protected AffineTransform[] glsTransforms;

    // array of chars defined in constructor
    public char[] charVector;

    // array of Glyph objects, that describe information about glyphs
    public Glyph[] vector;

    // array of default positions of glyphs in GlyphVector
    // without applying GlyphVector's transform
    float[] defaultPositions;

    // array of logical positions of glyphs in GlyphVector

    float[] logicalPositions;

    // array of visual (real) positions of glyphs in GlyphVector
    public float[] visualPositions;

    // FontRenderContext for this vector.
    protected FontRenderContext vectorFRC;

    // layout flags mask
    protected int layoutFlags = 0;

    // array of cached glyph outlines 
    protected Shape[] gvShapes;

    FontPeerImpl peer;

    // font corresponding to the GlyphVector 
    Font font;

    // ascent of the font
    float ascent;

    // height of the font
    float height;
    
    // leading of the font
    float leading;
    
    // descent of the font
    float descent;

    // transform of the GlyphVector
    AffineTransform transform;

    /**
     * Creates new CommonGlyphVector object from the specified parameters.
     * 
     * @param chars an array of chars
     * @param frc FontRenderContext object
     * @param fnt Font object
     * @param flags layout flags
     */
    @SuppressWarnings("deprecation")
    public CommonGlyphVector(char[] chars, FontRenderContext frc, Font fnt,
            int flags) {
        int len = chars.length;

        this.font = fnt;
        this.transform = fnt.getTransform();
        this.peer = (FontPeerImpl) fnt.getPeer();

        gvShapes = new Shape[len];

        // !! As pointed in API documentation for the 
        // getGlyphPosisitions(int index,int numEntries, float[] positionReturn) 
        // and getGlyphPosition(int index) methods, if the index is equals to 
        // the number of glyphs the position after the last glyph must be 
        // returned, thus there are n+1 positions and last (n+1) position 
        // points to the end of GlyphVector.

        logicalPositions = new float[(len+1)<<1];
        visualPositions = new float[(len+1)<<1];
        defaultPositions = new float[(len+1)<<1];

        glsTransforms = new AffineTransform[len];

        this.charVector = chars;
        this.vectorFRC = frc;
        //LineMetricsImpl lmImpl = (LineMetricsImpl)peer.getLineMetrics();

        LineMetricsImpl lmImpl = (LineMetricsImpl)fnt.getLineMetrics(String.valueOf(chars), frc);

        this.ascent = lmImpl.getAscent();
        this.height = lmImpl.getHeight();
        this.leading = lmImpl.getLeading();
        this.descent = lmImpl.getDescent();
        this.layoutFlags = flags;

        if ((flags & Font.LAYOUT_RIGHT_TO_LEFT) != 0){
            char vector[] = new char[len];
            for(int i=0; i < len; i++){
                vector[i] = chars[len-i-1];
            }
            this.vector = peer.getGlyphs(vector);

        } else {
            this.vector = peer.getGlyphs(chars);
        }

        this.glsTransforms = new AffineTransform[len];

        setDefaultPositions();
        performDefaultLayout();
    }

    /**
     * Creates new CommonGlyphVector object from the specified parameters. 
     * Layout flags set to default.
     * 
     * @param chars an array of chars
     * @param frc FontRenderContext object
     * @param fnt Font object
     */
    public CommonGlyphVector(char[] chars, FontRenderContext frc, Font fnt) {
        this(chars, frc, fnt, 0);
    }

    /**
     * Creates new CommonGlyphVector object from the specified parameters. 
     * Layout flags set to default.
     * 
     * @param str specified string
     * @param frc FontRenderContext object
     * @param fnt Font object
     */
    public CommonGlyphVector(String str, FontRenderContext frc, Font fnt) {
        this(str.toCharArray(), frc, fnt, 0);
    }

    /**
     * Creates new CommonGlyphVector object from the specified parameters.
     * 
     * @param str specified string
     * @param frc FontRenderContext object
     * @param fnt Font object
     * @param flags layout flags
     */
    public CommonGlyphVector(String str, FontRenderContext frc, Font fnt, int flags) {
        this(str.toCharArray(), frc, fnt, flags);
    }

    /**
     * Set array of logical positions of the glyphs to
     * default with their default advances and height.
     */
    void setDefaultPositions(){
        int len = getNumGlyphs();

        // First [x,y] is set into [0,0] position
        // for this reason start index is 1
        for (int i=1; i <= len; i++ ){
                int idx = i << 1;
                float advanceX = vector[i-1].getGlyphPointMetrics().getAdvanceX();
                float advanceY = vector[i-1].getGlyphPointMetrics().getAdvanceY();

                defaultPositions[idx] = defaultPositions[idx-2] + advanceX;
                defaultPositions[idx+1] = defaultPositions[idx-1] + advanceY;

        }
        transform.transform(defaultPositions, 0, logicalPositions, 0, getNumGlyphs()+1);

    }

    /**
     * Returnes the pixel bounds of this GlyphVector rendered at the 
     * specified x,y location with the given FontRenderContext.
     *  
     * @param frc a FontRenderContext that is used
     * @param x specified x coordinate value
     * @param y specified y coordinate value
     * @return a Rectangle that bounds pixels of this GlyphVector
     */
    @Override
    public Rectangle getPixelBounds(FontRenderContext frc, float x, float y) {

        double xM, yM, xm, ym;

        double minX = 0;
        double minY = 0;
        double maxX = 0;
        double maxY = 0;

        for (int i = 0; i < this.getNumGlyphs(); i++) {
            Rectangle glyphBounds = this.getGlyphPixelBounds(i, frc, 0, 0);
            xm = glyphBounds.getMinX();
            ym = glyphBounds.getMinY();
            xM = glyphBounds.getMaxX();
            yM = glyphBounds.getMaxY();

            if (i == 0) {
                minX = xm;
                minY = ym;
                maxX = xM;
                maxY = yM;
            }

            if (minX > xm) {
                minX = xm;
            }
            if (minY > ym) {
                minY = ym;
            }
            if (maxX < xM) {
                maxX = xM;
            }
            if (maxY < yM) {
                maxY = yM;
            }
        }
        return new Rectangle((int)(minX + x), (int)(minY + y), (int)(maxX - minX), (int)(maxY - minY));

    }

    /**
     * Returns the visual bounds of this GlyphVector.
     * The visual bounds is the bounds of the total outline of 
     * this GlyphVector.
     * @return a Rectangle2D that id the visual bounds of this GlyphVector
     */
    @Override
    public Rectangle2D getVisualBounds() {
        float xM, yM, xm, ym;
        float minX = 0;
        float minY = 0;
        float maxX = 0;
        float maxY = 0;
        boolean firstIteration = true;

        for (int i = 0; i < this.getNumGlyphs(); i++) {
            Rectangle2D bounds = this.getGlyphVisualBounds(i).getBounds2D();
            if (bounds.getWidth() == 0){
                continue;
            }
            xm = (float)bounds.getX();
            ym = (float)bounds.getY();

            xM = (float)(xm + bounds.getWidth());

            yM = ym + (float) bounds.getHeight();

            if (firstIteration) {
                minX = xm;
                minY = ym;
                maxX = xM;
                maxY = yM;
                firstIteration = false;
            } else {
                if (minX > xm) {
                    minX = xm;
                }
                if (minY > ym) {
                    minY = ym;
                }
                if (maxX < xM) {
                    maxX = xM;
                }
                if (maxY < yM) {
                    maxY = yM;
                }

            }
        }

        return (this.getNumGlyphs() != 0) ? new Rectangle2D.Float(minX, minY,
                (maxX - minX), (maxY - minY)) : null;
    }

    /**
     * Sets new position to the specified glyph.
     */
    @Override
    public void setGlyphPosition(int glyphIndex, Point2D newPos) {
        if ((glyphIndex > vector.length) || (glyphIndex < 0)) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IndexOutOfBoundsException(Messages.getString("awt.43")); //$NON-NLS-1$
        }
        float x = (float)newPos.getX();
        float y = (float)newPos.getY();
        int index = glyphIndex << 1;

        if ((x != visualPositions[index]) || (y != visualPositions[index + 1])){
            visualPositions[index] = x;
            visualPositions[index+1] = y;
            layoutFlags = layoutFlags | FLAG_HAS_POSITION_ADJUSTMENTS;
        }

    }

    /**
     * Returns the position of the specified glyph relative to the origin of
     * this GlyphVector
     * @return a Point2D that the origin of the glyph with specified index
     */
    @Override
    public Point2D getGlyphPosition(int glyphIndex) {
        if ((glyphIndex > vector.length) || (glyphIndex < 0)) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IndexOutOfBoundsException(Messages.getString("awt.43")); //$NON-NLS-1$
        }
        int index = glyphIndex << 1;
        Point2D pos = new Point2D.Float(visualPositions[index], visualPositions[index+1]);

        // For last position we don't have to transform !!
        if(glyphIndex==vector.length){
            return pos;
        }

        AffineTransform at = getGlyphTransform(glyphIndex);
        if ((at == null) || (at.isIdentity())){
            return pos;
        }

        pos.setLocation(pos.getX() + at.getTranslateX(), pos.getY() + at.getTranslateY());

        return pos;
    }

    /**
     * Sets new transform to the specified glyph.
     * 
     * @param glyphIndex specified index of the glyph
     * @param trans AffineTransform of the glyph with specified index
     */
    @Override
    public void setGlyphTransform(int glyphIndex, AffineTransform trans) {
        if ((glyphIndex >= vector.length) || (glyphIndex < 0)) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IndexOutOfBoundsException(Messages.getString("awt.43")); //$NON-NLS-1$
        }

        if ((trans == null) || (trans.isIdentity())) {
            glsTransforms[glyphIndex] = null;
        } else {
            glsTransforms[glyphIndex] = new AffineTransform(trans);
            layoutFlags = layoutFlags | FLAG_HAS_TRANSFORMS;
        }
    }

    /**
     * Returns the affine transform of the specified glyph.
     * 
     * @param glyphIndex specified index of the glyph
     * @return an AffineTransform of the glyph with specified index
     */
    @Override
    public AffineTransform getGlyphTransform(int glyphIndex) {
        if ((glyphIndex >= this.vector.length) || (glyphIndex < 0)) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IndexOutOfBoundsException(Messages.getString("awt.43")); //$NON-NLS-1$
        }
        return this.glsTransforms[glyphIndex];
    }

    /**
     * Returns the metrics of the specified glyph.
     * 
     * @param glyphIndex specified index of the glyph
     */
    @Override
    public GlyphMetrics getGlyphMetrics(int glyphIndex) {

        if ((glyphIndex < 0) || ((glyphIndex) >= this.getNumGlyphs())) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IndexOutOfBoundsException(Messages.getString("awt.43")); //$NON-NLS-1$
        }
        // TODO: is there a sence in GlyphMetrics
        // if certain glyph or Font has a transform??
        return this.vector[glyphIndex].getGlyphMetrics();
    }

    /**
     * Returns a justification information for the glyph with specified glyph 
     * index.
     * @param glyphIndex index of a glyph which GlyphJustificationInfo is to be 
     * received   
     * @return a GlyphJustificationInfo object that contains glyph justification 
     * properties of the specified glyph
     */
    @Override
    public GlyphJustificationInfo getGlyphJustificationInfo(int glyphIndex) {
        // TODO : Find out the source of Justification info
        if (true) {
            throw new RuntimeException("Method is not implemented"); //$NON-NLS-1$
        }
        return null;
    }

    /**
     * Returns the FontRenderContext parameter of this GlyphVector.
     */
    @Override
    public FontRenderContext getFontRenderContext() {
        return this.vectorFRC;
    }

    /**
     * Returns the visual bounds of the specified glyph.
     * 
     * @param glyphIndex specified index of the glyph
     */
    @Override
    public Shape getGlyphVisualBounds(int glyphIndex) {
        if ((glyphIndex < 0) || (glyphIndex >= this.getNumGlyphs())) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IndexOutOfBoundsException(Messages.getString("awt.43")); //$NON-NLS-1$
        }

        int idx  = glyphIndex << 1;

        AffineTransform fontTransform = this.transform;
        double xOffs = fontTransform.getTranslateX();
        double yOffs = fontTransform.getTranslateY();

        if (vector[glyphIndex].getWidth() == 0){
            return new Rectangle2D.Float((float)xOffs, (float)yOffs, 0, 0);
        }

        AffineTransform at = AffineTransform.getTranslateInstance(xOffs, yOffs);
        AffineTransform glyphTransform = getGlyphTransform(glyphIndex);

        if (transform.isIdentity() && ((glyphTransform == null) || glyphTransform.isIdentity())){
            Rectangle2D blackBox = vector[glyphIndex].getGlyphMetrics().getBounds2D();
            at.translate(visualPositions[idx], visualPositions[idx+1]);
            return(at.createTransformedShape(blackBox));
        }

        GeneralPath shape = (GeneralPath)this.getGlyphOutline(glyphIndex);
        shape.transform(at);
        return shape.getBounds2D();
    }

    /**
     * Returnes the pixel bounds of the specified glyph within GlyphVector 
     * rendered at the specified x,y location.
     *  
     * @param glyphIndex index of the glyph
     * @param frc a FontRenderContext that is used
     * @param x specified x coordinate value
     * @param y specified y coordinate value
     * @return a Rectangle that bounds pixels of the specified glyph
     */
    @Override
    public Rectangle getGlyphPixelBounds(int glyphIndex, FontRenderContext frc,
            float x, float y) {
        // TODO : need to be implemented with FontRenderContext
        if ((glyphIndex < 0) || (glyphIndex >= this.getNumGlyphs())) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IndexOutOfBoundsException(Messages.getString("awt.43")); //$NON-NLS-1$
        }

        int idx  = glyphIndex << 1;

        if (vector[glyphIndex].getWidth() == 0){
            AffineTransform fontTransform = this.transform;
            double xOffs = x + visualPositions[idx] + fontTransform.getTranslateX();
            double yOffs = y + visualPositions[idx+1] + fontTransform.getTranslateY();
            return new Rectangle((int)xOffs, (int)yOffs, 0, 0);
        }

        GeneralPath shape = (GeneralPath)this.getGlyphOutline(glyphIndex);

        AffineTransform at = AffineTransform.getTranslateInstance(x, y);

        if (frc != null){
            at.concatenate(frc.getTransform());
        }

        shape.transform(at);

        Rectangle bounds = shape.getBounds();
        return new Rectangle((int)bounds.getX(), (int)bounds.getY(),
                            (int)bounds.getWidth()-1, (int)bounds.getHeight()-1);
        }

    /**
     * Returns a Shape that encloses specified glyph.
     * 
     * @param glyphIndex specified index of the glyph
     */
    @Override
    public Shape getGlyphOutline(int glyphIndex) {
        if ((glyphIndex < 0) || (glyphIndex >= this.getNumGlyphs())) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IndexOutOfBoundsException(Messages.getString("awt.43")); //$NON-NLS-1$
        }

        if (gvShapes[glyphIndex] == null) {
            gvShapes[glyphIndex] = vector[glyphIndex].getShape();
        }

        GeneralPath gp = (GeneralPath)((GeneralPath)gvShapes[glyphIndex]).clone();

        /* Applying GlyphVector font transform */
        AffineTransform at = (AffineTransform)this.transform.clone();

        /* Applying Glyph transform */
        AffineTransform glyphAT = getGlyphTransform(glyphIndex);
        if (glyphAT != null){
            at.preConcatenate(glyphAT);
        }

        int idx  = glyphIndex << 1;

        gp.transform(at);
        gp.transform(AffineTransform.getTranslateInstance(visualPositions[idx], visualPositions[idx+1]));
        return gp;
    }


    /**
     * Returns a Shape that is the outline representation of this GlyphVector 
     * rendered at the specified x,y coordinates.
     * 
     * @param x specified x coordinate value
     * @param y specified y coordinate value
     * @return a Shape object that is the outline of this GlyphVector
     * at the specified coordinates.
     */
    @Override
    public Shape getOutline(float x, float y) {
        GeneralPath gp = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
        for (int i = 0; i < this.vector.length; i++) {
            GeneralPath outline = (GeneralPath)getGlyphOutline(i);

            /* Applying translation to actual visual bounds */
            outline.transform(AffineTransform.getTranslateInstance(x, y));
            gp.append(outline, false);
        }

        return gp;
    }

    /**
     * Returns a Shape that is the outline representation of this GlyphVector.
     * 
     * @return a Shape object that is the outline of this GlyphVector
     */
    @Override
    public Shape getOutline() {
        return this.getOutline(0, 0);
    }

    /**
     * Returns an array of glyphcodes for the specified glyphs.
     * 
     * @param beginGlyphIndex the start index
     * @param numEntries the number of glyph codes to get
     * @param codeReturn the array that receives glyph codes' values
     * @return an array that receives glyph codes' values
     */
    @Override
    public int[] getGlyphCodes(int beginGlyphIndex, int numEntries,
            int[] codeReturn) {

        if ((beginGlyphIndex < 0) || ((numEntries + beginGlyphIndex) > this.getNumGlyphs())) {
            // awt.44=beginGlyphIndex is out of vector's range
            throw new IndexOutOfBoundsException(Messages.getString("awt.44")); //$NON-NLS-1$
        }

        if (numEntries < 0) {
            // awt.45=numEntries is out of vector's range
            throw new IllegalArgumentException(Messages.getString("awt.45")); //$NON-NLS-1$
        }

        if (codeReturn == null) {
            codeReturn = new int[numEntries];
        }

        for (int i = beginGlyphIndex; i < beginGlyphIndex + numEntries; i++) {
            codeReturn[i-beginGlyphIndex] = this.vector[i].getGlyphCode();
        }

        return codeReturn;
    }

    /**
     * Returns an array of numEntries character indices for the specified glyphs.
     * 
     * @param beginGlyphIndex the start index
     * @param numEntries the number of glyph codes to get
     * @param codeReturn the array that receives glyph codes' values
     * @return an array that receives glyph char indices
     */
    @Override
    public int[] getGlyphCharIndices(int beginGlyphIndex, int numEntries,
            int[] codeReturn) {
        if ((beginGlyphIndex < 0) || (beginGlyphIndex >= this.getNumGlyphs())) {
            // awt.44=beginGlyphIndex is out of vector's range
            throw new IllegalArgumentException(Messages.getString("awt.44")); //$NON-NLS-1$
        }

        if ((numEntries < 0)
                || ((numEntries + beginGlyphIndex) > this.getNumGlyphs())) {
            // awt.45=numEntries is out of vector's range
            throw new IllegalArgumentException(Messages.getString("awt.45")); //$NON-NLS-1$
        }

        if (codeReturn == null) {
            codeReturn = new int[numEntries];
        }

        for (int i = 0; i < numEntries; i++) {
            codeReturn[i] = this.getGlyphCharIndex(i + beginGlyphIndex);
        }
        return codeReturn;
    }

    /**
     * Returns an array of numEntries glyphs positions from beginGlyphIndex
     * glyph in Glyph Vector.
     * 
     * @param beginGlyphIndex the start index
     * @param numEntries the number of glyph codes to get
     * @param positionReturn the array that receives glyphs' positions
     * @return an array of floats that receives glyph char indices
     */
    @Override
    public float[] getGlyphPositions(int beginGlyphIndex, int numEntries,
            float[] positionReturn) {

        int len = (this.getNumGlyphs()+1) << 1;
        beginGlyphIndex *= 2;
        numEntries *= 2;

        if ((beginGlyphIndex < 0) || ((numEntries + beginGlyphIndex) > len)) {
            // awt.44=beginGlyphIndex is out of vector's range
            throw new IndexOutOfBoundsException(Messages.getString("awt.44")); //$NON-NLS-1$
        }

        if (numEntries < 0) {
            // awt.45=numEntries is out of vector's range
            throw new IllegalArgumentException(Messages.getString("awt.45")); //$NON-NLS-1$
        }

        if (positionReturn == null) {
            positionReturn = new float[numEntries];
        }

        System.arraycopy(visualPositions, beginGlyphIndex, positionReturn, 0, numEntries);

        return positionReturn;
    }

    /**
     * Set numEntries elements of the visualPositions array from beginGlyphIndex
     * of numEntries glyphs positions from beginGlyphIndex glyph in Glyph Vector.
     * 
     * @param beginGlyphIndex the start index
     * @param numEntries the number of glyph codes to get
     * @param setPositions the array of positions to set
     */
    public void setGlyphPositions(int beginGlyphIndex, int numEntries,
            float[] setPositions) {

        int len = (this.getNumGlyphs()+1) << 1;
        beginGlyphIndex *= 2;
        numEntries *= 2;

        if ((beginGlyphIndex < 0) || ((numEntries + beginGlyphIndex) > len)) {
            // awt.44=beginGlyphIndex is out of vector's range
            throw new IndexOutOfBoundsException(Messages.getString("awt.44")); //$NON-NLS-1$
        }

        if (numEntries < 0) {
            // awt.45=numEntries is out of vector's range
            throw new IllegalArgumentException(Messages.getString("awt.45")); //$NON-NLS-1$
        }

        System.arraycopy(setPositions, 0, visualPositions, beginGlyphIndex, numEntries);
        layoutFlags = layoutFlags & FLAG_HAS_POSITION_ADJUSTMENTS;

    }

    /**
     * Set elements of the visualPositions array.
     * 
     * @param setPositions the array of positions to set
     */
    public void setGlyphPositions(float[] setPositions) {

        int len = (this.getNumGlyphs()+1) << 1;
        if (len != setPositions.length){
            // awt.46=length of setPositions array differs from the length of positions array
            throw new IllegalArgumentException(Messages.getString("awt.46")); //$NON-NLS-1$
        }

        System.arraycopy(setPositions, 0, visualPositions, 0, len);
        layoutFlags = layoutFlags & FLAG_HAS_POSITION_ADJUSTMENTS;

    }


    /**
     * Returns glyph code of the specified glyph.
     * 
     * @param glyphIndex specified index of the glyph
     */
    @Override
    public int getGlyphCode(int glyphIndex) {
        if (glyphIndex >= this.vector.length || glyphIndex < 0) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IndexOutOfBoundsException(Messages.getString("awt.43")); //$NON-NLS-1$
        }
        return this.vector[glyphIndex].getGlyphCode();
    }

    /**
     * Returns character index of the specified glyph.
     * 
     * @param glyphIndex specified index of the glyph
     */
    @Override
    public int getGlyphCharIndex(int glyphIndex) {

        if ((glyphIndex < 0) || (glyphIndex >= this.getNumGlyphs())) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IllegalArgumentException(Messages.getString("awt.43")); //$NON-NLS-1$
        }

        if ((this.layoutFlags & Font.LAYOUT_RIGHT_TO_LEFT) != 0) {
            return this.charVector.length - glyphIndex - 1;
        }

        return glyphIndex;
    }

    /**
     * Returns a character value of the specified glyph.
     * 
     * @param glyphIndex specified index of the glyph
     */
    public char getGlyphChar(int glyphIndex) {

        if ((glyphIndex < 0) || (glyphIndex >= this.getNumGlyphs())) {
            // awt.43=glyphIndex is out of vector's limits
            throw new IllegalArgumentException(Messages.getString("awt.43")); //$NON-NLS-1$
        }
        return this.charVector[glyphIndex];
    }

    /**
     * Assigns default positions to each glyph in this GlyphVector.
     */
    @Override
    public void performDefaultLayout() {

        System.arraycopy(logicalPositions, 0, visualPositions, 0, logicalPositions.length);

        // Set position changes flag to zero
        clearLayoutFlags(GlyphVector.FLAG_HAS_POSITION_ADJUSTMENTS);
    }

    /**
     * Returns the number of glyphs in this Glyph Vector
     */
    @Override
    public int getNumGlyphs() {
        return vector.length;
    }

    /**
     * Returns the logical bounds of this GlyphVector
     */
    @Override
    public Rectangle2D getLogicalBounds(){
        // XXX: for transforms where an angle between basis vectors is not 90 degrees
        // Rectanlge2D class doesn't fit as Logical bounds. For this reason we use
        // only non-transformed bounds!!

        float x = visualPositions[0];
        float width = visualPositions[visualPositions.length-2];

        double scaleY =  transform.getScaleY();

        Rectangle2D bounds = new Rectangle2D.Float(x, (float)((-this.ascent-this.leading)*scaleY), width, (float)(this.height*scaleY));
        return bounds;
    }


    /**
     * Checks whether given GlyphVector equals to this GlyphVector.
     * @param glyphVector GlyphVector object to compare
     */
    @Override
    public boolean equals(GlyphVector glyphVector){
        if (glyphVector == this){
            return true;
        }

        if (glyphVector != null) {

            if (!(glyphVector.getFontRenderContext().equals(this.vectorFRC) &&
                      glyphVector.getFont().equals(this.font))){
                return false;
            }

            try {
                boolean eq = true;
                for (int i = 0; i < getNumGlyphs(); i++) {

                    int idx = i*2;
                    eq = (((CommonGlyphVector)glyphVector).visualPositions[idx] == this.visualPositions[idx]) &&
                        (((CommonGlyphVector)glyphVector).visualPositions[idx+1] == this.visualPositions[idx+1]) &&
                        (glyphVector.getGlyphCharIndex(i) == this.getGlyphCharIndex(i));

                    if (eq){
                        AffineTransform trans = glyphVector.getGlyphTransform(i);
                        if (trans == null){
                            eq = (this.glsTransforms[i] == null);
                        }else{
                            eq = this.glsTransforms[i].equals(trans);
                        }
                    }

                    if (!eq){
                        return false;
                    }
                }

                return  eq;
            } catch (ClassCastException e) {
            }
        }

        return false;
    }


    /**
     * Returns flags describing the state of the GlyphVector.
     */
    @Override
    public int getLayoutFlags() {
        return layoutFlags;
    }

    /**
     * Returns char with the specified index.
     * 
     * @param index specified index of the char
     * 
     */
    public char getChar(int index) {
        return this.charVector[index];

    }

    /**
     * Clear desired flags in layout flags describing the state. 
     * 
     * @param clearFlags flags mask to clear 
     */
    
    private void clearLayoutFlags(int clearFlags){
        layoutFlags &= ~clearFlags;
    }

    /**
     * Returns the logical bounds of the specified glyph within this CommonGlyphVector.
     * 
     * @param glyphIndex index of the glyph to get it's logical bounds
     * @return logical bounds of the specified glyph
     */
    @Override
    public Shape getGlyphLogicalBounds(int glyphIndex){
        if ((glyphIndex < 0) || (glyphIndex >= this.getNumGlyphs())){
            // awt.43=glyphIndex is out of vector's limits
            throw new IndexOutOfBoundsException(Messages.getString("awt.43")); //$NON-NLS-1$
        }
        Glyph glyph = this.vector[glyphIndex];

        float x0 = visualPositions[glyphIndex*2];
        float y0 = visualPositions[glyphIndex*2+1];
        float advanceX = glyph.getGlyphPointMetrics().getAdvanceX();

        GeneralPath gp = new GeneralPath();
        gp.moveTo(0, -ascent - leading);
        gp.lineTo(advanceX ,-ascent - leading);
        gp.lineTo(advanceX, descent);
        gp.lineTo(0, descent);
        gp.lineTo(0, -ascent - leading);
        gp.closePath();

        /* Applying GlyphVector font transform */
        AffineTransform at = (AffineTransform)this.transform.clone();

        /* Applying Glyph transform */
        AffineTransform glyphTransform = getGlyphTransform(glyphIndex);
        if (glyphTransform != null){
            at.concatenate(glyphTransform);
        }

        /* Applying translation to actual visual bounds */
        at.preConcatenate(AffineTransform.getTranslateInstance(x0, y0));
        gp.transform(at);
        return gp;
    }

    /**
     * Returns the Font parameter of this GlyphVector
     */
    @Override
    public Font getFont(){
        return this.font;
    }


}