~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/awt/fonts-0.95.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2002, 2004, 2005, 2006, 2007 Jeroen Frijters
 
3
  Copyright (C) 2006 Active Endpoints, Inc.
 
4
  Copyright (C) 2006, 2007, 2009 - 2011 Volker Berlin
 
5
 
 
6
  This software is provided 'as-is', without any express or implied
 
7
  warranty.  In no event will the authors be held liable for any damages
 
8
  arising from the use of this software.
 
9
 
 
10
  Permission is granted to anyone to use this software for any purpose,
 
11
  including commercial applications, and to alter it and redistribute it
 
12
  freely, subject to the following restrictions:
 
13
 
 
14
  1. The origin of this software must not be misrepresented; you must not
 
15
     claim that you wrote the original software. If you use this software
 
16
     in a product, an acknowledgment in the product documentation would be
 
17
     appreciated but is not required.
 
18
  2. Altered source versions must be plainly marked as such, and must not be
 
19
     misrepresented as being the original software.
 
20
  3. This notice may not be removed or altered from any source distribution.
 
21
 
 
22
  Jeroen Frijters
 
23
  jeroen@frijters.net 
 
24
 
 
25
*/
 
26
 
 
27
using System;
 
28
using System.Drawing;
 
29
using System.Drawing.Drawing2D;
 
30
using System.Drawing.Text;
 
31
using System.Text;
 
32
using java.awt.font;
 
33
using java.awt.geom;
 
34
using java.util;
 
35
#if WINFX
 
36
using System.Windows;
 
37
using System.Windows.Media;
 
38
#endif
 
39
 
 
40
 
 
41
namespace ikvm.awt
 
