diff options
Diffstat (limited to 'wx+')
-rwxr-xr-x | wx+/image_tools.cpp | 64 | ||||
-rwxr-xr-x | wx+/image_tools.h | 2 | ||||
-rwxr-xr-x | wx+/std_button_layout.h | 2 |
3 files changed, 49 insertions, 19 deletions
diff --git a/wx+/image_tools.cpp b/wx+/image_tools.cpp index 90945a44..bb5cd6c3 100755 --- a/wx+/image_tools.cpp +++ b/wx+/image_tools.cpp @@ -130,22 +130,43 @@ void calcAlphaForBlackWhiteImage(wxImage& image) //assume black text on white ba } -wxSize getTextExtent(const wxString& text, const wxFont& font) +std::vector<std::pair<wxString, wxSize>> getTextExtentInfo(const wxString& text, const wxFont& font) { wxMemoryDC dc; //the context used for bitmaps dc.SetFont(font); //the font parameter of GetMultiLineTextExtent() is not evalated on OS X, wxWidgets 2.9.5, so apply it to the DC directly! - return dc.GetMultiLineTextExtent(replaceCpy(text, L"&", L"", false)); //remove accelerator + + std::vector<std::pair<wxString, wxSize>> lineInfo; //text + extent + for (const wxString& line : split(text, L"\n", SplitType::ALLOW_EMPTY)) + lineInfo.emplace_back(line, line.empty() ? wxSize() : dc.GetTextExtent(line)); + + return lineInfo; } } -wxImage zen::createImageFromText(const wxString& text, const wxFont& font, const wxColor& col) +wxImage zen::createImageFromText(const wxString& text, const wxFont& font, const wxColor& col, ImageStackAlignment textAlign) { - //wxDC::DrawLabel() doesn't respect alpha channel => calculate alpha values manually: + //assert(!contains(text, L"&")); //accelerator keys not supported here + wxString textFmt = replaceCpy(text, L"&", L"", false); - if (text.empty()) + //for some reason wxDC::DrawText messes up "weak" bidi characters even when wxLayout_RightToLeft is set! (--> arrows in hebrew/arabic) + //=> use mark characters instead: + const wchar_t rtlMark = L'\u200F'; //UTF-8: E2 80 8F + if (wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft) + textFmt = rtlMark + textFmt + rtlMark; + + const std::vector<std::pair<wxString, wxSize>> lineInfo = getTextExtentInfo(textFmt, font); + + int maxWidth = 0; + int lineHeight = 0; + for (const auto& li : lineInfo) + { + maxWidth = std::max(maxWidth, li.second.GetWidth()); + lineHeight = std::max(lineHeight, li.second.GetHeight()); //wxWidgets comment "GetTextExtent will return 0 for empty string" + } + if (maxWidth == 0 || lineHeight == 0) return wxImage(); - wxBitmap newBitmap(getTextExtent(text, font)); //seems we don't need to pass 24-bit depth here even for high-contrast color schemes + wxBitmap newBitmap(maxWidth, lineHeight * lineInfo.size()); //seems we don't need to pass 24-bit depth here even for high-contrast color schemes { wxMemoryDC dc(newBitmap); dc.SetBackground(*wxWHITE_BRUSH); @@ -155,22 +176,31 @@ wxImage zen::createImageFromText(const wxString& text, const wxFont& font, const dc.SetTextBackground(*wxWHITE); // dc.SetFont(font); - //assert(!contains(text, L"&")); //accelerator keys not supported here; see also getTextExtent() - wxString textFmt = replaceCpy(text, L"&", L"", false); - - //for some reason wxDC::DrawText messes up "weak" bidi characters even when wxLayout_RightToLeft is set! (--> arrows in hebrew/arabic) - //=> use mark characters instead: - const wchar_t rtlMark = L'\u200F'; //UTF-8: E2 80 8F - if (wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft) - textFmt = rtlMark + textFmt + rtlMark; - - dc.DrawText(textFmt, wxPoint()); + int posY = 0; + for (const auto& li : lineInfo) + { + if (!li.first.empty()) + switch (textAlign) + { + case ImageStackAlignment::LEFT: + dc.DrawText(li.first, wxPoint(0, posY)); + break; + case ImageStackAlignment::RIGHT: + dc.DrawText(li.first, wxPoint(maxWidth - li.second.GetWidth(), posY)); + break; + case ImageStackAlignment::CENTER: + dc.DrawText(li.first, wxPoint((maxWidth - li.second.GetWidth()) / 2, posY)); + break; + } + + posY += lineHeight; + } } + //wxDC::DrawLabel() doesn't respect alpha channel => calculate alpha values manually: wxImage output(newBitmap.ConvertToImage()); output.SetAlpha(); - //calculate alpha channel calcAlphaForBlackWhiteImage(output); //apply actual text color diff --git a/wx+/image_tools.h b/wx+/image_tools.h index cd0e28f0..a1fed061 100755 --- a/wx+/image_tools.h +++ b/wx+/image_tools.h @@ -32,7 +32,7 @@ enum class ImageStackAlignment }; wxImage stackImages(const wxImage& img1, const wxImage& img2, ImageStackLayout dir, ImageStackAlignment align, int gap = 0); -wxImage createImageFromText(const wxString& text, const wxFont& font, const wxColor& col); +wxImage createImageFromText(const wxString& text, const wxFont& font, const wxColor& col, ImageStackAlignment textAlign = ImageStackAlignment::LEFT); //CENTER/LEFT/RIGHT wxBitmap layOver(const wxBitmap& background, const wxBitmap& foreground); //merge diff --git a/wx+/std_button_layout.h b/wx+/std_button_layout.h index fa4a269e..0f0d607f 100755 --- a/wx+/std_button_layout.h +++ b/wx+/std_button_layout.h @@ -115,7 +115,7 @@ void setStandardButtonLayout(wxBoxSizer& sizer, const StdButtons& buttons) sizer.Add(spaceRimH, 0); //OS X: there should be at least one button following the gap after the "dangerous" no-button - assert(buttonsTmp.btnYes); + assert(buttonsTmp.btnYes || buttonsTmp.btnCancel); } } |