MCPcopy Create free account
hub / github.com/HumbleUI/Skija / drawParagraph

Method drawParagraph

examples/scenes/src/TextLineScene.java:136–199  ·  view source on GitHub ↗
(Canvas canvas, String[] texts, Font font, Point cursor)

Source from the content-addressed store, hash-verified

134 }
135
136 public float drawParagraph(Canvas canvas, String[] texts, Font font, Point cursor) {
137 float bottom = 0;
138 canvas.save();
139 for (var text: texts) {
140 try (TextStyle defaultTs = new TextStyle().setFontFamily(font.getTypeface().getFamilyName()).setFontSize(36).setColor(0xFFCC3333);
141 ParagraphStyle ps = new ParagraphStyle();
142 ParagraphBuilder pb = new ParagraphBuilder(ps, fc);)
143 {
144 pb.pushStyle(defaultTs);
145 pb.addText(text);
146 try (Paragraph p = pb.build();) {
147 p.layout(Float.POSITIVE_INFINITY);
148 LineMetrics lm = p.getLineMetrics()[0];
149
150 int offset = p.getGlyphPositionAtCoordinate(cursor.getX(), 0).getPosition();
151
152 // bounds
153 float lineWidth = (float) lm.getWidth();
154 float lineHeight = (float) lm.getHeight();
155 float baseline = (float) lm.getAscent();
156 canvas.drawRect(Rect.makeXYWH(0, 0, lineWidth, lineHeight), stroke);
157 canvas.drawLine(0, baseline, lineWidth, baseline, stroke);
158
159 // selection
160 float coord = 0;
161 TextBox[] rects = p.getRectsForRange(0, offset, RectHeightMode.TIGHT, RectWidthMode.TIGHT);
162 for (var rect: rects) {
163 canvas.drawRect(rect.getRect(), selectionFill);
164 coord = rect.getRect().getRight();
165 }
166
167 // text
168 p.paint(canvas, 0, 0);
169
170 // coords
171 for (int i = 0; i < text.length() + 1; ++i) {
172 rects = p.getRectsForRange(0, i, RectHeightMode.TIGHT, RectWidthMode.TIGHT);
173 for (var rect: rects) {
174 coord = rect.getRect().getRight();
175 canvas.drawLine(coord, baseline, coord, baseline + 4, stroke);
176 }
177 }
178
179 // extra info
180 canvas.save();
181 canvas.translate(0, lineHeight + 5);
182 var tableSize = drawTable(canvas, new String[] {
183 "Chars", text.chars().mapToObj(c -> String.format(c <= 256 ? "%02X" : "%04X", c)).collect(Collectors.joining(" ")),
184 "Coord", Integer.toString((int) cursor.getX()),
185 "Offset", offset + " " + text.length(),
186 });
187 canvas.restore();
188
189 float offsetLeft = Math.max(100, Math.max(lineWidth, tableSize.getX())) + 10;
190 canvas.translate(offsetLeft, 0);
191 cursor = cursor.offset(-offsetLeft, 0);
192 bottom = Math.max(bottom, lineHeight + 5 + tableSize.getY());
193 }

Callers 1

drawItMethod · 0.95

Calls 15

drawTableMethod · 0.95
saveMethod · 0.80
setFontFamilyMethod · 0.80
pushStyleMethod · 0.80
addTextMethod · 0.80
layoutMethod · 0.80
getLineMetricsMethod · 0.80
getXMethod · 0.80
getAscentMethod · 0.80
drawRectMethod · 0.80
getRectsForRangeMethod · 0.80

Tested by

no test coverage detected