42
{
 
43
 
 
44
    /*class NetFontMetrics : java.awt.FontMetrics
 
45
    {
 
46
        private static readonly Bitmap defaultbitmap = new Bitmap(1, 1);
 
47
        [ThreadStatic]
 
48
        private static Graphics threadLocalDefaultGraphics;
 
49
 
 
50
                private static Graphics GetDefaultGraphics()
 
51
                {
 
52
                        Graphics g = threadLocalDefaultGraphics;
 
53
                        if (g == null)
 
54
                        {
 
55
                                g = threadLocalDefaultGraphics = Graphics.FromImage(defaultbitmap);
 
56
                                g.SmoothingMode = SmoothingMode.None;
 
57
                                g.PixelOffsetMode = PixelOffsetMode.None;
 
58
                                g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
 
59
                        }
 
60
                        return g;
 
61
                }
 
62
 
 
63
        public NetFontMetrics(java.awt.Font font) : base(font)
 
64
        {
 
65
        }
 
66
 
 
67
        private Font GetNetFont()
 
68
        {
 
69
            return font.getNetFont();
 
70
        }
 
71
 
 
72
        public override int getHeight()
 
73
        {
 
74
            return GetNetFont().Height;
 
75
        }
 
76
 
 
77
        public override int getLeading()
 
78
        {
 
79
            return (int)Math.Round(GetLeadingFloat());
 
80
        }
 
81
 
 
82
        public override int getMaxAdvance()
 
83
        {
 
84
            // HACK very lame
 
85
            return charWidth('M');
 
86
        }
 
87
 
 
88
        public override int charWidth(char ch)
 
89
        {
 
90
            // HACK we average 20 characters to decrease the influence of the pre/post spacing
 
91
            return stringWidth(new String(ch, 20)) / 20;
 
92
        }
 
93
 
 
94
        public override int charsWidth(char[] data, int off, int len)
 
95
        {
 
96
            return stringWidth(new String(data, off, len));
 
97
        }
 
98
 
 
99
        public override int getAscent()
 
100
        {
 
101
            return (int)Math.Round(GetAscentFloat());
 
102
        }
 
103
 
 
104
        public override int getDescent()
 
105
        {
 
106
            return (int)Math.Round(GetDescentFloat());
 
107
        }
 
108
 
 
109
        public override int stringWidth(string s) 
 
110
        {
 
111
            return (int)Math.Round(GetStringWidth(s, GetDefaultGraphics()));
 
112
        }
 
113
 
 
114
        public float GetAscentFloat()
 
115
        {
 
116
            Font f = GetNetFont();
 
117
            int ascent = f.FontFamily.GetCellAscent(f.Style);
 
118
            return f.Size * ascent / f.FontFamily.GetEmHeight(f.Style);
 
119
        }
 
120
 
 
121
        public float GetDescentFloat()
 
122
        {
 
123
            Font f = GetNetFont();
 
124
            int descent = f.FontFamily.GetCellDescent(f.Style);
 
125
            return f.Size * descent / f.FontFamily.GetEmHeight(f.Style);
 
126
        }
 
127
 
 
128
        public float GetLeadingFloat()
 
129
        {
 
130
            float leading = getHeight() - (GetAscentFloat() + GetDescentFloat());
 
131
            return Math.Max(0.0f, leading);
 
132
        }
 
133
 
 
134
        internal float GetStringWidth(String aString, System.Drawing.Graphics g)
 
135
        {
 
136
            if (aString.Length == 0)
 
137
            {
 
138
                return 0;
 
139
            }
 
140
            // System.Windows.Forms.TextRenderer#MeasureText seems to large
 
141
            // Graphics#MeasureString is many faster but work only correct with TextRenderingHint.AntiAlias
 
142
            bool rounding;
 
143
            StringFormat format;
 
144
            switch (g.TextRenderingHint)
 
145
            {
 
146
                // Fractional metrics
 
147
                case TextRenderingHint.AntiAlias:
 
148
                case TextRenderingHint.SingleBitPerPixel:
 
149
                    // this very mystic, if a StringFormat extends from GenericTypographic then the metric are different but like Java with fractional metrics
 
150
                    format = new StringFormat(StringFormat.GenericTypographic);
 
151
                    rounding = false;
 
152
                    break;
 
153
                default:
 
154
                    format = new StringFormat();
 
155
                    rounding = true;
 
156
                    break;
 
157
            }
 
158
 
 
159
            format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap |
 
160
                                 StringFormatFlags.FitBlackBox;
 
161
            format.Trimming = StringTrimming.None;
 
162
            format.SetMeasurableCharacterRanges(new CharacterRange[] {new CharacterRange(0, aString.Length)});
 
163
            Region[] regions = g.MeasureCharacterRanges(aString, GetNetFont(),
 
164
                                                        new RectangleF(0, 0, int.MaxValue, int.MaxValue), format);
 
165
            SizeF size = regions[0].GetBounds(g).Size;
 
166
            regions[0].Dispose();
 
167
            //with Arial 9.0 and only one character under Vista .NET does not round it, that we rounding manualy
 
168
            return rounding ? (int) Math.Round(size.Width) : size.Width;
 
169
        }
 
170
 
 
171
        internal java.awt.geom.Rectangle2D GetStringBounds(String aString, System.Drawing.Graphics g)
 
172
        {
 
173
            Font netFont = GetNetFont();
 
174
            FontFamily family = netFont.FontFamily;
 
175
            FontStyle style = netFont.Style;
 
176
            float factor = netFont.Size / family.GetEmHeight(style);
 
177
            float height = family.GetLineSpacing(style) * factor;
 
178
            float descent = family.GetCellDescent(style) * factor;
 
179
            float ascent = family.GetCellAscent(style) * factor;
 
180
            float leading = height - ascent - descent;
 
181
 
 
182
            return new java.awt.geom.Rectangle2D.Float(0, -ascent - leading / 2, GetStringWidth(aString, g), height);
 
183
        }
 
184
 
 
185
        public override java.awt.geom.Rectangle2D getStringBounds(String aString, java.awt.Graphics gr)
 
186
        {
 
187
            if (gr is NetGraphics)
 
188
            {
 
189
                return GetStringBounds(aString, ((NetGraphics)gr).JGraphics);
 
190
            }
 
191
            return GetStringBounds(aString, GetDefaultGraphics());
 
192
        }
 
193
    }*/
 
194
 
 
195
/*    class NetFontPeer : java.awt.peer.FontPeer, IDisposable
 
196
    {
 
197
        internal readonly Font netFont;
 
198
 
 
199
        internal NetFontPeer(string name, java.util.Map attrs)
 
200
            : base(name, attrs)
 
201
        {
 
202
            netFont = J2C.ConvertFont(name, getStyle(null), getSize(null));
 
203
        }
 
204
 
 
205
        public override bool canDisplay(int codePoint)
 
206
        {
 
207
            //HACK There is no equivalent in C# http://msdn2.microsoft.com/en-us/library/sf4dhbw8(VS.80).aspx
 
208
            return true;
 
209
        }
 
210
 
 
211
        public override int canDisplayUpTo(java.awt.Font font, java.text.CharacterIterator param2, int param3, int param4)
 
212
        {
 
213
            //HACK There is no equivalent in C# http://msdn2.microsoft.com/en-us/library/e8bh4szw(VS.80).aspx
 
214
            return -1;
 
215
        }
 
216
 
 
217
        public override GlyphVector createGlyphVector(java.awt.Font font, FontRenderContext frc, int[] glyphCodes)
 
218
        {
 
219
            char[] chars = new char[glyphCodes.Length];
 
220
            for (int i = 0; i < chars.Length; i++)
 
221
            {
 
222
                chars[i] = (char)glyphCodes[i];
 
223
            }
 
224
            return new NetGlyphVector(netFont, font, frc, chars);
 
225
        }
 
226
 
 
227
        public override GlyphVector createGlyphVector(java.awt.Font font, FontRenderContext frc, java.text.CharacterIterator text)
 
228
        {
 
229
            int count = text.getEndIndex() - text.getBeginIndex();
 
230
            char[] chars = new char[count];
 
231
            text.first();
 
232
            for (int i = 0; i < count; i++)
 
233
            {
 
234
                chars[i] = text.current();
 
235
                text.next();
 
236
            }
 
237
            return new NetGlyphVector(netFont, font, frc, chars);
 
238
        }
 
239
 
 
240
        public override byte getBaselineFor(java.awt.Font font, char param2)
 
241
        {
 
242
            throw new NotImplementedException();
 
243
        }
 
244
 
 
245
        public override java.awt.FontMetrics getFontMetrics(java.awt.Font font)
 
246
        {
 
247
            return new NetFontMetrics(font);
 
248
        }
 
249
 
 
250
        public override string getGlyphName(java.awt.Font font, int param2)
 
251
        {
 
252
            throw new NotImplementedException();
 
253
        }
 
254
 
 
255
        public override LineMetrics getLineMetrics(java.awt.Font font, java.text.CharacterIterator aCharacterIterator, int aBegin, int aLimit, FontRenderContext aFontRenderContext)
 
256
        {
 
257
            string s = ToString(aCharacterIterator, aBegin, aLimit);
 
258
            return new NetLineMetrics(font, s);
 
259
        }
 
260
 
 
261
        public override java.awt.geom.Rectangle2D getMaxCharBounds(java.awt.Font font, FontRenderContext param2)
 
262
        {
 
263
            throw new NotImplementedException();
 
264
        }
 
265
 
 
266
        public override int getMissingGlyphCode(java.awt.Font font)
 
267
        {
 
268
            throw new NotImplementedException();
 
269
        }
 
270
 
 
271
        public override int getNumGlyphs(java.awt.Font font)
 
272
        {
 
273
            throw new NotImplementedException();
 
274
        }
 
275
 
 
276
        public override string getPostScriptName(java.awt.Font font)
 
277
        {
 
278
            throw new NotImplementedException();
 
279
        }
 
280
 
 
281
        public override bool hasUniformLineMetrics(java.awt.Font font)
 
282
        {
 
283
            throw new NotImplementedException();
 
284
        }
 
285
 
 
286
        public override GlyphVector layoutGlyphVector(java.awt.Font font, FontRenderContext frc, char[] text, int beginIndex, int endIndex, int flags)
 
287
        {
 
288
            char[] chars = new char[endIndex - beginIndex];
 
289
            Array.Copy(text, beginIndex, chars, 0, chars.Length);
 
290
            return new NetGlyphVector(netFont, font, frc, chars);
 
291
        }
 
292
 
 
293
        public override string getSubFamilyName(java.awt.Font font, Locale param2)
 
294
        {
 
295
            throw new NotImplementedException();
 
296
        }
 
297
 
 
298
        private static string ToString(java.text.CharacterIterator aCharacterIterator, int aBegin, int aLimit)
 
299
        {
 
300
            aCharacterIterator.setIndex(aBegin);
 
301
            StringBuilder sb = new StringBuilder();
 
302
 
 
303
            for (int i = aBegin; i <= aLimit; ++i)
 
304
            {
 
305
                char c = aCharacterIterator.current();
 
306
                sb.Append(c);
 
307
                aCharacterIterator.next();
 
308
            }
 
309
 
 
310
            return sb.ToString();
 
311
        }
 
312
 
 
313
        #region IDisposable Members
 
314
 
 
315
        public void Dispose()
 
316
        {
 
317
            netFont.Dispose();
 
318
        }
 
319
 
 
320
        #endregion
 
321
    }*/
 
322
 
 
323
    /*    class NetGlyphVector : GlyphVector
 
324
        {
 
325
            private readonly Font netFont;
 
326
            private readonly java.awt.Font font;
 
327
            private readonly FontRenderContext frc;
 
328
            private readonly char[] text;
 
329
            private NetFontMetrics metrics;
 
330
 
 
331
 
 
332
            internal NetGlyphVector(Font netFont, java.awt.Font font, FontRenderContext frc, char[] text)
 
333
            {
 
334
                this.netFont = netFont;
 
335
                this.font = font;
 
336
                this.frc = frc;
 
337
                this.text = text;
 
338
            }
 
339
 
 
340
            private NetFontMetrics getMetrics()
 
341
            {
 
342
                if(metrics == null)
 
343
                {
 
344
                    metrics = new NetFontMetrics(font);
 
345
                }
 
346
                return metrics;
 
347
            }
 
348
 
 
349
            public override bool equals(GlyphVector gv)
 
350
            {
 
351
                throw new NotImplementedException();
 
352
            }
 
353
 
 
354
            public override java.awt.Font getFont()
 
355
            {
 
356
                return font;
 
357
            }
 
358
 
 
359
            public override FontRenderContext getFontRenderContext()
 
360
            {
 
361
                return frc;
 
362
            }
 
363
 
 
364
            public override int getGlyphCode(int i)
 
365
            {
 
366
                throw new NotImplementedException();
 
367
            }
 
368
 
 
369
            public override int[] getGlyphCodes(int i1, int i2, int[] iarr)
 
370
            {
 
371
                throw new NotImplementedException();
 
372
            }
 
373
 
 
374
            public override GlyphJustificationInfo getGlyphJustificationInfo(int i)
 
375
            {
 
376
                throw new NotImplementedException();
 
377
            }
 
378
 
 
379
            public override java.awt.Shape getGlyphLogicalBounds(int i)
 
380
            {
 
381
                throw new NotImplementedException();
 
382
            }
 
383
 
 
384
            public override GlyphMetrics getGlyphMetrics(int i)
 
385
            {
 
386
                throw new NotImplementedException();
 
387
            }
 
388
 
 
389
            public override java.awt.Shape getGlyphOutline(int i)
 
390
            {
 
391
                throw new NotImplementedException();
 
392
            }
 
393
 
 
394
            public override Point2D getGlyphPosition(int i)
 
395
            {
 
396
                throw new NotImplementedException();
 
397
            }
 
398
 
 
399
            public override float[] getGlyphPositions(int i1, int i2, float[] farr)
 
400
            {
 
401
                throw new NotImplementedException();
 
402
            }
 
403
 
 
404
            public override AffineTransform getGlyphTransform(int i)
 
405
            {
 
406
                throw new NotImplementedException();
 
407
            }
 
408
 
 
409
            public override java.awt.Shape getGlyphVisualBounds(int index)
 
410
            {
 
411
                return getMetrics().GetStringBounds(new String(text, index, 1));
 
412
            }
 
413
 
 
414
            public override Rectangle2D getLogicalBounds()
 
415
            {
 
416
                return getMetrics().GetStringBounds(new String(text));
 
417
            }
 
418
 
 
419
            public override int getNumGlyphs()
 
420
            {
 
421
                return text.Length;
 
422
            }
 
423
 
 
424
            public override java.awt.Shape getOutline()
 
425
            {
 
426
                throw new NotImplementedException();
 
427
            }
 
428
 
 
429
            public override java.awt.Shape getOutline(float f1, float f2)
 
430
            {
 
431
                throw new NotImplementedException();
 
432
            }
 
433
 
 
434
            public override Rectangle2D getVisualBounds()
 
435
            {
 
436
                return new NetFontMetrics(font).GetStringBounds(new String(text));
 
437
            }
 
438
 
 
439
            public override void performDefaultLayout()
 
440
            {
 
441
                throw new NotImplementedException();
 
442
            }
 
443
 
 
444
            public override void setGlyphPosition(int i, Point2D pd)
 
445
            {
 
446
                throw new NotImplementedException();
 
447
            }
 
448
 
 
449
            public override void setGlyphTransform(int i, AffineTransform at)
 
450
            {
 
451
                throw new NotImplementedException();
 
452
            }
 
453
 
 
454
            public char[] getText()
 
455
            {
 
456
                return text;
 
457
            }
 
458
    }*/
 
459
 
 
460
    /*class NetLineMetrics : LineMetrics
 
461
    {
 
462
        private java.awt.Font mFont;
 
463
        private String mString;
 
464
        private FontFamily fontFamily;
 
465
        private FontStyle style;
 
466
        private float factor;
 
467
 
 
468
        public NetLineMetrics(java.awt.Font aFont, String aString)
 
469
        {
 
470
            mFont = aFont;
 
471
            mString = aString;
 
472
            fontFamily = J2C.CreateFontFamily(aFont.getName());
 
473
            style = (FontStyle)mFont.getStyle();
 
474
            factor = aFont.getSize2D() / fontFamily.GetEmHeight(style);
 
475
        }
 
476
 
 
477
        public override float getAscent()
 
478
        {
 
479
            return fontFamily.GetCellAscent(style) * factor;
 
480
        }
 
481
 
 
482
        public override int getBaselineIndex()
 
483
        {
 
484
            return 0; //I have no font see that return another value.
 
485
        }
 
486
 
 
487
        public override float[] getBaselineOffsets()
 
488
        {
 
489
            float ascent = getAscent();
 
490
            return new float[] { 0, (getDescent() / 2f - ascent) / 2f, -ascent };
 
491
        }
 
492
 
 
493
        public override float getDescent()
 
494
        {
 
495
            return fontFamily.GetCellDescent(style) * factor;
 
496
        }
 
497
 
 
498
        public override float getHeight()
 
499
        {
 
500
            return fontFamily.GetLineSpacing(style) * factor;
 
501
        }
 
502
 
 
503
        public override float getLeading()
 
504
        {
 
505
            return getHeight() - getAscent() - getDescent();
 
506
        }
 
507
 
 
508
        public override int getNumChars()
 
509
        {
 
510
            return mString.Length;
 
511
        }
 
512
 
 
513
#if WINFX
 
514
        private Typeface GetTypeface()
 
515
        {
 
516
            return new Typeface(fontFamily, style, FontWeight.Normal, FontStretch.Medium);
 
517
        }
 
518
#endif
 
519
 
 
520
        public override float getStrikethroughOffset()
 
521
        {
 
522
#if WINFX              
 
523
            return GetTypeface().StrikethroughPosition * factor;
 
524
#else
 
525
            return getAscent() / -3;
 
526
#endif
 
527
        }
 
528
 
 
529
        public override float getStrikethroughThickness()
 
530
        {
 
531
#if WINFX              
 
532
            return GetTypeface().StrikethroughThickness * factor;
 
533
#else
 
534
            return mFont.getSize2D() / 18;
 
535
#endif
 
536
        }
 
537
 
 
538
        public override float getUnderlineOffset()
 
539
        {
 
540
#if WINFX              
 
541
            return GetTypeface().UnderlinePosition * factor;
 
542
#else
 
543
            return mFont.getSize2D() / 8.7F;
 
544
#endif
 
545
        }
 
546
 
 
547
        public override float getUnderlineThickness()
 
548
        {
 
549
#if WINFX              
 
550
            return GetTypeface().UnderlineThickness * factor;
 
551
#else
 
552
            return mFont.getSize2D() / 18;
 
553
#endif
 
554
        }
 
555
    }*/
 
556
 
 
557
}