Ejemplo n.º 1
0
internal rectangle2
DEBUGTextOp(debug_state *DebugState, debug_text_op Op, v2 P, char *String, v4 Color = V4(1, 1, 1, 1)) {
    rectangle2 Result = InvertedInfinityRectangle2();
    if (DebugState && DebugState->DebugFont) {
        render_group *RenderGroup = DebugState->RenderGroup;
        loaded_font *Font = DebugState->DebugFont;
        hha_font *Info = DebugState->DebugFontInfo;

        u32 PrevCodePoint = 0;

        for (char *At = String; *At; ++At) {
            u32 CodePoint = *At;

            if (At[0] == '\\' && IsHex(At[1]) && IsHex(At[2]) && IsHex(At[3]) && IsHex(At[4])) {
                CodePoint = (GetHex(At[1]) << 12 |
                             GetHex(At[2]) <<  8 |
                             GetHex(At[3]) <<  4 |
                             GetHex(At[4]) <<  0);
                At += 4;
            }

            r32 AdvanceX = DebugState->FontScale * GetHorizontalAdvanceForPair(Info, Font, PrevCodePoint, CodePoint);
            P.x += AdvanceX;

            if (CodePoint != ' ') {
                bitmap_id BitmapID = GetBitmapForGlyph(RenderGroup->Assets, Info, Font, CodePoint);
                hha_bitmap *BitmapInfo = GetBitmapInfo(RenderGroup->Assets, BitmapID);

                r32 BitmapScale = DebugState->FontScale * (r32)BitmapInfo->Dim[1];
                v3 BitmapOffset = V3(P.x, P.y, 0);
                if (Op == DEBUGTextOp_DrawText) {
                    PushBitmap(RenderGroup, BitmapID, BitmapScale, BitmapOffset, Color);
                } else {
                    Assert(Op == DEBUGTextOp_SizeText);

                    loaded_bitmap *Bitmap = GetBitmap(RenderGroup->Assets, BitmapID, RenderGroup->GenerationID);
                    if (Bitmap) {
                        used_bitmap_dim Dim = GetBitmapDim(RenderGroup, Bitmap, BitmapScale, BitmapOffset, 1.0f);
                        rectangle2 GlyphDim = RectMinDim(Dim.P.xy, Dim.Size);
                        Result = Union(Result, GlyphDim);
                    }
                }
            }

            PrevCodePoint = CodePoint;
        }
    }

    return Result;
}
Ejemplo n.º 2
0
void GlyphCellRenderer::Draw(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, const wxRect &rect, int row, int col, bool isSelected)
{
	wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, false);

	wxString value = grid.GetTable()->GetValue(row, col);
	wxString label;

	auto it = glyphs.find(value);
	if (it == glyphs.end()) return;
	
	const SvgGlyph &glyph = it->second;

	if (!glyph.IsOk())
		return;

	if (showGlyphNames) label = glyph.glyphName;
	else label.Printf("%04x", glyph.unicode[0]);

	std::unique_ptr<wxGraphicsContext> gc(wxGraphicsContext::Create(static_cast<wxPaintDC&>(dc)));

	wxRect newRect = rect;

	// replace with gc->GetRenderer()->GetName() == L"cairo" after wx 3.1
	bool isCairo = false;

	#if wxUSE_CAIRO
		isCairo = true;
	#endif

	// Oh, crap
	if (isCairo)
	{
		newRect.x += dc.GetDeviceOrigin().x;
		newRect.y += dc.GetDeviceOrigin().y;
	}

	std::map<wxString, wxBitmap>::iterator findIt = glyphCache.find(glyph.unicode);

	if (findIt == glyphCache.end())
	{
		bool result;
		std::tie(findIt, result) = glyphCache.emplace(glyph.unicode, GetBitmapForGlyph(glyph, fontSize, glyphColor, attr.GetBackgroundColour(), false));

		if (!result) return;
	}

	if (hlCellCoords.GetCol() == col && hlCellCoords.GetRow() == row)
	{
		gc->SetPen(wxPen(hlColor, 1));
		gc->DrawRoundedRectangle(newRect.x + 1, newRect.y + 1, newRect.width - 2, newRect.height - 2, 5);
	}

	newRect.height -= labelFont.GetPixelSize().GetHeight() + 2 * padding;

	const wxBitmap &glyphBitmap = findIt->second;

	if (glyphBitmap.IsOk())
	{
		gc->DrawBitmap(glyphBitmap,
			newRect.x + (newRect.width - glyphBitmap.GetWidth()) / 2,
			newRect.y + (newRect.height - glyphBitmap.GetHeight()) / 2,
			glyphBitmap.GetWidth(), glyphBitmap.GetHeight());
	}

	double maxTextWidth = std::max(0, newRect.width - 2);
	double width, height, descent, externalLeading;

	gc->SetFont(labelFont, labelColor);
	gc->GetTextExtent(label, &width, &height, &descent, &externalLeading);

	wxString origLabel = label;
	size_t cutCntr = 1;

	while (width > maxTextWidth && !label.IsEmpty())
	{
		label = origLabel.Left(origLabel.Length() - cutCntr++) + L"\u2026";
		gc->GetTextExtent(label, &width, &height, &descent, &externalLeading);
	}

	gc->DrawText(label, newRect.x + (newRect.width - width) / 2, newRect.y + newRect.height + padding);
}