Code Viewer

./Classes/__font.js File Size: 22.77 KB

  1. /*
  2. * -----
  3. * Screenshot Sender - __font.js
  4. * -----
  5. * This library allows you to change the font of specific controls. Made by phalanxii.
  6. * URL: http://msghelp.net/showthread.php?tid=77717
  7. * -----
  8. */
  9.  
  10. // Font weight constants
  11. var FW_DONTCARE = 0;
  12. var FW_THIN = 100;
  13. var FW_EXTRALIGHT = 200;
  14. var FW_LIGHT = 300;
  15. var FW_NORMAL = 400;
  16. var FW_MEDIUM = 500;
  17. var FW_SEMIBOLD = 600;
  18. var FW_BOLD = 700;
  19. var FW_EXTRABOLD = 800;
  20. var FW_HEAVY = 900;
  21. var FW_ULTRALIGHT = FW_EXTRALIGHT;
  22. var FW_REGULAR = FW_NORMAL;
  23. var FW_DEMIBOLD = FW_SEMIBOLD;
  24. var FW_ULTRABOLD = FW_EXTRABOLD;
  25. var FW_BLACK = FW_HEAVY;
  26.  
  27. // Character set identifiers
  28. var ANSI_CHARSET = 0;
  29. var DEFAULT_CHARSET = 1;
  30. var SYMBOL_CHARSET = 2;
  31. var SHIFTJIS_CHARSET = 128;
  32. var HANGEUL_CHARSET = 129;
  33. var CHINESEBIG5_CHARSET = 136;
  34. var OEM_CHARSET = 255;
  35. var JOHAB_CHARSET = 130;
  36. var HEBREW_CHARSET = 177;
  37. var ARABIC_CHARSET = 178;
  38. var GREEK_CHARSET = 161;
  39. var TURKISH_CHARSET = 162;
  40. var VIETNAMESE_CHARSET = 163;
  41. var THAI_CHARSET = 222;
  42. var EASTEUROPE_CHARSET = 238;
  43. var RUSSIAN_CHARSET = 204;
  44. var MAC_CHARSET = 77;
  45. var BALTIC_CHARSET = 186;
  46.  
  47. // Output precision constants
  48. var OUT_DEFAULT_PRECIS = 0;
  49. var OUT_STRING_PRECIS = 1;
  50. var OUT_CHARACTER_PRECIS = 2;
  51. var OUT_STROKE_PRECIS = 3;
  52. var OUT_TT_PRECIS = 4;
  53. var OUT_DEVICE_PRECIS = 5;
  54. var OUT_RASTER_PRECIS = 6;
  55. var OUT_TT_ONLY_PRECIS = 7;
  56. var OUT_OUTLINE_PRECIS = 8;
  57.  
  58. // Clipping precision constants
  59. var CLIP_DEFAULT_PRECIS = 0;
  60. var CLIP_CHARACTER_PRECIS = 1;
  61. var CLIP_STROKE_PRECIS = 2;
  62. var CLIP_MASK = 15;
  63. var CLIP_LH_ANGLES = 1 << 4;
  64. var CLIP_TT_ALWAYS = 2 << 4;
  65. var CLIP_EMBEDDED = 8 << 4;
  66.  
  67. // Output quality constants
  68. var DEFAULT_QUALITY = 0;
  69. var DRAFT_QUALITY = 1;
  70. var PROOF_QUALITY = 2;
  71. var NONANTIALIASED_QUALITY = 3;
  72. var ANTIALIASED_QUALITY = 4;
  73. var CLEARTYPE_QUALITY = 5;
  74. var CLEARTYPE_NATURAL_QUALITY = 6;
  75.  
  76. // Font pitch constants
  77. var DEFAULT_PITCH = 0;
  78. var FIXED_PITCH = 1;
  79. var VARIABLE_PITCH = 2;
  80.  
  81. // Font family constants
  82. var FF_DONTCARE = 0 << 4;
  83. var FF_ROMAN = 1 << 4;
  84. var FF_SWISS = 2 << 4;
  85. var FF_MODERN = 3 << 4;
  86. var FF_SCRIPT = 4 << 4;
  87. var FF_DECORATIVE = 5 << 4;
  88.  
  89. // Message constants
  90. var WM_SETFONT = 48;
  91. var WM_GETFONT = 49;
  92.  
  93. // Choose font flags
  94. var CF_SCREENFONTS = 1;
  95. var CF_PRINTERFONTS = 2;
  96. var CF_BOTH = CF_SCREENFONTS | CF_PRINTERFONTS;
  97. var CF_SHOWHELP = 4;
  98. var CF_ENABLEHOOK = 8;
  99. var CF_ENABLETEMPLATE = 16;
  100. var CF_ENABLETEMPLATEHANDLE = 32;
  101. var CF_INITTOLOGFONTSTRUCT = 64;
  102. var CF_USESTYLE = 128;
  103. var CF_EFFECTS = 256;
  104. var CF_APPLY = 512;
  105. var CF_ANSIONLY = 1024;
  106. var CF_SCRIPTSONLY = CF_ANSIONLY;
  107. var CF_NOVECTORFONTS = 2048;
  108. var CF_NOOEMFONTS = CF_NOVECTORFONTS;
  109. var CF_NOSIMULATIONS = 4096;
  110. var CF_LIMITSIZE = 8192;
  111. var CF_FIXEDPITCHONLY = 16384;
  112. var CF_WYSIWYG = 32768;
  113. var CF_FORCEFONTEXIST = 65536;
  114. var CF_SCALABLEONLY = 131072;
  115. var CF_TTONLY = 262144;
  116. var CF_NOFACESEL = 524288;
  117. var CF_NOSTYLESEL = 1048576;
  118. var CF_NOSIZESEL = 2097152;
  119. var CF_SELECTSCRIPT = 4194304;
  120. var CF_NOSCRIPTSEL = 8388608;
  121. var CF_NOVERTFONTS = 16777216;
  122.  
  123. /*
  124. * -----
  125. * Class: Font
  126. * Parameters:
  127. *      Height         - [Integer] Specifies the height of characters in the font.
  128. *      Width          - [Integer] Specifies the average width of characters in the font.
  129. *      Escapement     - [Integer] Specifies the angle between the escapement vector and the x-axis of the device.
  130. *      Orientation    - [Integer] Specifies the angle between the base line of each character and the x-axis of the device.
  131. *      Weight         - [Integer] Specifies the weight of the font.
  132. *      Italic         - [Boolean] Specifies whether the font is an italic font.
  133. *      Underline      - [Boolean] Specifies whether the font is an underlined font.
  134. *      StrikeOut      - [Boolean] Specifies whether the font is a strikeout font.
  135. *      CharSet        - [Constant] Specifies the character set.
  136. *      OutPrecision   - [Constant] Specifies the output precision.
  137. *      ClipPrecision  - [Constant] Specifies the clipping precision.
  138. *      Quality        - [Constant] Specifies the output quality.
  139. *      PitchAndFamily - [Constant] Specifies the pitch and family of the font.
  140. *      FaceName       - [String] Specifies the typeface name of the font.
  141. * -----
  142. */
  143.  
  144. function Font(Height, Width, Escapement, Orientation, Weight, Italic, Underline, StrikeOut, CharSet, OutPrecision, ClipPrecision, Quality, PitchAndFamily, FaceName, ColorRef) {
  145.         // Default options
  146.         this.Height = 0;
  147.         this.Width = 0;
  148.         this.Escapement = 0;
  149.         this.Orientation = 0;
  150.         this.Weight = FW_DONTCARE;
  151.         this.Italic = false;
  152.         this.Underline = false;
  153.         this.StrikeOut = false;
  154.         this.CharSet = DEFAULT_CHARSET;
  155.         this.OutPrecision = OUT_DEFAULT_PRECIS;
  156.         this.ClipPrecision = CLIP_DEFAULT_PRECIS;
  157.         this.Quality = DEFAULT_QUALITY;
  158.         this.PitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
  159.         this.FaceName = 'MS Shell Dlg';
  160.         this.ColorRef = 0x0;
  161.  
  162.         if(arguments.length === 1 && typeof(arguments[0]) === 'object') {
  163.                 // Passed as source object
  164.                 var oSrc = arguments[0];
  165.                 if(typeof(oSrc.Height) === 'number')            this.Height = oSrc.Height;
  166.                 if(typeof(oSrc.Width) === 'number')                    this.Width = oSrc.Width;
  167.                 if(typeof(oSrc.Escapement) === 'number')                this.Escapement = oSrc.Escapement;
  168.                 if(typeof(oSrc.Orientation) === 'number')              this.Orientation = oSrc.Orientation;
  169.                 if(typeof(oSrc.Weight) === 'number')            this.Weight = oSrc.Weight;
  170.                 if(typeof(oSrc.Italic) !== 'undefined')   this.Italic = !!oSrc.Italic;
  171.                 if(typeof(oSrc.Underline) !== 'undefined')            this.Underline = !!oSrc.Underline;
  172.                 if(typeof(oSrc.StrikeOut) !== 'undefined')            this.StrikeOut = !!oSrc.StrikeOut;
  173.                 if(typeof(oSrc.CharSet) === 'number')         this.CharSet = oSrc.CharSet;
  174.                 if(typeof(oSrc.OutPrecision) === 'number')            this.OutPrecision = oSrc.OutPrecision;
  175.                 if(typeof(oSrc.ClipPrecision) === 'number')          this.ClipPrecision = oSrc.ClipPrecision;
  176.                 if(typeof(oSrc.Quality) === 'number')         this.Quality = oSrc.Quality;
  177.                 if(typeof(oSrc.PitchAndFamily) === 'number')    this.PitchAndFamily = oSrc.PitchAndFamily;
  178.                 if(typeof(oSrc.FaceName) === 'string')      this.FaceName = oSrc.FaceName;
  179.                 if(typeof(oSrc.ColorRef) === 'number')      this.ColorRef = oSrc.ColorRef;
  180.         } else {
  181.                 // Passed as arguments
  182.                 if(typeof(Height) === 'number')     this.Height = Height;
  183.                 if(typeof(Width) === 'number')          this.Width = Width;
  184.                 if(typeof(Escapement) === 'number')                    this.Escapement = Escapement;
  185.                 if(typeof(Orientation) === 'number')            this.Orientation = Orientation;
  186.                 if(typeof(Weight) === 'number')     this.Weight = Weight;
  187.                 if(typeof(Italic) !== 'undefined')                        this.Italic = !!Italic;
  188.                 if(typeof(Underline) !== 'undefined')         this.Underline = !!Underline;
  189.                 if(typeof(StrikeOut) !== 'undefined')         this.StrikeOut = !!StrikeOut;
  190.                 if(typeof(CharSet) === 'number')                                this.CharSet = CharSet;
  191.                 if(typeof(OutPrecision) === 'number')         this.OutPrecision = OutPrecision;
  192.                 if(typeof(ClipPrecision) === 'number')      this.ClipPrecision = ClipPrecision;
  193.                 if(typeof(Quality) === 'number')                                this.Quality = Quality;
  194.                 if(typeof(PitchAndFamily) === 'number')   this.PitchAndFamily = PitchAndFamily;
  195.                 if(typeof(FaceName) === 'string')                            this.FaceName = FaceName;
  196.                 if(typeof(ColorRef) === 'number')                            this.ColorRef = ColorRef;
  197.         }
  198.        
  199.         // Create a LOGFONT structure
  200.         this.LOGFONT = Interop.Allocate(92);
  201.        
  202.         // Update the LOGFONT and HFONT
  203.         this.Update();
  204. }
  205.  
  206. Font.prototype = {
  207.         // Update the LOGFONT and HFONT based on the font properties
  208.         'Update' : function () {
  209.                 this.UpdateLOGFONT();
  210.                 this.UpdateHFONT();
  211.         },
  212.        
  213.         // Update the LOGFONT based on the font properties
  214.         'UpdateLOGFONT' : function () {
  215.                 with (this.LOGFONT) {
  216.                         WriteDWORD(0, this.Height);
  217.                         WriteDWORD(4, this.Width);
  218.                         WriteDWORD(8, this.Escapement);
  219.                         WriteDWORD(12, this.Orientation);
  220.                         WriteDWORD(16, this.Weight);
  221.                         SetAt(20, (this.Italic) ? 1 : 0);
  222.                         SetAt(21, (this.Underline) ? 1 : 0);
  223.                         SetAt(22, (this.StrikeOut) ? 1 : 0);
  224.                         SetAt(23, this.CharSet);
  225.                         SetAt(24, this.OutPrecision);
  226.                         SetAt(25, this.ClipPrecision);
  227.                         SetAt(26, this.Quality);
  228.                         SetAt(27, this.PitchAndFamily);
  229.                         WriteString(28, this.FaceName, true);
  230.                 }
  231.         },
  232.        
  233.         // Update the HFONT based on the LOGFONT
  234.         'UpdateHFONT' : function () {
  235.                 this.HFONT = Interop.Call('gdi32.dll', 'CreateFontIndirectW', this.LOGFONT.DataPtr);
  236.                 this.Handle = this.HFONT;
  237.         },
  238.        
  239.         // Update the font properties based on the LOGFONT
  240.         'UpdateProperties' : function () {
  241.                 with (this) {
  242.                         Height = LOGFONT.ReadDWORD(0);
  243.                         Width = LOGFONT.ReadDWORD(4);
  244.                         Escapement = LOGFONT.ReadDWORD(8);
  245.                         Orientation = LOGFONT.ReadDWORD(12);
  246.                         Weight = LOGFONT.ReadDWORD(16);
  247.                         Italic = !!LOGFONT.GetAt(20);
  248.                         Underline = !!LOGFONT.GetAt(21);
  249.                         StrikeOut = !!LOGFONT.GetAt(22);
  250.                         CharSet = LOGFONT.GetAt(23);
  251.                         OutPrecision = LOGFONT.GetAt(24);
  252.                         ClipPrecision = LOGFONT.GetAt(25);
  253.                         Quality = LOGFONT.GetAt(26);
  254.                         PitchAndFamily = LOGFONT.GetAt(27);
  255.                         FaceName = LOGFONT.ReadString(28, true);
  256.                 }
  257.         },
  258.        
  259.         // Set the font of a control
  260.         'Set' : function (PlusWnd, ControlId) {
  261.                 PlusWnd.SendControlMessage(ControlId, WM_SETFONT, this.HFONT, 0);
  262.         },
  263.        
  264.         // Get the font of a control and update the LOGFONT, HFONT and font properties
  265.         'Get' : function (PlusWnd, ControlId) {
  266.                 Interop.Call('gdi32.dll', 'GetObjectW', PlusWnd.SendControlMessage(ControlId, WM_GETFONT, 0, 0), this.LOGFONT.Size, this.LOGFONT.DataPtr);
  267.                 this.UpdateHFONT();
  268.                 this.UpdateProperties();
  269.         },
  270.        
  271.         // Choose a font from a dialog box and update the LOGFONT, HFONT and font properties
  272.         'Choose' : function (Flags) {
  273.                 this.CHOOSEFONT = Interop.Allocate(60);
  274.                 this.CHOOSEFONT.WriteDWORD(0, this.CHOOSEFONT.Size);
  275.                 this.CHOOSEFONT.WriteDWORD(12, this.LOGFONT.DataPtr);
  276.                 this.CHOOSEFONT.WriteDWORD(20, Flags);
  277.                 this.CHOOSEFONT.WriteDWORD(24, this.ColorRef);
  278.                 var Height = this.LOGFONT.ReadDWORD(0);
  279.                 if (!Height) this.LOGFONT.WriteDWORD(0, (10).toLogical() );
  280.                 if (Interop.Call('comdlg32.dll', 'ChooseFontW', this.CHOOSEFONT.DataPtr) ) {
  281.                         this.Height = this.CHOOSEFONT.ReadDWORD(16).toLogical() / 10;
  282.                         this.ColorRef = this.CHOOSEFONT.ReadDWORD(24);
  283.                         this.UpdateHFONT();
  284.                         this.UpdateProperties();
  285.                 } else this.LOGFONT.WriteDWORD(0, Height);
  286.         },
  287.        
  288.         // Return the contents of the LOGFONT as a string
  289.         'Save' : function () {
  290.                 var Cache = '';
  291.                 for (var i = 0; i < 92; i++) Cache += this.LOGFONT.GetAt(i) + ' ';
  292.                 return Cache;
  293.         },
  294.        
  295.         // Load a string containing the contents of a LOGFONT and update the LOGFONT, HFONT and font properties
  296.         'Load' : function (Cache) {
  297.                 Cache = Cache.split(' ');
  298.                 for (var i = 0; i < 92; i++) this.LOGFONT.SetAt(i, Cache[i]);
  299.                 this.UpdateHFONT();
  300.                 this.UpdateProperties();
  301.         },
  302.        
  303.         // Create a copy of this Font instance
  304.         'Clone' : function() {
  305.                 return new Font(this);
  306.         }
  307. }
  308.  
  309. // Convert point units(common font units) to logical units(used in the LOGFONT)
  310. Number.prototype.toLogical = function () {
  311.         var hdc = Interop.Call('user32.dll', 'GetDC', null);
  312.         var LOGPIXELSY = 90;
  313.         LOGPIXELSY = Interop.Call('gdi32.dll', 'GetDeviceCaps', hdc, LOGPIXELSY);
  314.         Interop.Call('user32.dll', 'ReleaseDC', null, hdc);
  315.         return - Math.round( (this * LOGPIXELSY) / 72);
  316. }
  317.  
  318. // Convert logical units(used in the LOGFONT) to point units(common font units)
  319. Number.prototype.toPoint = function () {
  320.         var hdc = Interop.Call('user32.dll', 'GetDC', null);
  321.         var LOGPIXELSY = 90;
  322.         LOGPIXELSY = Interop.Call('gdi32.dll', 'GetDeviceCaps', hdc, LOGPIXELSY);
  323.         Interop.Call('user32.dll', 'ReleaseDC', null, hdc);
  324.         return - Math.round( (this * 72) / LOGPIXELSY);
  325. }
  326.  

Version

  • 5.0.0070_20100325_publicbeta1

Developers

Project Details

  • Folders8
  • Files122
  • Total Lines11,867
  • Repository Version70

User Count

  • 162