forked from revery-ui/revery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes revery-ui#165
- Loading branch information
Saito Nakamura
committed
Feb 10, 2019
1 parent
47e7b19
commit 15a26df
Showing
8 changed files
with
226 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
open Revery.UI; | ||
open Revery.Core; | ||
|
||
module SomeText = { | ||
let component = React.component("Example"); | ||
|
||
let make = () => { | ||
component(slots => { | ||
let (_focused, _setFocus, _slots: React.Hooks.empty) = | ||
React.Hooks.state(false, slots); | ||
|
||
let textContent = "All work and no play makes Jack a dull boy"; | ||
|
||
<Text | ||
logging=true | ||
style=Style.[ | ||
color(Colors.white), | ||
fontFamily("Roboto-Regular.ttf"), | ||
fontSize(20), | ||
lineHeight(20.), | ||
textWrap(TextWrapping.WhitespaceWrap), | ||
width(200), | ||
border(~color=Colors.blueViolet, ~width=5) | ||
] | ||
text=textContent | ||
/>; | ||
}) | ||
} | ||
|
||
let createElement = (~children as _, ()) => | ||
React.element(make()); | ||
} | ||
|
||
let render = () => <View | ||
style=Style.[ | ||
position(`Absolute), | ||
justifyContent(`Center), | ||
alignItems(`Center), | ||
bottom(0), | ||
top(0), | ||
left(0), | ||
right(0), | ||
]> | ||
<SomeText /> | ||
</View>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
type wrapType = | ||
| NoWrap | ||
| WhitespaceWrap; | ||
|
||
let splitByWhitespace = text => { | ||
let (_a, _) = | ||
text | ||
|> Str.split(Str.regexp("[ \n\r\x0c\t]+")) | ||
|> List.fold_left( | ||
((splitted, runningSumOffset), cur) => { | ||
/* TODO is it? */ | ||
let whitespaceWidth = 1; | ||
let currentRunningSumOffset = | ||
runningSumOffset + String.length(cur) + whitespaceWidth; | ||
|
||
( | ||
[(cur, currentRunningSumOffset), ...splitted], | ||
currentRunningSumOffset, | ||
); | ||
}, | ||
([], 0), | ||
); | ||
_a; | ||
}; | ||
|
||
let wrapText = (~logging=false, ~text, ~measureWidth, ~maxWidth, ~isWrappingPoint) => { | ||
let currWidth = ref(0); | ||
let currOffset = ref(0); | ||
let currLineOffset = ref(0); | ||
let currLineNumber = ref(0); | ||
let maxWidthLine = ref(0); | ||
let lines: ref(list(string)) = ref([]); | ||
|
||
String.iteri((i, ch) => { | ||
let a = isWrappingPoint(Char.escaped(ch)); | ||
let isEnd = i == String.length(text) - 1; | ||
|
||
if (logging) { | ||
print_endline("TextNode:: iter:: char: " ++ Char.escaped(ch) ++ ", i: " ++ string_of_int(i)); | ||
print_endline("TextNode:: iter:: currOffset: " ++ string_of_int(currOffset^) ++ ", currLineOffset: " ++ string_of_int(currLineOffset^)); | ||
} | ||
|
||
if (a || isEnd) { | ||
let word = String.sub(text, currOffset^, i - currOffset^ + 1); | ||
let dWidth = measureWidth(word); | ||
|
||
if (logging) | ||
print_endline("TextNode:: iter:: word: " ++ word ++ ", width: " ++ string_of_int(dWidth)); | ||
|
||
if ((currWidth^ + dWidth) >= maxWidth || isEnd) { | ||
let line = if (isEnd) | ||
String.sub(text, currLineOffset^, String.length(text) - currLineOffset^) | ||
else | ||
String.sub(text, currLineOffset^, currOffset^ - currLineOffset^ - 1) | ||
|
||
if (logging) | ||
print_endline("TextNode:: line: " ++ line); | ||
|
||
lines := [line, ...lines^]; | ||
currLineNumber := currLineNumber^ + 1; | ||
currLineOffset := currLineOffset^ + currOffset^ - currLineOffset^ - 1; | ||
currWidth := 0; | ||
|
||
if (maxWidthLine^ < currWidth^) { | ||
maxWidthLine:= currWidth^; | ||
(); | ||
} else { | ||
(); | ||
} | ||
} else { | ||
currOffset := currOffset^ + String.length(word); | ||
currWidth := currWidth^ + dWidth; | ||
(); | ||
} | ||
} else { | ||
(); | ||
} | ||
}, text); | ||
|
||
(lines^, maxWidthLine^); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.