public void ApplyStyle(HCCanvas aCanvas, Single aScale = 1) { if (FBackColor == HC.HCTransparentColor) { aCanvas.Brush.Style = HCBrushStyle.bsClear; } else { aCanvas.Brush.Color = FBackColor; } aCanvas.Font.BeginUpdate(); try { aCanvas.Font.Color = FColor; aCanvas.Font.Family = FFamily; if ((FFontStyles.Contains((byte)HCFontStyle.tsSuperscript)) || (FFontStyles.Contains((byte)HCFontStyle.tsSubscript))) { aCanvas.Font.Size = FSize / 2; } else { aCanvas.Font.Size = FSize; } aCanvas.Font.FontStyles.Value = FFontStyles.Value; // 防止后面其他地方修改canva的font时影响到原始的样式数据 } finally { aCanvas.Font.EndUpdate(); } TEXTMETRICW vTextMetric = new TEXTMETRICW(); aCanvas.GetTextMetrics(ref vTextMetric); FTextMetric_tmAveCharWidth = vTextMetric.tmAveCharWidth; FTextMetric_tmExternalLeading = vTextMetric.tmExternalLeading; FTextMetric_tmHeight = vTextMetric.tmHeight; FONTSIGNATURE vFontSignature = new FONTSIGNATURE(); FFontHeight = aCanvas.TextHeight("H"); if ((GDI.GetTextCharsetInfo(aCanvas.Handle, ref vFontSignature, 0) != GDI.DEFAULT_CHARSET) && ((vFontSignature.fsCsb[0] & CJK_CODEPAGE_BITS) != 0)) { FCJKFont = true; } else { FCJKFont = false; } FOutMetSize = GDI.GetOutlineTextMetrics(aCanvas.Handle, 0, IntPtr.Zero); if (FOutMetSize != 0) { //FOutMetSize = (uint)Marshal.SizeOf(FOutlineTextmetric); IntPtr vOutlineTextmetricPtr = Marshal.AllocHGlobal((int)FOutMetSize); try { if (GDI.GetOutlineTextMetrics(aCanvas.Handle, FOutMetSize, vOutlineTextmetricPtr) != 0) { //FOutlineTextmetric = new OUTLINETEXTMETRICW(); //FOutlineTextmetric = (OUTLINETEXTMETRICW)Marshal.PtrToStructure(FOutlineTextmetricPtr, typeof(OUTLINETEXTMETRICW)); //string otmpFamilyName = Marshal.PtrToStringUni(new IntPtr((int)FOutlineTextmetricPtr + (int)FOutlineTextmetric.otmpFamilyName)); //string otmpFaceName = Marshal.PtrToStringUni(new IntPtr((int)FOutlineTextmetricPtr + (int)FOutlineTextmetric.otmpFaceName)); ; //string otmpStyleName = Marshal.PtrToStringUni(new IntPtr((int)FOutlineTextmetricPtr + (int)FOutlineTextmetric.otmpStyleName)); ; //string otmpFullName = Marshal.PtrToStringUni(new IntPtr((int)FOutlineTextmetricPtr + (int)FOutlineTextmetric.otmpFullName)); ; // 以上为参考代码 //Marshal.PtrToStructure(FOutlineTextmetricPtr, FOutlineTextmetric); OUTLINETEXTMETRICW vOutlineTextmetric = (OUTLINETEXTMETRICW)Marshal.PtrToStructure(vOutlineTextmetricPtr, typeof(OUTLINETEXTMETRICW)); FOutlineTextmetric_otmfsSelection = vOutlineTextmetric.otmfsSelection; FOutlineTextmetric_otmAscent = vOutlineTextmetric.otmAscent; FOutlineTextmetric_otmDescent = vOutlineTextmetric.otmDescent; FOutlineTextmetric_otmEMSquare = vOutlineTextmetric.otmEMSquare; } } finally { Marshal.FreeHGlobal(vOutlineTextmetricPtr); } } TT_HHEA vFontHeader = new TT_HHEA(); if ((uint)GDI.GetFontData(aCanvas.Handle, MS_HHEA_TAG, 0, ref vFontHeader, Marshal.SizeOf(vFontHeader)) != GDI.GDI_ERROR) { FFontHeader_Ascender = vFontHeader.Ascender; FFontHeader_Descender = vFontHeader.Descender; } else { FFontHeader_Ascender = 0; FFontHeader_Descender = 0; } }
public void ApplyStyle(HCCanvas aCanvas, Single aScale = 1) { if (FBackColor == HC.HCTransparentColor) { aCanvas.Brush.Style = HCBrushStyle.bsClear; } else { aCanvas.Brush.Color = FBackColor; } aCanvas.Font.BeginUpdate(); try { aCanvas.Font.Color = FColor; aCanvas.Font.Family = FFamily; if ((FFontStyles.Contains((byte)HCFontStyle.tsSuperscript)) || (FFontStyles.Contains((byte)HCFontStyle.tsSubscript))) { aCanvas.Font.Size = FSize / 2; } else { aCanvas.Font.Size = FSize; } aCanvas.Font.FontStyles = FFontStyles; } finally { aCanvas.Font.EndUpdate(); } aCanvas.GetTextMetrics(ref FTextMetric); FONTSIGNATURE vFontSignature = new FONTSIGNATURE(); FFontHeight = aCanvas.TextHeight("H"); if ((GDI.GetTextCharsetInfo(aCanvas.Handle, ref vFontSignature, 0) != GDI.DEFAULT_CHARSET) && ((vFontSignature.fsCsb[0] & CJK_CODEPAGE_BITS) != 0)) { FCJKFont = true; } else { FCJKFont = false; } if (FOutMetSize > 0) { Marshal.FreeHGlobal(FOutlineTextmetricPtr); } FOutMetSize = GDI.GetOutlineTextMetrics(aCanvas.Handle, 0, IntPtr.Zero); if (FOutMetSize != 0) { //FOutMetSize = (uint)Marshal.SizeOf(FOutlineTextmetric); FOutlineTextmetricPtr = Marshal.AllocHGlobal((int)FOutMetSize); if (GDI.GetOutlineTextMetrics(aCanvas.Handle, FOutMetSize, FOutlineTextmetricPtr) != 0) { //FOutlineTextmetric = new OUTLINETEXTMETRICW(); //FOutlineTextmetric = (OUTLINETEXTMETRICW)Marshal.PtrToStructure(FOutlineTextmetricPtr, typeof(OUTLINETEXTMETRICW)); //string otmpFamilyName = Marshal.PtrToStringUni(new IntPtr((int)FOutlineTextmetricPtr + (int)FOutlineTextmetric.otmpFamilyName)); //string otmpFaceName = Marshal.PtrToStringUni(new IntPtr((int)FOutlineTextmetricPtr + (int)FOutlineTextmetric.otmpFaceName)); ; //string otmpStyleName = Marshal.PtrToStringUni(new IntPtr((int)FOutlineTextmetricPtr + (int)FOutlineTextmetric.otmpStyleName)); ; //string otmpFullName = Marshal.PtrToStringUni(new IntPtr((int)FOutlineTextmetricPtr + (int)FOutlineTextmetric.otmpFullName)); ; // 以上为参考代码 //Marshal.PtrToStructure(FOutlineTextmetricPtr, FOutlineTextmetric); FOutlineTextmetric = (OUTLINETEXTMETRICW)Marshal.PtrToStructure(FOutlineTextmetricPtr, typeof(OUTLINETEXTMETRICW)); } } if ((uint)GDI.GetFontData(aCanvas.Handle, MS_HHEA_TAG, 0, ref FFontHeader, Marshal.SizeOf(FFontHeader)) == GDI.GDI_ERROR) { return; } }