55 int Font11::Layout(
const std::wstring& text,LONG width,LONG height,UINT format,std::vector<Line>& lines,RECT& bounds)
58 TEXTMETRICW metric{};
if(!GetTextMetricsW(
dc,&metric))
return 0;
59 const auto measure=[&](
const std::wstring& line) {
60 std::vector<Line> words;
63 return words.empty() ? 0L : words.back().x+words.back().width;
65 RECT rectangle{};::DrawTextW(
dc,line.c_str(),
int(line.size()),&rectangle,
66 DT_CALCRECT|DT_SINGLELINE|DT_NOPREFIX|(format&(DT_EXPANDTABS|DT_RTLREADING)));
67 return rectangle.right;
69 std::wstring normalized;
70 for(
const auto character:text)
71 if(character!=L
'\r' && !(character==L
'\n' && (format&DT_SINGLELINE)) && !(character==L
'\t' && !(format&DT_EXPANDTABS)))
72 normalized+=character;
73 if(normalized.empty()){bounds={};
return 0;}
77 const size_t last=line.find_last_not_of(L
" \t");
78 if(last==std::wstring::npos)entry.
width=0;
79 else if(last+1<line.size())entry.
width-=measure(line.substr(last+1));
80 lines.push_back(std::move(entry));line.clear();
82 for(
size_t i=0;i<normalized.size();)
84 if(normalized[i]==L
'\n')
89 if((format&DT_WORDBREAK) && !(format&DT_SINGLELINE))
91 const size_t start=i;
while(i<normalized.size() && normalized[i]!=L
' ' && normalized[i]!=L
'\t' && normalized[i]!=L
'\n')++i;
92 if(i<normalized.size() && (normalized[i]==L
' ' || normalized[i]==L
'\t'))++i;
93 const auto word=normalized.substr(start,i-start);
96 if(!line.empty() && measure(line+word)>width)
98 while(!line.empty() && (line.back()==L
' ' || line.back()==L
'\t'))line.pop_back();
102 while(line.size()>1 && measure(line)>width)
105 while(count<line.size() && measure(line.substr(0,count+1))<=width)++count;
106 const auto remainder=line.substr(count);line.resize(count);append();line=remainder;
109 else line+=normalized[i++];
111 if(!line.empty() || lines.empty())append();
113 const LONG blockHeight=LONG(lines.size())*metric.tmHeight;
114 if(format&DT_VCENTER)y=LONG(std::floor(
double(height-blockHeight)/2));
115 else if(format&DT_BOTTOM)y=height-blockHeight;
116 bounds={LONG_MAX,y,LONG_MIN,y};
117 for(
auto& entry:lines)
119 bool right=(format&DT_RIGHT)!=0;
120 if(format&DT_RTLREADING)right=!right;
121 if(right && !(format&DT_CENTER))entry.x=width-entry.width;
122 else if(format&DT_CENTER)entry.x=(width-entry.width)/2;
123 entry.y=y;y+=metric.tmHeight;
124 bounds.left=(std::min)(bounds.left,entry.x);bounds.right=(std::max)(bounds.right,entry.x+entry.width);
126 bounds.bottom=y;
return y;
132 TEXTMETRICW metric{};
if(!GetTextMetricsW(
dc,&metric))
return E_FAIL;
133 const LONG border=(std::max)(4L,metric.tmHeight+metric.tmOverhang);
134 RECT measured{};std::vector<Line> lines;
136 const LONG originX=border-measured.left,originY=border-measured.top;
137 const LONG width=(std::max)(1L,measured.right-measured.left)+border*2;
138 const LONG height=(std::max)(1L,measured.bottom-measured.top)+border*2;
139 if(width>8192 || height>8192 ||
size_t(width)*height>
CacheLimit/4)
return E_INVALIDARG;
140 BITMAPINFO info{};info.bmiHeader.biSize=
sizeof(BITMAPINFOHEADER);
141 info.bmiHeader.biWidth=width;info.bmiHeader.biHeight=-height;
142 info.bmiHeader.biPlanes=1;info.bmiHeader.biBitCount=32;info.bmiHeader.biCompression=BI_RGB;
143 void* memory=
nullptr;HBITMAP bitmap=CreateDIBSection(
dc,&info,DIB_RGB_COLORS,&memory,
nullptr,0);
144 if(!bitmap)
return E_OUTOFMEMORY;
145 const auto previousBitmap=SelectObject(
dc,bitmap);
146 if(!previousBitmap || previousBitmap==HGDI_ERROR){DeleteObject(bitmap);
return E_FAIL;}
147 auto* pixels=
static_cast<uint32_t*
>(memory);std::fill(pixels,pixels+
size_t(width)*height,0);
148 const int saved=SaveDC(
dc);
149 if(!saved){SelectObject(
dc,previousBitmap);DeleteObject(bitmap);
return E_FAIL;}
150 if(!(run.
format&DT_NOCLIP))IntersectClipRect(
dc,originX,originY,originX+run.
width,originY+run.
height);
151 for(
const auto& line:lines)
153 RECT destination{originX+line.x,originY+line.y,originX+line.x+line.width,originY+line.y+metric.tmHeight};
154 std::vector<Line> words;
155 if(
Words(line.text,words))
157 for(
const auto& word:words)
159 RECT placed{destination.left+word.x,destination.top,destination.left+word.x+word.width,destination.bottom};
160 ::DrawTextW(
dc,word.text.c_str(),
int(word.text.size()),&placed,DT_SINGLELINE|DT_NOPREFIX|DT_NOCLIP);
164 ::DrawTextW(
dc,line.text.c_str(),
int(line.text.size()),&destination,
165 DT_SINGLELINE|DT_NOPREFIX|DT_NOCLIP|(run.
format&(DT_EXPANDTABS|DT_RTLREADING)));
169 RECT bounds{width,height,0,0};
170 for(LONG y=0;y<height;++y)
for(LONG x=0;x<width;++x)
if(pixels[
size_t(y)*width+x]&0xffffff)
172 bounds.left=(std::min)(bounds.left,x);bounds.top=(std::min)(bounds.top,y);
173 bounds.right=(std::max)(bounds.right,x+1);bounds.bottom=(std::max)(bounds.bottom,y+1);
176 if(bounds.right>bounds.left && bounds.bottom>bounds.top)
180 bounds.left=(std::max)(0L,bounds.left-1);bounds.top=(std::max)(0L,bounds.top-1);
181 bounds.right=(std::min)(width,bounds.right+1);bounds.bottom=(std::min)(height,bounds.bottom+1);
186 if(!(run.
format&DT_NOCLIP))
188 bounds.left=(std::max)(bounds.left,originX);bounds.top=(std::max)(bounds.top,originY);
189 bounds.right=(std::min)(bounds.right,originX+run.
width);bounds.bottom=(std::min)(bounds.bottom,originY+run.
height);
191 const UINT tw=UINT(bounds.right-bounds.left),th=UINT(bounds.bottom-bounds.top);
192 std::vector<uint32_t> coverage(
size_t(tw)*th);
193 for(UINT y=0;y<th;++y)
for(UINT x=0;x<tw;++x)
195 const uint32_t source=pixels[size_t(y+bounds.top)*width+x+bounds.left];
196 coverage[size_t(y)*tw+x]=0xffffff|((source&255)<<24);
198 D3D11_TEXTURE2D_DESC desc{};desc.Width=tw;desc.Height=th;desc.MipLevels=desc.ArraySize=1;
199 desc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;desc.SampleDesc.Count=1;
200 desc.Usage=D3D11_USAGE_IMMUTABLE;desc.BindFlags=D3D11_BIND_SHADER_RESOURCE;
201 const D3D11_SUBRESOURCE_DATA initial{coverage.data(),tw*4,0};
202 Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;
204 hr=
device->CreateTexture2D(&desc,&initial,&texture);
205 if(SUCCEEDED(hr))hr=
device->CreateShaderResourceView(texture.Get(),
nullptr,&run.
texture);
208 run.
source={0,0,LONG(tw),LONG(th)};run.
offset={bounds.left-originX,bounds.top-originY};
209 run.
bytes=coverage.size()*4;
211 SelectObject(
dc,previousBitmap);DeleteObject(bitmap);
218 constexpr UINT supported=DT_BOTTOM|DT_CALCRECT|DT_CENTER|DT_EXPANDTABS|DT_NOCLIP|DT_RIGHT|DT_RTLREADING|DT_SINGLELINE|DT_VCENTER|DT_WORDBREAK;
219 if(!
dc || !text || !rectangle || count<-1 || (format&~supported))
return 0;
220 const size_t length=count<0 ? wcslen(text) : size_t(count);
221 if(length>
size_t((std::numeric_limits<int>::max)()))
return 0;
222 if(format&DT_CALCRECT)
224 RECT bounds{};std::vector<Line> lines;
225 const int result=
Layout(std::wstring(text,length),rectangle->right-rectangle->left,rectangle->bottom-rectangle->top,format,lines,bounds);
226 OffsetRect(&bounds,rectangle->left,rectangle->top);*rectangle=bounds;
return result;
229 const LONG width=rectangle->right-rectangle->left,height=rectangle->bottom-rectangle->top;
231 auto found=std::find_if(
runs.begin(),
runs.end(),[&](
const Run& run) {
232 return run.width==width && run.height==height && run.format==format
233 && run.text.size()==length && run.text.compare(0,length,text,length)==0;
236 if(found==
runs.end())
245 const auto& run=
runs.front();
249 if(FAILED(sprite->
Draw(run.
texture.Get(),&run.
source,
nullptr,&position,color)))
return 0;
259 TEXTMETRICW metric{};
262 if(!GetTextMetricsW(
dc,&metric) || (metric.tmPitchAndFamily&TMPF_FIXED_PITCH)
263 || metric.tmItalic || metric.tmOverhang || metric.tmAveCharWidth<=0 || metric.tmHeight<=0
264 || metric.tmAveCharWidth>256 || metric.tmHeight>256)
return false;
266 for(
wchar_t c=L
' ';c<=L
'~';++c)
269 if(!GetTextExtentPoint32W(
dc,&c,1,&size) || size.cx!=
fixedAdvance)
return false;
271 const DWORD pairCount=GetKerningPairsW(
dc,0,
nullptr);
272 std::vector<KERNINGPAIR> pairs(pairCount);
273 if(pairCount && GetKerningPairsW(
dc,pairCount,pairs.data())!=pairCount)
return false;
274 for(
const auto& pair:pairs)
275 if(pair.iKernAmount && pair.wFirst>=32 && pair.wFirst<=126 && pair.wSecond>=32 && pair.wSecond<=126)
return false;
278 const LONG width=cellWidth*16,height=cellHeight*6;
279 if(width>8192 || height>8192 ||
size_t(width)*height>
CacheLimit/16)
return false;
280 BITMAPINFO info{};info.bmiHeader.biSize=
sizeof(BITMAPINFOHEADER);
281 info.bmiHeader.biWidth=width;info.bmiHeader.biHeight=-height;
282 info.bmiHeader.biPlanes=1;info.bmiHeader.biBitCount=32;info.bmiHeader.biCompression=BI_RGB;
283 void* memory=
nullptr;HBITMAP bitmap=CreateDIBSection(
dc,&info,DIB_RGB_COLORS,&memory,
nullptr,0);
284 if(!bitmap)
return false;
285 const auto previousBitmap=SelectObject(
dc,bitmap);
286 if(!previousBitmap || previousBitmap==HGDI_ERROR){DeleteObject(bitmap);
return false;}
287 auto* pixels=
static_cast<uint32_t*
>(memory);std::fill(pixels,pixels+
size_t(width)*height,0);
290 const wchar_t c=wchar_t(i+32);
291 RECT rectangle{LONG(i%16)*cellWidth+border,LONG(i/16)*cellHeight+border,0,0};
293 ::DrawTextW(
dc,&c,1,&rectangle,DT_SINGLELINE|DT_NOPREFIX|DT_NOCLIP);
298 const LONG left=LONG(i%16)*cellWidth,top=LONG(i/16)*cellHeight;
299 RECT bounds{cellWidth,cellHeight,0,0};
300 for(LONG y=0;y<cellHeight;++y)
for(LONG x=0;x<cellWidth;++x)
301 if(pixels[
size_t(top+y)*width+left+x]&0xffffff)
303 bounds.left=(std::min)(bounds.left,x);bounds.top=(std::min)(bounds.top,y);
304 bounds.right=(std::max)(bounds.right,x+1);bounds.bottom=(std::max)(bounds.bottom,y+1);
306 if(bounds.right>bounds.left && bounds.bottom>bounds.top)
311 || bounds.top<=0 || bounds.bottom>=cellHeight)
fixedGlyphs[i].independent=
false;
312 --bounds.left;--bounds.top;++bounds.right;++bounds.bottom;
313 fixedGlyphs[i].offset={bounds.left-border,bounds.top-border};
314 OffsetRect(&bounds,left,top);
fixedGlyphs[i].source=bounds;
320 if(first.source.right==first.source.left || second.source.right==second.source.left)
continue;
321 const LONG left=(std::max)(first.offset.x+1,
fixedAdvance+second.offset.x+1);
322 const LONG right=(std::min)(first.offset.x+first.source.right-first.source.left-1,
323 fixedAdvance+second.offset.x+second.source.right-second.source.left-1);
324 const LONG top=(std::max)(first.offset.y+1,second.offset.y+1);
325 const LONG bottom=(std::min)(first.offset.y+first.source.bottom-first.source.top-1,
326 second.offset.y+second.source.bottom-second.source.top-1);
327 for(LONG y=top;y<bottom;++y)
for(LONG x=left;x<right;++x)
328 if((pixels[
size_t(first.source.top+y-first.offset.y)*width+first.source.left+x-first.offset.x]&255)
329 && (pixels[
size_t(second.source.top+y-second.offset.y)*width+second.source.left+x-
fixedAdvance-second.offset.x]&255))
332 for(
size_t i=0;i<size_t(width)*height;++i)pixels[i]=0xffffff|((pixels[i]&255)<<24);
333 D3D11_TEXTURE2D_DESC desc{};desc.Width=width;desc.Height=height;desc.MipLevels=desc.ArraySize=1;
334 desc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;desc.SampleDesc.Count=1;
335 desc.Usage=D3D11_USAGE_IMMUTABLE;desc.BindFlags=D3D11_BIND_SHADER_RESOURCE;
336 const D3D11_SUBRESOURCE_DATA initial{pixels,UINT(width)*4,0};
337 Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;
339 if(SUCCEEDED(hr))hr=
device->CreateShaderResourceView(texture.Get(),
nullptr,&
fixedAtlas);
340 SelectObject(
dc,previousBitmap);DeleteObject(bitmap);
341 if(FAILED(hr))
return false;
348 if(!
dc || !text || !rectangle || count<-1)
return 0;
349 const size_t length=count<0 ? wcslen(text) : size_t(count);
350 const auto fallback=[&](){
return Draw(sprite,text,count,rectangle,format,color);};
351 if(!sprite || (format!=DT_NOCLIP && format!=(DT_NOCLIP|DT_SINGLELINE)) || length>4096)
return fallback();
357 const auto& transform=sprite->
Transform();
358 if(transform._11!=1 || transform._22!=1 || transform._44!=1
359 || transform._12 || transform._13 || transform._14 || transform._21 || transform._23 || transform._24
360 || transform._31 || transform._32 || transform._34
361 || transform._41!=std::floor(transform._41) || transform._42!=std::floor(transform._42))
return fallback();
362 for(
size_t i=0;i<length;++i)
if(text[i]<L
' ' || text[i]>L
'~')
return fallback();
365 for(
size_t i=0;i<length;++i)
367 const size_t glyph=size_t(text[i]-L
' ');
373 for(
size_t i=0;i<length;++i)
375 const auto& glyph=
fixedGlyphs[size_t(text[i]-L
' ')];
376 if(glyph.source.right==glyph.source.left)
continue;
378 if(FAILED(sprite->
Draw(
fixedAtlas.Get(),&glyph.source,
nullptr,&at,color)))
return 0;
385 if(!text || count<-1)
return 0;
386 const size_t inputLength=count<0 ? strlen(text) : size_t(count);
387 if(inputLength>
size_t((std::numeric_limits<int>::max)()))
return 0;
388 const int length=
int(inputLength);
389 const int required=MultiByteToWideChar(CP_ACP,0,text,length,
nullptr,0);
390 if(required<=0)
return 0;
391 std::wstring converted(
size_t(required),L
'\0');
392 if(!MultiByteToWideChar(CP_ACP,0,text,length,converted.data(),required))
return 0;
393 return Draw(sprite,converted.c_str(),required,rectangle,format,color);