📄 glyph_buffer.h
字号:
//: Returns a single CL_GlyphBuffer::Range containing the line that contains a given glyph.
//param idx: The index in the glpyhs vector of the glyph.
//- <p> The CL_GlyphBuffer's contents (the glyphs vector, the font markers map, and the effects maps),
//- if any, must not have been created/altered by anything but CL_Font::draw_to_gb() or CL_TextStyler::draw_to_gb()
//- or the CL_GlyphBuffer justification functions or using CL_GlyphBuffer::remove to take glyphs off the end
//- for this method to work.</p>
CL_GlyphBuffer::Range get_line(int idx) const;
//! Operations
public:
//: Add elements onto the end of the glyphs vector.
//return: A reference to this object, allowing you to chain multiple calls of add() together.
//param x, y: The position to add the characters at (always upper-left positioning inside the buffer).
//param center_x, center_y: Where to realign the origin of the input GlyphBuffer to in this GlyphBuffer.
//param character: A single character to add without preprocessing.
//param other: Another CL_GlyphBuffer to copy all glyphs and data from to the specified position.
//param range: The subsection of the other GlyphBuffer to add onto the end of this one
//- <p> It's usually more useful and efficient to have ClanLib draw many glyphs into the GlyphBuffer at once using
//- CL_Font::draw_to_gb() or CL_TextStyler::draw_to_gb() than to use this function or the glyphs vector
//- to manually add glyphs.</p>
CL_GlyphBuffer &add(int x, int y, unsigned char character)
{glyphs.push_back(Glyph(CL_Point(x,y), character)); return *this;}
CL_GlyphBuffer &add(const CL_GlyphBuffer &other, int center_x = 0, int center_y = 0)
{return add(other, Range(other), center_x, center_y);}
CL_GlyphBuffer &add(const CL_GlyphBuffer &other, Range range, int center_x = 0, int center_y = 0);
//: Removes an element or elements from the glyphs vector, reorganizes the font and effect maps to compensate.
//return: A reference to this object, allowing you to chain multiple calls of remove() together.
//param index: The index in the glyphs vector to remove (all elements beyond this are slid back)
//param range: The subsection of the glyphs vector to erase
CL_GlyphBuffer &remove(int index)
{return remove(Range(index, index+1));}
CL_GlyphBuffer &remove(Range range);
//: Adds a font change marker, set to occur before the next glyph/location you add is drawn.
//return: A reference to this object, allowing you to chain change_font() into chains of calls to add().
//param font: The font to change to.
CL_GlyphBuffer &change_font(const CL_Font &font);
//: Remove all the character position and font marker data in the buffer.
void clear();
//: Draw the glyphs in the buffer.
//param x, y: Anchor position to draw at. Actual drawing position depends on the alignment mode.
//param gc: Graphic context on which to render. If null, will use CL_Display's current graphic context.
//param dest: Rectangle to draw glyphs in. The glyphs will be positioned within the rectangle depending on the alignment mode.
//param range: The subsection of the glyphs vector to use.
void draw(
int x = 0,
int y = 0,
CL_GraphicContext *gc = 0)
{draw(Range(*this), CL_Rect(x, y, x, y), gc);}
void draw(
Range range,
int x = 0,
int y = 0,
CL_GraphicContext *gc = 0)
{draw(range, CL_Rect(x, y, x, y), gc);}
void draw(
CL_Rect dest,
CL_GraphicContext *gc = 0)
{draw(Range(*this), dest, gc);}
void draw(
Range range,
CL_Rect dest,
CL_GraphicContext *gc = 0)
{CL_Rect irect = internal_rect(range); draw_glyphs(range, i2e_offset(irect, dest), irect, gc);}
//: Draws the glyphs in the buffer without bounding rect alignment.
//param x, y: Glyphs are drawn with the CL_GlyphBuffer's internal origin at this location.
//param gc: Graphic context on which to render. If null, will use CL_Display's current graphic context.
//param range: The subsection of the glyphs vector to use.
void fixed_draw(
int x = 0,
int y = 0,
CL_GraphicContext *gc = 0)
{fixed_draw(Range(*this), x, y, gc);}
void fixed_draw(
Range range,
int x = 0,
int y = 0,
CL_GraphicContext *gc = 0)
{draw_glyphs(range, CL_Point(x, y), CL_Rect(0,0,0,0), gc);}
//: Rejustifies lines in the GlyphBuffer to the left.
//param range: The subsection of the glyphs vector to change, which is expanded out both ways to contain at least one line.
//param left: The x coordinate of the vertical line that text should be moved up against.
//- <p> The CL_GlyphBuffer's contents (the glyphs vector, the font markers map, and the effects maps),
//- if any, must not have been created/altered by anything but CL_Font::draw_to_gb() or CL_TextStyler::draw_to_gb()
//- or the CL_GlyphBuffer justification functions or using CL_GlyphBuffer::remove to take glyphs off the end
//- for this method to work.</p>
//- <p> To preserve indentation, this method takes into account whitespace (glyphs not provided by the font) when justifying.</p>
void justify_left(int left = 0)
{justify_left(Range(*this), left);}
void justify_left(Range range, int left = 0);
//: Rejustifies lines in the GlyphBuffer to the center.
//param range: The subsection of the glyphs vector to change, which is expanded out both ways to contain at least one line.
//param center: The x coordinate of the vertical line that text should be centered at.
//- <p> The CL_GlyphBuffer's contents (the glyphs vector, the font markers map, and the effects maps),
//- if any, must not have been created/altered by anything but CL_Font::draw_to_gb() or CL_TextStyler::draw_to_gb()
//- or the CL_GlyphBuffer justification functions or using CL_GlyphBuffer::remove to take glyphs off the end
//- for this method to work.</p>
//- <p> This method ignores whitespace (glyphs not provided by the font) when justifying. </p>
void justify_center(int center = 0)
{justify_center(Range(*this), center);}
void justify_center(Range range, int center = 0);
//: Rejustifies lines in the GlyphBuffer to the right.
//param range: The subsection of the glyphs vector to change, which is expanded out both ways to contain at least one line.
//param right: The x coordinate of the vertical line that text should be moved up against.
//- <p> The CL_GlyphBuffer's contents (the glyphs vector, the font markers map, and the effects maps),
//- if any, must not have been created/altered by anything but CL_Font::draw_to_gb() or CL_TextStyler::draw_to_gb()
//- or the CL_GlyphBuffer justification functions or using CL_GlyphBuffer::remove to take glyphs off the end
//- for this method to work.</p>
//- <p> This method ignores whitespace (glyphs not provided by the font) when justifying. </p>
void justify_right(int right = 0)
{justify_right(Range(*this), right);}
void justify_right(Range range, int right = 0);
//: Set absolute rotation angle.
//- <p> This rotates the entire glyph field when drawing.
//- The fonts' glyph rotation origin must be origin_top_left:0:0 for this
//- to produce legible results. See CL_Font::set_glyph_rotation_hotspot(). </p>
void set_angle(float angle);
//: Add angle to current angle.
//- <p> This rotates the entire glyph field when drawing.
//- The fonts' glyph rotation origin must be origin_top_left:0:0 for this
//- to produce legible results. See CL_Font::set_glyph_rotation_hotspot(). </p>
void rotate(float angle)
{angle += rot_angle; set_angle(angle);}
//: Set scale for x and y directions individually.
//- <p> 1.0f is normal scale, 2.0f is twice the size, etc. </p>
void set_scale(float x, float y)
{scale_x = x; scale_y = y;}
//: Sets translation hotspot.
void set_alignment(CL_Origin origin, int x = 0, int y = 0)
{trans_origin = origin; trans_x = x; trans_y = y;}
//: Sets rotation hotspot.
void set_rotation_hotspot(CL_Origin origin, int x = 0, int y = 0)
{rot_origin = origin; rot_x = x; rot_y = y;}
//! Implementation:
private:
//: Used to iterate through the buffer, changing font and applying effects as needed
class Iter;
friend class Iter;
class Iter {
private:
//Pointer to the GlyphBuffer
const CL_GlyphBuffer* pgb;
//The current index in the glyphs vector
int glyph_num;
//Do not allow default construction
Iter() {}
public:
//Constructs a new Iter
//param range: The beginning index in this Range is used as the initial glyph_num
Iter(const CL_GlyphBuffer& new_gb, Range range)
: pgb(&new_gb), glyph_num(range.start) {}
//Advances to the next glyph (prefix)
Iter& operator ++() {++glyph_num; return *this;}
//Advances to the next glyph (postfix)
const Iter operator ++(int) {Iter old = *this; ++(*this); return old;}
//Returns true if the iterator has covered everything (i.e. is past-the-end)
//param range: The ending index in this Range is checked against
bool at_end(Range range) const {if (glyph_num >= range.end) return true; return false;}
//Returns the index in the glyphs vector
int get_glyph_num() const {return glyph_num;}
//Returns the scale effect, or 1.0 if no scale effect for this glyph
void get_scale_eff(float& x, float& y) const;
//Returns the angle effect, or 0.0 if no angle effect for this glyph
float get_angle_eff() const;
//Returns the color effect, or given font's color if no color effect for this glyph
CL_Color get_color_eff(const CL_Font& fnt) const;
//Returns a non-mutable reference to the current font
const CL_Font& get_font() const;
};
//: Returns true if the given glyph index is directly before a linebreak, manual or logical
//param fnt: The font that idx is in
bool is_linebreak(int idx, const CL_Font& fnt) const;
//: Erases all elements of a map<int, T> in [start, end), then slides back elements
template <typename T>
static void cut_range(std::map<int, T>& target, int start, int end)
{
//Remove all the elements inside the range
target.erase(target.lower_bound(start), target.upper_bound(end));
//Move back all the elements from the end of the range to the end of the map
for (typename std::map<int, T>::iterator it = target.lower_bound(end); it != target.end();)
{
target[it->first - (end - start)] = it->second;
target.erase(it++);
}
}
//: Returns the offset to align an internal bounding rectangle to an external bounding rectangle.
CL_Point i2e_offset(CL_Rect src, CL_Rect dest) const;
//: Returns the point to scale away from.
//param irect: Internal rectangle containing all glyphs
CL_Point get_scale_hotspot(CL_Rect irect) const;
//: Returns the point to rotate around.
//param irect: Internal rectangle containing all glyphs
CL_Point get_rot_hotspot(CL_Rect irect) const;
//: Draws all the glyphs in the buffer.
//param offset: Added to each position right before drawing, to move to external coords
//param irect: Result of internal_rect() passed in if caller has it, for efficiency's sake, else CL_Rect(0,0,0,0)
//param gc: The graphics context to draw to (default if 0)
//param range: The subsection of the glyphs vector to use
void draw_glyphs(
Range range,
CL_Point offset,
CL_Rect irect,
CL_GraphicContext* gc) const;
//: Scale.
float scale_x;
float scale_y;
//: Angle and rotation hotspot.
float rot_angle;
CL_Origin rot_origin;
int rot_x;
int rot_y;
//: Alignment.
CL_Origin trans_origin;
int trans_x;
int trans_y;
//: The glyphs and their positions.
std::vector<Glyph> glyphs;
//: The font change markers.
mutable std::map<int, CL_Font> font_markers;
//: Color, scale, and angle font effect maps.
std::map<int, CL_Color> color_effects;
std::map<int, float> scale_x_effects;
std::map<int, float> scale_y_effects;
std::map<int, float> angle_effects;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -