1 | package org.expeditee.gio;
|
---|
2 |
|
---|
3 | import java.util.List;
|
---|
4 |
|
---|
5 | import org.expeditee.core.Font;
|
---|
6 | import org.expeditee.core.Line;
|
---|
7 | import org.expeditee.core.Point;
|
---|
8 | import org.expeditee.core.TextHitInfo;
|
---|
9 | import org.expeditee.core.TextLayout;
|
---|
10 | import org.expeditee.core.bounds.AxisAlignedBoxBounds;
|
---|
11 |
|
---|
12 | /** TODO: Comment. cts16 */
|
---|
13 | public abstract class TextLayoutManager {
|
---|
14 |
|
---|
15 | public abstract List<TextLayout> layoutString(String string, Font font, Point start, Line[] lineBreakers, int widthLimit, int lineSpacing, boolean dontBreakWords, boolean fullJustify);
|
---|
16 |
|
---|
17 | public List<TextLayout> layoutStringRelative(String string, Font font, int widthLimit, int lineSpacing, boolean dontBreakWords, boolean fullJustify)
|
---|
18 | {
|
---|
19 | return layoutString(string, font, Point.ORIGIN.clone(), null, widthLimit, lineSpacing, dontBreakWords, fullJustify);
|
---|
20 | }
|
---|
21 |
|
---|
22 | public TextLayout layoutStringSimple(String string, Font font)
|
---|
23 | {
|
---|
24 | List<TextLayout> layouts = layoutStringRelative(string, font, Integer.MAX_VALUE, 0, true, false);
|
---|
25 | if (layouts == null || layouts.size() == 0) return null;
|
---|
26 | return layouts.get(0);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public abstract float getAdvance(TextLayout layout);
|
---|
30 | public abstract int getCharacterCount(TextLayout layout);
|
---|
31 | public abstract TextHitInfo getNextLeftHit(TextLayout layout, int offset);
|
---|
32 | public abstract TextHitInfo getNextLeftHit(TextLayout layout, TextHitInfo hit);
|
---|
33 | public abstract TextHitInfo getNextRightHit(TextLayout layout, int offset);
|
---|
34 | public abstract TextHitInfo getNextRightHit(TextLayout layout, TextHitInfo hit);
|
---|
35 | public abstract float[] getCaretInfo(TextLayout layout, TextHitInfo hit);
|
---|
36 | public abstract AxisAlignedBoxBounds getPixelBounds(TextLayout layout, float x, float y);
|
---|
37 | public abstract AxisAlignedBoxBounds getLogicalHighlightShape(TextLayout layout, int firstEndpoint, int secondEndpoint);
|
---|
38 | public abstract float getAscent(TextLayout layout);
|
---|
39 | public abstract float getDescent(TextLayout layout);
|
---|
40 | public abstract float getLeading(TextLayout layout);
|
---|
41 | public abstract int getStringWidth(Font font, String string);
|
---|
42 | public abstract TextHitInfo hitTestChar(TextLayout layout, float x, float y);
|
---|
43 |
|
---|
44 | public abstract void releaseLayout(TextLayout layout);
|
---|
45 |
|
---|
46 | public void releaseLayouts(List<TextLayout> layouts)
|
---|
47 | {
|
---|
48 | if (layouts == null) return;
|
---|
49 |
|
---|
50 | for (TextLayout layout : layouts) {
|
---|
51 | releaseLayout(layout);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Calculates the maximum possible distance a line can extend to the right from a given point
|
---|
57 | * without crossing any of the lines in the given array.
|
---|
58 | *
|
---|
59 | * @param p
|
---|
60 | * The coordinates of the beginning point.
|
---|
61 | *
|
---|
62 | * @param lines
|
---|
63 | * The lines that should stop the extension.
|
---|
64 | *
|
---|
65 | * @return
|
---|
66 | * The length of the extended line.
|
---|
67 | */
|
---|
68 | public float getLineWidth(Point p, Line[] lines)
|
---|
69 | {
|
---|
70 | float width = (float) Integer.MAX_VALUE;
|
---|
71 | for(Line l : lines) {
|
---|
72 | // check for lines that cross over our y
|
---|
73 | if((l.getFirstEnd().getY() >= p.getY() && l.getSecondEnd().getY() <= p.getY()) || (l.getFirstEnd().getY() <= p.getY() && l.getSecondEnd().getY() >= p.getY())) {
|
---|
74 | float dX = l.getFirstEnd().getX() - l.getSecondEnd().getX();
|
---|
75 | float dY = l.getFirstEnd().getY() - l.getSecondEnd().getY();
|
---|
76 | float newWidth;
|
---|
77 | if(dX == 0) {
|
---|
78 | newWidth = l.getFirstEnd().getX();
|
---|
79 | } else if(dY == 0) {
|
---|
80 | newWidth = Math.min(l.getFirstEnd().getX(), l.getSecondEnd().getX());
|
---|
81 | } else {
|
---|
82 | newWidth = l.getFirstEnd().getX() + (p.getY() - l.getFirstEnd().getY()) * dX / dY;
|
---|
83 | }
|
---|
84 | if(newWidth < p.getX()) {
|
---|
85 | continue;
|
---|
86 | }
|
---|
87 | if(newWidth < width) {
|
---|
88 | width = newWidth;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return width - p.getX();
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|