modify assets's style parser

This commit is contained in:
侯歌 2022-02-08 11:44:31 +08:00
parent 2b59cfd062
commit 2f4054e2d5
6 changed files with 37 additions and 9 deletions

View File

@ -39,11 +39,35 @@ const std::string Assets::getTheme()const{
return mThemeName;
}
void Assets::normalizeProperty(const std::string&pkg,std::string&value){
size_t pos=value.find('?');
bool hasat=false;
if(pos!=std::string::npos){
value.erase(pos,1); hasat=true;
}
if((pos=value.find('@'))!=std::string::npos){
value.erase(pos,1); hasat=true;
}
if(hasat&&((pos=value.find(':'))==std::string::npos)){
value=pkg+":"+value;
}
}
void Assets::normalizeAttributes(const std::string&pkg,AttributeSet&atts){
std::map<const std::string,std::string>&entries=atts.getEntries();
for(auto it=entries.begin();it!=entries.end();it++){
normalizeProperty(pkg,it->second);
}
}
void Assets::setTheme(const std::string&theme){
auto it=mStyles.find(theme);
if(it!=mStyles.end()){
std::string pkg;
mThemeName= theme;
mTheme=it->second;
parseResource(theme,nullptr,&pkg);
normalizeAttributes(pkg,mTheme);
LOGD("set Theme to %s",theme.c_str());
}else{
LOGE("Theme %s not found",theme.c_str());
@ -380,26 +404,24 @@ void Assets::clearStyles(){
AttributeSet Assets::obtainStyledAttributes(const std::string&refname){
AttributeSet atts;
std::string name=refname;
std::string pkg,name=refname;
size_t pos=name.find("attr");
while(pos!=std::string::npos){
std::string pkg;
name=name.replace(pos,4,"style");
if((pos =name.find('?'))!=std::string::npos)
name=name.substr(pos+1);
parseResource(name,&name,&pkg);
name.erase(pos,1);
if((pos =name.find('/'))!=std::string::npos)
name=name.substr(pos+1);
name = mTheme.getString(name);
if((pos=name.find('@'))!=std::string::npos)
name.erase(pos,1);
if((pos=name.find(':'))==std::string::npos)
name=pkg+":"+name;
pos=name.find("attr");
}
auto it=mStyles.find(name);
if(it!=mStyles.end())atts=it->second;
const std::string parent=atts.getString("parent");
parseResource(name,nullptr,&pkg);
normalizeAttributes(pkg,atts);
if(parent.length()){
AttributeSet parentAtts=obtainStyledAttributes(parent);
atts.inherit(parentAtts);

View File

@ -24,6 +24,8 @@ private:
void parseResource(const std::string&fullresid,std::string*res,std::string*ns)const;
void parseItem(const std::string&package,const std::vector<std::string>&tag,std::vector<AttributeSet>atts,const std::string&value);
ZIPArchive*getResource(const std::string & fullresid, std::string* relativeResid)const;
void normalizeProperty(const std::string&pkg,std::string&value);
void normalizeAttributes(const std::string&pkg,AttributeSet&atts);
protected:
std::string mName;
DisplayMetrics mDisplayMetrics;

View File

@ -42,6 +42,10 @@ int AttributeSet::set(const char*atts[],int size){
return mAttrs.size();
}
std::map<const std::string,std::string>&AttributeSet::getEntries(){
return mAttrs;
}
int AttributeSet::inherit(const AttributeSet&other){
int inheritedCount=0;
for(auto it=other.mAttrs.begin();it!=other.mAttrs.end();it++){

View File

@ -15,6 +15,7 @@ public:
bool hasAttribute(const std::string&key)const;
int size()const;
int set(const char*atts[],int size=0);
std::map<const std::string,std::string>&getEntries();
int inherit(const AttributeSet&other);
const std::string getAttributeValue(const std::string&key)const;
bool getBoolean(const std::string&key,bool def=false)const;

View File

@ -3,8 +3,8 @@
#include <cdlog.h>
namespace cdroid{
#define DEFAULT_FRAME_DELAY 30
#define FRAME_CALLBACK_TOKEN 1
#define DEFAULT_FRAME_DELAY 20
#define FRAME_CALLBACK_TOKEN 1
class CallbackRecord {
public:
CallbackRecord* next;

View File

@ -271,7 +271,6 @@ TextView::TextView(Context*ctx,const AttributeSet& attrs)
setMinWidth(attrs.getDimensionPixelSize("minWidth", INT_MIN));
setMaxWidth(attrs.getDimensionPixelSize("maxWidth", INT_MAX));
mSingleLine=attrs.getBoolean("singleline",true);
attrs.dump();
if(attrs.hasAttribute("textAppearance")){
TextAppearanceAttributes attributes;
attributes.readTextAppearance(ctx,attrs);