MCPcopy Create free account
hub / github.com/JetBrains/skija / drawParagraph

Method drawParagraph

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

Source from the content-addressed store, hash-verified

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

Callers 1

drawItMethod · 0.95

Calls 15

makeXYWHMethod · 0.95
drawTableMethod · 0.95
saveMethod · 0.80
setFontFamilyMethod · 0.80
pushStyleMethod · 0.80
addTextMethod · 0.80
layoutMethod · 0.80
getLineMetricsMethod · 0.80
getPositionMethod · 0.80
getXMethod · 0.80
getAscentMethod · 0.80

Tested by

no test coverage detected