Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_43355755
engine
提交
89a92952
E
engine
项目概览
weixin_43355755
/
engine
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
engine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
89a92952
编写于
3月 04, 2016
作者:
J
Jason Simmons
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Match fonts based on weight and style attributes
上级
545f9000
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
137 addition
and
39 deletion
+137
-39
sky/shell/ui/flutter_font_selector.cc
sky/shell/ui/flutter_font_selector.cc
+126
-29
sky/shell/ui/flutter_font_selector.h
sky/shell/ui/flutter_font_selector.h
+11
-10
未找到文件。
sky/shell/ui/flutter_font_selector.cc
浏览文件 @
89a92952
...
...
@@ -18,10 +18,6 @@
namespace
sky
{
namespace
shell
{
namespace
{
const
char
kFontManifestAssetPath
[]
=
"FontManifest.json"
;
}
using
base
::
DictionaryValue
;
using
base
::
JSONReader
;
using
base
::
ListValue
;
...
...
@@ -32,9 +28,73 @@ using blink::FontData;
using
blink
::
FontDescription
;
using
blink
::
FontFaceCreationParams
;
using
blink
::
FontPlatformData
;
using
blink
::
FontStyle
;
using
blink
::
FontWeight
;
using
blink
::
SimpleFontData
;
using
mojo
::
asset_bundle
::
ZipAssetBundle
;
// Style attributes of a Flutter font asset.
struct
FlutterFontSelector
::
FlutterFontAttributes
{
FlutterFontAttributes
(
const
std
::
string
&
path
);
~
FlutterFontAttributes
();
std
::
string
asset_path
;
int
weight
;
FontStyle
style
;
};
// A Skia typeface along with a buffer holding the raw typeface asset data.
struct
FlutterFontSelector
::
TypefaceAsset
{
TypefaceAsset
();
~
TypefaceAsset
();
RefPtr
<
SkTypeface
>
typeface
;
std
::
vector
<
uint8_t
>
data
;
};
namespace
{
const
char
kFontManifestAssetPath
[]
=
"FontManifest.json"
;
// Weight values corresponding to the members of the FontWeight enum.
const
int
kFontWeightValue
[]
=
{
100
,
200
,
300
,
400
,
500
,
600
,
700
,
800
,
900
};
const
int
kFontWeightNormal
=
kFontWeightValue
[
FontWeight
::
FontWeightNormal
];
int
getFontWeightValue
(
FontWeight
weight
)
{
size_t
weight_index
=
weight
;
return
(
weight_index
<
arraysize
(
kFontWeightValue
))
?
kFontWeightValue
[
weight_index
]
:
kFontWeightNormal
;
}
// Compares fonts within a family to determine which one most closely matches
// a FontDescription.
struct
FontMatcher
{
using
FlutterFontAttributes
=
FlutterFontSelector
::
FlutterFontAttributes
;
FontMatcher
(
const
FontDescription
&
description
)
:
description_
(
description
),
target_weight_
(
getFontWeightValue
(
description
.
weight
()))
{
}
bool
operator
()(
const
FlutterFontAttributes
&
font1
,
const
FlutterFontAttributes
&
font2
)
{
if
(
font1
.
style
!=
font2
.
style
)
{
if
(
font1
.
style
==
description_
.
style
())
return
true
;
if
(
font2
.
style
==
description_
.
style
())
return
false
;
}
int
weight_delta1
=
abs
(
font1
.
weight
-
target_weight_
);
int
weight_delta2
=
abs
(
font2
.
weight
-
target_weight_
);
return
weight_delta1
<
weight_delta2
;
}
private:
const
FontDescription
&
description_
;
int
target_weight_
;
};
}
void
FlutterFontSelector
::
install
(
const
scoped_refptr
<
ZipAssetBundle
>&
zip_asset_bundle
)
{
RefPtr
<
FlutterFontSelector
>
font_selector
=
adoptRef
(
...
...
@@ -57,6 +117,16 @@ FlutterFontSelector::TypefaceAsset::TypefaceAsset() {
FlutterFontSelector
::
TypefaceAsset
::~
TypefaceAsset
()
{
}
FlutterFontSelector
::
FlutterFontAttributes
::
FlutterFontAttributes
(
const
std
::
string
&
path
)
:
asset_path
(
path
),
weight
(
kFontWeightNormal
),
style
(
FontStyle
::
FontStyleNormal
)
{
}
FlutterFontSelector
::
FlutterFontAttributes
::~
FlutterFontAttributes
()
{
}
void
FlutterFontSelector
::
parseFontManifest
()
{
std
::
vector
<
uint8_t
>
font_manifest_data
;
if
(
!
zip_asset_bundle_
->
GetAsBuffer
(
kFontManifestAssetPath
,
...
...
@@ -86,21 +156,32 @@ void FlutterFontSelector::parseFontManifest() {
if
(
!
family_dict
->
GetList
(
"fonts"
,
&
font_list
))
continue
;
if
(
font_list
->
GetSize
()
!=
1
)
{
LOG
(
WARNING
)
<<
"Font family "
<<
family_name
<<
" must have exactly one font"
;
continue
;
}
DictionaryValue
*
font_dict
;
if
(
!
font_list
->
GetDictionary
(
0
,
&
font_dict
))
continue
;
AtomicString
family_key
=
AtomicString
::
fromUTF8
(
family_name
.
c_str
());
auto
set_result
=
font_family_map_
.
set
(
family_key
,
std
::
vector
<
FlutterFontAttributes
>
());
std
::
vector
<
FlutterFontAttributes
>&
family_assets
=
set_result
.
storedValue
->
value
;
std
::
string
asset_path
;
if
(
!
font_dict
->
GetString
(
"asset"
,
&
asset_path
))
continue
;
for
(
Value
*
list_entry
:
*
font_list
)
{
DictionaryValue
*
font_dict
;
if
(
!
list_entry
->
GetAsDictionary
(
&
font_dict
))
continue
;
std
::
string
asset_path
;
if
(
!
font_dict
->
GetString
(
"asset"
,
&
asset_path
))
continue
;
FlutterFontAttributes
attributes
(
asset_path
);
font_dict
->
GetInteger
(
"weight"
,
&
attributes
.
weight
);
std
::
string
style
;
if
(
font_dict
->
GetString
(
"style"
,
&
style
))
{
if
(
style
==
"italic"
)
attributes
.
style
=
FontStyle
::
FontStyleItalic
;
}
font_asset_path_map_
.
set
(
AtomicString
::
fromUTF8
(
family_name
.
c_str
()),
AtomicString
::
fromUTF8
(
asset_path
.
c_str
()));
family_assets
.
push_back
(
attributes
);
}
}
}
...
...
@@ -112,7 +193,7 @@ PassRefPtr<FontData> FlutterFontSelector::getFontData(
RefPtr
<
SimpleFontData
>
font_data
=
font_platform_data_cache_
.
get
(
key
);
if
(
font_data
==
nullptr
)
{
SkTypeface
*
typeface
=
getTypefaceAsset
(
family_name
);
SkTypeface
*
typeface
=
getTypefaceAsset
(
f
ont_description
,
f
amily_name
);
if
(
typeface
==
nullptr
)
return
nullptr
;
...
...
@@ -139,21 +220,35 @@ PassRefPtr<FontData> FlutterFontSelector::getFontData(
}
SkTypeface
*
FlutterFontSelector
::
getTypefaceAsset
(
const
FontDescription
&
font_description
,
const
AtomicString
&
family_name
)
{
auto
it
=
typeface_cache_
.
find
(
family_name
);
if
(
it
!=
typeface_cache_
.
end
())
{
const
TypefaceAsset
*
cache_asset
=
it
->
value
.
get
();
return
cache_asset
?
cache_asset
->
typeface
.
get
()
:
nullptr
;
}
auto
family_iter
=
font_family_map_
.
find
(
family_name
);
if
(
family_iter
==
font_family_map_
.
end
())
return
nullptr
;
String
font_asset_path
=
font_asset_path_map_
.
get
(
family_name
)
;
if
(
font
_asset_path
.
isE
mpty
())
const
std
::
vector
<
FlutterFontAttributes
>&
fonts
=
family_iter
->
value
;
if
(
font
s
.
e
mpty
())
return
nullptr
;
std
::
vector
<
FlutterFontAttributes
>::
const_iterator
font_iter
;
if
(
fonts
.
size
()
==
1
)
{
font_iter
=
fonts
.
begin
();
}
else
{
font_iter
=
std
::
min_element
(
fonts
.
begin
(),
fonts
.
end
(),
FontMatcher
(
font_description
));
}
const
std
::
string
&
asset_path
=
font_iter
->
asset_path
;
auto
typeface_iter
=
typeface_cache_
.
find
(
asset_path
);
if
(
typeface_iter
!=
typeface_cache_
.
end
())
{
const
TypefaceAsset
*
cache_asset
=
typeface_iter
->
second
.
get
();
return
cache_asset
?
cache_asset
->
typeface
.
get
()
:
nullptr
;
}
std
::
unique_ptr
<
TypefaceAsset
>
typeface_asset
(
new
TypefaceAsset
);
if
(
!
zip_asset_bundle_
->
GetAsBuffer
(
font_asset_path
.
toUTF8
()
,
if
(
!
zip_asset_bundle_
->
GetAsBuffer
(
asset_path
,
&
typeface_asset
->
data
))
{
typeface_cache_
.
set
(
family_name
,
nullptr
);
typeface_cache_
.
insert
(
std
::
make_pair
(
asset_path
,
nullptr
)
);
return
nullptr
;
}
...
...
@@ -163,12 +258,14 @@ SkTypeface* FlutterFontSelector::getTypefaceAsset(
typeface_asset
->
typeface
=
adoptRef
(
font_mgr
->
createFromStream
(
typeface_stream
));
if
(
typeface_asset
->
typeface
==
nullptr
)
{
typeface_cache_
.
set
(
family_name
,
nullptr
);
typeface_cache_
.
insert
(
std
::
make_pair
(
asset_path
,
nullptr
)
);
return
nullptr
;
}
SkTypeface
*
result
=
typeface_asset
->
typeface
.
get
();
typeface_cache_
.
set
(
family_name
,
adoptPtr
(
typeface_asset
.
release
()));
typeface_cache_
.
insert
(
std
::
make_pair
(
asset_path
,
std
::
move
(
typeface_asset
)));
return
result
;
}
...
...
sky/shell/ui/flutter_font_selector.h
浏览文件 @
89a92952
...
...
@@ -5,6 +5,7 @@
#ifndef SKY_SHELL_UI_FLUTTER_FONT_SELECTOR_H_
#define SKY_SHELL_UI_FLUTTER_FONT_SELECTOR_H_
#include <unordered_map>
#include <vector>
#include "sky/engine/platform/fonts/FontCacheKey.h"
...
...
@@ -24,6 +25,8 @@ namespace shell {
// loaded from the FLX.
class
FlutterFontSelector
:
public
blink
::
FontSelector
{
public:
struct
FlutterFontAttributes
;
~
FlutterFontSelector
()
override
;
static
void
install
(
...
...
@@ -42,24 +45,22 @@ class FlutterFontSelector : public blink::FontSelector {
void
fontCacheInvalidated
()
override
;
private:
// A Skia typeface along with a buffer holding the raw typeface asset data.
struct
TypefaceAsset
{
TypefaceAsset
();
~
TypefaceAsset
();
RefPtr
<
SkTypeface
>
typeface
;
std
::
vector
<
uint8_t
>
data
;
};
struct
TypefaceAsset
;
FlutterFontSelector
(
const
scoped_refptr
<
mojo
::
asset_bundle
::
ZipAssetBundle
>&
zip_asset_bundle
);
void
parseFontManifest
();
SkTypeface
*
getTypefaceAsset
(
const
AtomicString
&
family_name
);
SkTypeface
*
getTypefaceAsset
(
const
blink
::
FontDescription
&
font_description
,
const
AtomicString
&
family_name
);
scoped_refptr
<
mojo
::
asset_bundle
::
ZipAssetBundle
>
zip_asset_bundle_
;
HashMap
<
AtomicString
,
String
>
font_asset_path_map_
;
HashMap
<
AtomicString
,
OwnPtr
<
TypefaceAsset
>>
typeface_cache_
;
HashMap
<
AtomicString
,
std
::
vector
<
FlutterFontAttributes
>>
font_family_map_
;
std
::
unordered_map
<
std
::
string
,
std
::
unique_ptr
<
TypefaceAsset
>>
typeface_cache_
;
typedef
HashMap
<
blink
::
FontCacheKey
,
RefPtr
<
blink
::
SimpleFontData
>
,
blink
::
FontCacheKeyHash
,
blink
::
FontCacheKeyTraits
>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录