1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Fix for CustomTypeface.

This commit is contained in:
jules 2013-04-03 20:18:59 +01:00
parent 35d695d123
commit 3daf0a4015

View file

@ -26,8 +26,8 @@
class CustomTypeface::GlyphInfo
{
public:
GlyphInfo (const juce_wchar character_, const Path& path_, const float width_) noexcept
: character (character_), path (path_), width (width_)
GlyphInfo (const juce_wchar c, const Path& p, const float w) noexcept
: character (c), path (p), width (w)
{
}
@ -49,11 +49,9 @@ public:
float getHorizontalSpacing (const juce_wchar subsequentCharacter) const noexcept
{
if (subsequentCharacter != 0)
{
for (int i = kerningPairs.size(); --i >= 0;)
if (kerningPairs.getReference(i).character2 == subsequentCharacter)
return width + kerningPairs.getReference(i).kerningAmount;
}
return width;
}
@ -124,9 +122,9 @@ CustomTypeface::CustomTypeface (InputStream& serialisedTypefaceStream)
ascent = in.readFloat();
defaultCharacter = CustomTypefaceHelpers::readChar (in);
int i, numChars = in.readInt();
int numChars = in.readInt();
for (i = 0; i < numChars; ++i)
for (int i = 0; i < numChars; ++i)
{
const juce_wchar c = CustomTypefaceHelpers::readChar (in);
const float width = in.readFloat();
@ -138,7 +136,7 @@ CustomTypeface::CustomTypeface (InputStream& serialisedTypefaceStream)
const int numKerningPairs = in.readInt();
for (i = 0; i < numKerningPairs; ++i)
for (int i = 0; i < numKerningPairs; ++i)
{
const juce_wchar char1 = CustomTypefaceHelpers::readChar (in);
const juce_wchar char2 = CustomTypefaceHelpers::readChar (in);
@ -161,22 +159,22 @@ void CustomTypeface::clear()
glyphs.clear();
}
void CustomTypeface::setCharacteristics (const String& name_, const float ascent_, const bool isBold,
const bool isItalic, const juce_wchar defaultCharacter_) noexcept
void CustomTypeface::setCharacteristics (const String& newName, const float newAscent, const bool isBold,
const bool isItalic, const juce_wchar newDefaultCharacter) noexcept
{
name = name_;
defaultCharacter = defaultCharacter_;
ascent = ascent_;
name = newName;
defaultCharacter = newDefaultCharacter;
ascent = newAscent;
style = FontStyleHelpers::getStyleName (isBold, isItalic);
}
void CustomTypeface::setCharacteristics (const String& name_, const String& style_, const float ascent_,
const juce_wchar defaultCharacter_) noexcept
void CustomTypeface::setCharacteristics (const String& newName, const String& newStyle, const float newAscent,
const juce_wchar newDefaultCharacter) noexcept
{
name = name_;
style = style_;
defaultCharacter = defaultCharacter_;
ascent = ascent_;
name = newName;
style = newStyle;
defaultCharacter = newDefaultCharacter;
ascent = newAscent;
}
void CustomTypeface::addGlyph (const juce_wchar character, const Path& path, const float width) noexcept
@ -272,9 +270,9 @@ bool CustomTypeface::writeToStream (OutputStream& outputStream)
CustomTypefaceHelpers::writeChar (out, defaultCharacter);
out.writeInt (glyphs.size());
int i, numKerningPairs = 0;
int numKerningPairs = 0;
for (i = 0; i < glyphs.size(); ++i)
for (int i = 0; i < glyphs.size(); ++i)
{
const GlyphInfo* const g = glyphs.getUnchecked (i);
CustomTypefaceHelpers::writeChar (out, g->character);
@ -286,7 +284,7 @@ bool CustomTypeface::writeToStream (OutputStream& outputStream)
out.writeInt (numKerningPairs);
for (i = 0; i < glyphs.size(); ++i)
for (int i = 0; i < glyphs.size(); ++i)
{
const GlyphInfo* const g = glyphs.getUnchecked (i);
@ -310,23 +308,22 @@ float CustomTypeface::getHeightToPointsFactor() const { return ascent; }
float CustomTypeface::getStringWidth (const String& text)
{
float x = 0;
String::CharPointerType t (text.getCharPointer());
while (! t.isEmpty())
for (String::CharPointerType t (text.getCharPointer()); ! t.isEmpty();)
{
const juce_wchar c = t.getAndAdvance();
const GlyphInfo* const glyph = findGlyph (c, true);
if (glyph == nullptr)
if (const GlyphInfo* const glyph = findGlyph (c, true))
{
x += glyph->getHorizontalSpacing (*t);
}
else
{
const Typeface::Ptr fallbackTypeface (Typeface::getFallbackTypeface());
if (fallbackTypeface != nullptr && fallbackTypeface != this)
x += fallbackTypeface->getStringWidth (String::charToString (c));
}
if (glyph != nullptr)
x += glyph->getHorizontalSpacing (*t);
}
return x;
@ -336,16 +333,22 @@ void CustomTypeface::getGlyphPositions (const String& text, Array <int>& resultG
{
xOffsets.add (0);
float x = 0;
String::CharPointerType t (text.getCharPointer());
while (! t.isEmpty())
for (String::CharPointerType t (text.getCharPointer()); ! t.isEmpty();)
{
const juce_wchar c = t.getAndAdvance();
const GlyphInfo* const glyph = findGlyph (c, true);
float width = 0.0f;
int glyphChar = 0;
if (glyph == nullptr)
const juce_wchar c = t.getAndAdvance();
if (const GlyphInfo* const glyph = findGlyph (c, true))
{
const Typeface::Ptr fallbackTypeface (Typeface::getFallbackTypeface());
width = glyph->getHorizontalSpacing (*t);
glyphChar = (int) glyph->character;
}
else
{
const Typeface::Ptr fallbackTypeface (getFallbackTypeface());
if (fallbackTypeface != nullptr && fallbackTypeface != this)
{
@ -355,58 +358,50 @@ void CustomTypeface::getGlyphPositions (const String& text, Array <int>& resultG
if (subGlyphs.size() > 0)
{
resultGlyphs.add (subGlyphs.getFirst());
x += subOffsets[1];
xOffsets.add (x);
glyphChar = subGlyphs.getFirst();
width = subOffsets[1];
}
}
}
if (glyph != nullptr)
{
x += glyph->getHorizontalSpacing (*t);
resultGlyphs.add ((int) glyph->character);
xOffsets.add (x);
}
x += width;
resultGlyphs.add (glyphChar);
xOffsets.add (x);
}
}
bool CustomTypeface::getOutlineForGlyph (int glyphNumber, Path& path)
{
const GlyphInfo* const glyph = findGlyph ((juce_wchar) glyphNumber, true);
if (glyph == nullptr)
{
const Typeface::Ptr fallbackTypeface (Typeface::getFallbackTypeface());
if (fallbackTypeface != nullptr && fallbackTypeface != this)
fallbackTypeface->getOutlineForGlyph (glyphNumber, path);
}
if (glyph != nullptr)
if (const GlyphInfo* const glyph = findGlyph ((juce_wchar) glyphNumber, true))
{
path = glyph->path;
return true;
}
const Typeface::Ptr fallbackTypeface (getFallbackTypeface());
if (fallbackTypeface != nullptr && fallbackTypeface != this)
return fallbackTypeface->getOutlineForGlyph (glyphNumber, path);
return false;
}
EdgeTable* CustomTypeface::getEdgeTableForGlyph (int glyphNumber, const AffineTransform& transform)
{
const GlyphInfo* const glyph = findGlyph ((juce_wchar) glyphNumber, true);
if (glyph == nullptr)
if (const GlyphInfo* const glyph = findGlyph ((juce_wchar) glyphNumber, true))
{
const Typeface::Ptr fallbackTypeface (Typeface::getFallbackTypeface());
if (! glyph->path.isEmpty())
return new EdgeTable (glyph->path.getBoundsTransformed (transform)
.getSmallestIntegerContainer().expanded (1, 0),
glyph->path, transform);
}
else
{
const Typeface::Ptr fallbackTypeface (getFallbackTypeface());
if (fallbackTypeface != nullptr && fallbackTypeface != this)
return fallbackTypeface->getEdgeTableForGlyph (glyphNumber, transform);
}
if (glyph != nullptr && ! glyph->path.isEmpty())
return new EdgeTable (glyph->path.getBoundsTransformed (transform).getSmallestIntegerContainer().expanded (1, 0),
glyph->path, transform);
return nullptr;
}