From f767727da8f5ac0698d05d8ee832d551f4a72ba5 Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Sun, 25 May 2014 08:16:18 +0000 Subject: [PATCH] fpvectorial-svg: Improves rectangle positioning git-svn-id: trunk@45165 - --- components/fpvectorial/fpvectorial.pas | 7 +- components/fpvectorial/svgvectorialreader.pas | 70 +++++++++++++------ 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/components/fpvectorial/fpvectorial.pas b/components/fpvectorial/fpvectorial.pas index d5828b6786..8930a0c04c 100644 --- a/components/fpvectorial/fpvectorial.pas +++ b/components/fpvectorial/fpvectorial.pas @@ -3565,7 +3565,7 @@ begin ALeft := X; ARight := X + CX; ATop := Y; - ABottom := Y + CY; + ABottom := Y - CY; end; procedure TvRectangle.Render(ADest: TFPCustomCanvas; ARenderInfo: TvRenderInfo; ADestX: Integer; @@ -6199,13 +6199,14 @@ begin if MinY = MaxY then MaxY := MinY + Height; lLeft := CoordToCanvasX(MinX); - lTop := CoordToCanvasY(MinY); + lTop := CoordToCanvasY(MaxY); lRight := CoordToCanvasX(MaxX); - lBottom := CoordToCanvasY(MaxY); + lBottom := CoordToCanvasY(MinY); ADest.Brush.Style := bsClear; ADest.Pen.FPColor := colBlack; ADest.Pen.Style := psSolid; + ADest.Pen.Width := 1; ADest.Rectangle(lLeft, lTop, lRight, lBottom); end; diff --git a/components/fpvectorial/svgvectorialreader.pas b/components/fpvectorial/svgvectorialreader.pas index 05353ebd31..869ac3640f 100644 --- a/components/fpvectorial/svgvectorialreader.pas +++ b/components/fpvectorial/svgvectorialreader.pas @@ -79,7 +79,7 @@ type function DebugOutTokensAsString: string; end; - TSVGCoordinateKind = (sckUnknown, sckX, sckY, sckYDelta); + TSVGCoordinateKind = (sckUnknown, sckX, sckY, sckXDelta, sckYDelta, sckYSize); TSVGUnit = (suPX, suMM); @@ -135,10 +135,16 @@ type function StringFloatZeroToOneToWord(AStr: string): Word; procedure ConvertSVGCoordinatesToFPVCoordinates( const AData: TvVectorialPage; - const ASrcX, ASrcY: Double; var ADestX, ADestY: Double); + const ASrcX, ASrcY: Double; var ADestX, ADestY: Double; + ADoViewBoxAdjust: Boolean = True); procedure ConvertSVGDeltaToFPVDelta( const AData: TvVectorialPage; - const ASrcX, ASrcY: Double; var ADestX, ADestY: Double); + const ASrcX, ASrcY: Double; var ADestX, ADestY: Double; + ADoViewBoxAdjust: Boolean = True); + procedure ConvertSVGSizeToFPVSize( + const AData: TvVectorialPage; + const ASrcX, ASrcY: Double; var ADestX, ADestY: Double; + ADoViewBoxAdjust: Boolean = True); procedure AutoDetectDocSize(var ALeft, ATop, ARight, ABottom: Double; ABaseNode: TDOMNode); function SVGColorValueStrToWord(AStr: string): Word; public @@ -2385,9 +2391,9 @@ begin ConvertSVGCoordinatesToFPVCoordinates( AData, lx, ly, lRect.X, lRect.Y); - ConvertSVGDeltaToFPVDelta( + ConvertSVGSizeToFPVSize( AData, cx, cy, lRect.CX, lRect.CY); - ConvertSVGDeltaToFPVDelta( + ConvertSVGSizeToFPVSize( AData, lrx, lry, lRect.RX, lRect.RY); lRect.RX := Abs(lRect.RX) * 2; lRect.RY := Abs(lRect.RY) * 2; @@ -2677,6 +2683,21 @@ end; function TvSVGVectorialReader.StringWithUnitToFloat(AStr: string; ACoordKind: TSVGCoordinateKind = sckUnknown; ADefaultUnit: TSVGUnit = suPX): Double; + + procedure DoViewBoxAdjust(); + begin + if ViewBoxAdjustment then + begin + case ACoordKind of + sckX: Result := (Result - ViewBox_Left) * Page_Width / ViewBox_Width; + sckXDelta: Result := Result * Page_Width / ViewBox_Width; + sckY: Result := Page_Height - (Result - ViewBox_Top) * Page_Height / ViewBox_Height; + sckYDelta: Result := - (Result - ViewBox_Top) * Page_Height / ViewBox_Height; + sckYSize: Result := Result * Page_Height / ViewBox_Height; + end; + end; + end; + var UnitStr, ValueStr: string; Len: Integer; @@ -2702,12 +2723,7 @@ begin begin ValueStr := Copy(AStr, 1, Len-2); Result := StrToFloat(ValueStr, FPointSeparator); - if ViewBoxAdjustment and (ACoordKind = sckX) then - Result := (Result - ViewBox_Left) * Page_Width / ViewBox_Width; - if ViewBoxAdjustment and (ACoordKind = sckY) then - Result := Page_Height - (Result - ViewBox_Top) * Page_Height / ViewBox_Height; - if ViewBoxAdjustment and (ACoordKind = sckYDelta) then - Result := - (Result - ViewBox_Top) * Page_Height / ViewBox_Height; + DoViewBoxAdjust(); end else if LastChar = '%' then begin @@ -2726,12 +2742,7 @@ begin else // If there is no unit, just use StrToFloat begin Result := StrToFloat(AStr, FPointSeparator); - if ViewBoxAdjustment and (ACoordKind = sckX) then - Result := (Result - ViewBox_Left) * Page_Width / ViewBox_Width; - if ViewBoxAdjustment and (ACoordKind = sckY) then - Result := Page_Height - (Result - ViewBox_Top) * Page_Height / ViewBox_Height; - if ViewBoxAdjustment and (ACoordKind = sckYDelta) then - Result := - (Result - ViewBox_Top) * Page_Height / ViewBox_Height; + DoViewBoxAdjust(); end; end; @@ -2742,30 +2753,43 @@ end; procedure TvSVGVectorialReader.ConvertSVGCoordinatesToFPVCoordinates( const AData: TvVectorialPage; const ASrcX, ASrcY: Double; - var ADestX,ADestY: Double); + var ADestX,ADestY: Double; ADoViewBoxAdjust: Boolean = True); begin ADestX := ASrcX * FLOAT_MILIMETERS_PER_PIXEL; ADestY := AData.Height - ASrcY * FLOAT_MILIMETERS_PER_PIXEL; - if ViewBoxAdjustment then + if ViewBoxAdjustment and ADoViewBoxAdjust then begin - ADestX := ASrcX * Page_Width / ViewBox_Width; - ADestY := AData.Height - ASrcY * Page_Height / ViewBox_Height; + ADestX := (ASrcX - ViewBox_Left) * Page_Width / ViewBox_Width; + ADestY := AData.Height - (ASrcY - ViewBox_Top) * Page_Height / ViewBox_Height; end; end; procedure TvSVGVectorialReader.ConvertSVGDeltaToFPVDelta( const AData: TvVectorialPage; const ASrcX, ASrcY: Double; var ADestX, - ADestY: Double); + ADestY: Double; ADoViewBoxAdjust: Boolean = True); begin ADestX := ASrcX * FLOAT_MILIMETERS_PER_PIXEL; ADestY := - ASrcY * FLOAT_MILIMETERS_PER_PIXEL; - if ViewBoxAdjustment then + if ViewBoxAdjustment and ADoViewBoxAdjust then begin ADestX := ASrcX * Page_Width / ViewBox_Width; ADestY := - ASrcY * Page_Height / ViewBox_Height; end; end; +procedure TvSVGVectorialReader.ConvertSVGSizeToFPVSize( + const AData: TvVectorialPage; const ASrcX, ASrcY: Double; var ADestX, + ADestY: Double; ADoViewBoxAdjust: Boolean = True); +begin + ADestX := ASrcX * FLOAT_MILIMETERS_PER_PIXEL; + ADestY := ASrcY * FLOAT_MILIMETERS_PER_PIXEL; + if ViewBoxAdjustment and ADoViewBoxAdjust then + begin + ADestX := ASrcX * Page_Width / ViewBox_Width; + ADestY := ASrcY * Page_Height / ViewBox_Height; + end; +end; + procedure TvSVGVectorialReader.AutoDetectDocSize(var ALeft, ATop, ARight, ABottom: Double; ABaseNode: TDOMNode); var