Skip to content

Commit e95e21b

Browse files
authored
Add getTextWidth() (#665)
1 parent f7f140a commit e95e21b

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

example/src/Examples/Neumorphism/Dashboard/components/Control.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ export const Control = ({
3434
progress,
3535
active,
3636
}: ControlProps) => {
37-
const labelWidth = font
38-
.getGlyphWidths(font.getGlyphIDs(label))
39-
.reduce((a, b) => a + b, 0);
37+
const labelWidth = font.getTextWidth(label);
4038
return (
4139
<Group transform={translate({ x: x + 30, y: y + 30 })}>
4240
<Text

example/src/Examples/Neumorphism/Dashboard/components/ProgressBar.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ export const ProgressBar = ({ progress }: ProgressBarProps) => {
4141
if (font === null) {
4242
return null;
4343
}
44-
const textWidth = font
45-
.getGlyphWidths(font.getGlyphIDs("00°C"))
46-
.reduce((a, b) => a + b, 0);
44+
const textWidth = font.getTextWidth("00°C");
4745
return (
4846
<Group transform={translate({ x: 100, y: 223 })}>
4947
<Group>

example/src/Examples/Neumorphism/Dashboard/components/Title.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ export const Title = ({ title }: Title) => {
1414
if (!font) {
1515
return null;
1616
}
17-
const titleWidth = font
18-
.getGlyphWidths(font.getGlyphIDs(title))
19-
.reduce((a, b) => a + b, 0);
17+
const titleWidth = font.getTextWidth(title);
2018
const offsetX = 30 + BUTTON_SIZE;
2119
const space = 298 - offsetX;
2220
return (

example/src/Examples/Wallet/components/Label.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,13 @@ export const Label = ({ state, y, graphs, width, height }: LabelProps) => {
4949
}
5050
const graph = graphs[state.current.current];
5151
const title = format(graph.data.maxPrice);
52-
const titleWidth = titleFont
53-
.getGlyphWidths(titleFont.getGlyphIDs(title))
54-
.reduce((a, b) => a + b, 0);
52+
const titleWidth = titleFont.getTextWidth(title);
5553
return width / 2 - titleWidth / 2;
5654
}, [state, titleFont]);
5755
if (!titleFont || !subtitleFont) {
5856
return null;
5957
}
60-
const subtitleWidth = subtitleFont
61-
.getGlyphWidths(subtitleFont.getGlyphIDs(subtitle))
62-
.reduce((a, b) => a + b, 0);
58+
const subtitleWidth = subtitleFont.getTextWidth(subtitle);
6359
return (
6460
<>
6561
<Text

package/cpp/api/JsiSkFont.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <memory>
44
#include <utility>
55
#include <vector>
6+
#include <numeric>
67

78
#include <jsi/jsi.h>
89
#include "JsiSkHostObjects.h"
@@ -86,6 +87,28 @@ namespace RNSkia
8687
return jsiWidths;
8788
}
8889

90+
JSI_HOST_FUNCTION(getTextWidth) {
91+
auto str = arguments[0].asString(runtime).utf8(runtime);
92+
auto numGlyphIDs = str.length();
93+
std::vector<SkGlyphID> glyphs;
94+
glyphs.resize(numGlyphIDs);
95+
int glyphsSize = static_cast<int>(numGlyphIDs);
96+
getObject()->textToGlyphs(str.c_str(), str.length(), SkTextEncoding::kUTF8,
97+
static_cast<SkGlyphID *>(glyphs.data()), glyphsSize);
98+
std::vector<SkScalar> widthPtrs;
99+
widthPtrs.resize(numGlyphIDs);
100+
if (count > 1)
101+
{
102+
auto paint = JsiSkPaint::fromValue(runtime, arguments[1]);
103+
getObject()->getWidthsBounds(glyphs.data(), glyphsSize, static_cast<SkScalar *>(widthPtrs.data()), nullptr, paint.get());
104+
}
105+
else
106+
{
107+
getObject()->getWidthsBounds(glyphs.data(), glyphsSize, static_cast<SkScalar *>(widthPtrs.data()), nullptr, nullptr);
108+
}
109+
return jsi::Value(std::accumulate(widthPtrs.begin(), widthPtrs.end(), 0));
110+
}
111+
89112
JSI_HOST_FUNCTION(getMetrics)
90113
{
91114
SkFontMetrics fm;
@@ -273,7 +296,8 @@ namespace RNSkia
273296
JSI_EXPORT_FUNC(JsiSkFont, setEmbolden),
274297
JSI_EXPORT_FUNC(JsiSkFont, setSubpixel),
275298
JSI_EXPORT_FUNC(JsiSkFont, setTypeface),
276-
JSI_EXPORT_FUNC(JsiSkFont, getGlyphWidths))
299+
JSI_EXPORT_FUNC(JsiSkFont, getGlyphWidths),
300+
JSI_EXPORT_FUNC(JsiSkFont, getTextWidth))
277301

278302
JsiSkFont(std::shared_ptr<RNSkPlatformContext> context, const SkFont &font)
279303
: JsiSkWrappingSharedPtrHostObject(std::move(context),

package/src/skia/types/Font/Font.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ export interface FontMetrics {
1212
}
1313

1414
export interface SkFont extends SkJSIInstance<"Font"> {
15+
/**
16+
* Retrieves the total width of the provided text
17+
* @param text
18+
* @param paint
19+
*/
20+
getTextWidth(text: string, paint?: SkPaint): number;
21+
1522
/**
1623
* Retrieves the advanceX measurements for each glyph.
1724
* If paint is not null, its stroking, PathEffect, and MaskFilter fields are respected.

package/src/skia/web/JsiSkFont.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ Clients should use "Font.getGlyphWidths" instead (the latter does no shaping)`
2727
return new JsiSkRect(this.CanvasKit, this.CanvasKit.XYWHRect(0, 0, 0, 0));
2828
}
2929

30+
getTextWidth(text: string, paint?: SkPaint | undefined) {
31+
const ids = this.getGlyphIDs(text);
32+
const widths = this.getGlyphWidths(ids, paint);
33+
return widths.reduce((a, b) => a + b, 0);
34+
}
35+
3036
getMetrics() {
3137
const result = this.ref.getMetrics();
3238
return {

0 commit comments

Comments
 (0)