Skip to content

Commit

Permalink
fix: Bind to new functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Et7f3 committed Jun 27, 2020
1 parent 862b0f0 commit 9120e8c
Show file tree
Hide file tree
Showing 20 changed files with 317 additions and 220 deletions.
11 changes: 7 additions & 4 deletions bench/lib/DrawBench.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ module Data = {
let testString = String.make(50, 'a') ++ String.make(50, 'X');
let paint = {
let textPaint = Skia.Paint.make();
Skia.Paint.setTextEncoding(textPaint, GlyphId);
Skia.Paint.setLcdRenderText(textPaint, true);
Skia.Paint.setAntiAlias(textPaint, true);
Skia.Paint.setTextSize(textPaint, 20.);
textPaint;
};
let font = {
let textFont = Skia.Font.makeDefault();
Skia.Font.setTextSize(textFont, 20.);
textFont;
};

let rectPaint = Skia.Paint.make();
};
Expand All @@ -29,7 +31,7 @@ let drawText = canvasContext => {
| Error(_) => failwith("Unable to load font!")
| Ok(font) =>
Skia.Paint.setColor(Data.paint, Revery_Core.Color.toSkia(Colors.white));
Skia.Paint.setTypeface(Data.paint, Revery.Font.getSkiaTypeface(font));
Skia.Font.setTypeface(Data.font, Revery.Font.getSkiaTypeface(font));

let shapedText =
Data.testString
Expand All @@ -41,6 +43,7 @@ let drawText = canvasContext => {
~y=1.,
~paint=Data.paint,
~text=shapedText,
~font=Data.font,
canvasContext,
);
};
Expand Down
18 changes: 10 additions & 8 deletions examples/CanvasExample.re
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,28 @@ module Sample = {
| Ok(font) =>
let textPaint = Skia.Paint.make();
Skia.Paint.setColor(textPaint, Color.toSkia(Colors.white));
Skia.Paint.setTypeface(
textPaint,
FontCache.getSkiaTypeface(font),
);
Skia.Paint.setLcdRenderText(textPaint, true);
// ASK: What we should do
// Skia.Paint.setLcdRenderText(textPaint, true);
Skia.Paint.setAntiAlias(textPaint, true);
Skia.Paint.setTextSize(textPaint, 20.);
let fontText =
Skia.Font.makeWithValues(
FontCache.getSkiaTypeface(font),
20.,
1.,
0.,
);

let shapedText =
"Hello, World"
|> FontCache.shape(font)
|> ShapeResult.getGlyphString;

Skia.Paint.setTextEncoding(textPaint, GlyphId);

CanvasContext.drawText(
~paint=textPaint,
~x=10.0,
~y=20.0,
~text=shapedText,
~font=fontText,
canvasContext,
);
};
Expand Down
27 changes: 14 additions & 13 deletions examples/skia-cli/SkiaCli.re
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ let draw = canvas => {

let fill3 = Paint.make();
Paint.setColor(fill3, Color.makeArgb(0xFFl, 0xFFl, 0xFFl, 0xFFl));
Paint.setTextSize(fill3, 30.);
// TODO: add some setter
// split creation of font

let nonExistentTypeface = Typeface.makeFromFile("non-existent-font.ttf", 0);
assert(nonExistentTypeface == None);
Expand All @@ -74,11 +75,11 @@ let draw = canvas => {
| None => failwith("Unable to load font: " ++ filePath)
| Some(typeFace) =>
print_endline(__LOC__ ++ ": we will set.");
Paint.setTypeface(fill3, typeFace);
print_endline(__LOC__ ++ ": setTypeface is OK.");
Canvas.drawText(canvas, "Hello, world!", 30., 30., fill3);
let font = Font.makeWithValues(typeFace, 30., 1., 0.);
print_endline(__LOC__ ++ ": makeWithValues is OK.");
Canvas.drawText(canvas, "Hello, world!", GlyphId, 30., 30., font, fill3);
let metrics = FontMetrics.make();
let _ret: float = Paint.getFontMetrics(fill3, metrics, 1.0);
let _ret: float = Font.getFontMetrics(font, metrics);

print_endline(
"-- Top: " ++ string_of_float(FontMetrics.getTop(metrics)),
Expand All @@ -99,10 +100,12 @@ let draw = canvas => {
print_endline(__LOC__ ++ ": We return.");

// Measure
let measurement = Paint.measureText(fill3, "Hello, world!", None);
let measurement =
Font.measureText(font, "Hello, world!", GlyphId, None, Some(fill3));
print_endline("Measured text: " ++ string_of_float(measurement));
Paint.setTextSize(fill3, 50.);
let largeMeasurement = Paint.measureText(fill3, "Hello, world!", None);
Font.setTextSize(font, 50.);
let largeMeasurement =
Font.measureText(font, "Hello, world!", GlyphId, None, Some(fill3));
print_endline(
"Large measured text: " ++ string_of_float(largeMeasurement),
);
Expand Down Expand Up @@ -155,12 +158,10 @@ let draw = canvas => {
switch (maybeTypeface) {
| None => failwith("Unable to load font: " ++ filePath)
| Some(typeFace) =>
let font = Font.makeWithValues(typeFace, 30., 1., 0.);
let fill = Paint.make();
Paint.setColor(fill, Color.makeArgb(0xFFl, 0xFFl, 0xFFl, 0xFFl));
Paint.setTextSize(fill, 30.);
Paint.setTypeface(fill, typeFace);
Paint.setSubpixelText(fill, true);
Paint.setTextEncoding(fill, GlyphId);
Font.setSubpixelText(font, true);

let glyphsToString = glyphs => {
let len = List.length(glyphs);
Expand All @@ -185,7 +186,7 @@ let draw = canvas => {

// For FiraCode, this is a==>b
let str = glyphsToString([136, 1624, 1624, 1495, 148]);
Canvas.drawText(canvas, str, 50., 100., fill);
Canvas.drawText(canvas, str, GlyphId, 50., 100., font, fill);
};

let fill = Paint.make();
Expand Down
29 changes: 19 additions & 10 deletions examples/skia-font-manager-cli/SkiaFontManagerCli.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ let draw = canvas => {
let maybeTypeface =
FontManager.matchFamilyStyle(fontManager, "Arial", style);
let fill = Paint.make();
let font = Font.makeDefault();

Paint.setColor(fill, Color.makeArgb(0xFFl, 0xFFl, 0xFFl, 0xFFl));
Paint.setTextSize(fill, 16.);
Paint.setSubpixelText(fill, true);
Font.setTextSize(font, 16.);
Font.setSubpixelText(font, true);

switch (maybeTypeface) {
| None =>
print_endline("Normal Arial not found. Ensure you have it available.")
| Some(typeface) =>
Paint.setTypeface(fill, typeface);
Canvas.drawText(canvas, "Arial (System)", 10., 20., fill);
Font.setTypeface(font, typeface);
Canvas.drawText(canvas, "Arial (System)", GlyphId, 10., 20., font, fill);
let stream = Typeface.toStream(typeface);
let length = Stream.getLength(stream);
Printf.printf("Stream length: %d\n", length);
Expand All @@ -50,8 +51,16 @@ let draw = canvas => {
"Normal Times New Roman not found. Ensure you have it available.",
)
| Some(typeface) =>
Paint.setTypeface(fill, typeface);
Canvas.drawText(canvas, "Times New Roman (System)", 10., 40., fill);
Font.setTypeface(font, typeface);
Canvas.drawText(
canvas,
"Times New Roman (System)",
GlyphId,
10.,
40.,
font,
fill,
);
};

let maybeTypeface =
Expand All @@ -62,9 +71,9 @@ let draw = canvas => {
"Normal Consolas not found. Ensure your system has it available.",
)
| Some(typeface) =>
Paint.setTypeface(fill, typeface);
Font.setTypeface(font, typeface);
let metrics = FontMetrics.make();
let _ret: float = Paint.getFontMetrics(fill, metrics, 1.0);
let _ret: float = Font.getFontMetrics(font, metrics);
print_endline("__Consolas__");
print_endline(
"-- Average character width: "
Expand All @@ -82,9 +91,9 @@ let draw = canvas => {
switch (maybeTypeface) {
| None => failwith("Unable to load font: " ++ filePath)
| Some(typeface) =>
Paint.setTypeface(fill, typeface);
Font.setTypeface(font, typeface);
let metrics = FontMetrics.make();
let _ret: float = Paint.getFontMetrics(fill, metrics, 1.0);
let _ret: float = Font.getFontMetrics(font, metrics);
print_endline("__Fira Code__");
print_endline(
"-- Average character width: "
Expand Down
2 changes: 1 addition & 1 deletion examples/skia-sdl2/SkiaSdl.re
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ let run = () => {
Skia.Color.makeArgb(0xFFl, 0xFFl, 0xFFl, 0xFFl),
);

Skia.Canvas.drawText(canvas, "Hello, world!", 50., 50., paint);
Skia.Canvas.drawText(canvas, "Hello, world!", GlyphId, 50., 50., Font.makeDefault(), paint);

Skia.Canvas.flush(canvas);
Sdl2.Gl.swapWindow(window);
Expand Down
20 changes: 13 additions & 7 deletions src/Draw/CanvasContext.re
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ let drawImage = (~x, ~y, ~width, ~height, ~paint=?, data: Skia.Image.t, v: t) =>
);
};

let drawText = (~paint, ~x=0., ~y=0., ~text, v: t) => {
Canvas.drawText(v.canvas, text, x, y, paint);
let drawText = (~paint, ~x=0., ~y=0., ~text, ~font, v: t) => {
Canvas.drawText(v.canvas, text, GlyphId, x, y, font, paint);
};

let _topMatrix = Skia.Matrix.make();
Expand Down Expand Up @@ -233,17 +233,23 @@ module Deprecated = {
| Error(_) => ()
| Ok(font) =>
let textPaint = Skia.Paint.make();
// ASK: put outside ?
let font' =
Skia.Font.makeWithValues(
FontCache.getSkiaTypeface(font),
fontSize,
1.,
0.,
);
Skia.Paint.setColor(textPaint, Revery_Core.Color.toSkia(color));
Skia.Paint.setTypeface(textPaint, FontCache.getSkiaTypeface(font));
Skia.Paint.setTextEncoding(textPaint, GlyphId);
Skia.Paint.setLcdRenderText(textPaint, true);
// ASK: What we should do
// Skia.Paint.setLcdRenderText(textPaint, true);
Skia.Paint.setAntiAlias(textPaint, true);
Skia.Paint.setTextSize(textPaint, fontSize);

let shapedText =
text |> FontCache.shape(font) |> ShapeResult.getGlyphString;

drawText(~x, ~y, ~paint=textPaint, ~text=shapedText, v);
drawText(~x, ~y, ~paint=textPaint, ~text=shapedText, ~font=font', v);
};
};
};
6 changes: 2 additions & 4 deletions src/Font/FontCache.re
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,10 @@ let getMetrics: (t, float) => FontMetrics.t =
MetricsLruHash.promote(size, metricsCache);
v;
| None =>
let paint = Skia.Paint.make();
Skia.Paint.setTypeface(paint, skiaFace);
Skia.Paint.setTextSize(paint, size);
let font = Skia.Font.makeWithValues(skiaFace, size, 1., 0.);

let metrics = Skia.FontMetrics.make();
let lineHeight = Skia.Paint.getFontMetrics(paint, metrics, 1.0);
let lineHeight = Skia.Font.getFontMetrics(font, metrics);

let ret = FontMetrics.ofSkia(size, lineHeight, metrics);
MetricsLruHash.add(size, ret, metricsCache);
Expand Down
10 changes: 5 additions & 5 deletions src/Font/FontRenderer.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ type measureResult = {

let measure = {
let paint = Skia.Paint.make();
Skia.Paint.setTextEncoding(paint, GlyphId);

(~smoothing: Smoothing.t, font, size, text: string) => {
let {height, _}: FontMetrics.t = FontCache.getMetrics(font, size);
let skiaFace = FontCache.getSkiaTypeface(font);
// TODO: put outside
let font' = Skia.Font.makeWithValues(skiaFace, size, 1., 0.);

let glyphString =
text |> FontCache.shape(font) |> ShapeResult.getGlyphString;

Smoothing.setPaint(~smoothing, paint);
Smoothing.setPaint(~smoothing, paint, font');

Skia.Paint.setTypeface(paint, skiaFace);
Skia.Paint.setTextSize(paint, size);
let width = Skia.Paint.measureText(paint, glyphString, None);
let width =
Skia.Font.measureText(font', glyphString, GlyphId, None, Some(paint));
{height, width};
};
};
8 changes: 4 additions & 4 deletions src/Font/Smoothing.re
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ type t =
// - https://github.com/onivim/oni2/issues/1592
let default = SubpixelAntialiased;

let setPaint = (~smoothing: t, paint: Skia.Paint.t) => {
let setPaint = (~smoothing: t, paint: Skia.Paint.t, font: Skia.Font.t) => {
switch (smoothing) {
| None =>
Skia.Paint.setAntiAlias(paint, false);
Skia.Paint.setSubpixelText(paint, false);
Skia.Font.setSubpixelText(font, false);
| Antialiased =>
Skia.Paint.setAntiAlias(paint, true);
Skia.Paint.setSubpixelText(paint, false);
Skia.Font.setSubpixelText(font, false);
| SubpixelAntialiased =>
Skia.Paint.setAntiAlias(paint, true);
Skia.Paint.setSubpixelText(paint, true);
Skia.Font.setSubpixelText(font, true);
};
};
28 changes: 16 additions & 12 deletions src/UI/TextNode.re
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class textNode (text: string) = {
val mutable _underlined = false;
val _textPaint = {
let paint = Skia.Paint.make();
Skia.Paint.setTextEncoding(paint, GlyphId);
Skia.Paint.setLcdRenderText(paint, true);
// ASK: What we should do
// Skia.Paint.setLcdRenderText(paint, true);
Skia.Paint.setAntiAlias(paint, true);
paint;
};
Expand All @@ -49,14 +49,17 @@ class textNode (text: string) = {
)
) {
| Error(_) => ()
| Ok(font) =>
Revery_Font.Smoothing.setPaint(~smoothing=_smoothing, _textPaint);
| Ok(fontCache) =>
let font =
Skia.Font.makeWithValues(
Revery_Font.FontCache.getSkiaTypeface(fontCache),
_fontSize,
1.,
0.,
);
// TODO: scale, skew
Revery_Font.Smoothing.setPaint(~smoothing=_smoothing, _textPaint, font);
Skia.Paint.setColor(_textPaint, Color.toSkia(colorWithAppliedOpacity));
Skia.Paint.setTypeface(
_textPaint,
Revery_Font.FontCache.getSkiaTypeface(font),
);
Skia.Paint.setTextSize(_textPaint, _fontSize);

let ascentPx =
Text.ascent(
Expand Down Expand Up @@ -93,24 +96,25 @@ class textNode (text: string) = {

let glyphString =
line
|> Revery_Font.shape(font)
|> Revery_Font.shape(fontCache)
|> Revery_Font.ShapeResult.getGlyphString;

CanvasContext.drawText(
~paint=_textPaint,
~x=0.,
~y=baselineY,
~text=glyphString,
~font,
canvas,
);
if (_underlined) {
let {underlinePosition, underlineThickness, _}: FontMetrics.t =
FontCache.getMetrics(font, _fontSize);
FontCache.getMetrics(fontCache, _fontSize);

let width =
FontRenderer.measure(
~smoothing=_smoothing,
font,
fontCache,
_fontSize,
line,
).
Expand Down
Loading

0 comments on commit 9120e8c

Please sign in to comment.