Skip to content

Commit

Permalink
Rerendering after all fonts of StyledText are loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh committed Jul 21, 2017
1 parent 364ccbf commit b39ecb6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
17 changes: 17 additions & 0 deletions framer/StyledText.coffee
Expand Up @@ -182,6 +182,14 @@ class StyledTextBlock
getStyle: (style) -> getStyle: (style) ->
_.first(@inlineStyles).getStyle(style) _.first(@inlineStyles).getStyle(style)


getFonts: ->
fonts = []
for style in @inlineStyles
font = style.getStyle("fontFamily")
if font?
fonts.push(font)
return fonts

replaceText: (search, replace) -> replaceText: (search, replace) ->
currentIndex = 0 currentIndex = 0
for style in @inlineStyles for style in @inlineStyles
Expand Down Expand Up @@ -295,6 +303,15 @@ class exports.StyledText
getStyle: (style, block=null) -> getStyle: (style, block=null) ->
return (block ? _.first(@blocks))?.getStyle(style) ? @element?.style[style] return (block ? _.first(@blocks))?.getStyle(style) ? @element?.style[style]


getFonts: ->
fonts = []
elementFont = @element?.style["fontFamily"]
if elementFont?
fonts.push(elementFont)
for block in @blocks
fonts = fonts.concat block.getFonts()
return _.uniq(fonts)

measure: (currentSize) -> measure: (currentSize) ->
constraints = {} constraints = {}
constraints.width = currentSize.width * currentSize.multiplier constraints.width = currentSize.width * currentSize.multiplier
Expand Down
5 changes: 5 additions & 0 deletions framer/TextLayer.coffee
Expand Up @@ -144,6 +144,11 @@ class exports.TextLayer extends Layer
set: (value) -> set: (value) ->
@_styledText = new StyledText(value) @_styledText = new StyledText(value)
@_styledText.setElement(@_elementHTML) @_styledText.setElement(@_elementHTML)
fonts = @_styledText.getFonts()
promise = Utils.isFontFamilyLoaded(fonts)
if _.isObject(promise)
promise.then =>
@renderText()


#Vekter properties #Vekter properties
@define "autoWidth", @proxyProperty("_styledText.autoWidth", @define "autoWidth", @proxyProperty("_styledText.autoWidth",
Expand Down
6 changes: 4 additions & 2 deletions framer/Utils.coffee
Expand Up @@ -495,10 +495,12 @@ Utils.deviceFont = (os) ->
# Load fonts from Google Web Fonts # Load fonts from Google Web Fonts
_isFontLoadedResults = {} _isFontLoadedResults = {}


Utils.isFontFamilyLoaded = (font, timeout = 10000) -> Utils.isFontFamilyLoaded = (fonts, timeout = 10000) ->
if not _.isArray(fonts)
fonts = [fonts]
return Utils.loadWebFontConfig return Utils.loadWebFontConfig
custom: custom:
families: [font] families: fonts
timeout: timeout timeout: timeout


fontsFromConfig = (config) -> fontsFromConfig = (config) ->
Expand Down

0 comments on commit b39ecb6

Please sign in to comment.