Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components): move font styling out of Style and use families #879

Merged
merged 18 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/Font/FontFamily.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
open FontManager;

type t = (FontWeight.t, bool, bool) => string;

type variantSolver =
(~fontWeight: FontWeight.t, ~italicized: bool, ~monospaced: bool, unit) =>
string;

let fromFile =
(
~variant:
option(
(
~fontWeight: FontWeight.t,
~italicized: bool,
~monospaced: bool,
unit
zbaylin marked this conversation as resolved.
Show resolved Hide resolved
) =>
string,
)=?,
default: string,
)
: t =>
switch (variant) {
| None => ((_, _, _) => default)
| Some(solver) => (
(w, i, m) =>
switch (w, i, m) {
| (FontWeight.Normal, false, false) => default
| _ => solver(~fontWeight=w, ~italicized=i, ~monospaced=m, ())
}
)
};

module FontFamilyHashable = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍 It's great that these implementation details aren't exposed to consumers of the FontFamily module - this is all tucked away behind the interface.

type t = {
familyName: string,
weight: FontWeight.t,
italicized: bool,
monospaced: bool,
};

let equal = (a, b) =>
String.equal(a.familyName, b.familyName)
&& a.weight == b.weight
&& a.italicized == b.italicized
&& a.monospaced == b.monospaced;
Comment on lines +34 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, why not just a == b? Does String.equal differ in behaviour from ==?


let hash = Hashtbl.hash;
};

module FontDescriptorWeight = {
type t = FontManager.FontDescriptor.t;
let weight = _ => 1;
};

module FontFamilyCache = Lru.M.Make(FontFamilyHashable, FontDescriptorWeight);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to use the LruCache, to help bound the number of familes we keep in memory!


let cache = FontFamilyCache.create(~initialSize=8, 64);

let system = (familyName): t =>
(weight, italicized, monospaced) => {
let fontDescr: FontFamilyHashable.t = {
familyName,
weight,
italicized,
monospaced,
};
switch (FontFamilyCache.find(fontDescr, cache)) {
| Some(fd) =>
FontFamilyCache.promote(fontDescr, cache);
fd.path;
| None =>
let fd =
Discovery.find(
~weight,
~mono=monospaced,
~italic=italicized,
familyName,
);
FontFamilyCache.add(fontDescr, fd, cache);
FontFamilyCache.trim(cache);
fd.path;
};
};

let default =
switch (Revery_Core.Environment.os) {
| Linux => system("Liberation Sans")
| Mac => system("System Font")
| _ => system("Arial")
};
20 changes: 20 additions & 0 deletions src/Font/FontFamily.rei
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
open FontManager;

type t;

let fromFile:
(
~variant: (
~fontWeight: FontWeight.t,
~italicized: bool,
~monospaced: bool,
unit
) =>
string
=?,
string
) =>
t;
let system: string => t;

let default: t;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! It's great to finally have a default!

1 change: 1 addition & 0 deletions src/Font/Revery_Font.re
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module FontCache = FontCache;
module FontRenderer = FontRenderer;
module ShapeResult = ShapeResult;
module Smoothing = Smoothing;
module Family = FontFamily;

type t = FontCache.t;

Expand Down
8 changes: 8 additions & 0 deletions src/UI/TextNode.re
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class textNode (text: string) = {
val mutable _isMeasured = false;
val mutable _lines: list(string) = [];
val mutable _smoothing = Smoothing.default;
val mutable _fontFamily = Family.default;
val mutable _fontWeight = Weight.Normal;
val mutable _italicized = false;
val mutable _monospaced = false;
Comment on lines +18 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these!

val _textPaint = {
let paint = Skia.Paint.make();
Skia.Paint.setTextEncoding(paint, GlyphId);
Expand Down Expand Up @@ -127,6 +131,10 @@ class textNode (text: string) = {
_this#markLayoutDirty();
};
pub setSmoothing = smoothing => _smoothing = smoothing;
pub setFontFamily = fontFamily => _fontFamily = fontFamily;
pub setFontWeight = fontWeight => _fontWeight = fontWeight;
pub setItalicized = italicized => _italicized = italicized;
pub setMonospaced = monospaced => _monospaced = monospaced;
pub measure = (width, _height) => {
_isMeasured = true;
/**
Expand Down
11 changes: 10 additions & 1 deletion src/UI_Primitives/Text.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open Revery_UI;
open Revery_Font;
open Style;
open React;

Expand All @@ -14,8 +15,12 @@ let%nativeComponent make =
~onMouseOut=?,
~ref=?,
~style=emptyTextStyle,
~fontFamily=Family.default,
~fontWeight=Weight.Normal,
~italicized=false,
~monospaced=false,
~text="",
~smoothing=Revery_Font.Smoothing.default,
~smoothing=Smoothing.default,
~children=React.empty,
~mouseBehavior=Revery_UI.Normal,
(),
Expand Down Expand Up @@ -66,6 +71,10 @@ let%nativeComponent make =
tn#setText(text);
tn#setSmoothing(smoothing);
tn#setMouseBehavior(mouseBehavior);
tn#setFontFamily(fontFamily);
tn#setFontWeight(fontWeight);
tn#setItalicized(italicized);
tn#setMonospaced(monospaced);
node;
},
children,
Expand Down