source: trunk/src/org/expeditee/gio/TextLayoutManager.java@ 1097

Last change on this file since 1097 was 1097, checked in by davidb, 6 years ago

Newly structured files from Corey's work on logic/graphics separation

File size: 3.4 KB
Line 
1package org.expeditee.gio;
2
3import java.util.List;
4
5import org.expeditee.core.Font;
6import org.expeditee.core.Line;
7import org.expeditee.core.Point;
8import org.expeditee.core.TextHitInfo;
9import org.expeditee.core.TextLayout;
10import org.expeditee.core.bounds.AxisAlignedBoxBounds;
11
12/** TODO: Comment. cts16 */
13public 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, 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.MAX_VALUE;
71 for(Line l : lines) {
72 // check for lines that cross over our y
73 if((l.getFirstEnd().y >= p.y && l.getSecondEnd().y <= p.y) || (l.getFirstEnd().y <= p.y && l.getSecondEnd().y >= p.y)) {
74 float dX = l.getFirstEnd().x - l.getSecondEnd().x;
75 float dY = l.getFirstEnd().y - l.getSecondEnd().y;
76 float newWidth;
77 if(dX == 0) {
78 newWidth = l.getFirstEnd().x;
79 } else if(dY == 0) {
80 newWidth = Math.min(l.getFirstEnd().x, l.getSecondEnd().x);
81 } else {
82 newWidth = l.getFirstEnd().x + (p.y - l.getFirstEnd().y) * dX / dY;
83 }
84 if(newWidth < p.x) {
85 continue;
86 }
87 if(newWidth < width) {
88 width = newWidth;
89 }
90 }
91 }
92 return width - p.x;
93 }
94
95}
Note: See TracBrowser for help on using the repository browser.