KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gl_resources.cpp
Go to the documentation of this file.
1/*
2* This program source code file is part of KICAD, a free EDA CAD application.
4* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5*
6* This program is free software; you can redistribute it and/or
7* modify it under the terms of the GNU General Public License
8* as published by the Free Software Foundation; either version 2
9* of the License, or (at your option) any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20// The current font is "Ubuntu Mono" available under Ubuntu Font Licence 1.0
21// (see ubuntu-font-licence-1.0.txt for details)
22#include <algorithm>
23#include <iterator>
24#include <limits>
25#include <core/utf8.h>
26#include "gl_resources.h"
27
28#define BITMAP_FONT_USE_SPANS
29
30namespace KIGFX {
31namespace BUILTIN_FONT {
32
33#include "bitmap_font_img.c"
34#include "bitmap_font_desc.c"
35
36const FONT_GLYPH_TYPE* LookupGlyph( unsigned int aCodepoint )
37{
38#ifdef BITMAP_FONT_USE_SPANS
39 auto *end = font_codepoint_spans + sizeof( font_codepoint_spans ) / sizeof(FONT_SPAN_TYPE);
40
41 auto ptr = std::upper_bound( font_codepoint_spans, end, aCodepoint,
42 []( unsigned int codepoint, const FONT_SPAN_TYPE& span )
43 {
44 return codepoint < span.end;
45 } );
46
47 if( ptr != end && ptr->start <= aCodepoint )
48 {
49 unsigned int index = aCodepoint - ptr->start + ptr->cumulative;
50 return &font_codepoint_infos[ index ];
51 }
52 else
53 {
54 return nullptr;
55 }
56#else
57 return &bitmap_chars[codepoint];
58#endif
59}
60
61
62unsigned int GlyphCount()
63{
64 return std::size( font_codepoint_infos );
65}
66
67
69{
70 const float border = font_information.smooth_pixels;
71
72 GLYPH_BOX box;
73 box.w = aGlyph.atlas_w - border * 2;
74 box.h = aGlyph.atlas_h - border * 2;
75 box.atlasX = aGlyph.atlas_x + border;
76 box.atlasY = aGlyph.atlas_y + border;
77 box.x = aGlyph.minx;
78
79 // The atlas cell height is rounded to whole texels, and every glyph hangs from the font's top
80 const float roundAdjust = ( aGlyph.maxy - aGlyph.miny ) - box.h;
81 const float topAdjust = font_information.max_y - aGlyph.maxy;
82 box.y = roundAdjust + topAdjust;
83
84 return box;
85}
86
87
89{
90 const FONT_GLYPH_TYPE* x = LookupGlyph( 'x' );
91
92 return x ? x->advance * 0.74 : 0.0;
93}
94
95
96std::pair<VECTOR2D, float> ComputeBitmapTextSize( const UTF8& aText )
97{
98 static const FONT_GLYPH_TYPE* defaultGlyph = LookupGlyph( '(' ); // for strange chars
99
100 VECTOR2D textSize( 0, 0 );
101 float commonOffset = std::numeric_limits<float>::max();
102 float charHeight = font_information.max_y - defaultGlyph->miny;
103 int overbarDepth = -1;
104 int braceNesting = 0;
105
106 for( UTF8::uni_iter chIt = aText.ubegin(), end = aText.uend(); chIt < end; ++chIt )
107 {
108 if( *chIt == '~' && overbarDepth == -1 )
109 {
110 UTF8::uni_iter lookahead = chIt;
111
112 if( ++lookahead != end && *lookahead == '{' )
113 {
114 chIt = lookahead;
115 overbarDepth = braceNesting;
116 braceNesting++;
117 continue;
118 }
120 else if( *chIt == '{' )
121 {
122 braceNesting++;
123 }
124 else if( *chIt == '}' )
125 {
126 if( braceNesting > 0 )
127 braceNesting--;
128
129 if( braceNesting == overbarDepth )
130 {
131 overbarDepth = -1;
132 continue;
133 }
134 }
135
136 const FONT_GLYPH_TYPE* glyph = LookupGlyph( *chIt );
137
138 if( !glyph // Not coded in font
139 || *chIt == '-' || *chIt == '_' ) // Strange size of these 2 chars
140 {
141 glyph = defaultGlyph;
142 }
143
144 if( glyph )
145 textSize.x += glyph->advance;
146 }
147
148 textSize.y = std::max<float>( textSize.y, charHeight );
149 commonOffset = std::min<float>( font_information.max_y - defaultGlyph->maxy, commonOffset );
150 textSize.y -= commonOffset;
151
152 return std::make_pair( textSize, commonOffset );
153}
154
155}
156}
int index
uni_iter is a non-mutating iterator that walks through unicode code points in the UTF8 encoded string...
Definition utf8.h:226
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
uni_iter uend() const
Return a uni_iter initialized to the end of "this" UTF8 byte sequence.
Definition utf8.h:309
uni_iter ubegin() const
Returns a uni_iter initialized to the start of "this" UTF8 byte sequence.
Definition utf8.h:301
unsigned int GlyphCount()
The number of glyphs in the font, starting at LookupGlyph( '!
std::pair< VECTOR2D, float > ComputeBitmapTextSize(const UTF8 &aText)
Size of a line of text in atlas units, and how far its glyphs sit above their lowest point.
double SpaceAdvance()
How far a space moves the pen.
const FONT_GLYPH_TYPE * LookupGlyph(unsigned int aCodepoint)
FONT_SPAN_TYPE font_codepoint_spans[]
FONT_INFO_TYPE font_information
FONT_GLYPH_TYPE font_codepoint_infos[]
GLYPH_BOX ComputeGlyphBox(const FONT_GLYPH_TYPE &aGlyph)
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:30
Where a glyph's quad sits and what it samples, in font units, which are also atlas texels.
float h
size of the quad and of its cell in the atlas
float atlasY
corner of the cell in the atlas, inside its smoothing border
float y
corner of the quad from the pen position
VECTOR2I end
VECTOR2< double > VECTOR2D
Definition vector2d.h:707