Skip to content

Commit

Permalink
Fix #357: TextNode.re: render text when dimensions are specified (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
romgrk authored and bryphe committed Mar 12, 2019
1 parent 816c606 commit 562ecf8
Showing 1 changed file with 75 additions and 57 deletions.
132 changes: 75 additions & 57 deletions src/UI/TextNode.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class textNode (text: string) = {
as _this;
val mutable text = text;
val mutable gamma = 2.2;
val mutable _isMeasured = false;
val _lines: ref(list(string)) = ref([]);
inherit (class viewNode)() as _super;
pub! draw = (parentContext: NodeDrawContext.t) => {
Expand All @@ -31,6 +32,11 @@ class textNode (text: string) = {
let lineHeightPx =
Text.getLineHeight(~fontFamily, ~fontSize, ~lineHeight, ());

/* when style.width & style.height are defined, Layout doesn't call the measure function */
if (!_isMeasured) {
_this#measure(style.width, style.height) |> ignore;
};

List.iteri(
(lineNum, line) =>
Text.drawString(
Expand Down Expand Up @@ -62,71 +68,83 @@ class textNode (text: string) = {
pub setText = t =>
if (!String.equal(t, text)) {
text = t;
_isMeasured = false;
_this#markLayoutDirty();
};
pub! getMeasureFunction = () => {
let measure =
(_mode, width, _widthMeasureMode, _height, _heightMeasureMode) => {
/* TODO: Cache font locally in variable */
let style = _super#getStyle();
let textWrap = style.textWrap;

let fontFamily = style.fontFamily;
let fontSize = style.fontSize;
let lineHeight = style.lineHeight;

let lineHeightPx =
Text.getLineHeight(~fontFamily, ~fontSize, ~lineHeight, ());

switch (textWrap) {
| WhitespaceWrap =>
let (lines, maxWidthLine) =
TextWrapping.wrapText(
~text,
~measureWidth=
str => Text.measure(~fontFamily, ~fontSize, str).width,
~maxWidth=width,
~wrapHere=TextWrapping.isWhitespaceWrapPoint,
);

_lines := lines;

let dimensions: Layout.LayoutTypes.dimensions = {
width: int_of_float(float_of_int(maxWidthLine)),
height:
int_of_float(float_of_int(List.length(lines)) *. lineHeightPx),
};

dimensions;
| NoWrap =>
let d = Text.measure(~fontFamily, ~fontSize, text);
let dimensions: Layout.LayoutTypes.dimensions = {
width: d.width,
height: d.height,
};

_lines := [text];

dimensions;
| UserDefined(wrapFunc) =>
let (lines, maxWidthLine) =
wrapFunc(
text,
pub measure = (width: int, _height: int) => {
/* TODO: Cache font locally in variable */
_isMeasured = true;

let style = _super#getStyle();
let textWrap = style.textWrap;

let fontFamily = style.fontFamily;
let fontSize = style.fontSize;
let lineHeight = style.lineHeight;

let lineHeightPx =
Text.getLineHeight(~fontFamily, ~fontSize, ~lineHeight, ());

switch (textWrap) {
| WhitespaceWrap =>
let (lines, maxWidthLine) =
TextWrapping.wrapText(
~text,
~measureWidth=
str => Text.measure(~fontFamily, ~fontSize, str).width,
width,
);
~maxWidth=width,
~wrapHere=TextWrapping.isWhitespaceWrapPoint,
);

_lines := lines;

let dimensions: Layout.LayoutTypes.dimensions = {
width: int_of_float(float_of_int(maxWidthLine)),
height:
int_of_float(float_of_int(List.length(lines)) *. lineHeightPx),
};

dimensions;
| NoWrap =>
let d = Text.measure(~fontFamily, ~fontSize, text);
let dimensions: Layout.LayoutTypes.dimensions = {
width: d.width,
height: d.height,
};

_lines := lines;
_lines := [text];

let dimensions: Layout.LayoutTypes.dimensions = {
width: maxWidthLine,
height:
int_of_float(float_of_int(List.length(lines)) *. lineHeightPx),
};
dimensions;
| UserDefined(wrapFunc) =>
let (lines, maxWidthLine) =
wrapFunc(
text,
str => Text.measure(~fontFamily, ~fontSize, str).width,
width,
);

dimensions;
_lines := lines;

let dimensions: Layout.LayoutTypes.dimensions = {
width: maxWidthLine,
height:
int_of_float(float_of_int(List.length(lines)) *. lineHeightPx),
};

dimensions;
};
};
pub! getMeasureFunction = () => {
let measure =
(
_mode: LayoutTypes.node,
width: int,
_widthMeasureMode: LayoutTypes.measureMode,
height: int,
_heightMeasureMode: LayoutTypes.measureMode,
) =>
_this#measure(width, height);

Some(measure);
};
};

0 comments on commit 562ecf8

Please sign in to comment.