2013-06-25 20:29:31 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file gfx_layout.cpp Handling of laying out text. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "gfx_layout.h"
|
|
|
|
#include "string_func.h"
|
2013-06-25 20:40:58 +00:00
|
|
|
#include "strings_func.h"
|
2013-11-16 20:57:54 +00:00
|
|
|
#include "debug.h"
|
2013-06-25 20:29:31 +00:00
|
|
|
|
|
|
|
#include "table/control_codes.h"
|
|
|
|
|
2019-03-10 15:43:59 +00:00
|
|
|
#ifdef WITH_ICU_LX
|
2013-06-25 20:40:58 +00:00
|
|
|
#include <unicode/ustring.h>
|
2019-03-10 15:43:59 +00:00
|
|
|
#endif /* WITH_ICU_LX */
|
2013-06-25 20:40:58 +00:00
|
|
|
|
2018-05-26 14:13:12 +00:00
|
|
|
#ifdef WITH_UNISCRIBE
|
|
|
|
#include "os/windows/string_uniscribe.h"
|
|
|
|
#endif /* WITH_UNISCRIBE */
|
|
|
|
|
2018-10-28 22:30:49 +00:00
|
|
|
#ifdef WITH_COCOA
|
|
|
|
#include "os/macosx/string_osx.h"
|
|
|
|
#endif
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2013-07-06 18:56:23 +00:00
|
|
|
|
2013-07-06 19:00:33 +00:00
|
|
|
/** Cache of ParagraphLayout lines. */
|
2013-07-07 12:07:06 +00:00
|
|
|
Layouter::LineCache *Layouter::linecache;
|
2013-07-06 19:00:33 +00:00
|
|
|
|
2013-07-06 18:56:23 +00:00
|
|
|
/** Cache of Font instances. */
|
|
|
|
Layouter::FontColourMap Layouter::fonts[FS_END];
|
|
|
|
|
|
|
|
|
2013-06-25 20:29:31 +00:00
|
|
|
/**
|
|
|
|
* Construct a new font.
|
|
|
|
* @param size The font size to use for this font.
|
|
|
|
* @param colour The colour to draw this font in.
|
|
|
|
*/
|
|
|
|
Font::Font(FontSize size, TextColour colour) :
|
|
|
|
fc(FontCache::Get(size)), colour(colour)
|
|
|
|
{
|
|
|
|
assert(size < FS_END);
|
|
|
|
}
|
|
|
|
|
2019-03-10 15:43:59 +00:00
|
|
|
#ifdef WITH_ICU_LX
|
2013-06-25 20:40:58 +00:00
|
|
|
/* Implementation details of LEFontInstance */
|
|
|
|
|
|
|
|
le_int32 Font::getUnitsPerEM() const
|
|
|
|
{
|
|
|
|
return this->fc->GetUnitsPerEM();
|
|
|
|
}
|
|
|
|
|
|
|
|
le_int32 Font::getAscent() const
|
|
|
|
{
|
|
|
|
return this->fc->GetAscender();
|
|
|
|
}
|
|
|
|
|
|
|
|
le_int32 Font::getDescent() const
|
|
|
|
{
|
|
|
|
return -this->fc->GetDescender();
|
|
|
|
}
|
|
|
|
|
|
|
|
le_int32 Font::getLeading() const
|
|
|
|
{
|
|
|
|
return this->fc->GetHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
float Font::getXPixelsPerEm() const
|
|
|
|
{
|
|
|
|
return (float)this->fc->GetHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
float Font::getYPixelsPerEm() const
|
|
|
|
{
|
|
|
|
return (float)this->fc->GetHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
float Font::getScaleFactorX() const
|
|
|
|
{
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Font::getScaleFactorY() const
|
|
|
|
{
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *Font::getFontTable(LETag tableTag) const
|
|
|
|
{
|
2013-06-27 21:21:47 +00:00
|
|
|
size_t length;
|
|
|
|
return this->getFontTable(tableTag, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *Font::getFontTable(LETag tableTag, size_t &length) const
|
|
|
|
{
|
|
|
|
return this->fc->GetFontTable(tableTag, length);
|
2013-06-25 20:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LEGlyphID Font::mapCharToGlyph(LEUnicode32 ch) const
|
|
|
|
{
|
|
|
|
if (IsTextDirectionChar(ch)) return 0;
|
|
|
|
return this->fc->MapCharToGlyph(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Font::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
|
|
|
|
{
|
|
|
|
advance.fX = glyph == 0xFFFF ? 0 : this->fc->GetGlyphWidth(glyph);
|
|
|
|
advance.fY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
le_bool Font::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
|
|
|
|
{
|
2020-10-30 17:45:20 +00:00
|
|
|
return false;
|
2013-06-25 20:40:58 +00:00
|
|
|
}
|
|
|
|
|
2013-11-16 20:57:54 +00:00
|
|
|
/**
|
|
|
|
* Wrapper for doing layouts with ICU.
|
|
|
|
*/
|
2019-04-02 19:31:10 +00:00
|
|
|
class ICUParagraphLayout : public ParagraphLayouter {
|
2018-09-20 20:36:45 +00:00
|
|
|
icu::ParagraphLayout *p; ///< The actual ICU paragraph layout.
|
2013-11-16 20:57:54 +00:00
|
|
|
public:
|
|
|
|
/** Visual run contains data about the bit of text with the same font. */
|
|
|
|
class ICUVisualRun : public ParagraphLayouter::VisualRun {
|
2018-09-20 20:36:45 +00:00
|
|
|
const icu::ParagraphLayout::VisualRun *vr; ///< The actual ICU vr.
|
2013-11-16 20:57:54 +00:00
|
|
|
|
|
|
|
public:
|
2018-09-20 20:36:45 +00:00
|
|
|
ICUVisualRun(const icu::ParagraphLayout::VisualRun *vr) : vr(vr) { }
|
2013-11-16 20:57:54 +00:00
|
|
|
|
2019-04-02 19:30:34 +00:00
|
|
|
const Font *GetFont() const override { return (const Font*)vr->getFont(); }
|
|
|
|
int GetGlyphCount() const override { return vr->getGlyphCount(); }
|
|
|
|
const GlyphID *GetGlyphs() const override { return vr->getGlyphs(); }
|
|
|
|
const float *GetPositions() const override { return vr->getPositions(); }
|
|
|
|
int GetLeading() const override { return vr->getLeading(); }
|
|
|
|
const int *GetGlyphToCharMap() const override { return vr->getGlyphToCharMap(); }
|
2013-11-16 20:57:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** A single line worth of VisualRuns. */
|
2019-04-02 19:30:53 +00:00
|
|
|
class ICULine : public std::vector<ICUVisualRun>, public ParagraphLayouter::Line {
|
2018-09-20 20:36:45 +00:00
|
|
|
icu::ParagraphLayout::Line *l; ///< The actual ICU line.
|
2013-11-16 20:57:54 +00:00
|
|
|
|
|
|
|
public:
|
2018-09-20 20:36:45 +00:00
|
|
|
ICULine(icu::ParagraphLayout::Line *l) : l(l)
|
2013-11-16 20:57:54 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < l->countRuns(); i++) {
|
2019-04-02 19:30:53 +00:00
|
|
|
this->emplace_back(l->getVisualRun(i));
|
2013-11-16 20:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 19:30:34 +00:00
|
|
|
~ICULine() override { delete l; }
|
2013-11-16 20:57:54 +00:00
|
|
|
|
2019-04-02 19:30:34 +00:00
|
|
|
int GetLeading() const override { return l->getLeading(); }
|
|
|
|
int GetWidth() const override { return l->getWidth(); }
|
|
|
|
int CountRuns() const override { return l->countRuns(); }
|
2019-04-02 19:30:53 +00:00
|
|
|
const ParagraphLayouter::VisualRun &GetVisualRun(int run) const override { return this->at(run); }
|
2013-11-17 17:08:20 +00:00
|
|
|
|
2019-04-02 19:30:34 +00:00
|
|
|
int GetInternalCharLength(WChar c) const override
|
2013-11-17 17:08:20 +00:00
|
|
|
{
|
|
|
|
/* ICU uses UTF-16 internally which means we need to account for surrogate pairs. */
|
|
|
|
return Utf8CharLen(c) < 4 ? 1 : 2;
|
|
|
|
}
|
2013-11-16 20:57:54 +00:00
|
|
|
};
|
|
|
|
|
2018-09-20 20:36:45 +00:00
|
|
|
ICUParagraphLayout(icu::ParagraphLayout *p) : p(p) { }
|
2019-04-02 19:30:34 +00:00
|
|
|
~ICUParagraphLayout() override { delete p; }
|
|
|
|
void Reflow() override { p->reflow(); }
|
2013-11-16 20:57:54 +00:00
|
|
|
|
2019-04-02 19:31:10 +00:00
|
|
|
std::unique_ptr<const Line> NextLine(int max_width) override
|
2013-11-16 20:57:54 +00:00
|
|
|
{
|
2018-09-20 20:36:45 +00:00
|
|
|
icu::ParagraphLayout::Line *l = p->nextLine(max_width);
|
2019-04-10 21:07:06 +00:00
|
|
|
return std::unique_ptr<const Line>(l == nullptr ? nullptr : new ICULine(l));
|
2013-11-16 20:57:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-29 14:43:30 +00:00
|
|
|
/**
|
|
|
|
* Helper class to construct a new #ICUParagraphLayout.
|
|
|
|
*/
|
|
|
|
class ICUParagraphLayoutFactory {
|
|
|
|
public:
|
|
|
|
/** Helper for GetLayouter, to get the right type. */
|
|
|
|
typedef UChar CharType;
|
|
|
|
/** Helper for GetLayouter, to get whether the layouter supports RTL. */
|
|
|
|
static const bool SUPPORTS_RTL = true;
|
2013-06-25 20:40:58 +00:00
|
|
|
|
2018-04-29 14:43:30 +00:00
|
|
|
static ParagraphLayouter *GetParagraphLayout(UChar *buff, UChar *buff_end, FontMap &fontMapping)
|
|
|
|
{
|
|
|
|
int32 length = buff_end - buff;
|
2013-06-25 20:40:58 +00:00
|
|
|
|
2018-04-29 14:43:30 +00:00
|
|
|
if (length == 0) {
|
|
|
|
/* ICU's ParagraphLayout cannot handle empty strings, so fake one. */
|
|
|
|
buff[0] = ' ';
|
|
|
|
length = 1;
|
2019-02-17 11:20:52 +00:00
|
|
|
fontMapping.back().first++;
|
2018-04-29 14:43:30 +00:00
|
|
|
}
|
2013-06-25 20:40:58 +00:00
|
|
|
|
2018-04-29 14:43:30 +00:00
|
|
|
/* Fill ICU's FontRuns with the right data. */
|
2018-09-23 11:23:54 +00:00
|
|
|
icu::FontRuns runs(fontMapping.size());
|
2019-02-17 11:20:52 +00:00
|
|
|
for (auto &pair : fontMapping) {
|
|
|
|
runs.add(pair.second, pair.first);
|
2018-04-29 14:43:30 +00:00
|
|
|
}
|
2013-11-16 20:57:54 +00:00
|
|
|
|
2018-04-29 14:43:30 +00:00
|
|
|
LEErrorCode status = LE_NO_ERROR;
|
|
|
|
/* ParagraphLayout does not copy "buff", so it must stay valid.
|
|
|
|
* "runs" is copied according to the ICU source, but the documentation does not specify anything, so this might break somewhen. */
|
2019-04-06 19:03:20 +00:00
|
|
|
icu::ParagraphLayout *p = new icu::ParagraphLayout(buff, length, &runs, nullptr, nullptr, nullptr, _current_text_dir == TD_RTL ? 1 : 0, false, status);
|
2018-04-29 14:43:30 +00:00
|
|
|
if (status != LE_NO_ERROR) {
|
|
|
|
delete p;
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2018-04-29 14:43:30 +00:00
|
|
|
}
|
2013-06-25 20:40:58 +00:00
|
|
|
|
2018-04-29 14:43:30 +00:00
|
|
|
return new ICUParagraphLayout(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t AppendToBuffer(UChar *buff, const UChar *buffer_last, WChar c)
|
|
|
|
{
|
|
|
|
/* Transform from UTF-32 to internal ICU format of UTF-16. */
|
|
|
|
int32 length = 0;
|
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
|
|
|
u_strFromUTF32(buff, buffer_last - buff, &length, (UChar32*)&c, 1, &err);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
};
|
2019-03-10 15:43:59 +00:00
|
|
|
#endif /* WITH_ICU_LX */
|
2013-06-25 20:40:58 +00:00
|
|
|
|
2013-06-25 20:29:31 +00:00
|
|
|
/*** Paragraph layout ***/
|
2013-11-16 20:57:54 +00:00
|
|
|
/**
|
|
|
|
* Class handling the splitting of a paragraph of text into lines and
|
|
|
|
* visual runs.
|
|
|
|
*
|
|
|
|
* One constructs this class with the text that needs to be split into
|
2019-04-10 21:07:06 +00:00
|
|
|
* lines. Then nextLine is called with the maximum width until nullptr is
|
2013-11-16 20:57:54 +00:00
|
|
|
* returned. Each nextLine call creates VisualRuns which contain the
|
|
|
|
* length of text that are to be drawn with the same font. In other
|
|
|
|
* words, the result of this class is a list of sub strings with their
|
|
|
|
* font. The sub strings are then already fully laid out, and only
|
|
|
|
* need actual drawing.
|
|
|
|
*
|
|
|
|
* The positions in a visual run are sequential pairs of X,Y of the
|
|
|
|
* begin of each of the glyphs plus an extra pair to mark the end.
|
|
|
|
*
|
|
|
|
* @note This variant does not handle left-to-right properly. This
|
|
|
|
* is supported in the one ParagraphLayout coming from ICU.
|
|
|
|
*/
|
|
|
|
class FallbackParagraphLayout : public ParagraphLayouter {
|
|
|
|
public:
|
|
|
|
/** Visual run contains data about the bit of text with the same font. */
|
|
|
|
class FallbackVisualRun : public ParagraphLayouter::VisualRun {
|
|
|
|
Font *font; ///< The font used to layout these.
|
|
|
|
GlyphID *glyphs; ///< The glyphs we're drawing.
|
|
|
|
float *positions; ///< The positions of the glyphs.
|
|
|
|
int *glyph_to_char; ///< The char index of the glyphs.
|
|
|
|
int glyph_count; ///< The number of glyphs.
|
|
|
|
|
|
|
|
public:
|
|
|
|
FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x);
|
2019-04-02 19:30:53 +00:00
|
|
|
FallbackVisualRun(FallbackVisualRun &&other) noexcept;
|
2019-04-02 19:30:34 +00:00
|
|
|
~FallbackVisualRun() override;
|
|
|
|
const Font *GetFont() const override;
|
|
|
|
int GetGlyphCount() const override;
|
|
|
|
const GlyphID *GetGlyphs() const override;
|
|
|
|
const float *GetPositions() const override;
|
|
|
|
int GetLeading() const override;
|
|
|
|
const int *GetGlyphToCharMap() const override;
|
2013-11-16 20:57:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** A single line worth of VisualRuns. */
|
2019-04-02 19:30:53 +00:00
|
|
|
class FallbackLine : public std::vector<FallbackVisualRun>, public ParagraphLayouter::Line {
|
2013-11-16 20:57:54 +00:00
|
|
|
public:
|
2019-04-02 19:30:34 +00:00
|
|
|
int GetLeading() const override;
|
|
|
|
int GetWidth() const override;
|
|
|
|
int CountRuns() const override;
|
2019-04-02 19:30:53 +00:00
|
|
|
const ParagraphLayouter::VisualRun &GetVisualRun(int run) const override;
|
2013-11-17 17:08:20 +00:00
|
|
|
|
2019-04-02 19:30:34 +00:00
|
|
|
int GetInternalCharLength(WChar c) const override { return 1; }
|
2013-11-16 20:57:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const WChar *buffer_begin; ///< Begin of the buffer.
|
|
|
|
const WChar *buffer; ///< The current location in the buffer.
|
|
|
|
FontMap &runs; ///< The fonts we have to use for this paragraph.
|
|
|
|
|
|
|
|
FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs);
|
2019-04-02 19:30:34 +00:00
|
|
|
void Reflow() override;
|
2019-04-02 19:31:10 +00:00
|
|
|
std::unique_ptr<const Line> NextLine(int max_width) override;
|
2013-11-16 20:57:54 +00:00
|
|
|
};
|
2013-06-25 20:29:31 +00:00
|
|
|
|
2018-04-29 14:43:30 +00:00
|
|
|
/**
|
|
|
|
* Helper class to construct a new #FallbackParagraphLayout.
|
|
|
|
*/
|
|
|
|
class FallbackParagraphLayoutFactory {
|
|
|
|
public:
|
|
|
|
/** Helper for GetLayouter, to get the right type. */
|
|
|
|
typedef WChar CharType;
|
|
|
|
/** Helper for GetLayouter, to get whether the layouter supports RTL. */
|
|
|
|
static const bool SUPPORTS_RTL = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the actual ParagraphLayout for the given buffer.
|
|
|
|
* @param buff The begin of the buffer.
|
|
|
|
* @param buff_end The location after the last element in the buffer.
|
|
|
|
* @param fontMapping THe mapping of the fonts.
|
|
|
|
* @return The ParagraphLayout instance.
|
|
|
|
*/
|
|
|
|
static ParagraphLayouter *GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
|
|
|
|
{
|
|
|
|
return new FallbackParagraphLayout(buff, buff_end - buff, fontMapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append a wide character to the internal buffer.
|
|
|
|
* @param buff The buffer to append to.
|
|
|
|
* @param buffer_last The end of the buffer.
|
|
|
|
* @param c The character to add.
|
|
|
|
* @return The number of buffer spaces that were used.
|
|
|
|
*/
|
|
|
|
static size_t AppendToBuffer(WChar *buff, const WChar *buffer_last, WChar c)
|
|
|
|
{
|
|
|
|
*buff = c;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-25 20:29:31 +00:00
|
|
|
/**
|
|
|
|
* Create the visual run.
|
|
|
|
* @param font The font to use for this run.
|
|
|
|
* @param chars The characters to use for this run.
|
|
|
|
* @param char_count The number of characters in this run.
|
|
|
|
* @param x The initial x position for this run.
|
|
|
|
*/
|
2013-11-16 20:32:55 +00:00
|
|
|
FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(Font *font, const WChar *chars, int char_count, int x) :
|
2013-06-25 20:29:31 +00:00
|
|
|
font(font), glyph_count(char_count)
|
|
|
|
{
|
2022-11-13 17:10:34 +00:00
|
|
|
const bool isbuiltin = font->fc->IsBuiltInFont();
|
|
|
|
|
2013-06-25 20:29:31 +00:00
|
|
|
this->glyphs = MallocT<GlyphID>(this->glyph_count);
|
2013-08-06 17:35:11 +00:00
|
|
|
this->glyph_to_char = MallocT<int>(this->glyph_count);
|
2013-06-25 20:29:31 +00:00
|
|
|
|
|
|
|
/* Positions contains the location of the begin of each of the glyphs, and the end of the last one. */
|
|
|
|
this->positions = MallocT<float>(this->glyph_count * 2 + 2);
|
|
|
|
this->positions[0] = x;
|
|
|
|
|
|
|
|
for (int i = 0; i < this->glyph_count; i++) {
|
|
|
|
this->glyphs[i] = font->fc->MapCharToGlyph(chars[i]);
|
2022-11-13 17:10:34 +00:00
|
|
|
if (isbuiltin) {
|
|
|
|
this->positions[2 * i + 1] = font->fc->GetAscender(); // Apply sprite font's ascender.
|
|
|
|
} else if (chars[i] >= SCC_SPRITE_START && chars[i] <= SCC_SPRITE_END) {
|
|
|
|
this->positions[2 * i + 1] = font->fc->GetAscender() - font->fc->GetGlyph(this->glyphs[i])->height - 1; // Align sprite glyphs to font baseline.
|
|
|
|
} else {
|
|
|
|
this->positions[2 * i + 1] = 0; // No ascender adjustment.
|
|
|
|
}
|
2013-06-25 20:29:31 +00:00
|
|
|
this->positions[2 * i + 2] = this->positions[2 * i] + font->fc->GetGlyphWidth(this->glyphs[i]);
|
2013-08-06 17:35:11 +00:00
|
|
|
this->glyph_to_char[i] = i;
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 19:30:53 +00:00
|
|
|
/** Move constructor for visual runs.*/
|
|
|
|
FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(FallbackVisualRun &&other) noexcept : font(other.font), glyph_count(other.glyph_count)
|
|
|
|
{
|
|
|
|
this->positions = other.positions;
|
|
|
|
this->glyph_to_char = other.glyph_to_char;
|
|
|
|
this->glyphs = other.glyphs;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
other.positions = nullptr;
|
|
|
|
other.glyph_to_char = nullptr;
|
|
|
|
other.glyphs = nullptr;
|
2019-04-02 19:30:53 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 20:29:31 +00:00
|
|
|
/** Free all data. */
|
2013-11-16 20:32:55 +00:00
|
|
|
FallbackParagraphLayout::FallbackVisualRun::~FallbackVisualRun()
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
|
|
|
free(this->positions);
|
2013-08-06 17:35:11 +00:00
|
|
|
free(this->glyph_to_char);
|
2013-06-25 20:29:31 +00:00
|
|
|
free(this->glyphs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the font associated with this run.
|
|
|
|
* @return The font.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
const Font *FallbackParagraphLayout::FallbackVisualRun::GetFont() const
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
|
|
|
return this->font;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-09 18:43:44 +00:00
|
|
|
* Get the number of glyphs in this run.
|
2013-06-25 20:29:31 +00:00
|
|
|
* @return The number of glyphs.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
int FallbackParagraphLayout::FallbackVisualRun::GetGlyphCount() const
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
|
|
|
return this->glyph_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-09 18:43:44 +00:00
|
|
|
* Get the glyphs of this run.
|
2013-06-25 20:29:31 +00:00
|
|
|
* @return The glyphs.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
const GlyphID *FallbackParagraphLayout::FallbackVisualRun::GetGlyphs() const
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
|
|
|
return this->glyphs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the positions of this run.
|
|
|
|
* @return The positions.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
const float *FallbackParagraphLayout::FallbackVisualRun::GetPositions() const
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
|
|
|
return this->positions;
|
|
|
|
}
|
|
|
|
|
2013-08-06 17:35:11 +00:00
|
|
|
/**
|
|
|
|
* Get the glyph-to-character map for this visual run.
|
|
|
|
* @return The glyph-to-character map.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
const int *FallbackParagraphLayout::FallbackVisualRun::GetGlyphToCharMap() const
|
2013-08-06 17:35:11 +00:00
|
|
|
{
|
|
|
|
return this->glyph_to_char;
|
|
|
|
}
|
|
|
|
|
2013-06-25 20:29:31 +00:00
|
|
|
/**
|
|
|
|
* Get the height of this font.
|
|
|
|
* @return The height of the font.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
int FallbackParagraphLayout::FallbackVisualRun::GetLeading() const
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
2013-11-16 21:05:26 +00:00
|
|
|
return this->GetFont()->fc->GetHeight();
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the height of the line.
|
|
|
|
* @return The maximum height of the line.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
int FallbackParagraphLayout::FallbackLine::GetLeading() const
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
|
|
|
int leading = 0;
|
2019-04-02 19:30:53 +00:00
|
|
|
for (const auto &run : *this) {
|
2021-01-08 10:16:18 +00:00
|
|
|
leading = std::max(leading, run.GetLeading());
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return leading;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the width of this line.
|
|
|
|
* @return The width of the line.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
int FallbackParagraphLayout::FallbackLine::GetWidth() const
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
2018-09-23 11:23:54 +00:00
|
|
|
if (this->size() == 0) return 0;
|
2013-06-25 20:29:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The last X position of a run contains is the end of that run.
|
|
|
|
* Since there is no left-to-right support, taking this value of
|
|
|
|
* the last run gives us the end of the line and thus the width.
|
|
|
|
*/
|
2019-04-02 19:30:53 +00:00
|
|
|
const auto &run = this->GetVisualRun(this->CountRuns() - 1);
|
|
|
|
return (int)run.GetPositions()[run.GetGlyphCount() * 2];
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of runs in this line.
|
|
|
|
* @return The number of runs.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
int FallbackParagraphLayout::FallbackLine::CountRuns() const
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
2019-03-27 23:09:33 +00:00
|
|
|
return (uint)this->size();
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a specific visual run.
|
|
|
|
* @return The visual run.
|
|
|
|
*/
|
2019-04-02 19:30:53 +00:00
|
|
|
const ParagraphLayouter::VisualRun &FallbackParagraphLayout::FallbackLine::GetVisualRun(int run) const
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
2018-09-25 20:20:24 +00:00
|
|
|
return this->at(run);
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new paragraph layouter.
|
|
|
|
* @param buffer The characters of the paragraph.
|
|
|
|
* @param length The length of the paragraph.
|
|
|
|
* @param runs The font mapping of this paragraph.
|
|
|
|
*/
|
2013-11-16 20:32:55 +00:00
|
|
|
FallbackParagraphLayout::FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs) : buffer_begin(buffer), buffer(buffer), runs(runs)
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
|
|
|
assert(runs.End()[-1].first == length);
|
|
|
|
}
|
|
|
|
|
2013-07-06 19:00:33 +00:00
|
|
|
/**
|
|
|
|
* Reset the position to the start of the paragraph.
|
|
|
|
*/
|
2013-11-16 21:05:26 +00:00
|
|
|
void FallbackParagraphLayout::Reflow()
|
2013-07-06 19:00:33 +00:00
|
|
|
{
|
|
|
|
this->buffer = this->buffer_begin;
|
|
|
|
}
|
|
|
|
|
2013-06-25 20:29:31 +00:00
|
|
|
/**
|
|
|
|
* Construct a new line with a maximum width.
|
|
|
|
* @param max_width The maximum width of the string.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return A Line, or nullptr when at the end of the paragraph.
|
2013-06-25 20:29:31 +00:00
|
|
|
*/
|
2019-04-02 19:31:10 +00:00
|
|
|
std::unique_ptr<const ParagraphLayouter::Line> FallbackParagraphLayout::NextLine(int max_width)
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
|
|
|
/* Simple idea:
|
|
|
|
* - split a line at a newline character, or at a space where we can break a line.
|
|
|
|
* - split for a visual run whenever a new line happens, or the font changes.
|
|
|
|
*/
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->buffer == nullptr) return nullptr;
|
2013-06-25 20:29:31 +00:00
|
|
|
|
2019-04-02 19:31:10 +00:00
|
|
|
std::unique_ptr<FallbackLine> l(new FallbackLine());
|
2013-06-25 20:29:31 +00:00
|
|
|
|
2013-06-27 16:24:19 +00:00
|
|
|
if (*this->buffer == '\0') {
|
|
|
|
/* Only a newline. */
|
2019-04-10 21:07:06 +00:00
|
|
|
this->buffer = nullptr;
|
2019-04-02 19:30:53 +00:00
|
|
|
l->emplace_back(this->runs.front().second, this->buffer, 0, 0);
|
2019-06-29 19:12:29 +00:00
|
|
|
return l;
|
2013-06-27 16:24:19 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 20:29:31 +00:00
|
|
|
int offset = this->buffer - this->buffer_begin;
|
2019-02-17 11:20:52 +00:00
|
|
|
FontMap::iterator iter = this->runs.data();
|
2013-06-25 20:29:31 +00:00
|
|
|
while (iter->first <= offset) {
|
|
|
|
iter++;
|
2013-06-27 16:24:19 +00:00
|
|
|
assert(iter != this->runs.End());
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const FontCache *fc = iter->second->fc;
|
2013-06-30 07:29:31 +00:00
|
|
|
const WChar *next_run = this->buffer_begin + iter->first;
|
2013-06-25 20:29:31 +00:00
|
|
|
|
2018-10-14 17:17:09 +00:00
|
|
|
const WChar *begin = this->buffer;
|
2019-04-10 21:07:06 +00:00
|
|
|
const WChar *last_space = nullptr;
|
2018-10-14 17:17:09 +00:00
|
|
|
const WChar *last_char;
|
|
|
|
int width = 0;
|
2013-06-25 20:29:31 +00:00
|
|
|
for (;;) {
|
2013-06-30 07:46:10 +00:00
|
|
|
WChar c = *this->buffer;
|
|
|
|
last_char = this->buffer;
|
2013-06-25 20:29:31 +00:00
|
|
|
|
|
|
|
if (c == '\0') {
|
2019-04-10 21:07:06 +00:00
|
|
|
this->buffer = nullptr;
|
2013-06-25 20:29:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->buffer == next_run) {
|
2013-11-16 21:05:26 +00:00
|
|
|
int w = l->GetWidth();
|
2019-04-02 19:30:53 +00:00
|
|
|
l->emplace_back(iter->second, begin, this->buffer - begin, w);
|
2013-06-25 20:29:31 +00:00
|
|
|
iter++;
|
2013-06-27 16:24:19 +00:00
|
|
|
assert(iter != this->runs.End());
|
2013-06-25 20:29:31 +00:00
|
|
|
|
2013-06-30 07:29:31 +00:00
|
|
|
next_run = this->buffer_begin + iter->first;
|
2013-06-25 20:29:31 +00:00
|
|
|
begin = this->buffer;
|
2013-06-30 07:21:37 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
last_space = nullptr;
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsWhitespace(c)) last_space = this->buffer;
|
|
|
|
|
|
|
|
if (IsPrintable(c) && !IsTextDirectionChar(c)) {
|
|
|
|
int char_width = GetCharacterWidth(fc->GetSize(), c);
|
|
|
|
width += char_width;
|
|
|
|
if (width > max_width) {
|
|
|
|
/* The string is longer than maximum width so we need to decide
|
|
|
|
* what to do with it. */
|
|
|
|
if (width == char_width) {
|
|
|
|
/* The character is wider than allowed width; don't know
|
|
|
|
* what to do with this case... bail out! */
|
2019-04-10 21:07:06 +00:00
|
|
|
this->buffer = nullptr;
|
2019-06-29 19:12:29 +00:00
|
|
|
return l;
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (last_space == nullptr) {
|
2013-06-25 20:29:31 +00:00
|
|
|
/* No space has been found. Just terminate at our current
|
|
|
|
* location. This usually happens for languages that do not
|
|
|
|
* require spaces in strings, like Chinese, Japanese and
|
|
|
|
* Korean. For other languages terminating mid-word might
|
|
|
|
* not be the best, but terminating the whole string instead
|
|
|
|
* of continuing the word at the next line is worse. */
|
|
|
|
last_char = this->buffer;
|
|
|
|
} else {
|
|
|
|
/* A space is found; perfect place to terminate */
|
2013-07-06 18:55:38 +00:00
|
|
|
this->buffer = last_space + 1;
|
2013-06-30 07:46:10 +00:00
|
|
|
last_char = last_space;
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-30 07:46:10 +00:00
|
|
|
|
|
|
|
this->buffer++;
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 11:23:54 +00:00
|
|
|
if (l->size() == 0 || last_char - begin != 0) {
|
2013-11-16 21:05:26 +00:00
|
|
|
int w = l->GetWidth();
|
2019-04-02 19:30:53 +00:00
|
|
|
l->emplace_back(iter->second, begin, last_char - begin, w);
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
2019-06-29 19:12:29 +00:00
|
|
|
return l;
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-16 20:57:54 +00:00
|
|
|
/**
|
|
|
|
* Helper for getting a ParagraphLayouter of the given type.
|
|
|
|
*
|
2019-04-10 21:07:06 +00:00
|
|
|
* @note In case no ParagraphLayouter could be constructed, line.layout will be nullptr.
|
2013-11-16 20:57:54 +00:00
|
|
|
* @param line The cache item to store our layouter in.
|
|
|
|
* @param str The string to create a layouter for.
|
|
|
|
* @param state The state of the font and color.
|
|
|
|
* @tparam T The type of layouter we want.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2013-11-18 19:35:06 +00:00
|
|
|
static inline void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
|
2013-11-16 20:57:54 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (line.buffer != nullptr) free(line.buffer);
|
2013-11-16 20:57:54 +00:00
|
|
|
|
|
|
|
typename T::CharType *buff_begin = MallocT<typename T::CharType>(DRAW_STRING_BUFFER);
|
|
|
|
const typename T::CharType *buffer_last = buff_begin + DRAW_STRING_BUFFER;
|
|
|
|
typename T::CharType *buff = buff_begin;
|
|
|
|
FontMap &fontMapping = line.runs;
|
|
|
|
Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
|
|
|
|
|
|
|
|
line.buffer = buff_begin;
|
2018-09-20 22:44:14 +00:00
|
|
|
fontMapping.clear();
|
2013-11-16 20:57:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Go through the whole string while adding Font instances to the font map
|
|
|
|
* whenever the font changes, and convert the wide characters into a format
|
|
|
|
* usable by ParagraphLayout.
|
|
|
|
*/
|
|
|
|
for (; buff < buffer_last;) {
|
|
|
|
WChar c = Utf8Consume(const_cast<const char **>(&str));
|
|
|
|
if (c == '\0' || c == '\n') {
|
|
|
|
break;
|
|
|
|
} else if (c >= SCC_BLUE && c <= SCC_BLACK) {
|
|
|
|
state.SetColour((TextColour)(c - SCC_BLUE));
|
2018-04-19 18:33:21 +00:00
|
|
|
} else if (c == SCC_PUSH_COLOUR) {
|
|
|
|
state.PushColour();
|
|
|
|
} else if (c == SCC_POP_COLOUR) {
|
|
|
|
state.PopColour();
|
2018-04-17 17:41:31 +00:00
|
|
|
} else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
|
|
|
|
state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
|
2013-11-16 20:57:54 +00:00
|
|
|
} else {
|
2020-02-15 02:08:29 +00:00
|
|
|
/* Filter out non printable characters */
|
|
|
|
if (!IsPrintable(c)) continue;
|
2013-11-16 20:57:54 +00:00
|
|
|
/* Filter out text direction characters that shouldn't be drawn, and
|
|
|
|
* will not be handled in the fallback non ICU case because they are
|
|
|
|
* mostly needed for RTL languages which need more ICU support. */
|
|
|
|
if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
|
2018-04-29 14:43:30 +00:00
|
|
|
buff += T::AppendToBuffer(buff, buffer_last, c);
|
2013-11-16 20:57:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fontMapping.Contains(buff - buff_begin)) {
|
|
|
|
fontMapping.Insert(buff - buff_begin, f);
|
|
|
|
}
|
|
|
|
f = Layouter::GetFont(state.fontsize, state.cur_colour);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Better safe than sorry. */
|
|
|
|
*buff = '\0';
|
|
|
|
|
|
|
|
if (!fontMapping.Contains(buff - buff_begin)) {
|
|
|
|
fontMapping.Insert(buff - buff_begin, f);
|
|
|
|
}
|
2018-04-29 14:43:30 +00:00
|
|
|
line.layout = T::GetParagraphLayout(buff_begin, buff, fontMapping);
|
2013-11-16 20:57:54 +00:00
|
|
|
line.state_after = state;
|
|
|
|
}
|
2013-06-25 20:29:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new layouter.
|
|
|
|
* @param str The string to create the layout for.
|
|
|
|
* @param maxw The maximum width.
|
|
|
|
* @param colour The colour of the font.
|
|
|
|
* @param fontsize The size of font to use.
|
|
|
|
*/
|
2013-08-05 20:35:23 +00:00
|
|
|
Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsize) : string(str)
|
2013-06-25 20:29:31 +00:00
|
|
|
{
|
2013-07-06 18:54:26 +00:00
|
|
|
FontState state(colour, fontsize);
|
2013-06-27 18:33:46 +00:00
|
|
|
WChar c = 0;
|
2013-06-27 16:24:19 +00:00
|
|
|
|
|
|
|
do {
|
2013-07-06 19:00:33 +00:00
|
|
|
/* Scan string for end of line */
|
|
|
|
const char *lineend = str;
|
|
|
|
for (;;) {
|
|
|
|
size_t len = Utf8Decode(&c, lineend);
|
|
|
|
if (c == '\0' || c == '\n') break;
|
|
|
|
lineend += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
LineCacheItem& line = GetCachedParagraphLayout(str, lineend - str, state);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (line.layout != nullptr) {
|
2013-07-06 19:00:33 +00:00
|
|
|
/* Line is in cache */
|
|
|
|
str = lineend + 1;
|
|
|
|
state = line.state_after;
|
2013-11-16 21:05:26 +00:00
|
|
|
line.layout->Reflow();
|
2013-07-06 19:00:33 +00:00
|
|
|
} else {
|
|
|
|
/* Line is new, layout it */
|
2013-11-18 19:47:43 +00:00
|
|
|
FontState old_state = state;
|
2019-03-10 15:43:59 +00:00
|
|
|
#if defined(WITH_ICU_LX) || defined(WITH_UNISCRIBE) || defined(WITH_COCOA)
|
2013-11-18 19:47:43 +00:00
|
|
|
const char *old_str = str;
|
2018-07-07 16:48:17 +00:00
|
|
|
#endif
|
2013-11-18 19:47:43 +00:00
|
|
|
|
2019-03-10 15:43:59 +00:00
|
|
|
#ifdef WITH_ICU_LX
|
2018-04-29 14:43:30 +00:00
|
|
|
GetLayouter<ICUParagraphLayoutFactory>(line, str, state);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (line.layout == nullptr) {
|
2013-11-16 20:57:54 +00:00
|
|
|
static bool warned = false;
|
|
|
|
if (!warned) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(misc, 0, "ICU layouter bailed on the font. Falling back to the fallback layouter");
|
2013-11-16 20:57:54 +00:00
|
|
|
warned = true;
|
2013-07-06 19:00:33 +00:00
|
|
|
}
|
2013-11-18 19:47:43 +00:00
|
|
|
|
|
|
|
state = old_state;
|
|
|
|
str = old_str;
|
2013-06-27 16:24:19 +00:00
|
|
|
}
|
2013-11-16 20:57:54 +00:00
|
|
|
#endif
|
2018-05-26 14:13:12 +00:00
|
|
|
|
|
|
|
#ifdef WITH_UNISCRIBE
|
2019-04-10 21:07:06 +00:00
|
|
|
if (line.layout == nullptr) {
|
2018-05-26 14:13:12 +00:00
|
|
|
GetLayouter<UniscribeParagraphLayoutFactory>(line, str, state);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (line.layout == nullptr) {
|
2018-05-26 14:13:12 +00:00
|
|
|
state = old_state;
|
|
|
|
str = old_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-28 22:30:49 +00:00
|
|
|
#ifdef WITH_COCOA
|
2019-04-10 21:07:06 +00:00
|
|
|
if (line.layout == nullptr) {
|
2018-10-28 22:30:49 +00:00
|
|
|
GetLayouter<CoreTextParagraphLayoutFactory>(line, str, state);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (line.layout == nullptr) {
|
2018-10-28 22:30:49 +00:00
|
|
|
state = old_state;
|
|
|
|
str = old_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (line.layout == nullptr) {
|
2018-05-26 14:13:12 +00:00
|
|
|
GetLayouter<FallbackParagraphLayoutFactory>(line, str, state);
|
|
|
|
}
|
2013-06-27 16:24:19 +00:00
|
|
|
}
|
2013-06-25 20:29:31 +00:00
|
|
|
|
2019-04-02 19:31:10 +00:00
|
|
|
/* Move all lines into a local cache so we can reuse them later on more easily. */
|
|
|
|
for (;;) {
|
|
|
|
auto l = line.layout->NextLine(maxw);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (l == nullptr) break;
|
2019-04-02 19:31:10 +00:00
|
|
|
this->push_back(std::move(l));
|
2013-06-27 16:24:19 +00:00
|
|
|
}
|
2013-07-06 19:00:33 +00:00
|
|
|
} while (c != '\0');
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the boundaries of this paragraph.
|
|
|
|
* @return The boundaries.
|
|
|
|
*/
|
|
|
|
Dimension Layouter::GetBounds()
|
|
|
|
{
|
|
|
|
Dimension d = { 0, 0 };
|
2019-04-02 19:31:10 +00:00
|
|
|
for (const auto &l : *this) {
|
2021-01-08 10:16:18 +00:00
|
|
|
d.width = std::max<uint>(d.width, l->GetWidth());
|
2019-02-17 11:20:52 +00:00
|
|
|
d.height += l->GetLeading();
|
2013-06-25 20:29:31 +00:00
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
2013-07-06 18:56:23 +00:00
|
|
|
|
2013-08-05 20:35:23 +00:00
|
|
|
/**
|
|
|
|
* Get the position of a character in the layout.
|
|
|
|
* @param ch Character to get the position of.
|
|
|
|
* @return Upper left corner of the character relative to the start of the string.
|
|
|
|
* @note Will only work right for single-line strings.
|
|
|
|
*/
|
|
|
|
Point Layouter::GetCharPosition(const char *ch) const
|
|
|
|
{
|
|
|
|
/* Find the code point index which corresponds to the char
|
|
|
|
* pointer into our UTF-8 source string. */
|
|
|
|
size_t index = 0;
|
|
|
|
const char *str = this->string;
|
|
|
|
while (str < ch) {
|
|
|
|
WChar c;
|
|
|
|
size_t len = Utf8Decode(&c, str);
|
|
|
|
if (c == '\0' || c == '\n') break;
|
|
|
|
str += len;
|
2019-02-17 11:20:52 +00:00
|
|
|
index += this->front()->GetInternalCharLength(c);
|
2013-08-05 20:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (str == ch) {
|
|
|
|
/* Valid character. */
|
2019-04-02 19:31:10 +00:00
|
|
|
const auto &line = this->front();
|
2013-08-05 20:35:23 +00:00
|
|
|
|
|
|
|
/* Pointer to the end-of-string/line marker? Return total line width. */
|
|
|
|
if (*ch == '\0' || *ch == '\n') {
|
2013-11-16 21:05:26 +00:00
|
|
|
Point p = { line->GetWidth(), 0 };
|
2013-08-05 20:35:23 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scan all runs until we've found our code point index. */
|
2013-11-16 21:05:26 +00:00
|
|
|
for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
|
2019-04-02 19:30:53 +00:00
|
|
|
const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
|
2013-08-05 20:35:23 +00:00
|
|
|
|
2019-04-02 19:30:53 +00:00
|
|
|
for (int i = 0; i < run.GetGlyphCount(); i++) {
|
2013-08-05 20:35:23 +00:00
|
|
|
/* Matching glyph? Return position. */
|
2019-04-02 19:30:53 +00:00
|
|
|
if ((size_t)run.GetGlyphToCharMap()[i] == index) {
|
|
|
|
Point p = { (int)run.GetPositions()[i * 2], (int)run.GetPositions()[i * 2 + 1] };
|
2013-08-05 20:35:23 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Point p = { 0, 0 };
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:53 +00:00
|
|
|
/**
|
|
|
|
* Get the character that is at a position.
|
|
|
|
* @param x Position in the string.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return Pointer to the character at the position or nullptr if no character is at the position.
|
2013-08-05 20:37:53 +00:00
|
|
|
*/
|
|
|
|
const char *Layouter::GetCharAtPosition(int x) const
|
|
|
|
{
|
2019-04-02 19:31:10 +00:00
|
|
|
const auto &line = this->front();
|
2013-08-05 20:37:53 +00:00
|
|
|
|
2013-11-16 21:05:26 +00:00
|
|
|
for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
|
2019-04-02 19:30:53 +00:00
|
|
|
const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
|
2013-08-05 20:37:53 +00:00
|
|
|
|
2019-04-02 19:30:53 +00:00
|
|
|
for (int i = 0; i < run.GetGlyphCount(); i++) {
|
2013-08-05 20:37:53 +00:00
|
|
|
/* Not a valid glyph (empty). */
|
2019-04-02 19:30:53 +00:00
|
|
|
if (run.GetGlyphs()[i] == 0xFFFF) continue;
|
2013-08-05 20:37:53 +00:00
|
|
|
|
2019-04-02 19:30:53 +00:00
|
|
|
int begin_x = (int)run.GetPositions()[i * 2];
|
|
|
|
int end_x = (int)run.GetPositions()[i * 2 + 2];
|
2013-08-05 20:37:53 +00:00
|
|
|
|
|
|
|
if (IsInsideMM(x, begin_x, end_x)) {
|
|
|
|
/* Found our glyph, now convert to UTF-8 string index. */
|
2019-04-02 19:30:53 +00:00
|
|
|
size_t index = run.GetGlyphToCharMap()[i];
|
2013-08-05 20:37:53 +00:00
|
|
|
|
|
|
|
size_t cur_idx = 0;
|
|
|
|
for (const char *str = this->string; *str != '\0'; ) {
|
|
|
|
if (cur_idx == index) return str;
|
|
|
|
|
2013-11-17 17:08:20 +00:00
|
|
|
WChar c = Utf8Consume(&str);
|
|
|
|
cur_idx += line->GetInternalCharLength(c);
|
2013-08-05 20:37:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2013-08-05 20:37:53 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 18:56:23 +00:00
|
|
|
/**
|
|
|
|
* Get a static font instance.
|
|
|
|
*/
|
|
|
|
Font *Layouter::GetFont(FontSize size, TextColour colour)
|
|
|
|
{
|
|
|
|
FontColourMap::iterator it = fonts[size].Find(colour);
|
|
|
|
if (it != fonts[size].End()) return it->second;
|
|
|
|
|
|
|
|
Font *f = new Font(size, colour);
|
2019-02-18 22:39:06 +00:00
|
|
|
fonts[size].emplace_back(colour, f);
|
2013-07-06 18:56:23 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset cached font information.
|
|
|
|
* @param size Font size to reset.
|
|
|
|
*/
|
|
|
|
void Layouter::ResetFontCache(FontSize size)
|
|
|
|
{
|
2019-02-17 11:20:52 +00:00
|
|
|
for (auto &pair : fonts[size]) {
|
|
|
|
delete pair.second;
|
2013-07-06 18:56:23 +00:00
|
|
|
}
|
2018-09-20 22:44:14 +00:00
|
|
|
fonts[size].clear();
|
2013-07-06 19:00:33 +00:00
|
|
|
|
|
|
|
/* We must reset the linecache since it references the just freed fonts */
|
|
|
|
ResetLineCache();
|
2018-05-26 14:13:12 +00:00
|
|
|
|
|
|
|
#if defined(WITH_UNISCRIBE)
|
|
|
|
UniscribeResetScriptCache(size);
|
|
|
|
#endif
|
2018-10-28 22:30:49 +00:00
|
|
|
#if defined(WITH_COCOA)
|
|
|
|
MacOSResetScriptCache(size);
|
|
|
|
#endif
|
2013-07-06 19:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get reference to cache item.
|
|
|
|
* If the item does not exist yet, it is default constructed.
|
|
|
|
* @param str Source string of the line (including colour and font size codes).
|
|
|
|
* @param len Length of \a str in bytes (no termination).
|
|
|
|
* @param state State of the font at the beginning of the line.
|
|
|
|
* @return Reference to cache item.
|
|
|
|
*/
|
|
|
|
Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (linecache == nullptr) {
|
2013-07-07 12:07:06 +00:00
|
|
|
/* Create linecache on first access to avoid trouble with initialisation order of static variables. */
|
|
|
|
linecache = new LineCache();
|
|
|
|
}
|
|
|
|
|
2021-08-16 09:18:47 +00:00
|
|
|
if (auto match = linecache->find(LineCacheQuery{state, std::string_view{str, len}});
|
|
|
|
match != linecache->end()) {
|
|
|
|
return match->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create missing entry */
|
2013-07-06 19:00:33 +00:00
|
|
|
LineCacheKey key;
|
|
|
|
key.state_before = state;
|
|
|
|
key.str.assign(str, len);
|
2013-07-07 12:07:06 +00:00
|
|
|
return (*linecache)[key];
|
2013-07-06 19:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear line cache.
|
|
|
|
*/
|
|
|
|
void Layouter::ResetLineCache()
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (linecache != nullptr) linecache->clear();
|
2013-07-06 19:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduce the size of linecache if necessary to prevent infinite growth.
|
|
|
|
*/
|
|
|
|
void Layouter::ReduceLineCache()
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (linecache != nullptr) {
|
2013-07-07 12:07:06 +00:00
|
|
|
/* TODO LRU cache would be fancy, but not exactly necessary */
|
|
|
|
if (linecache->size() > 4096) ResetLineCache();
|
|
|
|
}
|
2013-07-06 18:56:23 +00:00
|
|
|
}
|