You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.9 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Kicad Developers, see change_log.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <preview_items/preview_utils.h>
  24. #include <gal/graphics_abstraction_layer.h>
  25. #include <base_units.h>
  26. #include <pcb_painter.h>
  27. #include <view/view.h>
  28. double KIGFX::PREVIEW::PreviewOverlayDeemphAlpha( bool aDeemph )
  29. {
  30. return aDeemph ? 0.5 : 1.0;
  31. }
  32. static wxString formatPreviewDimension( double aVal, EDA_UNITS_T aUnits )
  33. {
  34. int precision = 4;
  35. // show a sane precision for the preview, which doesn't need to
  36. // be accurate down to the nanometre
  37. switch( aUnits )
  38. {
  39. case MILLIMETRES:
  40. precision = 2; // 10um
  41. break;
  42. case INCHES:
  43. precision = 4; // 0.1mil
  44. break;
  45. case DEGREES:
  46. precision = 1; // 0.1deg
  47. break;
  48. case UNSCALED_UNITS:
  49. break;
  50. }
  51. const wxString fmtStr = wxString::Format( "%%.%df", precision );
  52. wxString str = wxString::Format( fmtStr, To_User_Unit( aUnits, aVal ) );
  53. const wxString symbol = GetAbbreviatedUnitsLabel( aUnits, false );
  54. if( symbol.size() )
  55. str << " " << symbol;
  56. return str;
  57. }
  58. wxString KIGFX::PREVIEW::DimensionLabel( const wxString& prefix,
  59. double aVal, EDA_UNITS_T aUnits )
  60. {
  61. wxString str;
  62. if( prefix.size() )
  63. str << prefix << ": ";
  64. str << formatPreviewDimension( aVal, aUnits );
  65. return str;
  66. }
  67. void KIGFX::PREVIEW::SetConstantGlyphHeight( KIGFX::GAL& aGal, double aHeight )
  68. {
  69. aHeight /= aGal.GetWorldScale();
  70. auto glyphSize = aGal.GetGlyphSize();
  71. glyphSize = glyphSize * ( aHeight / glyphSize.y );
  72. aGal.SetGlyphSize( glyphSize );
  73. }
  74. void KIGFX::PREVIEW::DrawTextNextToCursor( KIGFX::VIEW* aView,
  75. const VECTOR2D& aCursorPos, const VECTOR2D& aTextQuadrant,
  76. const std::vector<wxString>& aStrings )
  77. {
  78. auto gal = aView->GetGAL();
  79. auto glyphSize = gal->GetGlyphSize();
  80. auto rs = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( aView->GetPainter()->GetSettings() );
  81. const auto lineSpace = glyphSize.y * 0.2;
  82. auto linePitch = glyphSize.y + lineSpace;
  83. // radius string goes on the right of the cursor centre line
  84. // with a small horizontal offset (enough to keep clear of a
  85. // system cursor if present)
  86. auto textPos = aCursorPos;
  87. // if the text goes above the cursor, shift it up
  88. if( aTextQuadrant.y > 0 )
  89. {
  90. textPos.y -= linePitch * ( aStrings.size() + 1 );
  91. }
  92. if( aTextQuadrant.x < 0 )
  93. {
  94. gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_LEFT );
  95. textPos.x += 15.0 / gal->GetWorldScale();
  96. }
  97. else
  98. {
  99. gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_RIGHT );
  100. textPos.x -= 15.0 / gal->GetWorldScale();
  101. }
  102. gal->SetStrokeColor( rs->GetLayerColor( LAYER_AUX_ITEMS ).WithAlpha(
  103. PreviewOverlayDeemphAlpha( true ) ) );
  104. gal->SetIsFill( false );
  105. // write strings top-to-bottom
  106. for( const auto& str : aStrings )
  107. {
  108. textPos.y += linePitch;
  109. gal->BitmapText( str, textPos, 0.0 );
  110. }
  111. }