aboutsummaryrefslogtreecommitdiff
path: root/src/share/demo/java2d/J2DBench/src/j2dbench/tests/text/TextMeasureTests.java
blob: 6f9bb150b1dcdcb8081bb355bb2f88ec24b160ca (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
/*
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This source code is provided to illustrate the usage of a given feature
 * or technique and has been deliberately simplified. Additional steps
 * required for a production-quality application, such as security checks,
 * input validation and proper error handling, might not be present in
 * this sample code.
 */


/*
 * (C) Copyright IBM Corp. 2003, All Rights Reserved.
 * This technology is protected by multiple US and International
 * patents. This notice and attribution to IBM may not be removed.
 */

package j2dbench.tests.text;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.font.GlyphMetrics;
import java.awt.font.GlyphVector;
import java.awt.font.TextHitInfo;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.text.Bidi;
import java.util.ArrayList;

import j2dbench.Group;
import j2dbench.Result;
import j2dbench.TestEnvironment;

public abstract class TextMeasureTests extends TextTests {
    static Group measureroot;
    static Group measuretestroot;

    public static void init() {
        measureroot = new Group(textroot, "Measuring", "Measuring Benchmarks");
        measuretestroot = new Group(measureroot, "tests", "Measuring Tests");

        new StringWidth();
        new StringBounds();
        new CharsWidth();
        new CharsBounds();
        new FontCanDisplay();

        if (hasGraphics2D) {
            new GVWidth();
            new GVLogicalBounds();
            new GVVisualBounds();
            new GVPixelBounds();
            new GVOutline();
            new GVGlyphLogicalBounds();
            new GVGlyphVisualBounds();
            new GVGlyphPixelBounds();
            new GVGlyphOutline();
            new GVGlyphTransform();
            new GVGlyphMetrics();

            new TLAdvance();
            new TLAscent();
            new TLBounds();
            new TLGetCaretInfo();
            new TLGetNextHit();
            new TLGetCaretShape();
            new TLGetLogicalHighlightShape();
            new TLHitTest();
            new TLOutline();

        /*
            new FontLineMetrics();
            new FontStringBounds();
        */
        }
    }

    public TextMeasureTests(Group parent, String nodeName, String description) {
        super(parent, nodeName, description);
    }

    static class SWContext extends TextContext {
        FontMetrics fm;

        public void init(TestEnvironment env, Result results) {
            super.init(env, results);
            fm = graphics.getFontMetrics(font);
        }
    }

    public Context createContext() {
        return new SWContext();
    }

    public static class StringWidth extends TextMeasureTests {
        public StringWidth() {
            super(measuretestroot, "stringWidth", "Measuring String Width");
        }

        public void runTest(Object ctx, int numReps) {
            SWContext swctx = (SWContext)ctx;
            String text = swctx.text;
            FontMetrics fm = swctx.fm;
            int wid = 0;
            do {
                wid += fm.stringWidth(text);
            } while (--numReps >= 0);
        }
    }

    public static class StringBounds extends TextMeasureTests {
        public StringBounds() {
            super(measuretestroot, "stringBounds", "Measuring String Bounds");
        }

        public void runTest(Object ctx, int numReps) {
            SWContext swctx = (SWContext)ctx;
            String text = swctx.text;
            FontMetrics fm = swctx.fm;
            int wid = 0;
            Rectangle r = null;
            do {
                r = null;
                int dx = fm.stringWidth(text);
                int dy = fm.getAscent() + fm.getDescent() + fm.getLeading();
                int x = 0;
                int y = -fm.getAscent();
                r = new Rectangle(x, y, dx, dy);
            } while (--numReps >= 0);
        }
    }

    public static class CharsWidth extends TextMeasureTests {
        public CharsWidth() {
            super(measuretestroot, "charsWidth", "Measuring Chars Width");
        }

        public void runTest(Object ctx, int numReps) {
            SWContext swctx = (SWContext)ctx;
            FontMetrics fm = swctx.fm;
            char[] chars = swctx.chars;
            int wid = 0;
            do {
                wid += fm.charsWidth(chars, 0, chars.length);
            } while (--numReps >= 0);
        }
    }

    public static class CharsBounds extends TextMeasureTests {
        public CharsBounds() {
            super(measuretestroot, "charsBounds", "Measuring Chars Bounds");
        }

        public void runTest(Object ctx, int numReps) {
            SWContext swctx = (SWContext)ctx;
            FontMetrics fm = swctx.fm;
            char[] chars = swctx.chars;
            int wid = 0;
            Rectangle r = null;
            do {
                r = null;
                int dx = fm.charsWidth(chars, 0, chars.length);
                int dy = fm.getAscent() + fm.getDescent() + fm.getLeading();
                int x = 0;
                int y = -fm.getAscent();
                r = new Rectangle(x, y, dx, dy);
            } while (--numReps >= 0);
        }
    }

    public static class FontCanDisplay extends TextMeasureTests {
        public FontCanDisplay() {
            super(measuretestroot, "fontcandisplay", "Font canDisplay(char)");
        }

        public void runTest(Object ctx, int numReps) {
            Font font = ((TextContext)ctx).font;
            boolean b = false;
            do {
                for (int i = 0; i < 0x10000; i += 0x64) {
                    b ^= font.canDisplay((char)i);
                }
            } while (--numReps >= 0);
        }
    }

    public static class GVContext extends G2DContext {
        GlyphVector gv;

        public void init(TestEnvironment env, Result results) {
            super.init(env, results);

            int flags = Font.LAYOUT_LEFT_TO_RIGHT;
            if (Bidi.requiresBidi(chars, 0, chars.length)) { // assume rtl
                flags = Font.LAYOUT_RIGHT_TO_LEFT;
            }
            gv = font.layoutGlyphVector(frc, chars, 0, chars.length, flags);

            // gv options
        }
    }

    public static abstract class GVMeasureTest extends TextMeasureTests {
        protected GVMeasureTest(Group parent, String nodeName, String description) {
            super(parent, nodeName, description);
        }

        public Context createContext() {
            return new GVContext();
        }
    }

    public static class GVWidth extends GVMeasureTest {
        public GVWidth() {
            super(measuretestroot, "gvWidth", "Measuring GV Width");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            double wid = 0;
            do {
                wid += gv.getGlyphPosition(gv.getNumGlyphs()).getX();
            } while (--numReps >= 0);
        }
    }

    public static class GVLogicalBounds extends GVMeasureTest {
        public GVLogicalBounds() {
            super(measuretestroot, "gvLogicalBounds", "Measuring GV Logical Bounds");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            Rectangle2D r;
            do {
                r = gv.getLogicalBounds();
            } while (--numReps >= 0);
        }
    }

    public static class GVVisualBounds extends GVMeasureTest {
        public GVVisualBounds() {
            super(measuretestroot, "gvVisualBounds", "Measuring GV Visual Bounds");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            Rectangle2D r;
            do {
                r = gv.getVisualBounds();
            } while (--numReps >= 0);
        }
    }

    public static class GVPixelBounds extends GVMeasureTest {
        public GVPixelBounds() {
            super(measuretestroot, "gvPixelBounds", "Measuring GV Pixel Bounds");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            Rectangle2D r;
            do {
                r = gv.getPixelBounds(null, 0, 0); // !!! add opt to provide different frc?
            } while (--numReps >= 0);
        }
    }

    public static class GVOutline extends GVMeasureTest {
        public GVOutline() {
            super(measuretestroot, "gvOutline", "Getting GV Outline");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            Shape s;
            do {
                s = gv.getOutline();
            } while (--numReps >= 0);
        }
    }

    public static class GVGlyphLogicalBounds extends GVMeasureTest {
        public GVGlyphLogicalBounds() {
            super(measuretestroot, "gvGlyphLogicalBounds", "Measuring GV Glyph Logical Bounds");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            Shape s;
            do {
                for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
                    s = gv.getGlyphLogicalBounds(i);
                }
            } while (--numReps >= 0);
        }
    }

    public static class GVGlyphVisualBounds extends GVMeasureTest {
        public GVGlyphVisualBounds() {
            super(measuretestroot, "gvGlyphVisualBounds", "Measuring GV Glyph Visual Bounds");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            Shape s;
            do {
                for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
                    s = gv.getGlyphVisualBounds(i);
                }
            } while (--numReps >= 0);
        }
    }


    public static class GVGlyphPixelBounds extends GVMeasureTest {
        public GVGlyphPixelBounds() {
            super(measuretestroot, "gvGlyphPixelBounds", "Measuring GV Glyph Pixel Bounds");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            Rectangle2D r;
            do {
                for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
                    r = gv.getGlyphPixelBounds(i, null, 0, 0); // !!! add opt to provide different frc?
                }
            } while (--numReps >= 0);
        }
    }

    public static class GVGlyphOutline extends GVMeasureTest {
        public GVGlyphOutline() {
            super(measuretestroot, "gvGlyphOutline", "Getting GV Glyph Outline");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            Shape s;
            do {
                for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
                    s = gv.getGlyphOutline(i);
                }
            } while (--numReps >= 0);
        }
    }

    public static class GVGlyphTransform extends GVMeasureTest {
        public GVGlyphTransform() {
            super(measuretestroot, "gvGlyphTransform", "Getting GV Glyph Transform");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            AffineTransform tx;
            do {
                for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
                    tx = gv.getGlyphTransform(i);
                }
            } while (--numReps >= 0);
        }
    }

    public static class GVGlyphMetrics extends GVMeasureTest {
        public GVGlyphMetrics() {
            super(measuretestroot, "gvGlyphMetrics", "Getting GV Glyph Metrics");
        }

        public void runTest(Object ctx, int numReps) {
            GVContext gvctx = (GVContext)ctx;
            GlyphVector gv = gvctx.gv;
            GlyphMetrics gm;
            do {
                for (int i = 0, e = gv.getNumGlyphs(); i < e; ++i) {
                    gm = gv.getGlyphMetrics(i);
                }
            } while (--numReps >= 0);
        }
    }

    public static class TLContext extends G2DContext {
        TextLayout tl;

        public void init(TestEnvironment env, Result results) {
            super.init(env, results);

            // need more tl options here
            tl = new TextLayout(text, font, frc);
        }
    }

    public static abstract class TLMeasureTest extends TextMeasureTests {
        protected TLMeasureTest(Group parent, String nodeName, String description) {
            super(parent, nodeName, description);
        }

        public Context createContext() {
            return new TLContext();
        }
    }

    public static class TLAdvance extends TLMeasureTest {
        public TLAdvance() {
            super(measuretestroot, "tlAdvance", "Measuring TL advance");
        }

        public void runTest(Object ctx, int numReps) {
            TLContext tlctx = (TLContext)ctx;
            TextLayout tl = tlctx.tl;
            double wid = 0;
            do {
                wid += tl.getAdvance();
            } while (--numReps >= 0);
        }
    }

    public static class TLAscent extends TLMeasureTest {
        public TLAscent() {
            super(measuretestroot, "tlAscent", "Measuring TL ascent");
        }

        public void runTest(Object ctx, int numReps) {
            TLContext tlctx = (TLContext)ctx;
            TextLayout tl = tlctx.tl;
            float ht = 0;
            do {
                ht += tl.getAscent();
            } while (--numReps >= 0);
        }
    }

    public static class TLBounds extends TLMeasureTest {
        public TLBounds() {
            super(measuretestroot, "tlBounds", "Measuring TL advance");
        }

        public void runTest(Object ctx, int numReps) {
            TLContext tlctx = (TLContext)ctx;
            TextLayout tl = tlctx.tl;
            Rectangle2D r;
            do {
                r = tl.getBounds();
            } while (--numReps >= 0);
        }
    }

    static class TLExContext extends TLContext {
        TextHitInfo[] hits;
        Rectangle2D lb;

        public void init(TestEnvironment env, Result results) {
            super.init(env, results);

            ArrayList list = new ArrayList(text.length() * 2 + 2);
            TextHitInfo hit = TextHitInfo.trailing(-1);
            do {
                list.add(hit);
                hit = tl.getNextRightHit(hit);
            } while (hit != null);
            hits = (TextHitInfo[])list.toArray(new TextHitInfo[list.size()]);

            lb = tl.getBounds();
            lb.setRect(lb.getMinX() - 10, lb.getMinY(), lb.getWidth() + 20, lb.getHeight());
        }
    }

    public static abstract class TLExtendedMeasureTest extends TLMeasureTest {
        protected TLExtendedMeasureTest(Group parent, String nodeName, String description) {
            super(parent, nodeName, description);
        }

        public Context createContext() {
            return new TLExContext();
        }
    }

    public static class TLGetCaretInfo extends TLExtendedMeasureTest {
        public TLGetCaretInfo() {
            super(measuretestroot, "tlGetCaretInfo", "Measuring TL caret info");
        }

        public void runTest(Object ctx, int numReps) {
            TLExContext tlctx = (TLExContext)ctx;
            TextLayout tl = tlctx.tl;
            TextHitInfo[] hits = tlctx.hits;
            do {
                for (int i = 0; i < hits.length; ++i) {
                    tl.getCaretInfo(hits[i]);
                }
            } while (--numReps >= 0);
        }
    }

    public static class TLGetNextHit extends TLExtendedMeasureTest {
        public TLGetNextHit() {
            super(measuretestroot, "tlGetNextHit", "Measuring TL getNextRight/LeftHit");
        }

        public void runTest(Object ctx, int numReps) {
            TLExContext tlctx = (TLExContext)ctx;
            TextLayout tl = tlctx.tl;
            TextHitInfo[] hits = tlctx.hits;
            TextHitInfo hit;
            do {
                for (int i = 0; i < hits.length; ++i) {
                    hit = tl.getNextLeftHit(hits[i]);
                }
            } while (--numReps >= 0);
        }
    }

    public static class TLGetCaretShape extends TLExtendedMeasureTest {
        public TLGetCaretShape() {
            super(measuretestroot, "tlGetCaretShape", "Measuring TL getCaretShape");
        }

        public void runTest(Object ctx, int numReps) {
            TLExContext tlctx = (TLExContext)ctx;
            TextLayout tl = tlctx.tl;
            TextHitInfo[] hits = tlctx.hits;
            Shape s;
            do {
                for (int i = 0; i < hits.length; ++i) {
                    s = tl.getCaretShape(hits[i]);
                }
            } while (--numReps >= 0);
        }
    }

    public static class TLGetLogicalHighlightShape extends TLExtendedMeasureTest {
        public TLGetLogicalHighlightShape() {
            super(measuretestroot, "tlGetLogicalHighlightShape", "Measuring TL getLogicalHighlightShape");
        }

        public void runTest(Object ctx, int numReps) {
            TLExContext tlctx = (TLExContext)ctx;
            TextLayout tl = tlctx.tl;
            int len = tlctx.text.length();
            Rectangle2D lb = tlctx.lb;
            Shape s;
            if (len < 3) {
                do {
                    s = tl.getLogicalHighlightShape(0, len, lb);
                } while (--numReps >= 0);
            } else {
                do {
                    for (int i = 3; i < len; ++i) {
                        s = tl.getLogicalHighlightShape(i-3, i, lb);
                    }
                } while (--numReps >= 0);
            }
        }
    }

    public static class TLGetVisualHighlightShape extends TLExtendedMeasureTest {
        public TLGetVisualHighlightShape() {
            super(measuretestroot, "tlGetVisualHighlightShape", "Measuring TL getVisualHighlightShape");
        }

        public void runTest(Object ctx, int numReps) {
            TLExContext tlctx = (TLExContext)ctx;
            TextLayout tl = tlctx.tl;
            TextHitInfo[] hits = tlctx.hits;
            Rectangle2D lb = tlctx.lb;
            Shape s;
            if (hits.length < 3) {
                do {
                    s = tl.getVisualHighlightShape(hits[0], hits[hits.length - 1], lb);
                } while (--numReps >= 0);
            } else {
                do {
                    for (int i = 3; i < hits.length; ++i) {
                        s = tl.getVisualHighlightShape(hits[i-3], hits[i], lb);
                    }
                } while (--numReps >= 0);
            }
        }
    }

    public static class TLHitTest extends TLExtendedMeasureTest {
        public TLHitTest() {
            super(measuretestroot, "tlHitTest", "Measuring TL hitTest");
        }

        public void runTest(Object ctx, int numReps) {
            TLExContext tlctx = (TLExContext)ctx;
            TextLayout tl = tlctx.tl;
            int numhits = tlctx.hits.length;
            Rectangle2D lb = tlctx.lb;
            TextHitInfo hit;
            for (int i = 0; i <= numhits; ++i) {
                float x = (float)(lb.getMinX() + lb.getWidth() * i / numhits);
                float y = (float)(lb.getMinY() + lb.getHeight() * i / numhits);
                hit = tl.hitTestChar(x, y, lb);
            }
        }
    }

    public static class TLOutline extends TLMeasureTest {
        public TLOutline() {
            super(measuretestroot, "tlOutline", "Measuring TL outline");
        }

        public void runTest(Object ctx, int numReps) {
            TLContext tlctx = (TLContext)ctx;
            TextLayout tl = tlctx.tl;
            Shape s;
            do {
                s = tl.getOutline(null);
            } while (--numReps >= 0);
        }
    }
}