private void AddKerningTable(TextAsset XMLFile, Platform platform, Compression compression, Stopwatch timer, ref bool measured) { timer.Reset(); timer.Start(); KerningTable kerningTable = new KerningTable(XMLFile, platform, compression); timer.Stop(); measured = true; nameToIdx.Add(XMLFile.name, kerningTables.Count); kerningTables.Add(kerningTable); }
private void Kern(List <UIVertex> verts) { if (!IsActive() || !useKerning || pairAsset == null) { return; } if (kerningTable == null || kerningTable.name != pairAsset.XMLFile.name) { kerningTable = kerningManager.GetKerningTable(pairAsset.XMLFile, pairAsset.platform, pairAsset.compression, out timer, out measured); if (kerningTable == null) { return; } } bool isRichText = usingRichText && supportRichText; //int[] originalIdxs, actualFontSizes; //string[] lines = SplitToLines(text, isRichText, out originalIdxs, out actualFontSizes); int lastEnterIdx = -1; float kerningScale = .01f * kerningScaleFactor; MatchCollection matchCollection = null; Match match = null; Queue <int> Q = null; int matchCollectionIdx = 0; if (isRichText) { matchCollection = Regex.Matches(text, SupportedTagRegexPatterns); } while (lastEnterIdx < text.Length) { float lineOffset = CalculateLineOffset(lastEnterIdx + 1, text, matchCollection, isRichText, Q) * kerningScale * GetAlignmentFactor(alignment); if (lastEnterIdx + 1 > text.Length - 1 || text[lastEnterIdx + 1] == '\n') { ++lastEnterIdx; continue; } float prefixOffset = 0f; MoveGlyph(verts, lastEnterIdx + 1, Vector3.right * (0f - lineOffset)); int idx = lastEnterIdx + 1; uint leftGlyph = uint.MaxValue; int leftGlyphSize = 0; while (idx < text.Length && text[idx] != '\n') { if (isRichText && matchCollectionIdx < matchCollection.Count) { if (match == null) { match = matchCollection[matchCollectionIdx]; } if (idx == match.Index) { if (IsSizeStartMatch(match.Value)) { int newFontSize = StringToInt(match.Value.Substring(6, match.Value.Length - 7)); if (Q == null) { Q = new Queue <int>(); } Q.Enqueue(newFontSize); } else if (IsSizeEndMatch(match.Value)) { if (Q != null && Q.Count > 0) { Q.Dequeue(); } } idx += match.Length; match = null; ++matchCollectionIdx; continue; } } uint rightGlyph = text[idx]; int rightGlyphSize = fontSize; if (Q != null && Q.Count > 0) { rightGlyphSize = Q.Peek(); } KerningPairValue kerningPairValue = kerningTable.GetKerningPair(leftGlyph, rightGlyph); prefixOffset += kerningPairValue.XAdvance1 * leftGlyphSize * kerningScale; MoveGlyph(verts, idx, Vector3.right * (prefixOffset - lineOffset)); prefixOffset += kerningPairValue.XAdvance2 * rightGlyphSize * kerningScale; leftGlyph = rightGlyph; leftGlyphSize = rightGlyphSize; ++idx; } lastEnterIdx = idx; } /*int glyphIdx = 0; * * for (int lineIndex = 0; lineIndex < lines.Length; ++lineIndex) * { * string line = lines[lineIndex]; * float[] prefixOffset = new float[line.Length + 1]; * float lineOffset = 0f; * * for (int i = 1; i < line.Length; ++i) * { * uint leftGlyph = line[i - 1]; * uint rightGlyph = line[i]; * * KerningPairValue kernValue = kerningTable.GetKerningPair(leftGlyph, rightGlyph); * * float leftScaleFactor = actualFontSizes[glyphIdx + i - 1] * .01f * kerningScaleFactor; * float rightScaleFactor = actualFontSizes[glyphIdx + i] * .01f * kerningScaleFactor; * * lineOffset += kernValue.XAdvance1 * leftScaleFactor + kernValue.XAdvance2 * rightScaleFactor; * prefixOffset[i] += kernValue.XAdvance1 * leftScaleFactor; * prefixOffset[i + 1] = prefixOffset[i] + kernValue.XAdvance2 * rightScaleFactor; * } * * lineOffset *= GetAlignmentFactor(alignment); * * for (int i = 0; i < line.Length; ++i) * { * int originalIdx = originalIdxs[glyphIdx]; * Vector3 offset = Vector3.right * (prefixOffset[i] - lineOffset); * * if (6 * originalIdx + 5 > verts.Count - 1) * return; * * for (int j = 0; j < 6; ++j) * { * int pos = 6 * originalIdx + j; * UIVertex vert = verts[pos]; * vert.position += offset; * verts[pos] = vert; * } * ++glyphIdx; * } * ++glyphIdx; * }*/ }