bool IPAddress::IsIP6(StringRef val) { RETURN_FALSE_IF_EMPTY(val); in6_addr sAddr; // IPv6地址结构体 int r = inet_pton((int)SocketAddressFamily::IP6, val.c_str(), &sAddr); return r > 0; //<=0 means error }
bool IPAddress::IsIP(StringRef val) { //IP address format:<ddd.ddd.ddd.ddd> RETURN_FALSE_IF_EMPTY(val); in_addr sAddr; // IPv4地址结构体 int r = inet_pton((int)SocketAddressFamily::IP, val.c_str(), &sAddr); return r > 0; //<=0 means error }
bool IFileLoadable::LoadFromFileSystem(const FileIdRef& fileId, uint format/*=(uint)-1*/) { MemoryByteData data = FileSystem::Instance().ReadAllData(fileId); if (format == (uint)-1) { format = OnCheckFormat(fileId.Name); } RETURN_FALSE_IF_EMPTY(data); return LoadFromData(fileId.Name, data, format); }
bool IPAddress::IsPort(StringRef inValue) { //Port format:<d*> RETURN_FALSE_IF_EMPTY(inValue); size_t length = inValue.Length(); for (size_t i = 0; i < length; i++) { if (!isdigit(inValue[i])) { return false; } } return true; }
bool TextLayouter::LayoutMultipleLineText(List<Share<BaseFontMesh>>& outMeshes, List<TextureAtlasPage*>& outPages, Size2F& outSize, IFont& font, const WStringRef& text, Alignment alignment/*=Alignment::LeftBottom*/, const Size2F& restrictSize/*==Size2F::Zero*/, ILabel* label/*=nullptr*/, bool isStatic/*=false*/) { RETURN_FALSE_IF_EMPTY(text); outSize = restrictSize; List<WHeapString> outLines; List<float> outLineWidths; if (restrictSize.Width != 0) { if (!WordWrapWithWidth(outLines, outLineWidths, outSize.Width, font, text, restrictSize.Width)) { Log::Error("Invalid layout"); return false; } } else { if (!WordWrap(outLines, outLineWidths, outSize.Width, font, text)) { Log::Error("Invalid layout"); return false; } } outSize.Height = static_cast<float>(outLineWidths.Count()*font.LineHeight()); ReserveMesh(outMeshes, text); auto maxOrigin= LayoutMultipleLineMesh(outMeshes,outPages, font, outSize, outLineWidths, outLines, alignment, restrictSize, label); outSize = GetBoundingSize(restrictSize.Width,outSize.Height,maxOrigin, alignment, restrictSize); ShrinkMesh(outMeshes); return true; }
bool TextLayouter::LayoutSingleLineText(List<Share<BaseFontMesh>>& outMeshes, List<TextureAtlasPage*>& outPages, Size2F& outSize, IFont& font, const WStringRef& text, Alignment alignment/*=Alignment::LeftBottom*/, const Size2F& restrictSize/*=Size2F::Zero*/, ILabel* label/*=nullptr*/, bool isStatic/*=false*/) { RETURN_FALSE_IF_EMPTY(text); outSize = restrictSize; WHeapString outString; ExpandSingleLine(font, text, outString, outSize.Width); outSize.Height = (float)font.LineHeight(); ReserveMesh(outMeshes, text); auto maxOrigin= LayoutSingleLineMesh(outMeshes,outPages, font, outSize, outSize.Width, outString, alignment, restrictSize, label); outSize = GetBoundingSize(restrictSize.Width,(float)font.LineHeight(),maxOrigin, alignment, restrictSize); ShrinkMesh(outMeshes); return true; }
bool StringTable::Initialize(const FileIdRef& fileId, uint format /*= 0*/) { MemoryByteData data = FileSystem::Instance().ReadAllData(fileId); RETURN_FALSE_IF_EMPTY(data); return LoadFromData(fileId.Name, data, format); }
bool TextLayouter::WordWrapWithWidth(List<WHeapString>& outLines, List<float>& outLineWidths, float& outMaxWidth, IFont& font, const WStringRef& text, float restrictWidth) { RETURN_FALSE_IF_EMPTY(text); outMaxWidth = 0; /* items begins with "ok", record the offset can break line according to "Unicode Line Breaking Algorithm". items begins with "fit", record the line width,the max pos that can allow chars for each char width array, add char width to w then compare w to width limit that passed in when w < width limit, will update items that begins with "fit" after updated,check if it's the fit break line pos according to "Unicode Line Breaking Algorithm" if yes,update "ok" items . if w> width limit, output current line datas. */ uint textLength = static_cast<uint>(text.Length()); uint okBegin = 0; float okWidth = 0; float fitWidth = 0; outLines.Append(WHeapString::Empty); WHeapString* line = &outLines.Last(); float lineWidth = 0; for (uint i = 0; i < textLength; ++i) { wchar c = text[i]; switch (c) { case L'\n': { //force to break line //add left chars to cur line line->Append(text.c_str() + okBegin, i - okBegin); //ignore '\n' okWidth += fitWidth; lineWidth += okWidth; outLineWidths.Add(lineWidth); outMaxWidth = Math::Max(outMaxWidth, lineWidth); outLines.Append(WHeapString::Empty); line = &outLines.Last(); lineWidth = 0; fitWidth = 0; okWidth = 0; okBegin = i + 1; } break; case L'\t': { //add left chars to line line->Append(text.c_str() + okBegin, i - okBegin); //ignore '\t' okWidth += fitWidth; lineWidth += okWidth; okBegin = i + 1; fitWidth = 0; okWidth = 0; //try to expand tab float spaceWidth = (float)font.SpaceWidth(); float copyFitWidth = fitWidth; size_t spaceCount = 0; size_t planSpaceCount = TabWidth - line->Length() % TabWidth; FOR_EACH_SIZE(j, planSpaceCount) { copyFitWidth += spaceWidth; if (copyFitWidth > restrictWidth) { spaceCount = j; break; } } //add left chars if (spaceCount == 0) //can add all spaces { line->Append(L' ', planSpaceCount); lineWidth += planSpaceCount*spaceWidth; } else { //add some spaces line->Append(L' ', spaceCount); lineWidth += spaceCount*spaceWidth; //have to break line outLineWidths.Add(lineWidth); outMaxWidth = Math::Max(outMaxWidth, lineWidth); outLines.Append(WHeapString::Empty); line = &outLines.Last(); lineWidth = 0; } } break; case L'\r': case L'\f': case L'\v': //ignore break; default: { float charWidth = GetCharWidth(font, c); fitWidth += charWidth; if (fitWidth < restrictWidth) { if (IsLineBreak(text, i)) { //safe to add current ok chars to line line->Append(text.c_str() + okBegin, i - okBegin + 1); okWidth += fitWidth; lineWidth += okWidth; okWidth = 0; okBegin = i + 1; fitWidth = 0; } else { //do nothing } } else { //have to break line //cur line is enough okWidth += fitWidth - charWidth; lineWidth += okWidth; outLineWidths.Add(lineWidth); outMaxWidth = Math::Max(outMaxWidth, lineWidth); outLines.Append(WHeapString::Empty); line = &outLines.Last(); line->Append(text.c_str() + okBegin, i - okBegin + 1); fitWidth = charWidth; okWidth = charWidth; okBegin = i + 1; lineWidth = 0; // generate next line //add left chars to next line } } break; } }