mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Use detail::RangedValues<Font> on ShapedText related interfaces
Previously using the FontForRange type was motivated by hiding the RangedValues type in case we wanted to expose the ShapedText API. This introduced unnecessary conversions between FontForRange and RangedValues<Font>.
This commit is contained in:
parent
ad43702e88
commit
ef840b7472
3 changed files with 28 additions and 47 deletions
|
|
@ -494,19 +494,19 @@ static auto makeSpan (T& array)
|
||||||
return Span { array.getRawDataPointer(), (size_t) array.size() };
|
return Span { array.getRawDataPointer(), (size_t) array.size() };
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::vector<FontForRange> findSuitableFontsForText (const Font& font,
|
static detail::RangedValues<Font> findSuitableFontsForText (const Font& font,
|
||||||
const String& text,
|
const String& text,
|
||||||
const String& language = {})
|
const String& language = {})
|
||||||
{
|
{
|
||||||
detail::RangedValues<std::optional<Font>> fonts;
|
detail::RangedValues<std::optional<Font>> fonts;
|
||||||
fonts.set ({ 0, (int64) text.length() }, font);
|
fonts.set ({ 0, (int64) text.length() }, font);
|
||||||
|
|
||||||
const auto getResult = [&]
|
const auto getResult = [&]
|
||||||
{
|
{
|
||||||
std::vector<FontForRange> result;
|
detail::RangedValues<Font> result;
|
||||||
|
|
||||||
for (const auto [r, v] : fonts)
|
for (const auto [r, v] : fonts)
|
||||||
result.emplace_back (r, v.value_or (font));
|
result.set (r, v.value_or (font));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
@ -540,7 +540,7 @@ static std::vector<FontForRange> findSuitableFontsForText (const Font& font,
|
||||||
// can't find any more suitable fonts or all codepoints have one
|
// can't find any more suitable fonts or all codepoints have one
|
||||||
for (auto numMissingGlyphs = markMissingGlyphs(); numMissingGlyphs > 0;)
|
for (auto numMissingGlyphs = markMissingGlyphs(); numMissingGlyphs > 0;)
|
||||||
{
|
{
|
||||||
std::vector<FontForRange> changes;
|
std::vector<std::pair<Range<int64>, Font>> changes;
|
||||||
|
|
||||||
for (const auto [r, f] : fonts)
|
for (const auto [r, f] : fonts)
|
||||||
{
|
{
|
||||||
|
|
@ -575,10 +575,8 @@ static RangedValues<Font> resolveFontsWithFallback (const String& string, const
|
||||||
auto rf = findSuitableFontsForText (f, string.substring ((int) r.getStart(),
|
auto rf = findSuitableFontsForText (f, string.substring ((int) r.getStart(),
|
||||||
(int) std::min (r.getEnd(), (int64) string.length())));
|
(int) std::min (r.getEnd(), (int64) string.length())));
|
||||||
|
|
||||||
for (auto& item : rf)
|
for (const auto [subRange, font] : rf)
|
||||||
item.first += r.getStart();
|
resolved.set<MergeEqualItems::no> (subRange + r.getStart(), font);
|
||||||
|
|
||||||
resolved.setForEach<MergeEqualItems::no> (rf.begin(), rf.end());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolved;
|
return resolved;
|
||||||
|
|
@ -785,16 +783,11 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static auto createRangedValues (const std::vector<std::pair<Range<int64>, T>>& pairs, int64 offset = 0)
|
static auto rangedValuesWithOffset (detail::RangedValues<T> rv, int64 offset = 0)
|
||||||
{
|
{
|
||||||
detail::RangedValues<T> result;
|
rv.shift (std::numeric_limits<int64>::min(), -offset);
|
||||||
|
rv.eraseUpTo (0);
|
||||||
for (const auto& [range, value] : pairs)
|
return rv;
|
||||||
result.insert (range.movedToStartAt (range.getStart() - offset), value);
|
|
||||||
|
|
||||||
result.eraseUpTo (0);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Shaper
|
struct Shaper
|
||||||
|
|
@ -819,8 +812,8 @@ struct Shaper
|
||||||
const auto bidiLevels = bidiParagraph.getResolvedLevels();
|
const auto bidiLevels = bidiParagraph.getResolvedLevels();
|
||||||
|
|
||||||
const auto fonts = resolveFontsWithFallback (string,
|
const auto fonts = resolveFontsWithFallback (string,
|
||||||
createRangedValues (options.getFontsForRange(),
|
rangedValuesWithOffset (options.getFontsForRange(),
|
||||||
shapingRange.getStart()));
|
shapingRange.getStart()));
|
||||||
|
|
||||||
for (Unicode::LineBreakIterator lineIter { makeSpan (analysis) }; auto lineRun = lineIter.next();)
|
for (Unicode::LineBreakIterator lineIter { makeSpan (analysis) }; auto lineRun = lineIter.next();)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,6 @@
|
||||||
namespace juce::detail
|
namespace juce::detail
|
||||||
{
|
{
|
||||||
|
|
||||||
using FontForRange = std::pair<Range<int64>, Font>;
|
|
||||||
|
|
||||||
/** Types of text direction. This may also be applied to characters. */
|
/** Types of text direction. This may also be applied to characters. */
|
||||||
enum class TextDirection
|
enum class TextDirection
|
||||||
{
|
{
|
||||||
|
|
@ -64,12 +62,13 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] ShapedTextOptions withFont (Font x) const
|
[[nodiscard]] ShapedTextOptions withFont (Font x) const
|
||||||
{
|
{
|
||||||
return withMember (*this, &ShapedTextOptions::fontsForRange,
|
RangedValues<Font> fonts;
|
||||||
std::vector<FontForRange> { { { 0, std::numeric_limits<int64>::max() },
|
fonts.set ({ 0, std::numeric_limits<int64>::max() }, x);
|
||||||
x } });
|
|
||||||
|
return withMember (*this, &ShapedTextOptions::fontsForRange, std::move (fonts));
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] ShapedTextOptions withFontsForRange (const std::vector<FontForRange>& x) const
|
[[nodiscard]] ShapedTextOptions withFonts (const detail::RangedValues<Font>& x) const
|
||||||
{
|
{
|
||||||
return withMember (*this, &ShapedTextOptions::fontsForRange, x);
|
return withMember (*this, &ShapedTextOptions::fontsForRange, x);
|
||||||
}
|
}
|
||||||
|
|
@ -151,8 +150,14 @@ private:
|
||||||
std::optional<TextDirection> readingDir;
|
std::optional<TextDirection> readingDir;
|
||||||
std::optional<float> maxWidth;
|
std::optional<float> maxWidth;
|
||||||
std::optional<float> height;
|
std::optional<float> height;
|
||||||
std::vector<FontForRange> fontsForRange { { { 0, std::numeric_limits<int64>::max() },
|
|
||||||
FontOptions { 15.0f } } };
|
detail::RangedValues<Font> fontsForRange = std::invoke ([&]
|
||||||
|
{
|
||||||
|
detail::RangedValues<Font> result;
|
||||||
|
result.set ({ 0, std::numeric_limits<int64>::max() }, FontOptions { 15.0f });
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
|
||||||
String language = SystemStats::getDisplayLanguage();
|
String language = SystemStats::getDisplayLanguage();
|
||||||
float firstLineIndent = 0.0f;
|
float firstLineIndent = 0.0f;
|
||||||
float leading = 1.0f;
|
float leading = 1.0f;
|
||||||
|
|
|
||||||
|
|
@ -325,23 +325,6 @@ static auto castTo (const Range<U>& r)
|
||||||
return Range<T> (static_cast<T> (r.getStart()), static_cast<T> (r.getEnd()));
|
return Range<T> (static_cast<T> (r.getStart()), static_cast<T> (r.getEnd()));
|
||||||
}
|
}
|
||||||
|
|
||||||
static auto getFontsForRange (const detail::RangedValues<Font>& fonts)
|
|
||||||
{
|
|
||||||
using namespace detail;
|
|
||||||
|
|
||||||
std::vector<FontForRange> result;
|
|
||||||
result.reserve (fonts.size());
|
|
||||||
|
|
||||||
std::transform (fonts.begin(),
|
|
||||||
fonts.end(),
|
|
||||||
std::back_inserter (result),
|
|
||||||
[] (auto entry) {
|
|
||||||
return FontForRange { entry.range, entry.value };
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Range<int64> getInputRange (const detail::ShapedText& st, Range<int64> glyphRange)
|
static Range<int64> getInputRange (const detail::ShapedText& st, Range<int64> glyphRange)
|
||||||
{
|
{
|
||||||
if (glyphRange.isEmpty())
|
if (glyphRange.isEmpty())
|
||||||
|
|
@ -425,7 +408,7 @@ void TextLayout::createStandardLayout (const AttributedString& text)
|
||||||
colours.set (range, attribute.colour);
|
colours.set (range, attribute.colour);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto shapedTextOptions = ShapedTextOptions{}.withFontsForRange (getFontsForRange (fonts))
|
auto shapedTextOptions = ShapedTextOptions{}.withFonts (fonts)
|
||||||
.withLanguage (SystemStats::getUserLanguage())
|
.withLanguage (SystemStats::getUserLanguage())
|
||||||
.withTrailingWhitespacesShouldFit (false)
|
.withTrailingWhitespacesShouldFit (false)
|
||||||
.withJustification (justification)
|
.withJustification (justification)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue