Dot gives us the width of the string it asks us to display. Since we
don't necessarily have a font with the same metrics, scale the text down
to make it always fit within this width.
There's also some disabled debugging code to visually show where dot
thinks the text should live.
From: Marius Gedminas <marius@gedmin.as>
... | ... |
@@ -128,6 +128,15 @@ class TextShape(Shape): |
128 | 128 |
width, height = layout.get_size() |
129 | 129 |
width = float(width)/pango.SCALE |
130 | 130 |
height = float(height)/pango.SCALE |
131 |
+ # we know the width that dot thinks this text should have |
|
132 |
+ # we do not necessarily have a font with the same metrics |
|
133 |
+ # scale it so that the text fits inside its box |
|
134 |
+ if width > self.w: |
|
135 |
+ f = self.w / width |
|
136 |
+ width = self.w # equivalent to width *= f |
|
137 |
+ height *= f |
|
138 |
+ else: |
|
139 |
+ f = 1.0 |
|
131 | 140 |
|
132 | 141 |
descent = 2 # XXX get descender from font metrics |
133 | 142 |
|
... | ... |
@@ -146,8 +155,24 @@ class TextShape(Shape): |
146 | 155 |
|
147 | 156 |
cr.move_to(x, y) |
148 | 157 |
|
158 |
+ cr.save() |
|
159 |
+ cr.scale(f, f) |
|
149 | 160 |
cr.set_source_rgba(*self.pen.color) |
150 | 161 |
cr.show_layout(layout) |
162 |
+ cr.restore() |
|
163 |
+ |
|
164 |
+ if 0: # DEBUG |
|
165 |
+ # show where dot thinks the text should appear |
|
166 |
+ cr.set_source_rgba(1, 0, 0, .9) |
|
167 |
+ if self.j == self.LEFT: |
|
168 |
+ x = self.x |
|
169 |
+ elif self.j == self.CENTER: |
|
170 |
+ x = self.x - 0.5*self.w |
|
171 |
+ elif self.j == self.RIGHT: |
|
172 |
+ x = self.x - self.w |
|
173 |
+ cr.move_to(x, self.y) |
|
174 |
+ cr.line_to(x+self.w, self.y) |
|
175 |
+ cr.stroke() |
|
151 | 176 |
|
152 | 177 |
|
153 | 178 |
class EllipseShape(Shape): |